# Ray Marching Cheat-Sheet

--

This assume you already have the algorithms implemented in a fragment shader. I will use GLSL, but one can easily just switch to HLSL for DX pipeline.

# Animation and movement

Rotate a plane

vec3 planeOrientation = vec3(0.0, 1.0, 0.0);
float planeH = dot(pos, normalize(planeOrientation));
float box = sdBox(pos + vec3(1., -1.5, 0.), vec3(1.5));
return min(planeH, box);

Then we can change where the plane is pointing to

vec3 planeOrientation = vec3(0.5 * sin(iTime), 1., 0.0);
float planeH = dot(pos, normalize(planeOrientation));
float box = sdBox(pos + vec3(1., -1.5, 0.), vec3(1.5));
return min(planeH, box);

Move an object from within the SDF Call:

float d1 = sphere(pos + vec3(1.0, 0.0, 0.0), 2.0);

Move an object using time so it moves:

float d1 = sphere(pos + vec3(sin(iTime), 0.0, 0.0), 2.0);

Make the change of position more than between -1 and 1.

float d1 = sphere(pos + vec3(sin(iTime) * 6, 0.0, 0.0), 2.0);

Make the animation quicker by changing the frequency

float d1 = sphere(pos + vec3(sin(iTime * 5.), 0.0, 0.0), 2.0);

Make animation on more than one axis

float d1 = sphere(pos + vec3(sin(iTime * 5.),-abs(cos(iTime * 2.5)) * 12., 0.0), 2.0);

# Object combination

Chain SDF.

float planeDist = plane(pos);
float d1 = sphere(pos + vec3(3.0, -1.0, 0.), 2.);
float d2 = sphere(pos + vec3(-3.0, -1.0, 0.), 2.);
return min(planeDist, min(d1, d2));

Show the object used by both object

float d1 = sphere(pos + vec3(3.0, -1.0, 0.), 2.);
float d2 = sphere(pos + vec3(3.5, -1.7, 0.), 2.);
return max(d1, d2);

Giving us:

Remove a part from an object

float d1 = sphere(pos + vec3(-1.5, -1., 1.), 2.);
float d2 = sphere(pos, 2.);
return max(-d1,d2);

Let’s say I have two sphere and I want to remove the smaller from the bigger, as it carve a hole.

float d1 = sphere(pos + vec3(-1., -1., 1.), 1.);
float d2 = sphere(pos, 2.);
return max(-d1, d2);

We have this as our main Map return

Create a shell from two object

float d1 = sphere(pos + vec3(3.0, -1.0, 0.), 3.);
float d2 = sphere(pos + vec3(2., -1.7, 1.), 2.);
d2 = abs(d2) — .08;
return max(d1, d2);

Shell an object using a plane

vec3 planeOrientation = vec3(
0.5 * sin(iTime),
sin(iTime), 1.2 * (cos(iTime)))
— sin(cos(iTime * 5.));
float planeH = dot(pos, normalize(planeOrientation));
float box = sdBox(pos + vec3(1., -1.5, 0.), vec3(1.5));
box = abs(box) — .1;
return max(planeH, box);

Slice object and combine them with a plane

vec3 sliceOrientation = vec3(1., 1, -3.);
float pSlice = dot(pos, normalize(sliceOrientation));
float plane = plane(pos);
float box = sdBox(pos + vec3(1., -1.5, 0.), vec3(1.5));
box = abs(box) — .1;
return min(plane, max(box, pSlice));

--

--

In between Montreal and London, I love to write, read, learn and explore.