Creating custom ray tracing shaders for unity

I have been hoping to implement some cool real-time ray tracing effects for an independent study, without taking the time to build my own DirectX12 engine from the ground up. But I have hit some significant snags when trying to write my own ray tracing shaders in Unity.

First of all, I am having difficulties defining a “raygeneration” shader for the camera to use. I have done quite a bit of digging and it seems like I need to bind a “raygeneration” shader to a camera, using a CommandBuffer. More specifically, using the method “CommandBuffer.DispatchRays”. It also appears that I need to send an “accelerationstructure” to the shader so it has access to all of the ray traceable objects in the environment, using “commandBuffer.SetRayTracingAccelerationStructure()”. So, I am wondering what I need to do to get/update the accelerationstructure and to create and bind everything I need to the CommandBuffer.

Secondly, as far as I can tell, .raytrace shaders cannot be applied to a material, unlike many other surface shaders. To get a hitshader to work on a material, it must be defined within another kind of shader. I got this information from this Siggraph 2019 slideset:

Is this correct? If so, can I recursively spawn rays in these surface shaders?

I have recently been searching for info on this topic too. there’s not much out there from what I can tell, but it seems like you (at the time of your post) and I have a similar understanding. I believe you also need to call BuildRayTracingAccelerationStructure once per frame to handle updates, as it mentions in the slides you provided a link to. However, when I check Unity’s documentation, I don’t see any sign of this function. (Unity - Scripting API: RayTracingAccelerationStructure)

I also think Unity only automatically handles game objects with mesh renderer components. Otherwise you’d have to manually handle the acceleration structure yourself.

TraceRay() can be called from any of the ray tracing shader types I believe: RayGen, Intersection, Hit, AnyHit, and Miss shaders. So wherever you need to recursively cast a ray, I believe you can just call TraceRay().

However, I have done 0 testing of this, and still trying to find information on how to implement custom shaders using the API and not Unitys predefined raytracing shaders in the HDRP… there doesnt seam to be any examples on anything though… disappointing!

Did you have any luck figuring things out?