sdfBox

distance function for a rectangular box.

Parameters

NameTypeDescription
pvec2<f32>Point to evaluate distance from.
bvec2<f32>Box half-dimensions (width/2, height/2).

Returns

f32distance to box surface (negative inside, positive outside).

WGSL Code

fn sdfBox(p: vec2<f32>, b: vec2<f32>) -> f32 {
let d = abs(p) - b;
return length(max(d, vec2(0.0))) + min(max(d.x, d.y), 0.0);
}

sdfBoxFrame

a signed distance field for a 3D box frame (hollow box).

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
sizevec3<f32>Half-extents of the box.
thicknessf32Wall thickness of the frame.

Returns

f32distance to the box frame surface.

WGSL Code

fn sdfBoxFrame(position: vec3<f32>, size: vec3<f32>, thickness: f32) -> f32 {
let q = abs(position) - size;
let w = abs(q + thickness) - thickness;
return min(min(
length(max(vec3<f32>(q.x, w.y, w.z), vec3<f32>(0.0))) + min(max(q.x, max(w.y, w.z)), 0.0),
length(max(vec3<f32>(w.x, q.y, w.z), vec3<f32>(0.0))) + min(max(w.x, max(q.y, w.z)), 0.0)),
length(max(vec3<f32>(w.x, w.y, q.z), vec3<f32>(0.0))) + min(max(w.x, max(w.y, q.z)), 0.0));
}
Showing 5/8 lines

sdfCappedTorus

a signed distance field for a capped torus.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
majorRadiusf32Major radius of the torus.
minorRadiusf32Minor radius of the torus.
anglef32Cap angle in radians.

Returns

f32distance to the capped torus surface.

WGSL Code

fn sdfCappedTorus(position: vec3<f32>, majorRadius: f32, minorRadius: f32, angle: f32) -> f32 {
let sc = vec2<f32>(sin(angle), cos(angle));
let q = vec3<f32>(abs(position.x), position.y, position.z);
let k = select(
length(q.xy),
dot(q.xy, sc),
sc.y * q.x > sc.x * q.y
);
return sqrt(dot(q, q) +
majorRadius * majorRadius -
2.0 * majorRadius * k) -
minorRadius;
}
Showing 5/13 lines

sdfCapsule

a signed distance field for a capsule (cylinder with rounded caps).

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the capsule.
heightf32Height of the cylindrical portion.

Returns

f32distance to the capsule surface.

WGSL Code

fn sdfCapsule(position: vec3<f32>, radius: f32, height: f32) -> f32 {
let d = abs(length(position.xz)) - radius;
let p = vec2<f32>(d, abs(position.y) - height * 0.5);
return length(max(p, vec2<f32>(0.0))) + min(max(p.x, p.y), 0.0) - radius;
}

sdfCircle

distance function for a circle.

Parameters

NameTypeDescription
pvec2<f32>Point to evaluate distance from.
rf32Circle radius.

Returns

f32distance to circle surface (negative inside, positive outside).

WGSL Code

fn sdfCircle(p: vec2<f32>, r: f32) -> f32 {
return length(p) - r;
}

sdfCone

a signed distance field for a cone.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Base radius of the cone.
heightf32Height of the cone.

Returns

f32distance to the cone surface.

WGSL Code

fn sdfCone(position: vec3<f32>, radius: f32, height: f32) -> f32 {
let q = vec2<f32>(length(position.xz), position.y);
let h = height;
let r = radius;
// Calculate distance
let d1 = -q.y - h;
let d2 = max(q.x * h - q.y * r, q.y * h + q.x * r);
return length(max(vec2<f32>(d1, d2), vec2<f32>(0.0))) + min(max(d1, d2), 0.0);
}
Showing 5/11 lines

sdfCylinder

a signed distance field for a cylinder.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the cylinder.
heightf32Height of the cylinder.

Returns

f32distance to the cylinder surface.

WGSL Code

fn sdfCylinder(position: vec3<f32>, radius: f32, height: f32) -> f32 {
let d = vec2<f32>(length(position.xz), abs(position.y)) - vec2<f32>(radius, height * 0.5);
return min(max(d.x, d.y), 0.0) + length(max(d, vec2<f32>(0.0)));
}

sdfEllipsoid

a signed distance field for an ellipsoid.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusvec3<f32>Radii along each axis.

Returns

f32distance to the ellipsoid surface.

WGSL Code

