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. More information might be found in a section of the talk page. (February 2026)
Ray marching example showing a Menger sponge fractal.

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 using SDFs works by estimating the closest distance, moving the ray that distance, and repeating the process over and over until the ray intersects with the object. Ray marching is useful in situations where estimating a distance to the scene's shapes are efficient, such as in fractals. Elsewhere, ray marching is slower than ray tracing due to the need for many steps.

Tutorial

An SDF based ray marcher uses SDFs, which stands for Signed Distance Functions. These are calculations and instructions used to see if the ray has touched an object. Another thing that makes this special is that it draws individual pixels instead of columns, and can easily draw objects at different heights.

Variables needed

(Ray X)
(Ray Y)
(Ray Z)
(Length)
(Origin X)
(Origin Y)
(Origin Z)
(Camera X)
(Camera Y)
(Camera Z)
(Collided?)
(Distance :: variables)
(Render Distance)
(Pen Size)
(Nearest Distance)
(Color)

Main Loop

This starts all the other scripts.

when gf clicked
erase all
set [Render Distance v] to (5000)
set [Pen Size v] to (15) //Can be any size greater than 0 but this is better for speed
set [Camera X v] to (0)
set [Camera Y v] to (0)
set [Camera Z v] to (-100)
go to x: (-240) y: (-180)
forever
go to x: (-240) y: (-180)
repeat (((360)/(Pen Size))+(1))
set x to (-240)
repeat (((480)/(Pen Size))+(1))
read keys :: custom //We will cover this later
raycast pixel :: custom //Next script in the tutorial
change x by (Pen Size)
end
change y by (Pen Size)
end
erase all
end

Ray Marching Script

This script is what casts the ray.

define raycast pixel
set [Nearest Distance v] to (Render Distance)
set [Collision? v] to (0)
set [Ray X v] to (Center X)
set [Ray Y v] to (Center Y)
set [Ray Z v] to (Center Z)
set [Distance v] to (0)
set pen color to (#000000)
repeat until <<(Collision?) = (1)> or <(Dist) > (Render Distance)>>
check distances :: custom //Will be covered later
move (5) steps in direction (y position) (x position) :: custom // Next in tutorial
end
if <(Collision?) = (1)> then
set pen color to (#FF0000)
set pen (color v) to (Color)
end
pen down
pen up

Moving the Ray

define move (n) steps in direction (rotation x) (rotation y)
change [Ray X v] by ([sin v] of (rotation y))
change [Ray Y v] by ([cos v] of (rotation y))
change [Ray Z v] by ([sin v] of (rotation x))

Checking If the Ray Has Collided

This is the distance function that will be used to get the distance from any point to the ray position.

define length (a) (b) (c)
set [Length v] to ([sqrt v] of ((((a) - (Ray X)) * ((a) - (Ray X))) + ((((b) - (Ray Y)) * ((b) - (Ray Y))) + (((c) - (Ray Z)) * ((c) - (Ray Z)))))

This checks collision with a sphere.

define sphere (x) (y) (z) (radius) (color)
length (x) (y) (z) :: custom //Distance from center of sphere to ray
set [Distance v] to ((Length) - (Radius))
collision (color)

define collision (color)
if <not<(Distance :: variables) > (0)>> then
if <(Distance :: variables) < (Nearest Distance)> then
set [Nearest Distance v] to (Distance :: variables)
set [color v] to (color)
end
set [collision v] to (1)

This block contains all the SDFs and will check collisions with all objects.

define check distances
sphere (0) (0) (0) (50) (50) :: custom //Add as much as you want

Moving the Camera

define read keys
change [Camera X v] by (<key (d v) pressed?> - <key (a v) pressed?>)
change [Camera Y v] by (<key (q v) pressed?> - <key (z v) pressed?>)
change [Camera Z v] by (<key (w v) pressed?> - <key (s v) pressed?>)

Your SDF-based ray marcher is now ready!

Examples

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.