Model loading · function

bakeObjectSdf

Bake one mesh into a signed distance field.

Explained in Importing models.

function bakeObjectSdf(mesh: MeshData, resolution: number): ObjectSdf
import { bakeObjectSdf } from '@driftengine/assets';

Parameters

ParameterTypeDescription
meshMeshData
resolutionnumber

In depth

resolution is voxels along the mesh's longest axis; the other two get however many the same spacing needs. Cubic voxels are not tidiness — a field with a different step per axis has a gradient whose length varies with direction, which is the one property a sphere trace depends on, and the failure is a march that overshoots along the coarse axis.

The sign comes from how many surfaces separate a point from open air, not from the winding. A face normal, an angle-weighted pseudonormal and a generalised winding number all flip when a mesh is exported with its triangles the other way round, and importers disagree about that constantly. A walk over the grid from its own faces does not care which way a triangle faces, and unlike crossing parity along one axis it does not need the mesh to be closed. See surfaceDepth.ts.

The magnitude comes from exact point-to-triangle distance, not from voxelisation. Marking the cells a triangle passes through and flooding outward loses any wall thinner than a cell, because no cell centre is inside it — and a wall thinner than a cell is most of them. This measures the distance from every grid point in a band around each triangle to that triangle, so a plate a fifth of a voxel thick is still a plate at a fifth of a voxel.