Feature Library

SDF meshing: distance fields to triangles, live, on the CPU or the GPU (engine-free WebGPU)

1.30 GPU · 0.24 CPU ms
A still of the SDF meshing: distance fields to triangles, live, on the CPU or the GPU (engine-free WebGPU) demo
Still
The live demo starts when it’s in view.Scrub the throw and the pot re-meshes · Mesh on the GPU or the CPU · Cells across · Dual contouring or surface nets · Blend width · Curvature and cavity views · Show the triangles · Drag to look round

The HaTac engine’s distance-field mesher, lifted whole and engine-free.

Give it a signed distance field in local metres, a box and a cell size, and it returns an indexed, outward-wound triangle mesh with a normal, a signed mean curvature and a cavity value at every vertex. Two paths share one assembly: a CPU path that evaluates a JavaScript callback, and a WebGPU path that evaluates a WGSL function in four compute passes (sample the lattice, classify cells, plan an indirect dispatch, place vertices) and assembles the read-back records on the CPU. Dual contouring solves a small regularised least-squares problem per cell so box corners and cut rims stay sharp; surface nets average the crossings for a softer, cheaper surface. Ambiguous faces get their own vertices, so meshes stay closed and manifold; edges are refined by bracketed secant steps; a mesh that crosses its box can grow inward skirts to hide the seam between chunks at two resolutions. Sphere, box, rounded box, capsule, cylinder and torus, with hard and smooth union, subtraction and intersection, in both languages. The library adds a uniform the WGSL field can read, so a shape that changes every frame is re-meshed by writing a buffer instead of recompiling, and a small host to upload and draw the result. The demo is a potter’s workshop in low window light: a pot is thrown on the wheel from a lump to a jug, re-meshed from its field as it changes shape, and the glazed pots on the shelves read their curvature and cavity.

What it costs a frame

1.30 ms
0.24 ms
GPU
1.30 ms
CPU, median
0.24 ms
CPU, 95th pct
0.35 ms
Draw calls
0
Weight
Moderate

Measured 28 November 2026 on RTX 4060 Laptop, 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 158, 1920×1080 at 1×. 600 frames × 3 rounds, median kept (rounds ranged GPU 0.89–1.60, CPU 0.23–0.24 ms). What ran: Alone: the throw playing, meshed on the GPU at four resolutions (32, 64, 96, 128 cells across a 0.4 m box), one of them re-meshed each frame in turn (compute, readback, CPU assembly, upload), drawn with its shadow map in the workshop’s light. Phones: tiers are being measured next.

How steadily it runs on RTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference

Typical frame ?
16.72 ms
1% low ?
53 fps
Varies by ?
±0.53 ms
All metricsRTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900H · reference
p50p95p9916.02 ms18.64 ms +
How long each frame took. Most frames sit in the tall bars; a long tail to the right is hitching.

Frames run on a 60 Hz timer (a page demo), so most frames take about one tick, 16.7 ms: the spread around it is what matters.

Frames

Median (p50) ?
16.72 ms
p95 ?
17.73 ms
p99 ?
18.09 ms
Slowest ?
21.26 ms
Mean ?
16.78 ms
Standard deviation ?
0.53 ms
1% low ?
53 fps
0.1% low ?
47 fps
Hitches ?
0 of 600
Over budget ?
0 of 600

Main thread

Long tasks ?
0
Long frames ?
0
Event-loop delay ?
0.0 / 1.0 / 5.6 ms

Input

No input during this run.

Memory

JavaScript heap ?
89.6 → 88.0 MB
Growth ?
-1.58 MB

Browser

JavaScript a frame ?
0.29 ms
All tasks a frame ?
4.04 ms
Layout / style ?
0.32 / 0.16 ms
Main thread busy ?
24%

Garbage collection

Collections ?
46 (18 minor, 28 major)
Pauses ?
83.1 ms in all, longest 3.8

What it needs

