Ray Tracing

With the Unreal Engine 5 demo and ray tracing in general being on the rise in game development, I decided to give it a go in Unity using a compute shader.

Ray tracing comes down to shooting rays into your scene from the camera, in the camera direction, for every pixel. When the ray hits something, it generates a color based on what it hit and its material properties. The reason it looks good is because in ray tracing, these rays can bounce around further than just a single hit, just like light does. Along its path it includes everything it hit for that final pixel color. This means that you get realistic reflections from the get go.

I simply represent the world mathematically, which is easy to do for spheres (position and radius) or an infinite ground plane, trace rays against these representations and output the result to a render texture. The scene you see here actually has nothing in it!

Before trying anything, I read up on common practises and methods. I came across this article series which was very helpful. In case you’re interested in making your own ray tracing effect, I definitely recommend those.

After a while I wanted to try and ray trace a model placed in the scene. This took a lot of math to accomplish since instead of using distance fields, I now had to actually calculate whether the ray intersects with a model triangle with arbitrary shape. 300 of them.

It was an interesting little experiment. I’ll definitely do more with it in the future.