Skip to content

Pjbomb2/TrueTrace-Unity-Pathtracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

If you like what I do and want to support me and this project(as this takes a LOT of my time), Please consider becoming a Github Sponsor or a Patron at patreon.com/Pjbomb2! This allows me to keep this free for everyone!

Notes:

Compute Shader Based Unity PathTracer

A passion projects that has been going on for a while with the goal of bringing at least interactive path-tracing to everyone in unity, regardless of their hardware

Features:

  • Fast Compute Shader based path tracing without RT cores
  • Full Disney BSDF for materials with support for emissive meshes and Video Players
  • Ability to move, add, and remove objects during play
  • Ability to update material properties on the fly during play
  • ASVGF and my own Recurrent Denoisers
  • Compressed Wide Bounding Volume Hierarchy as the Acceleration Structure (See Ylitie et al. 2017 below)
  • PBR Texture Support
  • Next Event Estimation with Multiple Importance Sampling for Explicit Light Sampling
  • Light BVH from PBRT 4 for NEE
  • Support for all default unity lights, which interact via NEE
  • Bloom, Depth of Field, AutoExposure, TAA, Tonemapping
  • No specific GPU vendor needed(this will run on integrated graphics if you so wish it, aka no RTX cores needed)
  • Precomputed Multiple Atmospheric Scattering for dynamic and realtime sky(from ebruneton below)
  • Object Instancing
  • ReSTIR GI for faster convergence in complex scenes and more complete images in scenes with changing lighting
  • Simple upscaling ability
  • Hardware RT Support for modern cards
  • Supports Built-in, HDRP, and URP
  • Full skinned mesh support for animated skinned meshes
  • Supports deformable standard meshes
  • Supports unity heightmap terrain and heightmap trees
  • Supports OIDN Denoiser
  • Enironment Map Importance Sampling

Ylitie et al
ebruneton

If you have any questions, or suggestions, etc. let me know either through github issues, my twitter, or my discord! I am always looking for more stuff to add, and more ways to make this project more user friendly or appealing for others to use

