sdf
stableSigned Distance Function primitives for 2D and 3D shapes, plus Boolean CSG operations (union, intersection, subtraction) with optional smooth blending.
use plugin sdf Plugin snippets can be edited in the Playground, but native capabilities are executed locally with the Zolo CLI.
Functions (15)
- sdf_circle Signed distance from a point to a 2D circle
- sdf_box_2d Signed distance from a point to a 2D box
- sdf_sphere Signed distance from a point to a 3D sphere
- sdf_box_3d Signed distance from a point to a 3D box
- sdf_torus Signed distance from a point to a 3D torus
- sdf_cylinder Signed distance from a point to a 3D cylinder
- sdf_plane Signed distance from a point to a 3D plane
- sdf_union CSG union of two distances
- sdf_intersection CSG intersection of two distances
- sdf_subtraction CSG subtraction of two distances
- sdf_smooth_union Smooth CSG union with blending factor
- sdf_smooth_intersection Smooth CSG intersection with blending factor
- sdf_smooth_subtraction Smooth CSG subtraction with blending factor
- sdf_round Round a shape by offsetting its surface
- sdf_annular Make a shape hollow (annular shell)
From import to result
- 01Import only what you use
- 02Copy the smallest example
- 03Run it with the Zolo CLI
Signed distance from a point to a 2D circle
Returns the signed distance from point (px, py) to a circle centered at (cx, cy) with the given radius. Negative values mean the point is inside the circle.
use plugin sdf::{sdf_circle}
let d = sdf_circle(0.0, 0.0, 0.0, 0.0, 5.0)
print(d) // -5.0 (at center, inside)
let d2 = sdf_circle(7.0, 0.0, 0.0, 0.0, 5.0)
print(d2) // 2.0 (outside, 2 units past edge)Signed distance from a point to a 2D box
Returns the signed distance from point (px, py) to an axis-aligned 2D box centered at (cx, cy) with half-extents half_w and half_h.
use plugin sdf::{sdf_box_2d}
let d = sdf_box_2d(0.0, 0.0, 0.0, 0.0, 2.0, 1.0)
print(d) // -1.0 (inside, closest edge is 1 unit away)
let d2 = sdf_box_2d(4.0, 0.0, 0.0, 0.0, 2.0, 1.0)
print(d2) // 2.0 (outside, 2 units past the right edge)Signed distance from a point to a 3D sphere
Returns the signed distance from point (px, py, pz) to a 3D sphere centered at (cx, cy, cz).
use plugin sdf::{sdf_sphere}
let d = sdf_sphere(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0)
print(d) // -3.0 (at center)
let d2 = sdf_sphere(5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0)
print(d2) // 2.0 (outside)Signed distance from a point to a 3D box
Returns the signed distance from point (px, py, pz) to an axis-aligned 3D box. hw, hh, hd are the half-extents along X, Y, Z respectively.
use plugin sdf::{sdf_box_3d}
let d = sdf_box_3d(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
print(d) // -1.0 (inside unit cube at center)Signed distance from a point to a 3D torus
Returns the signed distance from point (px, py, pz) to a torus centered at the origin lying in the XZ plane. R is the major radius (center of tube) and r is the minor radius (tube thickness).
use plugin sdf::{sdf_torus}
let d = sdf_torus(3.0, 0.0, 0.0, 3.0, 0.5)
print(d) // ~-0.5 (on the ring center, inside the tube)Signed distance from a point to a 3D cylinder
Returns the signed distance from point (px, py, pz) to a finite cylinder centered at the origin aligned along the Y axis.
use plugin sdf::{sdf_cylinder}
let d = sdf_cylinder(0.0, 0.0, 0.0, 1.0, 2.0)
print(d) // -1.0 (at center, inside)
let d2 = sdf_cylinder(2.0, 0.0, 0.0, 1.0, 2.0)
print(d2) // 1.0 (outside radially)Signed distance from a point to a 3D plane
Returns the signed distance from point (px, py, pz) to an infinite plane defined by normal (nx, ny, nz) and a scalar offset from the origin.
use plugin sdf::{sdf_plane}
// Horizontal plane at y=0, normal pointing up
let d = sdf_plane(0.0, 3.0, 0.0, 0.0, 1.0, 0.0, 0.0)
print(d) // 3.0 (3 units above the plane)
let d2 = sdf_plane(0.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
print(d2) // -1.0 (below the plane)CSG union of two distances
Returns the union (minimum) of two signed distances. Represents the surface that encloses both shapes.
use plugin sdf::{sdf_circle, sdf_union}
let d1 = sdf_circle(3.0, 0.0, 0.0, 0.0, 2.0)
let d2 = sdf_circle(3.0, 0.0, 5.0, 0.0, 2.0)
let combined = sdf_union(d1, d2)CSG intersection of two distances
Returns the intersection (maximum) of two signed distances. Represents only the region inside both shapes simultaneously.
use plugin sdf::{sdf_sphere, sdf_box_3d, sdf_intersection}
let d1 = sdf_sphere(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 2.0)
let d2 = sdf_box_3d(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.5, 1.5, 1.5)
let carved = sdf_intersection(d1, d2)CSG subtraction of two distances
Subtracts shape d1 from shape d2. The result is the region inside d2 but outside d1.
use plugin sdf::{sdf_sphere, sdf_subtraction}
let outer = sdf_sphere(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0)
let inner = sdf_sphere(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5)
let shell = sdf_subtraction(inner, outer)Smooth CSG union with blending factor
Smooth union with blending radius k. When k > 0, the boundary between the two shapes is blended rather than sharply joined. Larger k produces a rounder blend.
use plugin sdf::{sdf_circle, sdf_smooth_union}
let d1 = sdf_circle(2.0, 0.0, 0.0, 0.0, 1.5)
let d2 = sdf_circle(2.0, 0.0, 4.0, 0.0, 1.5)
let blended = sdf_smooth_union(d1, d2, 0.8)Smooth CSG intersection with blending factor
Smooth intersection of two distances with blending radius k. The transition zone at the boundary is smoothed instead of sharp.
use plugin sdf::{sdf_sphere, sdf_smooth_intersection}
let d1 = sdf_sphere(1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0)
let d2 = sdf_sphere(1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0)
let blended = sdf_smooth_intersection(d1, d2, 0.5)Smooth CSG subtraction with blending factor
Smooth subtraction of d1 from d2 with blending radius k.
use plugin sdf::{sdf_sphere, sdf_smooth_subtraction}
let outer = sdf_sphere(0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 3.0)
let hole = sdf_sphere(0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5)
let result = sdf_smooth_subtraction(hole, outer, 0.3)Round a shape by offsetting its surface
Rounds a shape by subtracting r from its distance. This expands the surface outward by r units, giving sharp edges a rounded appearance.
use plugin sdf::{sdf_box_2d, sdf_round}
let box_d = sdf_box_2d(3.0, 0.0, 0.0, 0.0, 2.0, 1.0)
let rounded = sdf_round(box_d, 0.2) // corners rounded by 0.2Make a shape hollow (annular shell)
Creates a hollow shell (annular) version of a shape. Takes the absolute value of the distance and subtracts r, producing a thin surface at distance r from the original boundary.
use plugin sdf::{sdf_circle, sdf_annular}
let circle_d = sdf_circle(5.0, 0.0, 0.0, 0.0, 3.0)
let ring = sdf_annular(circle_d, 0.1) // thin ring shell