SDF Lens Blur
SDF shapes with distance-based blur
Move your cursor over the preview to control the focal point.
Quick Start
Loading...
Source
Loading...
Documentation
sdf-lens-blur
SDF shapes with distance-based edge blur. The closer a region is to a focal point (typically the cursor), the softer its edges become — like a depth-of-field effect applied to 2D geometry.
Includes 5 shape variations: rounded rectangle stroke, circle fill/stroke, pill fill/stroke.
Quick Start
import { createSdfLensBlur } from './gpu-modules/sdf-lens-blur/sdf-lens-blur';
const lens = createSdfLensBlur(device);
// Each frame
lens.render(targetTexture, {
mouseX: normalizedX, // 0–1
mouseY: normalizedY, // 0–1
variation: 'round-rect-stroke'
});
lens.destroy();
API
createSdfLensBlur(device, options?)
device—GPUDeviceoptions.format— Output texture format. Default'rgba8unorm'.
Returns { render, destroy }.
render(target, params?)
Renders the SDF shape into a GPUTexture.
target— TheGPUTextureto render into.params.mouseX— Focal point X (0–1). Default0.5.params.mouseY— Focal point Y (0–1). Default0.5.params.variation— Shape type:'round-rect-stroke','circle-fill','circle-stroke','pill-stroke','pill-fill'. Default'round-rect-stroke'.params.circleSize— Radius of the blur influence zone. Default0.3.params.circleEdge— Softness of the influence zone edge. Default0.5.params.shapeSize— Size of the shape. Default1.2.params.roundness— Corner roundness (for round-rect). Default0.4.params.borderSize— Stroke width (for stroke variations). Default0.05.params.shapeRadius— Radius (for circle/pill variations). Default0.3.params.invert— Invert the output. Defaultfalse.
How to Modify
- Add a new shape: Write a new SDF function (see
sd_round_rect,sd_circle,sd_pillfor examples), add a variation branch in the fragment shader, and add the corresponding string toVARIATION_MAPin the TypeScript. - Change the blur source: Replace cursor distance with time, audio input, or any other value — edit the
sdf_circlelens calculation in the fragment shader. - Use as a mask: The output is grayscale. Sample it in another shader to mask or composite layers.
- Add color: Replace the
vec4(v, v, v, 1.0)output with a color ramp or gradient based onv.
Further Reading
Rationale
SDF-based rendering lets you define shapes mathematically and control their edges with precision. By varying the edge softness based on distance from a point, you get a lens/depth-of-field effect — shapes are sharp near focus and blurry elsewhere. This technique is useful for interactive UI effects, creative coding, transitions, and anywhere you want organic, resolution-independent shape rendering.
Original Source
- Codrops: SDF Lens Blur — The technique this module is based on. Demonstrates cursor-driven edge softness on SDF shapes. https://tympanus.net/codrops/
How It Works
The shader evaluates an SDF (signed distance function) for the chosen shape at each pixel. The smoothstep edge width is not constant — it's derived from the distance to the cursor via a second SDF (a soft circle around the focal point). Where the cursor circle has high influence, the edge width is large (blurry); where influence is low, the edge is tight (sharp).
The fill and stroke helpers wrap smoothstep to draw filled or outlined shapes from any SDF.
Further Learning
Inigo Quilez: 2D Distance Functions — Comprehensive collection of 2D SDF primitives. Use these to add more shapes to the module. https://iquilezles.org/articles/distfunctions2d/
Inigo Quilez: Smooth Minimum — Techniques for blending SDFs smoothly, useful if you want to combine multiple shapes. https://iquilezles.org/articles/smin/
The Book of Shaders: Shapes — Introduction to drawing with distance functions and smoothstep. https://thebookofshaders.com/07/