home/ atoms/ sdf-surface-normal-gradient

The SDF gradient estimated by finite differences gives the surface normal needed for lighting

Lighting requires knowing which direction a surface faces (its normal). For an SDF scene the normal is the gradient of the distance field at the hit point, approximated by sampling the SDF at six nearby points (±epsilon in x, y, z) and taking finite differences. If the SDF is smaller to the left than to the right, the normal’s x-component points right (towards the outside). This tetrahedral or central-difference gradient is equivalent to the analytic surface normal for exact SDFs and remains a good approximation for blended or displaced geometry.

Examples

vec3 calcNormal(vec3 p){ vec2 e=vec2(0.001,0); return normalize(vec3(map(p+e.xyy)-map(p-e.xyy), map(p+e.yxy)-map(p-e.yxy), map(p+e.yyx)-map(p-e.yyx))); }

Assessment

Describe what happens to the estimated normal when the epsilon value is too large vs too small, and why this matters for lighting quality.

“evaluate derivatives, so we are going to approximate them actually. very quickly with small differences. I was looking at my phone”
corpus · inigo-quilez-live-coding-happy-jumping-video · chunk 3