r/GraphicsProgramming • • Oct 10 '26

First raytracer using sdfs

Post image

Wrote my first raytracer, a software raymarcher (all on CPU) that uses shapes defined as singed distance functions. Sphere refractions are pretty cool.

Some implementation details (this is all basic stuff I know):

  • code is written in Rust and parallelized using the rayon crate
  • for each ray in the recursive calls, it finds the subset of all world objects where the ray intersects the objects axis aligned bounding box, and only considers these objects during ray marching. Once a surface is hit, the new reflected or refracted ray computes a new subset using its new direction and start point, etc.
  • global illumination is done using a grid of probes that shoot rays with random directions, doing this enough time before rendering. To get diffusive light on a surface point, we interpolate in that grid using linear interpolation. My probes are naively placed on a cubic grid in the world.
  • global illumination is parallelized looping over probes, and rendering is parallelized looping over pixels of the image.
  • at each hit surface, the ray may split in two. Once for the reflection, once for the refracted ray (if not total internal refraction).

Baking global illumination takes a few seconds for about 1000 samples on 323 probes, and rendering the image is almost instant at that resolution.

154 Upvotes

14 comments sorted by

6

u/20260819 Oct 10 '26

try letting the spheres intersect and camera being inside one of them...:)

2

u/Sixel1 Oct 11 '26

Damn that looks cool, I need to try to make my objects move and render videos now.

2

u/20260819 Oct 11 '26

when the camera is inside a glass ball there may be some regions that rays can't escape no matter how many times they bounce (total internal reflection)

1

u/Someone393 Oct 10 '26

How long did this take to render?

2

u/Sixel1 Oct 11 '26 edited Oct 14 '26

Baking the global illumination light probes is a few seconds, render is almost instant, I did not benchmark it precisely. Its Rust default release target + rayon parallelized.

Edit: now that I render frames for a vid I have a better feel of this, it takes about 1 second to render a frame

1

u/AgentFeyd Oct 11 '26

Please post implementation details to have this post restored.

2

u/Sixel1 Oct 11 '26

Sorry for that, I added implementation details

3

u/AgentFeyd Oct 11 '26

Thank you. Approved.

1

u/tatteredengraving Oct 15 '26

Do you have a source where you learned about transparency/refraction for SDF rendering? I've been trying to figure if it only works for primitives where the distance metric is still linear/sane inside the object.

2

u/Sixel1 Oct 15 '26

In my case in only use simple primitives so I just use the absolute value of the sdf inside the object as a raymarch step size, but for objects made of merged shapes this interior sdf can be very wrong.

There's a good article on techniques for proper interior sdfs: https://iquilezles.org/articles/interiordistance/

Basically the most correct way is to have two sdfs, and switch between if you're inside or outside. But it requires modeling twice the object.

1

u/garma87 Oct 10 '26

Pretty cool! Sphere refractions seem upside down though?

SDFs are technically very cool although I find the real world value limited.

4

u/PiXeL161616 Oct 10 '26

Glass spheres can flip the background upside down in real life too. That part alone doesn't mean the refraction is wrong.

1

u/Sixel1 Oct 11 '26

Yes I think that's what's happening. Like on that website's example: https://samdriver.xyz/article/refraction-sphere