Renders with
WebGPUNeeds WebGPU: current Chrome and Edge, Safari 26+, Firefox 141+ on Windows.
Your browser
Checking…Looking for WebGPU
Depends on
NothingNo other packages
With an agent
install("sdf-meshing")Your coding agent reads its pitfalls and installs it through the library’s MCP server. Set it up

What it’s for

  1. Organic props a game or a configurator builds from code instead of a modelling tool: rocks, pots, blobby creatures, carved stone, machined parts with fillets
  2. A shape that changes while people watch (sculpting, a growing form, a part being machined) re-meshed every frame on the GPU
  3. Meshing an implicit surface once at load, on a worker, for a page that must not block its main thread
  4. Materials that need to know where a surface is a ridge or a hollow: dirt and moss in crevices, worn paint and polish on edges, glaze that pools
  5. Terrain or cave chunks meshed at several resolutions, with skirts so the coarse and fine chunks meet without cracks
  6. A closed, manifold mesh from constructive solid geometry for export, 3D printing or physics
  7. Comparing a CPU and a GPU path for the same field: the same topology from both

Pitfalls, in brief

  1. The demo needs WebGPU: the wheel’s pot is meshed in compute and the workshop is drawn with WGSL pipelines.
  2. Use local coordinates and place the mesh with a matrix when you draw it.
  3. Sample thin walls and small holes finely.
  4. Keep the field a true distance in metres.
  5. Make the field’s CPU and WGSL versions the same arithmetic in the same order if both paths mesh it.
9 more in brief
  1. Recreate a GPU mesher when the field’s WGSL, the box, the cell size or the device changes: its pipelines and buffers are built for them.
  2. Read the field’s uniform inside sdf itself.
  3. Run one mesh at a time per mesher: mesh() and read() refuse while a readback is in flight.
  4. Budget the CPU side of the GPU path.
  5. The CPU callback path blocks its thread for the whole mesh: hundreds of milliseconds for a blended field at 128³.
  6. Dense surfaces overflow the first buffer estimate (a surface-area guess); the mesher reads the true count back and runs again with room, so the first mesh of a busier shape costs two readbacks.
  7. Flat faces cost as much as curved ones: every cell the surface crosses becomes two triangles.
  8. Chunks at two resolutions do not meet exactly.
  9. Use the attributes at the scale of the feature: curvature is 1/m (a 1 cm rim reads 100), so normalise it by a radius (SDF_ATTRIBUTE_WGSL’s sdfConvexity) before using it as a mask; cavity looks along the normal cavityReach cells, so it changes with the cell size.
14 pitfalls in full, the hook-up guide, the module source and its tests come with the part.

Where it comes from

From HaTac. Adapted from published work, named below.

Hannes Andersson: The HaTac engine’s distance-field mesher, its WGSL passes, its assembly and its tests (built for his game and proven there); the library’s host, demo, pot and workshop.

Tao Ju, Frank Losasso, Scott Schaefer & Joe Warren, Dual Contouring of Hermite Data (SIGGRAPH 2002): Vertices placed by minimising the squared distances to the tangent planes at the edge crossings, which keeps sharp features.

Sarah F. Frisken Gibson, Constrained Elastic Surface Nets (1998): The surface-nets placement: the mean of a cell’s edge crossings.

Inigo Quilez, distance functions and smooth minimum: The primitives’ distance formulas and the quadratic smooth minimum the smooth blends use.

Brian Karis, Real shading in Unreal Engine 4 (SIGGRAPH 2013): The GGX distribution and Schlick–Smith visibility in the demo’s light model.

Krzysztof Narkowicz, ACES filmic tone mapping curve (2016): The fitted curve the demo tone-maps with.

sdfsigned distance fielddistance fieldisosurfacemeshingmesherdual contouringsurface netsqefhermite datasharp featuressmooth unionsmooth subtractioncsgconstructive solid geometryblendimplicit surfaceprocedural modellingrocksorganic shapessculptingpotterycurvaturecavity

Near it in the frame