fn sdfEllipsoid(position: vec3<f32>, radius: vec3<f32>) -> f32 {
let k0 = length(position / radius);
let k1 = length(position / (radius * radius));
return k0 * (k0 - 1.0) / k1;
}

sdfGyroid

a signed distance field for a gyroid surface.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
scalef32Scale factor for the gyroid pattern.
thicknessf32Thickness of the gyroid surface.

Returns

f32distance to the gyroid surface.

WGSL Code

fn sdfGyroid(position: vec3<f32>, scale: f32, thickness: f32) -> f32 {
let p = position * scale;
return (abs(dot(sin(p), cos(p.zxy))) - thickness) / scale;
}

sdfHexagonalPrism

a signed distance field for a hexagonal prism.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the hexagon.
heightf32Height of the prism.

Returns

f32distance to the hexagonal prism surface.

WGSL Code

fn sdfHexagonalPrism(position: vec3<f32>, radius: f32, height: f32) -> f32 {
// Project into 2D
var p = abs(position);
let k = vec3<f32>(-0.866025404, 0.5, 0.577350269);
// Hexagon in xy-plane
p = vec3<f32>(p.x + p.y * k.x, p.y * k.y, p.z);
p = vec3<f32>(p.x - min(p.x, p.y), p.y, p.z);
let d = vec2<f32>(length(vec2<f32>(p.x, p.y - radius * k.z)) - radius, abs(p.z) - height * 0.5);
return min(max(d.x, d.y), 0.0) + length(max(d, vec2<f32>(0.0)));
}
Showing 5/12 lines

sdfIcosahedron

a signed distance field for an icosahedron.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
sizef32Size of the icosahedron.

Returns

f32distance to the icosahedron surface.

WGSL Code

fn sdfIcosahedron(position: vec3<f32>, size: f32) -> f32 {
var p = position;
let s = size;
// Constants for icosahedron
let phi = 1.618033988749895;
let a = s;
let b = s * phi;
// Compute distance to icosahedron
p = abs(p / s);
let d = p.x * p.y * p.z;
let m = max(max(p.x, p.y), p.z);
let n = min(min(p.x, p.y), p.z);
let mid = p.x + p.y + p.z - m - n;
// Calculate the signed distance
let q = select(mid, d, m < phi * n);
return (length(p) - phi) * s;
}
Showing 5/20 lines

sdfIntersection

two SDFs using intersection operation (overlapping area only).

Parameters

NameTypeDescription
d1f32Distance from first shape.
d2f32Distance from second shape.

Returns

f32distance representing intersection of both shapes.

WGSL Code

fn sdfIntersection(d1: f32, d2: f32) -> f32 {
return max(d1, d2);
}

sdfJulia

a signed distance field for a 4D Julia set fractal.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
cvec4<f32>Julia set parameter (quaternion).
iterationsf32Maximum number of iterations.
bailoutf32Bailout radius for iteration escape.

Returns

f32distance to the Julia set surface.

WGSL Code

fn sdfJulia(position: vec3<f32>, c: vec4<f32>, iterations: f32, bailout: f32) -> f32 {
var z = vec4<f32>(position, 0.0);
var dz = vec4<f32>(1.0, 0.0, 0.0, 0.0);
var m = dot(z, z);
var i = 0;
// Julia set iteration
for (i = 0; i < i32(iterations) && m < bailout * bailout; i += 1) {
// Quaternion multiplication for dz = 2.0 * z * dz
dz = 2.0 * vec4<f32>(
z.x * dz.x - z.y * dz.y - z.z * dz.z - z.w * dz.w,
z.x * dz.y + z.y * dz.x + z.z * dz.w - z.w * dz.z,
z.x * dz.z - z.y * dz.w + z.z * dz.x + z.w * dz.y,
z.x * dz.w + z.y * dz.z - z.z * dz.y + z.w * dz.x
);
// Quaternion multiplication for z = z * z + c
z = vec4<f32>(
z.x * z.x - z.y * z.y - z.z * z.z - z.w * z.w,
z.x * z.y + z.y * z.x + z.z * z.w - z.w * z.z,
z.x * z.z - z.y * z.w + z.z * z.x + z.w * z.y,
z.x * z.w + z.y * z.z - z.z * z.y + z.w * z.x
) + c;
m = dot(z, z);
}
// Compute the distance
let dist = 0.5 * log(m) * sqrt(m) / length(dz);
return dist;
}
Showing 5/31 lines

