Ray Marching Distance Functions


Ray Marching Distance Functions

Hope
Welcome to another coding adventure, Maria! Today, we're diving into something really cool that builds on our previous chat about ray-tracing: ray marching. It opens up a whole new world of possibilities for rendering graphics.

Maria
Oh, that sounds intriguing, Hope! Ray-tracing was fascinating, so I'm excited to see what ray marching brings to the table. What's the fundamental idea behind it, and what kind of cool stuff can we actually do?

Hope
Before we get to the really cool visuals, we need to cover the basics: calculating distances. It all starts with the distance between two points, which, thankfully, Pythagoras already solved for us with his famous theorem. It's the building block for everything else.

Maria
Ah, Pythagoras, a classic! So we're essentially measuring how far things are from each other in our virtual space. What's the next step after basic point-to-point distances? Does it get more complex with shapes?

Hope
Absolutely. Next up is the distance from a point to a circle. This is simply the distance from the point to the circle's center, minus the circle's radius. What's interesting here is that this is called a signed distance function.

Maria
A signed distance function? That sounds like it has some hidden meaning. Why is it 'signed'? Does the sign of the distance actually tell us something important, like whether a point is inside or outside the circle?

Hope
Exactly! If the point is inside the circle's radius, the distance will actually be negative. This might seem counterintuitive at first, but it's incredibly useful later on for more advanced techniques, as you'll soon see. It's a key concept.

Maria
That's a clever trick! So, a positive distance means outside, and a negative distance means inside. It's like a concise way to encode spatial relationships. What other basic shapes do we need to calculate distances for to build our scene?

Hope
For now, I'll just add one more: the distance from a point to the edge of a box. With these three functions – point-to-point, point-to-circle, and point-to-box – we can actually figure out how far any point is from the nearest surface in a scene.

Maria
So, by looping through all the shapes and finding the smallest distance, we can always know the closest surface. That's a powerful idea for navigating a virtual world. But how does this connect to 'ray marching' itself?

Hope
Imagine this point is an 'eye,' and we want to see from its perspective. We could cast out a bunch of rays to see what surfaces they collide with. Instead of complex intersection functions for every shape, ray marching offers a more flexible approach.

Maria
Okay, so instead of directly calculating where a ray hits, which can be hard for complex shapes, ray marching uses these distance functions. How does that 'marching' part actually work to find a collision point?

Hope
We start by calculating the distance from our current point on the ray to the entire scene. This gives us a safe radius. We can then move our point along the ray by that exact distance without worrying about passing through any objects.

Maria
That's brilliant! So, the 'distance to the scene' tells us the maximum safe step we can take. We just keep stepping along the ray, calculating the distance at each new position, until we get really, really close to a surface, right?

Hope
Precisely! We repeat this process until the distance becomes extremely small, which signifies that we've effectively collided with something. So, ray marching really just means moving a ray along in steps until you find an intersection.

Maria
That's a much more general approach than specific intersection calculations. It sounds like it could work for literally any shape as long as you can define its distance function. Is there a specific name for this kind of ray marching?

Hope
Yes, the example I'm showing here is a type of ray marching called sphere tracing. It speeds things up by using the radius of that calculated sphere, or circle in 2D, as the step size. It's an optimization that makes it very efficient.

Maria
Sphere tracing, that makes sense. Using the largest possible safe step at each stage is smart. So, if you extend this to three dimensions, you can render cubes, spheres, and really any shape, as long as you have its distance function?

Hope
Exactly! If you can define a distance function for a shape, you can render it with ray marching. And this representation allows us to do some really interesting and powerful things that go beyond just rendering individual objects.

Maria
Okay, 'interesting and powerful' sounds like we're about to get to the 'cool stuff' you mentioned earlier. What's one of the first advanced tricks we can perform with these signed distance functions and ray marching?

Hope
Take this function, for example: it calculates the distance to a cube and a sphere, and then returns the smallest of those two values. Effectively, it's taking the 'min' of the two distances, creating a combined shape where they both exist.

