1. Home
  2. Explore
  3. Computer Science
  4. Ray Marching Distance Functions

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Ray Marching Distance Functions

Transcript

Hello and welcome to another coding adventure. While I was messing around with ray-tracing last   episode, I came across a technique called ray  marching which lets one do some pretty cool stuff.  But before we can get to the cool stuff, we need  to be able to calculate distances, starting with   the distance between two points. Fortunately, Pythagoras   has this one figured out already. Next the distance from a point to a circle. This is just the distance between the point and  the circle Center minus the radius of that circle.  By the way, this is called a signed distance  function, because if the point was inside the   radius of the circle the distance would  actually be negative and you'll see why   this is useful a little later on. But for now, I'm just going to add   one more distance function, which is the  distance from a point to the edge of a box.

Wherever the point travels in this world of  boxes and circles we can figure out how far   away it is from the nearest surface by just  looping over all of the shapes in the scene,   calculating their distances and  returning the smallest of those values.  Imagine now that this point is actually an  eye, and we want to see from its point of   view. So we could cast out a bunch of rays  and see which surfaces they collide with. To tell where this collision occurs, we could  write some kind of intersection function,   but that would limit us to using only shapes  whose intersection we know how to calculate.  To get around this we'll start by  calculating the distance to the scene.  Which gives us a radius in which we  can safely move our point along the   ray without it going through anything. We then simply repeat this process until

the distance becomes really, really small,  which means we've collided with something.  So ray marching really just means:  moving a ray along in small steps,   until you find an intersection. The example I'm showing here is a type of   ray marching called sphere tracing, which speeds  things up by using the radius of this sphere or   circle that we're calculating as the step size. If you extend this idea to three dimensions and manage not screw it up in the process,  like I did, you can then render cubes,   spheres, and really any other shape that  you can define a distance function for.  With this representation, we  can do some interesting stuff.  For example, take this function, which  calculates the distance to a cube and a   sphere and returns whichever is smallest.  In other words the min of the two values.  If I change it to the max of the two  values, suddenly both objects disappear.

This is because the surface of the shape is  where the distance is zero and the only way   the maximum of two values is going to be zero  or less is if both values are zero or less.  In other words, we've created a new shape  that only exists at the intersection   of the cube and the sphere. If I then multiply the distance   to the sphere by negative 1 this will  essentially invert the sphere, making the outside of it solid and the inside hollow. So the shape now only exists at the intersection   of the cube and the outside of the sphere. Meaning, wherever the inside of the sphere   touches will get cut away. This ability to invert a shape   is one of the reasons why it's useful for our  distance function to be negative on the inside.  Another cool thing we can do is replace  the min function with a smooth version.  For example, if we have two equations  this would be the min of them.

But a smooth min would look like this. The result of this is that objects that are   close together will be blended together, which  I find tremendously satisfying for some reason.  The smoothing can of course be dialed down to  more reasonable levels, but I prefer it like this.  Yet another thing is: notice how dramatically  the number of ray march steps increases as we   get close to the edge of a shape. We can use this data to generate a nice glowing outline around our  objects entirely free of charge.  Once I had all this working, I thought it'd be fun  to just mess around a bit, so I made up a distance   function that takes the sign of the current  position and blends that with some other nonsense.  It resulted in this repetitive little world  that I could fly around in, and tweaking the   parameters made it all glitchy and weird. It was kind of fun to explore for a little

bit and for whatever reason this  got me thinking about fractals.  Since I've previously made a very underwhelming  visualization of the Mandelbrot Set, I searched   for some kind of 3D variation on it, and came  across a distance function for the Mandel Bulb.  I have no idea how it works but I  plugged the code in and let it run.  And I think the results is somewhat mesmerizing.

[Music] Well that   was quite an adventure but I've of course  only barely scratched the surface of what   can be done with ray marching. If you'd like to learn more,   have a look at the links in the description. You can also find my project files there,   if you'd like to play with the  stuff I showed in this video.  Anyway, I hope you'll join me on more coding  adventures in the future, but until then: Cheers

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent