Using the DirectX Raytracing API in Unity
I'm trying to find examples / info on how to build custom shaders using the DirectX Raytracing API in unity.
This presentation:
(video) https://developer.nvidia.com/siggraph/2019/video/sig940-vid
shows exactly what I want to accomplish : use the DirectX API to handle my custom raytracing methods. However, there are not enough details, and it seems like it may be a little out of data now based on the documentation that is available in Unity:
https://docs.unity3d.com/ScriptReference/Experimental.Rendering.RayTracingAccelerationStructure.html
So, here's where my understanding is, and please correct me if I'm wrong (I probably am)!
1)
I need to write a Ray Generation shader by creating a .raytrace shader. This is easy ( I think ). It can be dispatched like a compute shader from the c# side in Unity.
2)
Because the direction of my rays will change continuously as it moves through space searching for an intersection, I need to take more of a ray marching approach with a small step size. So, from the Ray Generation shader, I will call TraceRay() function from the DirectX API on the initially created ray with a small tMax parameter.
3)
Since I am ray marching to find an intersection, and updating the direction of the ray at each step, I need to spawn a new ray on every miss - so I need a custom Miss shader that accompanies my Ray Generation shader (same file?). The miss shader will calculate the new ray direction and origin and call TraceRay() again. So this is a recursive process, and the Miss shader will be calculated MANY times until some max distance traveled by the ray has been reached or until we encounter a hit!
4)
In order to tests for a hit, I need an Intersection shader. Since I will be using meshes in my scene - with mesh renderers, I believe Unity will automatically manage the Acceleration Structure by adding the game objects with a mesh renderer to the structure in order to be tested for intersection. And so I don't think I need to make my own custom intersection shader or manually manage the Acceleration structure. But I'm confused on how exactly I use the RayTracingAccelerationStructure in Unity - the documentation is not clear to me. Can someone clear this up?
5)
When an intersection is encountered, a Hit shader will be called (I don't need to worry about AnyHit shaders I don't think, so we'll ignore those for now). Basically, I just want this shader to return the mesh texture at the intersected UV coordinate. I believe for this I just need to add a Hit shader that does this as a sub shader in the mesh's material, right?
6)
From there I can graphics.Blit the texture that the raytracing writes to (?)
I would really like to see a full unity example that implements these steps like I've laid out. It would be really helpful! Also, if I am misunderstanding something, please let me know.
Thanks.
Your answer
Follow this Question
Related Questions
Performance of looping compute shader in Update() 2 Answers
Making a custom shader use GPU Instancing? 1 Answer
Read float values from RGBAFloat texture 1 Answer
Shader Sprites/Default shader is not supported on this GPU iPad 8th Gen iOS 14.5 0 Answers
How to read vertex positions back after they have been transformed in shader(GPU)? 1 Answer