/Realtime-Compute-Shader-Unity-PathTracer

A passion projects that has been going on for awhile, finally at a place where I feel comfortable tentatively uploading it to Github for others

Primary LanguageC#

Notes:
Currently working on:

  • Need Ideas, father has passed away so work will either slow down or speed up
Currently needs to be done but havent implemented fully:
  • Volumetric Clouds(struggling, they look garbage)
  • Volumetric Voxels(Struggling to make it fast enough, have so far tried sparse voxel octrees, DDA, and now brickmaps, but its not fast enough, any advice is welcome)
Currently want to do but havent started:
  • Need More Ideas

Compute Shader Based Unity PathTracer

A passion projects that has been going on for awhile(about a year in unity, with my earliest version I can find being version 30(whereas I am now on version 263), which was made on 5-7-2021), finally at a place where I feel comfortable tentatively uploading it to Github for others to use What is it? Its my attempt at a Real-Time pathtracer built from scratch in Unity using Compute Shaders

Features:

  • Somewhat fast Compute Shader based path tracing
  • Diffuse, Glossy(kinda), Dielectric(think of glass), Conductor(metal), Diffuse Transmission, Emissive, Plastic, and Disney BSDF materials
  • Ability to move, add, and remove objects during play
  • Ability to update material properties on the fly during play
  • SVGF Denoiser and Atrous Denoiser
  • BVH Building off of main thread for loading objects, allows objects to be spawned, and then built without lagging the main thread, and appearing when its done(All lag from spawning objects actually comes from remaking the texture atlas, lower res atlas's remove all lag, still investigating different ways of loading textures because of this)
  • Compressed Wide Bounding Volume Hierarchy as the Acceleration Structure (See Ylitie et al. 2017 below)
  • Albedo Textures(just apply them to the GameObjects material)
  • Next Event Estimation with Multiple Importance Sampling for Explicit Light Sampling
  • Objects are loaded as gameobject meshes(most common way of having meshes in unity)
  • Support for default unity lights which interact via NEE(Supports Directional, Point, and Spot lights)
  • Support for Normal maps and Emission masks
  • Global homogenous fog with adjustable density
  • Taking Full Resolution Screenshots
  • Bloom
  • PBR Texture Support
  • No specific GPU vendor needed(this will run on integrated graphics if you so wish it, aka no RTX cores)
  • MagicaVoxel support
  • Ability to pathtrace voxels and triangle scenes at the same time seamlessly
  • Depth of Field
  • AutoExposure
  • Temporal Anti-Aliasing
  • ReSTIR for better sampling of many lights
  • Explicit light sampling for faster convergence
  • Precomputed Multiple Atmospheric Scattering for dynamic and realtime sky(from ebruneton below)
  • Object Instancing
  • Multiple Importance Sampling for helping NEE converge much faster

Ylitie et al
ebruneton

If you have any questions, or suggestions, etc. let me know either through github issues or something else! I am always looking for more stuff to add, and more ways to make it more user friendly or appealing for others to use, and ways to improve this overall You can contact me easiest through my discord: Pjbomb2#6129

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:

  • Set the Color Space to Linear through Edit Tab(Top Left) -> Project Settings -> Player -> Other Settings -> Color Space, and change from Gamma to Linear
  • 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
  • Enable Unsafe Code(Its for memory management) through Edit -> Project Settings -> Player -> Other Settings -> "Allow 'unsafe' Code" (near the bottom)

## Additional Requirements
  • You need to make sure that all textures have Read/Write enabled in their import settings(click on a texture in the Project menu, look at its options in the inspector, turning on Read/Write, and clicking apply at the bottom). I would also reccomend turning off MipMapping
  • For Skinned Meshes, their index format needs to be set to 32 bits, and their mesh to Read/Write enabled. This can be found by clicking on the imported fbx, going to it in the inspector, going to the Model tab, turning on Read/Write, changing the Index Format from Auto to 32 Bit, and clicking Apply at the bottom

## General Setup
  • Download and import the UnityPackage provided
  • For quick setup, make sure you have a Main Camera(there by unity default), just open the Pathtracer Settings menu under the Pathtracer tab, it will reorganize the Hierarchy a bit, and give everything their required scripts If you dont want it to do this, and do it manually read below

## Setting it up manually
  • You need to do the below before opening the Pathtracer settings window or else it will do its autosetup. Additionally, you can study the provided DemoScene's hierarchy, as it provides basically all common variations of objects and their relations
  • First, you need a main camera, which unity automatically provides. This camera will need attatched to it the RayTracingMaster script, under Assets/Resources, and should have the FlyCamera Script attatched(Under Assets/Resources/Utility)
  • Next, you need a GameObject called Scene, and this will be the gameobject that all others except the camera will be parented to(It will be the root object). This gameobject will need the AssetManager script attatched to it, found under Assets/Resources/BVH
  • Next, all objects you want to trace will need a parent. This parent can either be themselves for individual objects, or will need to be nested under another gameobject to be grouped(for increasing performance, group wherever you can). These Parents need to have a ParentObject attatched, as this defines groups, with its children all being seen as one group(Located under Assets/Resources/BVH)
  • Finally, all meshes and skinned meshes you want to trace need a RayTracingObject script attatched to them, as this defines what should be and should not be pathtraced(Located under Assets/Resources)
  • One last note, if your using Unity lights as well, each one of these needs a RayTracingLights script attatched to it(Located under Assets/Resources)
## General Use/Notes
  • Objects can be added and removed at will simply by toggling them on/off in the hierarchy(dont click them if they are complex objects), but they will take time to appear
  • If you change the emissiveness of an object, you need to dissable and re-enable its parent(basically reloading it) if you want to take advantage of NEE correctly sampling it
  • If you use normal maps, they need to be in unity normal map format
  • To set up PBR, all textures go into their proper names, but Roughness goes into the Occlusion texture(Since path tracing gets ambient occlusion by default, this texture is not normally needed, and there being no proper place for a Roughness texture in the default material, I have decided this was a good compromise)
  • 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
## MagicaVoxel Usage
  • Before anything, the files need to be in .txt format, to do this, go to the file in file explorer, and rename the .vox to .txt and change the format
  • Firstly, you still need to have a gameobject under the scene gameobject to attatch your voxel model to
  • Second, you need to attatch a VoxelObject to that gameobject(Located under Assets->Resources->BVH->VoxelObject)
  • Next you need to attatch the voxel model to this script, by dragging your voxel model asset in the project tab to the VoxelRef space in the VoxelObject script
  • That should be it, it will get grouped into the building along with meshes, and having at least 1 voxel object in the scene will turn on its inclusion. Removing or turning off all voxel related gameobjects will turn it back off

## Using Instancing
  • First, there needs to be a gameobject called InstancedStorage in the scene with the InstanceManager attatched to it
  • Second, 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 their layout of parentobject to raytracingobjects)
  • Finally, to instance the objects, you just need empty 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(all of this is displayed in the demoscene)

