GLSL shader example

Ray-Marched CSG Cutaway

A max with the negated sphere field carves the box. Estimate normals from the combined field.

Live example

Edit the code and the preview updates as you type.

What to observe

Ray-Marched CSG Cutaway.

Mechanism

A max with the negated sphere field carves the box. Estimate normals from the combined field.

The controlling code

vec3 q=abs(p)-vec3(.65);float b=length(max(q,0.))+min(max(q.x,max(q.y,q.z)),0.);return max(b,-(length(p-vec3(.4,0,0))-.65));

Experiment

In scene, change the subtracting sphere radius in length(p-vec3(.3,0,.45))-.65 from .65 to .45. The box's carved cavity shrinks while the box half-size vec3(.65) stays unchanged.

Debugging and cost

Both surface hits and six normal samples evaluate the combined max(b,-sphere) field. Replace the final max expression with b to inspect the intact box; this isolates the subtraction from camera motion and lighting.

Built-in functions used

Learn it step by step