A Signed Distance Field (SDF) is a function or texture where each point stores the distance to the nearest edge or surface. The “Sign” indicates whether you are inside (negative) or outside (positive) the shape.
if (dist > 0 && dist < thickness)In a shader, rendering a circle is just calculating the distance from the current pixel to the center, then subtracting the radius.
float sdfCircle(vec2 p, float r) {
return length(p) - r;
}
// In Fragment Shader:
float d = sdfCircle(uv, 0.5);
float col = d > 0.0 ? 0.0 : 1.0; // Hard edge
float aaCol = smoothstep(pixelSize, 0.0, d); // Anti-aliased edge
Because SDFs are just numbers, you can combine shapes using simple math instead of complex mesh clipping:
min(dA, dB) (Combine shapes)max(dA, dB) (Where they overlap)max(dA, -dB) (Cut a hole in A using B)min(dA, dB) - k * max(0, ...) (The “Metaball” or “Blobby” effect)SDFs are the foundation of Raymarching (seen on Shadertoy). Instead of checking for triangle intersections, a ray “marches” forward by the distance stored in the SDF. Since the SDF tells you the “safe” distance to the nearest surface, you never over-step.
Standard SDFs can lose detail at sharp corners. MSDF stores distance information in three color channels (RGB). By taking the median of the three, you can maintain perfectly sharp corners even for complex fonts and icons at tiny texture sizes.
SDFs turn geometry into a continuous mathematical field. They are the ultimate tool for high-quality UI, procedural 3D modeling, and performant visual effects.