SDF

sdf contains a number of helper functions for working with signed distance fields.

I highly recommend checking out Inigo Quilez’s excelent resource on the sdf primitives.

2d SDF


3d SDF


module sdf
sphere( samplePosition: float3,sphereCenter: float3,radius: float ) -> float
box( samplePosition: float3,boxCenter: float3,boxSize: float3 ) -> float
roundBox( samplePosition: float3,boxCenter: float3,boxSize: float3,cornerRadius: float ) -> float
boxFrame( samplePosition: float3,boxCenter: float3,boxSize: float3,frameThickness: float ) -> float
torus( samplePosition: float3,torusCenter: float3,torusSize: float2 ) -> float
cappedTorus( samplePosition: float3,torusCenter: float3,torusSize: float2,ra: float,rb: float ) -> float
infinteCylinder( samplePosition: float3,cylinderCenter: float3,cylinderSize: float3 ) -> float
cone( samplePosition: float3,coneCenter: float3,c: float2,coneHeight: float ) -> float
plane( samplePosition: float3,planeCenter: float3,planeNormal: float3,planeHeight: float ) -> float
hexPrism( samplePosition: float3,hexCenter: float3,hexSize: float2 ) -> float
triPrism( samplePosition: float3,triCenter: float3,triSize: float2 ) -> float
capsuleLine( samplePosition: float3,lineStart: float3,lineEnd: float3,capsuleRadius: float ) -> float
capsule( samplePosition: float3,capsuleCenter: float3,height: float,radius: float ) -> float
cylinder( samplePosition: float3,cylinderCenter: float3,height: float,radius: float ) -> float
cylinderLine( samplePosition: float3,lineStart: float3,lineEnd: float3,radius: float ) -> float
roundedCylinder( samplePosition: float3,cylinderCenter: float,cylinderRadius: float,cornerRadius: float,height: float ) -> float
cappedCone( samplePosition: float3,coneCenter: float3,height: float,r1: float,r2: float ) -> float
solidAngle( samplePosition: float3,solidCenter: float3,size: float2,radius: float ) -> float
cutSphere( samplePosition: float3,sphereCenter: float3,radius: float,height: float ) -> float
cutHollowSphere( samplePosition: float3,sphereCenter: float3,radius: float,height: float,thickness: float ) -> float
roundCone( samplePosition: float3,coneCenter: float3,coneRadius: float,cornerRadius: float,height: float ) -> float
ellipsoid( samplePosition: float3,center: float3,radius: float3 ) -> float
rhombus( samplePosition: float3,rhombusCenter: float3,la: float,lb: float,height: float,ra: float ) -> float
octahedron( samplePosition: float3,center: float3,size: float ) -> float
pyramid( samplePosition: float3,pyramidCenter: float3,height: float ) -> float
udTriangle( samplePosition: float3,a: float3,b: float3,c: float3 ) -> float
udQuad( samplePosition: float3,a: float3,b: float3,c: float3,d: float3 ) -> float

Returns the intersection of two SDFs = max(a, b)

intersect( a: float,b: float ) -> float

Returns the union of two SDFs = min(a, b)

union( a: float,b: float ) -> float
subtract( a: float,b: float ) -> float
smoothIntersect( a: float,b: float,k: float ) -> float

Smoothly joins two SDFs using blending distance k

smoothUnion( a: float,b: float,k: float ) -> float
smoothSubtract( a: float,b: float,k: float ) -> float

Blends between two SDFs using interpolation

blend( a: float,b: float,t: float ) -> float
round( a: float,r: float ) -> float

Carve out the interior of an SDF

onion( a: float,thickness: float ) -> float
grad( p: float3,sampler: (pos: float3) => float ) -> float3

Poorly returns the closest point on the surface of an SDF This is an inaccurate method

closest( position: float3,sampler: (pos: float3) => float ) -> float3
normal( position: float3,sampler: (pos: float3) => float ) -> float3
vizRings( d: float ) -> float4
march( position: float3,direction: float3,sampler: (pos: float3) => float ) -> float

See: https://iquilezles.org/articles/distfunctions/ Infinitely repeats an SDF with space in between. This returns an updated position

Example:

sdf::sphere(sdf::repeat(p, 100.xyz), 0.xyz, 50.0);
repeat( position: float3,space: float3 ) -> float3

See: https://iquilezles.org/articles/distfunctions/ Repeats an SDF with space in between upto the limit. This returns an updated position

Example:

sdf::sphere(sdf::repeatLimited(p, 100, 10.xyz), 0.xyz, 50.0);
repeatLimited( position: float3,space: float,limit: float3 ) -> float3
clone( ) -> sdf