sdfOctahedron

a signed distance field for an octahedron.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
sizef32Size of the octahedron.

Returns

f32distance to the octahedron surface.

WGSL Code

fn sdfOctahedron(position: vec3<f32>, size: f32) -> f32 {
let p = abs(position);
let m = p.x + p.y + p.z - size;
// Calculate the distance
var q: vec3<f32>;
if (3.0 * p.x < m) {
q = p;
} else if (3.0 * p.y < m) {
q = vec3<f32>(p.x, p.z, p.y);
} else if (3.0 * p.z < m) {
q = vec3<f32>(p.x, p.y, p.z);
} else {
q = p;
}
let k = clamp(0.5 * (q.z - q.y + size), 0.0, size);
return length(vec3<f32>(q.x, q.y - size + k, q.z - k));
}
Showing 5/19 lines

sdfPlane

a signed distance field for an infinite plane.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
normalvec3<f32>Normal vector of the plane (should be normalized).

Returns

f32distance to the plane surface.

WGSL Code

fn sdfPlane(position: vec3<f32>, normal: vec3<f32>) -> f32 {
let n = normalize(normal);
return dot(position, n);
}

sdfPyramid

a signed distance field for a pyramid.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
sizef32Base size of the pyramid.
heightf32Height of the pyramid.

Returns

f32distance to the pyramid surface.

WGSL Code

fn sdfPyramid(position: vec3<f32>, size: f32, height: f32) -> f32 {
// Normalize position
var p = position;
let h = height;
let m2 = h * h + size * size;
// Project into 2D
let q = abs(p);
p.y -= h;
p.y = max(p.y, 0.0);
// Distance calculation
var d: f32;
if (max(q.x, q.z) < size) {
d = length(vec2<f32>(length(p.xz), p.y)) - sqrt(m2);
} else {
d = length(vec2<f32>(length(max(abs(p.xz) - vec2<f32>(size), vec2<f32>(0.0))), p.y));
}
// Account for position below base
d = select(d, length(p) - sqrt(m2), p.y < 0.0);
return d;
}
Showing 5/24 lines

sdfRhombus

a signed distance field for a rhombus.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
dimensionsvec3<f32>Dimensions of the rhombus.
sharpnessf32Sharpness factor for the edges.

Returns

f32distance to the rhombus surface.

WGSL Code

fn sdfRhombus(position: vec3<f32>, dimensions: vec3<f32>, sharpness: f32) -> f32 {
var p = abs(position);
let b = dimensions;
let e = sharpness;
// Calculate distance to rhombus
p = p - b;
let q = abs(p.x + p.y + p.z) + e;
let h = max(vec3<f32>(q) - vec3<f32>(e), vec3<f32>(0.0));
return min(max(p.x, max(p.y, p.z)), 0.0) + length(h);
}
Showing 5/12 lines

sdfRoundBox

a signed distance field for a rounded box.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
sizevec3<f32>Half-extents of the box.
radiusf32Rounding radius for the edges.

Returns

f32distance to the rounded box surface.

WGSL Code

fn sdfRoundBox(position: vec3<f32>, size: vec3<f32>, radius: f32) -> f32 {
let q = abs(position) - size;
return length(max(q, vec3<f32>(0.0))) +
min(max(q.x, max(q.y, q.z)), 0.0) -
radius;
}
Showing 5/6 lines

sdfRoundedCone

a signed distance field for a rounded cone.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radius1f32Bottom radius of the cone.
radius2f32Top radius of the cone.
heightf32Height of the cone.
roundnessf32Rounding factor for the edges.

Returns

f32distance to the rounded cone surface.

WGSL Code

fn sdfRoundedCone(position: vec3<f32>, radius1: f32, radius2: f32, height: f32, roundness: f32) -> f32 {
// Calculate distances
let p = position;
let r1 = radius1 - roundness;
let r2 = radius2 - roundness;
let h = height;
// Squared distance from axis
let q = length(p.xz);
// Project into 2D space
let k1 = (r2 - r1) / h;
let k2 = h / (r1 - r2);
let projected = vec2<f32>(q - r1 + r1 * (p.y / h) * (r1 - r2) / r1, p.y - h);
let ca = p.y * k1 - q;
let cb = p.y - r1 * k2 + q * k2;
var s: f32;
if (ca < 0.0 && projected.y < 0.0) {
s = length(projected) - roundness;
} else if (ca > 0.0 && cb < 0.0) {
s = -ca - roundness;
} else {
s = length(vec2<f32>(max(ca, 0.0), max(projected.y, 0.0))) - roundness;
}
return s;
}
Showing 5/28 lines

