Raymarching is a 3D rendering technique popular in the demoscene and in websites like Shadertoy.
One of the impressive things about demos is that there is no 3D geometry whatsoever. All the objects that make up the scene are generated in real time using mathematical functions.
In this interactive tutorial, we will learn how the raymarching algorithm works from the ground up and create our own raymarcher with shaders.
Before diving into the raymarching algorithm, we need to talk about distances.
You may be familiar with the formula for the distance between two points, it is known as the Euclidean distance and is calculated using the Pythagorean theorem:
Now instead of two points, imagine we want to calculate the distance between a point and a circle. To do that we first calculate the euclidean distance between the point and the center of the circle, we then substract the radius of the circle:
Notice that when the point is inside the circle, the distance is negative—hence "signed" distance function. This fact will become useful later on.
The SDF of an object is a function that, given a point in space as input, outputs the distance from that point to the closest point of the object.
The SDF of rectangle is a little bit more complicated, here is a very good explanation on how to derive it.
If we have multiple shapes and we want to find the closets point to P, we calculate the minimum of all the SDFs of the shapes:
The raymarching algorithm goes like this:
The idea is to run the above algorithm to each pixel on the screen. If the ray hits an object, we set the pixel's color to the object's color. Otherwise, we set the pixel to the background's color.
You can see the iterations of the algorithm in the demo below. click on to start the animation. You can also change the camera's parameters like the field of view or the number of rays we send.Near plane:
Until now, we have only seen how raymarching works in two dimensions. To do 3D rendering we are going to use a fragment shader.