- For more information, see Ray marching on Wikipedia.
| Please expand this article or section. You can help by adding more information if you are an editor. (February 2026) |

Ray marching is a method of iteratively stepping through a scene until a collision is detected, often for the purpose of 3D rendering which this article focuses on. Ray marching is not to be confused with the similarly-named ray casting or ray tracing.
The basic ray marching algorithm works by estimating the closest distance to any object, moving the ray that distance, and repeating the process over and over until the ray reaches an object (its distance is sufficiently small). Ray marching is best for situations where estimating distance is efficient compared to other geometrical representations of the scene (which may be better handled by ray tracing, for example).
The distances are found through evaluating Signed distance functions (SDFs) that represent the shapes.
Tutorial
SDF
This tutorial will use the example of an infinite grid of spheres, each spaced 10 units apart. The spheres will have a radius of 2. The following can be used to find the distance to the closest sphere in such a grid, given a point:
define closest distance (x) (y) (z) set [cell x v] to (((x) mod (10)) - (5)) set [cell y v] to (((y) mod (10)) - (5)) set [cell z v] to (((z) mod (10)) - (5)) set [closest distance v] to (([sqrt v] of (((cell x) * (cell x)) + (((cell y) * (cell y)) + ((cell z) * (cell z))))) - (2))
Main Loop
The following repeatedly draws a raster image on the screen with pen dots.
when gf clicked
set [resolution v] to [16] // the size of the pen dots, larger is faster
forever
erase all
draw screen
end
define draw screen // run without screen refresh
set pen color to [#7acbff]
if <(resolution) > [1]> then
set pen size to ((resolution) * (1.41421))
else
set pen size to (1)
end
set y to ((-180) + ([floor v] of ((resolution) / (2))))
repeat ([ceiling v] of ((360) / (resolution)))
set x to ((-240) + ([floor v] of ((resolution) / (2))))
repeat ([ceiling v] of ((480) / (resolution)))
ray march pixel ((x position) / (480)) ((y position) / (480)) :: custom
set pen (brightness v) to ((50) + (depth))
pen down // draw a pen dot, with brightness dependent on ray depth
pen up
change x by (resolution)
end
change y by (resolution)
end
Ray Stepping
Finally, this script handles the motion of the ray. It first sets the position and direction of the ray based on the given pixel and then steps forward repeatedly, until it is sufficiently close to an object or has traveled to far without doing so.
define ray march pixel (x) (y) set [ray pos x v] to ((0) - ((mouse x) / (50))) // initial ray position (mouse position for interactivity) set [ray pos y v] to ((0) - ((mouse y) / (50))) set [ray pos z v] to [3] set [ray dir x v] to (x) // initial ray direction set [ray dir y v] to (y) set [ray dir z v] to (-0.5) set [magnitude v] to ([sqrt v] of (((ray dir x) * (ray dir x)) + (((ray dir y) * (ray dir y)) + ((ray dir z) * (ray dir z))))) set [ray dir x v] to ((ray dir x) / (magnitude)) // vector normalization to ensure the direction is a unit vector set [ray dir y v] to ((ray dir y) / (magnitude)) set [ray dir z v] to ((ray dir z) / (magnitude)) set [closest distance v] to [Infinity] set [depth v] to [0] // how far the ray has moved repeat until <<(closest distance) < [0.05]> or <(depth) > [100]>> // minimum distance to object or maximum depth without reaching an object distance at (ray pos x) (ray pos y) (ray pos z) :: custom change [ray pos x v] by ((ray dir x) * (closest distance)) // move the ray change [ray pos y v] by ((ray dir y) * (closest distance)) change [ray pos z v] by ((ray dir z) * (closest distance)) change [depth v] by (closest distance) end
Examples
- (100% pen) 3D Raymarcher by Howtomakeausername
- Menger Sponge Raymarcher! by piano_miles
- Realistic raymarcher by BasisEssence779
- Raymarching Tutorial — Part I: Objects by Raihan142857
- 3D fractal raymarcher & point cloud renderer by PeaBrainProgram
- Minecraft Scene Raymarcher by MyRaycasterArchives
- Ray Marching 3D SDFs by sureornot