sdfRoundedCylinder

a signed distance field for a rounded cylinder.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the cylinder.
heightf32Height of the cylinder.
roundnessf32Rounding factor for the edges.

Returns

f32distance to the rounded cylinder surface.

WGSL Code

fn sdfRoundedCylinder(position: vec3<f32>, radius: f32, height: f32, roundness: f32) -> f32 {
// Calculate distances
let radiusOffset = radius - roundness;
let heightOffset = height * 0.5 - roundness;
// Generate rounded cylinder
let d = vec2<f32>(length(position.xz) - radiusOffset, abs(position.y) - heightOffset);
return min(max(d.x, d.y), 0.0) + length(max(d, vec2<f32>(0.0))) - roundness;
}
Showing 5/9 lines

sdfSphere

a signed distance field for a sphere.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the sphere.

Returns

f32distance to the sphere surface.

WGSL Code

fn sdfSphere(position: vec3<f32>, radius: f32) -> f32 {
return length(position) - radius;
}

sdfSubtraction

two SDFs using subtraction operation (first shape minus second).

Parameters

NameTypeDescription
d1f32Distance from shape to subtract from.
d2f32Distance from shape to subtract.

Returns

f32distance representing first shape with second subtracted.

WGSL Code

fn sdfSubtraction(d1: f32, d2: f32) -> f32 {
return max(-d1, d2);
}

sdfTetrahedron

a signed distance field for a tetrahedron.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
sizef32Size of the tetrahedron.

Returns

f32distance to the tetrahedron surface.

WGSL Code

fn sdfTetrahedron(position: vec3<f32>, size: f32) -> f32 {
var p = position;
let s = size;
// Set initial values
let signVal = sign(p.x + p.y + p.z);
p.x = abs(p.x);
p.y = abs(p.y);
p.z = abs(p.z);
// Calculate the distance
if (p.x < p.y) {
let t = p.x;
p.x = p.y;
p.y = t;
}
if (p.x < p.z) {
let t = p.x;
p.x = p.z;
p.z = t;
}
if (p.y < p.z) {
let t = p.y;
p.y = p.z;
p.z = t;
}
let k = clamp((p.x + p.z - p.y) * 0.5, 0.0, p.z);
return signVal * (length(vec3<f32>(p.x, p.y - s, p.z - k)) - s);
}
Showing 5/30 lines

sdfTorus

a signed distance field for a torus.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
majorRadiusf32Major radius of the torus.
minorRadiusf32Minor radius of the torus.

Returns

f32distance to the torus surface.

WGSL Code

fn sdfTorus(position: vec3<f32>, majorRadius: f32, minorRadius: f32) -> f32 {
let q = vec2<f32>(length(position.xz) - majorRadius, position.y);
return length(q) - minorRadius;
}

sdfTriangularPrism

a signed distance field for a triangular prism.

Parameters

NameTypeDescription
positionvec3<f32>3D position to evaluate.
radiusf32Radius of the triangular base.
heightf32Height of the prism.

Returns

f32distance to the triangular prism surface.

WGSL Code

fn sdfTriangularPrism(position: vec3<f32>, radius: f32, height: f32) -> f32 {
var q = abs(position);
// Triangle distance in xy-plane
let k = sqrt(3.0);
q.x = abs(q.x - q.y * k * 0.5);
q.y = q.y * 0.866025404 + q.x * 0.5;
// Combine with z distance
let d1 = vec2<f32>(q.x - radius, q.y);
let d2 = vec2<f32>(q.y - radius, q.x);
let d = min(d1, d2);
// Account for height
let h = height * 0.5;
let dz = q.z - h;
let dz2 = max(dz, 0.0);
return length(max(vec2<f32>(max(d.x, 0.0), dz2), vec2<f32>(0.0))) + min(max(d.x, dz), 0.0);
}
Showing 5/20 lines

sdfUnion

two SDFs using union operation (closest surface).

Parameters

NameTypeDescription
d1f32Distance from first shape.
d2f32Distance from second shape.

Returns

f32distance representing union of both shapes.

WGSL Code

fn sdfUnion(d1: f32, d2: f32) -> f32 {
return min(d1, d2);
}