Controls:

Camera Controls: WASD, Mouse, and press T to freeze the camera

Editor Window Guide

BVH Options Description -

  • Build Aggregated BVH - 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. To know when its done building, the Object Parent Name will appear in the console telling you its complete(and thus wont need to be rebuilt every time you hit play)
  • Clear Parent Data - Clears the data stored in parent gameobjects, allowing you to actually click them without crashing or lagging(but will then require the BVH to be rebuilt)
  • Sun Position - REMOVED, so now the sun can be anywhere, not just in a disk
  • Max Bounces - Sets the maximum number of bounces a ray can achieve
  • Use Russian Roulette - Highly reccomended 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
  • Allow Volumetrics - Turns on pathtracing of global volumetric homogenous fog
  • (If Allow Volumetrics is on) Volume Density - Adjusts density of the global fog
  • Allow Mesh Skinning - Turns on the ability for skinned meshes to be animated or deformed with respect to their armeture
  • Allow Bloom - Turns on or off Bloom
  • Use DoF - Turns on or off Depth of Field, and its associated settings
  • Use Auto Exposure - Turns on or off Auto Exposure(impacts a lot more than I thought it would)
  • Use ReSTIR - Enables the much better sampling for lots of lights
  • Allow ReSTIR Sample Regeneration - Applies if Precomputed Sampling is on, Regenerates the light samples every frame
  • Allow ReSTIR Precomputed Sampling - Samples lights in a more efficient way but introduces artifacts due to sample correlation
  • Allow ReSTIR Temporal - Enables the Temporal pass of ReSTIR(allows samples to travel across time
  • Allow ReSTIR Spatial - Enables the Spatial pass of ReSTIR(Allows pixels to choose to use the neighboring pixels sample instead)
  • Use Temporal Antialiasing - Enables Temporal Antialiasing(TAA)
  • Use SVGF Denoiser - Turns on the SVGF denoiser
  • (If SVGF Denosier is on)Atrous Kernel Size - The amount of times the SVGF denoiser runs through the Atrous kernel
  • Use Atrous Denoiser - Turns on the Atrous denoiser(can be combined with SVGF)
  • Enable Tonemapping - Turns on Filmic Tonemapping
  • Current Samples - Shows how many samples have currently been accumulated
  • Take Screenshot - Takes a screenshot at game view resolution and saves it to Assets/ScreenShots(You need to create this folder)
  • QuickStart - Will attempt to automatially assign RayTracingObjects and ParentObjects to all child under the GameObject named "Scene" with an AssetManager attatched

Materials

  • Emission - Pretty self explanatory, the higher it is, the bright the object is(and the higher chance it will be sampled for NEE)
  • Emission Color - Only used for emission masks, if you have an emission mask, make this non-zero to color the emission from the mask, otherwise keep it at 0
  • Roughness - Applys to Conductors, Dielectrics, glossy, Diffuse Transmission, and plastics - Higher roughness makes objects more rough, but in Diffuse Transmission it basically represents how clear the material is
  • Eta - For Conductors it changes the color of reflected light, but inverted, but for Dielectrics, only the x component is used, and that X component is the Dielectrics IOR(Index of Refraction, with 1 being air), and for Subsurface Scattering, it is used the same as for conductors, and the same for Plastic materials
  • Base Color - So this will be automatically set to whatever the material of the objects color is, and it will also be overridden by textures, but its there so you can manually change it, works for all material types

Sample Images(Taken from various stages of development)

Credits(will continue to expand when I have time)

Biggest thanks to Zuen 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://www.shadertoy.com/view/sltXRl

Ideas/Reminders for later

ReGIR, Find an alternative to atlas's, Reduce memory consumption, work on adding back in Mitsuba scene support