Creating a Ray Tracer in Geometry Nodes

This was a silly side project and I don’t see a real use for it, but I wanted to share it anyway.
TLDR: I created a path tracer in geometry nodes, including a depth and normal buffer and stochastic sampling with the simulation zone.

The raycast node is pretty fascinating, and I thought it should be possible to simulate several bounces in a scene, accumulate the sampled values and thus create a sort of path tracer completely inside geometry nodes.

image

Here’s how it works:

  1. I created a grid and parented it to the camera. I subdivided the grid to change the resolution of the canvas
  2. I created rays that shot out from every face of the grid onto the scene. The scene (=target) needs to be a single object for this to work
  3. Various attributes can be sampled from the surface of the target. In my case I sampled the normal value, the distance from the camera and a vertex color value.
  4. I then calculated the outgoing ray direction based on the incoming direction, reflected along the normal vector of the surface position
  5. In total I perform three bounces, so four ray casts. Each bounce contributes less to the pixel values
  6. A single ray direction would result in a super noise image. That’s why I moved all the ray bounce logic into a simulation zone. The ray directions are randomized ever so slightly with every frame, and the sum of sampled values is then divided by the frame count to get an average and less noise pixel value

Here’s the depth buffer:

And the normal buffer:

The accumulating indirect light bounces:

Using a simple phong shader in the material nodes, I added direct light to the scene. The phong shader takes the camera direction, the normal buffer and a light direction to simulate diffuse light and highlights on a surface:

A fun way to create a sort of watercolor effect is by displacing the pixel grid itself:

The whole thing is obviously pretty slow, as it all runs unoptimized on the CPU, and I haven’t yet found a proper use case for this thing. But at least it gives me complete control over the whole tracing and render pipeline, so maybe there’s potential.

13 Likes

This reminds me of the raytracer made within the scripting language of PovRay
…saying this… there was also something within blender… :thinking: ( in 2.79 already ??)

1 Like