You can contact me easiest through my discord server(above) or my twitter(https://twitter.com/Pjbomb2)

Notes:

Let me know if you use this for anything, I would be excited to see any use of this! Just please give some credit somewhere if you use it, thank you!

Requires Unity 2021 or higher

Instructions:

Required Settings Changes:

  • If you plan to use DX12(otherwise look down below for DX11 instructions): Change the Graphics Api for Windows to DirectX12 through Edit Tab(Top Left) -> Project Settings -> Player -> Other Settings -> Untoggle "Auto Graphics API For Windows", then click the little + that appears, select "Direct3D12(Experimental)", and drag that to the top. A restart of the editor is required
  • Your target camera NEEDS to be deferred, this will usually be automatically done for you by TrueTrace

Controls:

Camera Controls: WASD, Mouse, hold right click rotate the camera, and shift increases speed

General Setup

  • Download and import the UnityPackage provided and open the new TrueTrace Settings tab at the top of the screen, and click "Arrange Hierarchy"(This WILL re-arrange your hierarchy)
  • Its recommended you press "Auto Assign Scripts" to automatically assign the needed scripts to new objects

Basic script structure breakdown:

  • Top Level is a GameObject called Scene with an AssetManager and RayTracingMaster script attached
  • Second Level: Parent Object Script - Attach this to all objects that will have children with meshes you want to raytrace
  • Third Level: RayTracingObject Script - This defines what meshes get raytraced, must either be a direct child of a GameObject with the ParentObject Script, or in the same GameObject as the ParentObject Script
  • Misc Level: Unity Lights - Must have a RayTracingLight script attached to be considered(and UseNEE needs to be on)

General Use/Notes

  • The camera you want to render from, you attach the RenderHandler script to(if you have a camera tagged MainCamera, this will be done automatically)
  • The green/red rectangle shows when the acceleration structure is done building, and thus ready to render, red means that its not done, and green means its done building, a ding will sound when it completes if it takes longer than 15 seconds
  • Objects can be added and removed at will simply by toggling the associated GameObject with a ParentObject script on/off in the hierarchy(clicking on parent objects with complex objects for children will lag), but they will take time to appear as the acceleration structure needs to be rebuilt for them
  • Emissive meshes need to be have a non-zero emissive value when they are built or rebuilt to work with NEE, but after that can have their emissiveness changed at will
  • To set up PBR with the DEFAULT material, all textures go into their proper names, but Roughness goes into the Occlusion texture(This can be changed in the MaterialPairing menu)
  • If you are using blendshapes to change geometry of a skinned mesh, you may need to go to the import settings of it(in the inspector), turn off Legacy Blendshape Normals, and make sure all normals are imported, not calculated, otherwise the normals for blendshapes might be wrong
  • A fun thing you may want to do is go to TrueTrace -> Resources -> RenderPipelines -> RendererHandle, and uncomment the "[ImageEffectOpaque]"
  • If you use HDRIs, or CubeMaps for the skybox, you need to format as the texture to a Texture2D in the inspector of the image, unity will convert it automatically, then put it in the slot in "Scene Settings" in the TrueTrace settings menu
  • With multi-pass ReSTIR GI, the additional passes for spatial are in the RayTracingMaster script, where you can add more to the "Spatials" array. X is spatial sample count, Y is spatial sample radius.

Using Instancing

  • THIS DOES NOT WORK WITH RT CORES
  • Firstly, all objects that will be the source of instanced objects will need to go under the InstancedStorage and can be arranged like normal objects(with regards to the layout of parentobject to raytracingobjects)
  • Then, to instance the objects, you just need GameObjects with the InstanceObject script attatched to them under the Scene GameObject, and then drag the desired object instance from the hierarchy to the Instance Parent slot in the InstanceObject script

Linking your own Materials

  • This is how to take your material, and have the textures assign properly, meaning you don't need to use a specific material
  • In the PathTracingSettings, click the tab called "Material Pair Options"
  • Drag any material that has the shader you want to pair into the material slot that appears
  • From here, you need to select each dropdown that appears and select the property that is associated with the text to the left of the dropdown
  • Once this is done, click "Apply Material Links" and rebuild the BVH in the "Main Options" tab to update the objects in the scene

Using OIDN

  • Go to "Functionality Settings" in the TrueTrace Options, and check "Enable OIDN"

Using HDRP

  • Go into TrueTrace -> Resources -> GlobalDefines.cginc, and uncomment the #define HDRP
  • Create a new custom pass(Hierarchy -> Volume -> Custom Pass) and add the custom pass in the inspector called "HDRP Compatibility"

Using Hardware RT

  • First off, this REQUIRES unity 2023 or higher
  • In the TrueTrace settings menu, click on the top right button "Functionality Settings" and toggle HardwareRT
  • Uncomment the #define HardwareRT in: TrueTrace -> Resources -> GlobalDefines.cginc
  • Then just use like normal, but this does not support Instances

Using DX11 Only

  • DX11 does not currently work with ASVGF(just havent fixed yet), or hue shift(seems to be a compiler bug)
  • In the TrueTrace settings menu, click on the top right button "Functionality Settings" and toggle "Use DX11"
  • Uncomment the "#define DX11" in: TrueTrace -> Resources -> GlobalDefines.cginc

Using Lightmapping(EXPERIMENTAL, ONLY DIFFUSE)

  • In the TrueTrace settings menu, click on the top right button "Functionality Settings" and toggle "Use TrueTrace As A Lightmapper"
  • Uncomment the "#define LightMapping" in: TrueTrace -> Resources -> GlobalDefines.cginc
  • Make sure the lightmapping textures for the scene already exist(you need to already have unity bake the scene once, at any quality)
  • Make sure to set the Game view size to the size of the lightmapping textures(Again its very experimental)
  • Under the Scene gameobject, the RayTracingMaster script, adjust the sample count you want to use for each lightmap
  • Click play and run truetrace as normal, it will cycle through each lightmap and do its thing, you should use ReSTIR GI with a low temporal M

Editor Window Guide

TrueTrace Options Description -

  • Build Aggregated BVH(Recommended to do any time you change objects in edit mode)- Allows you to pre-build objects BVH's before running so you dont have to wait every time you go into play mode for it to build.
  • Clear Parent Data - Clears the data stored in parent GameObjects, allowing you to actually click them without lagging
  • Take Screenshot - Takes a screenshot to the path under "Functionality Settings" in the TrueTrace options
  • Auto Assign Scripts - Assigns all required scripts to all objects under the Scene GameObject, best way to add objects
  • Make All Static - Utility button that takes all objects in the scene and puts them under one parent object, not recommended for general use
  • Force Instances - Looks at all meshes in the scene, sees what objects have the same meshes, and makes them into instances, keep in mind instances use the same material and textures
  • Remaining Objects - Objects still being processed
  • Max Bounces - Sets the maximum number of bounces a ray can achieve
  • Internal Resolution Ratio - Render scale in comparison to gameview size, turn to below 1 while in edit mode to decrease rendered resolution(to then be upscaled)
  • Atlas Size - Maximum size of the texture atlas used(All textures are packed into atlas's so I can send them to the GPU)
  • Use Russian Roulette - Highly recommended to leave this on, kills rays that may not contribute much early, and thus greatly increases performance
  • Enable Object Moving - Allows objects to be moved during play, and allows for added objects to spawn in when they are done building
  • Allow Image Accumulation - Allows the image to accumulate while the camera is not moving
  • Use Next Event Estimation - Enables shadow rays/NEE for direct light sampling
  • RIS Count - Number of RIS passes done for lights(select the best light out of X number of randomly selected lights, only works if LBVH is off in "GlobalDefines.cginc")
  • Allow Mesh Skinning - Turns on the ability for skinned meshes to be animated or deformed with respect to their armeture
  • Denoiser - Allows you to switch between different denoisers
  • Allow Bloom - Turns on or off Bloom
  • Sharpness Filter - Contrast Adaptive Sharpening
  • Enable DoF - Turns on or off Depth of Field, and its associated settings
  • Autofocus DoF - Sets the focal length to bring whatever is in the center of the screen into focus
  • Enable Auto/Manual Exposure - Turns on or off Exposure
  • Use ReSTIR GI - Enables ReSTIR GI which is usually much higher quality(Works with Recur and SVGF denoisers)
  • Do Sample Connection Validation - Confirms that two samples are mutually visable and throws it away if they are not
  • Update Rate - How many pixels per frame get re-traced to ensure they are still valid paths(7 or 33 is a good number to aim for here at 1080p)
  • Unmarked Toggle - Unlabeled toggle, switches between old ReSTIR GI and new ReSTIR GI
  • Enable Temporal - Enables the Temporal pass of ReSTIR GI(allows samples to travel across time
  • Temporal M Cap - How long a sample may live for, lower means lighting updates faster(until 0 which is the opposite) but more noise(recommended either 0 or around 12, but can be played with)
  • Enable Spatial - Enables the Spatial pass of ReSTIR GI(Allows pixels to choose to use the neighboring pixels sample instead)
  • Spatial Sample Count - How many neighboring pixels are looked at(turn to 0 to make it adapative to sample count)
  • Minimum Spatial Radius - The minimum radius the spatial pass can sample from
  • Enable TAA - Enables Temporal Antialiasing
  • Enable Tonemapping - Turns on tonemapping, and allows you to select a specific tonemapper
  • Enable TAAU - Use TAAU for upscaling(if off, you use my semi custom upscaler instead)
  • Use Partial Rendering - Traces only 1 out of X rays
  • Use AntiFirefly - Enables RCRS filter for getting rid of those single bright pixels
  • RR Ignores Primary Hit - Allows for an extra bounce basically, makes it so that dark objects arent noisier, but at the cost of performance
  • Atmospheric Scatter Samples - Lower this to 1 if you keep crashing on entering play mode(controls how many atmospheric samples are precomputed)
  • Current Samples - Shows how many samples have currently been accumulated

Materials(RayTracingObject script)

  • Selected Material - Selects which material you want to edit on the mesh
  • Material Type - Select your type of material you want(You usually want Disney, its the most versatile)
  • Base Color - If theres no Albedo Texture, this is the color the object will be
  • Emission - The emittance of an object(how much light it gives off)
  • Emission Color - Changes the color of emissive objects, most useful when you have an emission mask on an object
  • Thin - Marks an object as thin, Influences Diffuse, DiffTrans, and SpecTrans
  • Roughness - Roughness of the object
  • Roughness Remap - Remaps from the [0,1] of Roughness Slider to the min and max defined here
  • Metallic - How metallic an object is
  • Metallic Remap - Remaps from the [0,1] of Metallic Slider to the min and max defined here
  • IOR - Index of Refraction of an object - Affects Glass and Specular
  • Specular - Adds specular reflection to an object, use in conjunction with Roughness and IOR
  • Specular Tint - Weights color more towards the objects color for specular reflections
  • Sheen - Adds Sheen to objects
  • SheenTint - Allows you to choose if an objects sheen is white or is that objects base color
  • ClearCoat - Adds a ClearCoat effect to the object
  • ClearCoatGloss - Influences the ClearCoat
  • Anisotropic - Makes the material anisotropic based on roughness
  • SpecTrans(Glass) - Makes an object more or less like glass
  • Diffuse Transmission - Makes an object Diffuse but Transmissive(transluscent)
  • Transmission Color - Affects Diffuse Transmission color, must be greater than 0 for diffuse transmission
  • Flatness - Affects Thin objects
  • Scatter Distance - Affects SpecTrans and Diffuse Transmission, must be greater than 0 for Diffuse Transmission
  • Alpha Cutoff - For Cutout objects, this is the value compared against the alpha texture
  • Normalmap Strength - Strength of the Normal Map
  • Blend Color - Color to blend to
  • Blend Factor - Blending weight between Blend Color and the usual color an object would have
  • MainTex Scale/Offset - Albedo texture scales and all texture offset
  • SecondaryTex Scale - All texture scale other than Albedo
  • Link Mat To Unity Material - The material will have its properties overridden by the unity material
  • Texture Scroll Changed - Updates the textures offset/scaling from the shader
  • Propogate To Materials - Copies properties of local material to all other objects in the scene with the same material

Known Bugs:


  • Report any you find! There WILL be bugs, I just dont know what they are
  • DX11 does not work with ASVGF

Huge thanks to these people for being sponsors/patrons:

  • MakIt3D
  • Andrew Varga
  • Duong Nguyen
  • jhintringer

Sample Images(Taken from various stages of development)

Credits(will continue to expand when I have time)

Biggest thanks to Zuen(R.I.P. You will be missed) who helped me a huge amount with the new BVH and traversal, thanks to them I got to where I am now, and I am very thankful to them for their help and patience
https://github.com/jan-van-bergen

Scenes From:


Disney BSDF from: https://schuttejoe.github.io/post/disneybsdf/ Rectangle packer for faster atlas creation from here: https://github.com/ThomasMiz/RectpackSharp/tree/main/RectpackSharp GPU Texture Compression: https://github.com/aras-p/UnityGPUTexCompression OIDN Wrapper: https://github.com/guoxx/UnityDenoiserPlugin

This project uses: Crytek Sponza CC BY 3.0 © 2010 Frank Meinl, Crytek