Maria
So, 'min' essentially merges them, showing the parts where either shape is closest to the ray. What happens if you change that to the 'max' of the two values instead of the 'min'?

Hope
If I change it to the 'max' of the two values, both objects suddenly disappear. This is because the surface of a shape is where the distance is zero. For the maximum of two values to be zero or less, both values must be zero or less.

Maria
That's a profound effect! It means we've created a new shape that only exists at the intersection of the cube and the sphere. It's like Boolean operations on 3D objects, but done purely with distance functions.

Hope
Exactly! It's a very elegant way to achieve complex geometry. Now, what if I multiply the distance to the sphere by negative one? Remember how the signed distance function works?

Maria
Multiplying by negative one would invert the sphere! The outside would become solid, and the inside would be hollow. So, if we take the 'max' again with this inverted sphere, we'd be looking for the intersection of the cube and the *outside* of the sphere.

Hope
You've got it! Wherever the inside of the original sphere would touch the cube, that part gets cut away. This ability to invert a shape is a prime example of why having that negative distance value for points inside a shape is so incredibly useful.

Maria
That's incredibly powerful for constructive solid geometry. You can combine, subtract, and intersect shapes just by manipulating their distance functions. What other visual effects can we achieve with this system?

Hope
Another really cool thing is replacing the standard 'min' function with a smooth version. If you have two equations, a regular min creates a sharp junction. But a smooth min function blends them together, creating a seamless connection.

Maria
A smooth min! That would eliminate those sharp edges when combining objects. So, instead of a distinct corner, you'd get a gradual, rounded transition where two shapes meet? That sounds aesthetically pleasing.

Hope
Precisely! Objects that are close together will be blended, and I find the visual result tremendously satisfying. You can also dial down the smoothing to more subtle levels, but I personally love the extreme blend.

Maria
That's a fantastic artistic control. Beyond combining shapes, can we extract other information from the ray marching process itself to create effects?

Hope
Absolutely! Notice how dramatically the number of ray march steps increases as we get very close to the edge of a shape? We can actually use this data. This density of steps tells us something important.

Maria
The number of steps increases near the edge because the safe step size gets smaller and smaller as the ray approaches the surface. So, more steps means closer to a surface? Could we use that for something like an outline?

Hope
Exactly! We can use that data to generate a beautiful, glowing outline around our objects, completely free of charge. It's a wonderful little bonus effect that comes directly from the marching process itself.

Maria
That's a brilliant way to leverage the inherent properties of the algorithm for visual flair. So, once you had all these techniques working, did you start experimenting with more abstract or complex shapes?

Hope
I certainly did! I started messing around, creating a distance function that takes the sign of the current position and blends that with some other nonsense. It resulted in this repetitive, almost glitchy world I could fly around in.

Maria
That sounds like a lot of fun, just exploring a procedural, generated environment. Did this experimentation lead you to think about any other complex mathematical shapes?

Hope
It did! For whatever reason, this got me thinking about fractals. I'd done a basic Mandelbrot Set visualization before, but I wanted something 3D. So I searched for a 3D variation and found the Mandelbulb.

Maria
The Mandelbulb! That's a famous 3D fractal. I've seen images of it, but generating it in real-time with ray marching sounds incredibly complex. Did you have to write a custom distance function for it?

Hope
I actually just found the distance function code and plugged it in, then let it run. I have no idea how it truly works under the hood, but the results were absolutely mesmerizing. It's a testament to the power of these distance functions.

Maria
That's incredible! To go from basic distance calculations to rendering a complex fractal like the Mandelbulb with just a distance function is mind-blowing. This really opens up so many creative possibilities.

Hope
It really does. That was quite an adventure, and we've really only scratched the surface of what can be done with ray marching. There's so much more to explore, from advanced lighting to material properties.

Maria
I can see that! Ray marching is a powerful and versatile technique, far more flexible than I initially imagined. Thanks for taking us through this, Hope. It's been incredibly insightful!

Ask anything
We've read this whole study set, so questions about it get the best answers.