Skip to main content

@luma.gl/graph/gpu-geometry

@luma.gl/graph/gpu-geometry provides experimental WebGPU mesh operations that compose in a caller-owned GPUCommandGraph. These are computation operations, independent of engine Geometry.

OperationInputsOutputCost
gpuMeshFaceNormalsPacked positions and triangle-list indicesOne face normal per triangleO(triangles)
gpuMeshVertexNormalsFace normals and CSR vertex-to-face incidenceOne unit normal per vertexO(vertices + incidences)
gpuMeshTangentsPositions, vertex normals and UVs, plus indices and CSR incidence for indexed meshesOne glTF tangent per vertexO(vertices + incidences)
gpuMeshSmoothingPositions and CSR vertex-neighbor adjacencyOne updated position per vertexO(vertices + adjacency entries)

Use graph.add(operation, props) to get a graph-managed result. When composing a nested command producer that supplies its destinations, use operation.commands(props).

Compose named normals in GPUGraph​

Supply packed positions, triangle indices, and vertex-to-face incidence at execution. The face count comes from the triangle indices. The vertex count comes from the CSR offsets, which include a final sentinel. Only the selected vertex normals are returned.

import {GPUGraph} from '@luma.gl/graph/gpu-graph';
import {gpuMeshFaceNormals, gpuMeshVertexNormals} from '@luma.gl/graph/gpu-geometry';

const graph = new GPUGraph();
graph.addInput('positions', {type: 'vector', format: 'float32x3'});
graph.addInput('indices', {type: 'vector', format: 'uint32'});
graph.addInput('offsets', {type: 'vector', format: 'uint32'});
graph.addInput('incidence', {type: 'vector', format: 'uint32'});
graph.add(gpuMeshFaceNormals, {
positions: 'positions',
indices: 'indices',
output: 'faces',
areaWeighted: true
});
graph.add(gpuMeshVertexNormals, {
faceNormals: 'faces',
offsets: 'offsets',
faceIndices: 'incidence',
output: 'normals'
});
const compiledGraph = graph.compile(device, {
outputs: {normals: {type: 'vector', format: 'float32x3'}}
});
const {normals} = await compiledGraph.run({inputs: {positions, indices, offsets, incidence}});

Smooth a mesh and draw it in one submission​

Pass graph views of the original mesh to a GPUCommandGraph. Smoothing and normal generation reuse the original topology. The graph allocates the produced positions and normals.

import {GPURenderInputs} from '@luma.gl/graph/gpu-graph';
import {
gpuMeshSmoothing,
gpuMeshFaceNormals,
gpuMeshVertexNormals,
type GPUMeshNormalsResult,
type GPUMeshSmoothingResult
} from '@luma.gl/graph/gpu-geometry';

graph.add(gpuMeshSmoothing, {
id: 'smooth', positions, offsets: neighborOffsets, neighbors, pinned, factor: 0.5
});
const {positions: smoothed} = graph.getContribution<GPUMeshSmoothingResult>('smooth');

graph.add(gpuMeshFaceNormals, {
id: 'faces', positions: smoothed, indices, areaWeighted: true
});
const {normals: faceNormals} = graph.getContribution<GPUMeshNormalsResult>('faces');

graph.add(gpuMeshVertexNormals, {
id: 'vertices', faceNormals, offsets: incidenceOffsets, faceIndices
});
const {normals} = graph.getContribution<GPUMeshNormalsResult>('vertices');

const inputs = new GPURenderInputs({
attributes: {position: {source: smoothed}, normal: {source: normals}}
});
// Supply inputs on the render pass. Use inputs.bufferLayout when creating its Model,
// then call inputs.apply(model, context) inside the pass's encode callback.

Keep the triangle index view for the draw and declare its index usage in the render pass. Compile after adding the draw, then encode and submit once. If you draw from a separate graph, supply your own destination views to the mesh steps and keep those buffers alive through rendering.

Generate vertex tangents​

Use gpuMeshTangents with packed float32x3 positions and normals and packed float32x2 texture coordinates. GPUGraph derives the length of the float32x4 output from the position input.

graph.add(gpuMeshTangents, {
positions: 'positions',
normals: 'vertexNormals',
textureCoordinates: 'uvs',
output: 'tangents',
topology: {indices: 'triangles', offsets: 'vertexFaceOffsets', faceIndices: 'incidentFaces'}
});

Use the same CSR incidence as gpuMeshVertexNormals for indexed meshes. Omit topology for an unindexed triangle list. In a caller-owned command graph, pass concrete views.

commandGraph.add(gpuMeshTangents, {
id: 'meshTangents',
positions,
normals,
textureCoordinates,
topology: {indices, offsets, faceIndices}
});
const {tangents} = commandGraph.getContribution<GPUMeshTangentsResult>('meshTangents');

The graph allocates a vertex-ready tangent view unless you supply the tangents destination. Use gpuMeshTangents.commands(props) inside a command producer when you already have a destination.

The operation averages triangle UV derivative directions and orthogonalizes them against the vertex normal. Degenerate triangles contribute nothing. If the accumulated direction vanishes, the operation chooses a perpendicular fallback. Split shared vertices at mirrored UV seams beforehand. A vertex with mixed handedness or a zero normal yields the all-zero sentinel, which is not a valid tangent. Use authored MikkTSpace tangents for normal maps baked against that basis.

Data and ownership​

Positions and normals are packed GPUGraphDataView<'float32x3'>; indices, offsets, adjacency and pin flags are packed GPUGraphDataView<'uint32'>. Nonzero byte offsets are supported. Each step borrows its inputs and any supplied output. The enclosing graph owns destinations allocated for contributions. Output buffers must be separate from input buffers. Multiple writable output bindings must remain disjoint after their start offsets are rounded down to 256-byte alignment. The caller submits work and owns any readback. Destroy the compiled graph to release its kernels and graph-owned buffers, then release your own buffers separately.

CSR offsets have vertexCount + 1 rows, start at zero, are monotonic, and end at the entry count. Adjacency should contain each neighbor once. Incidence should contain each incident face once. Prepare topology explicitly when loading a mesh; this version does not construct it automatically. No implicit packing or cross-batch topology reconciliation occurs. These initial APIs accept one resident packed view per attribute; partitioning and stitching are caller responsibilities.

Source POSITION and NORMAL attribute names remain unchanged in CPU geometry containers. Map them to shader attributes only when creating renderer bindings.

Numerical behavior and limits​

  • Triangle winding determines the normal direction. Default face normals have unit length; areaWeighted: true returns the unnormalized cross product (twice oriented triangle area).
  • Degenerate faces and faces with out-of-range vertex IDs produce zero. Supply finite positions in a scale where cross products do not overflow float32.
  • Vertex normals normalize the sum of incident faces. Isolated vertices and cancelling sums produce zero. Out-of-range face references are ignored.
  • Smoothing performs one simultaneous Jacobi iteration toward the neighbor mean, with factor in [0, 1]. Nonzero pin flags preserve boundaries or constrained vertices. Isolated vertices remain unchanged; self-neighbors and out-of-range neighbor IDs are ignored.
  • Compose explicit ping-pong buffers for multiple iterations. Uniform Laplacian smoothing shrinks surfaces and does not guarantee volume preservation or protect sharp features.
  • CSR payloads are a caller contract, not validated through CPU readback. Shaders clamp traversal to the entry capacity, but malformed topology has no meaningful geometric interpretation.
  • Each bound view must fit the device's storage-binding limit. WebGPU is required.

Run the surface lab to compare a noisy mesh with its smoothed, GPU-normalized result. The initial scope does not include surface extraction, mesh simplification, registration, or topology generation.