Triangle mesh processing
import {transformGeometry, mergeGeometries, weldGeometry, getDegenerateTriangles}
from '@math.gl/geometry/processing';
These operations accept GeometryInput with topology: 'triangle-list', packed
typed-array attributes, and optional indices. They accept compatible geometry generator
outputs directly. Positions use POSITION or
positions with size 3. Each other attribute needs an explicit size and the same
vertex count. Both value and legacy values storage are accepted. Interleaved and
constant attributes are not supported. Values must be finite and indices valid.
Outputs have fresh buffers and Uint32 indices; inputs are never modified.
transformGeometry(geometry, matrix)โ
Accepts a finite affine column-major 4ร4 matrix. Positions become Float64 values.
NORMAL or normals uses the inverse transpose and normalization. Size-4 TANGENT
uses the linear transform, orthogonalization against the transformed normal when
present, and normalization. Reflection flips tangent handedness and triangle winding.
Normals and tangents require an invertible transform. Auxiliary attributes retain
their storage types. Projective matrices are rejected.
mergeGeometries(geometries)โ
Concatenates one or more triangle meshes and offsets their indices. Attribute names, sizes and typed-array constructors must match. Nonindexed meshes receive sequential indices. The merged output retains all attributes.
weldGeometry(geometry, options?)โ
Returns {geometry, vertexMap, removedVertices}. vertexMap maps each original
vertex row to its output row. The default compares complete attribute tuples exactly,
preserving UV seams, hard normal edges and differing colors. The first matching row
is retained.
positionGridSize defaults to zero. A positive value compares positions rounded to
that grid while comparing all other attributes exactly. This is quantization, not a
radius search: nearby points across grid boundaries can remain separate. Original
coordinates of the first row are retained. Welding can create degenerate triangles;
it does not remove them or unused vertices.
getDegenerateTriangles(geometry, areaEpsilon = 0)โ
Returns a Uint32Array of triangle numbers in draw order whose area is at most the finite, nonnegative threshold. The default detects zero-area triangles, including repeated indices and collinear vertices. Use a threshold in squared coordinate units to include near-degenerate triangles. This reports triangles without changing them.
computeVertexTangents(geometry)โ
Generates a packed Float32 array with four components per vertex. Attach the array
as TANGENT: {size: 4, value: computeVertexTangents(geometry)}. The function requires
nonzero size-3 NORMAL (or normals) and size-2 TEXCOORD_0 (or texCoords).
The tangent is unit length and perpendicular to the normal. Reconstruct the
bitangent as cross(normal, tangent.xyz) * tangent.w.
Each vertex receives the average of its triangle UV derivative directions, orthogonalized against its normal. Degenerate triangles contribute nothing. Vertices without a usable tangent receive an arbitrary perpendicular direction. The function preserves topology and throws if a vertex needs both handedness signs. Split shared vertices at mirrored UV seams before calling it. For maps baked using MikkTSpace, use the matching authored tangents.