home/ atoms/ sdf-boolean-operations

SDF union, intersection, and subtraction combine primitive shapes into complex ones

Signed distance field shapes can be combined using boolean-style operations performed on the raw distance values. Union (min(d1,d2)) merges two shapes into one. Intersection (max(d1,d2)) keeps only the overlapping region. Subtraction removes one shape from another by negating one distance (max(-d1,d2) or max(d1,-d2) depending on which is subtracted). XOR takes the non-overlapping parts of both. All these operations work purely on the scalar distance values returned by each SDF, so they compose freely regardless of shape. A common misconception is that these require geometry processing — they operate on distance numbers, not vertices. The ordering of operands in subtraction matters: max(-d1,d2) removes d1 from d2, while max(d1,-d2) removes d2 from d1.

Examples

float d1 = sdCircle(uv, 0.2);
float d2 = sdBox(uv, vec2(0.2));
float res = min(d1, d2);   // union
// float res = max(d1, d2);  // intersection
// float res = max(-d1, d2); // subtract circle from box

Assessment

Given two SDFs d1 and d2, write the GLSL expression for: (a) their union, (b) the region inside d1 but not d2. Then predict what max(-d1, d2) looks like compared to max(d1, -d2).

“we can use 2D SDF operations to create more complex shapes by combining primitive shapes together.”
corpus · shadertoy-tutorial-nathan-vaughn-inspirnathan · chunk 1