Raymarching a Sphere
by GameDev.net · GLSL ES 3.00 (WebGL2) · 25 Aug 2026
Run the shader to adjust these controls.
What it demonstrates
Raymarching renders a 3D scene with no geometry at all. Each pixel fires a ray and walks it forward, asking the distance function how far it is safe to move. When the answer is small enough, the ray has arrived. That is the whole algorithm, kept in one file with the step count and hit threshold exposed so both failure modes are reachable.
Shader inputs
void mainImage(out vec4 fragColor, in vec2 fragCoord)
Called once per pixel. Write the colour to fragColor.
-
iResolutionvec3 - Viewport size in pixels (z is the pixel aspect ratio).
-
iTimefloat - Seconds since the shader started.
-
iTimeDeltafloat - Seconds since the previous frame.
-
iFrameRatefloat - Frames per second, smoothed.
-
iFrameint - Frames rendered since the start.
-
iMousevec4 - Mouse position: xy while held, zw of the last click.
-
iDatevec4 - Year, month, day, and seconds within the day.
-
iChannel0sampler2D - Texture bound to channel 0.
-
iChannel1sampler2D - Texture bound to channel 1.
-
iChannel2sampler2D - Texture bound to channel 2.
-
iChannel3sampler2D - Texture bound to channel 3.
-
iChannelResolutionvec3[4] - Pixel size of each bound channel texture.
-
iChannelTimefloat[4] - Playback time of each channel, in seconds.
-
iSampleRatefloat - Audio sample rate, always 44100.
Main Image7
uniform float uCameraDistance; // @param 1.6..6.0 = 3.0 "Camera distance"
uniform int uMaxSteps; // @param 4..128 = 64 "Max steps"
uniform float uSurfaceEpsilon; // @param 0.0005..0.05 = 0.002 "Surface epsilon"
float mapScene(vec3 position) {
return length(position) - 1.0;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
vec3 origin = vec3(0.0, 0.0, -uCameraDistance);
vec3 direction = normalize(vec3(uv, 1.0));
float travelled = 0.0;
float hit = -1.0;
for (int i = 0; i < 128; i++) {
if (i >= uMaxSteps) break;
vec3 position = origin + direction * travelled;
float dist = mapScene(position);
if (dist < uSurfaceEpsilon) {
hit = travelled;
break;
}
travelled += dist;
if (travelled > 12.0) break;
}
vec3 sky = mix(vec3(0.05, 0.07, 0.12), vec3(0.14, 0.20, 0.32), uv.y + 0.5);
if (hit < 0.0) {
fragColor = vec4(sky, 1.0);
return;
}
vec3 position = origin + direction * hit;
vec3 normal = normalize(position);
vec3 toLight = normalize(vec3(0.6, 0.8, -0.5));
float diffuse = max(dot(normal, toLight), 0.0);
float rim = pow(1.0 - max(dot(normal, -direction), 0.0), 3.0);
vec3 color = vec3(0.20, 0.45, 0.78) * (0.12 + 0.88 * diffuse);
color += vec3(0.35, 0.55, 0.85) * rim * 0.6;
fragColor = vec4(color, 1.0);
}
Learn from this shader
How it works
The fragment coordinate becomes a point on an image plane, and the ray direction is that point pushed one unit forward and normalised. Marching by exactly the distance the field returns is the key insight: nothing is nearer than that, so a full step can never pass through a surface. Near a surface the steps become tiny, which is why arrival is a threshold rather than zero. Two escapes bound the loop, a step budget and a maximum travel distance, so a ray aimed at empty space terminates. The sphere uses an exact normal, which for a unit sphere is the surface point itself.
Try changing
Reduce Max steps until the silhouette dissolves into bands: the rays that fail first are the grazing ones, which take many small steps near the surface. Raise Surface epsilon and watch the sphere inflate and its edge soften, since the ray stops further out. Pull the camera in close to see the field of view widen.
Using it in a game
This is the core loop behind volumetric fog, cloud and smoke rendering, screen space refraction, and every procedural scene that has no mesh. Step counts are the cost, and grazing rays are the worst case, so real uses clamp the budget and accept the artefacts you just produced deliberately.
Explore the techniques
Continue with curated explanations and progressively related examples.
Discussion