Build, quantize, resample
A grid image is a Uint32Array holding one NanoVDB grid: the exact bytes VDBGridPool.add and writeNvdb
take. Everything on this page returns one, or a VDBBuiltGrid that carries one in .image with its stats.
Build
buildFromVdb(grid, options?)streams a parsed.vdbgrid into the serializer, with no dense array. Float and Vec3f grids are supported.buildFromVdbDetailedreturns theVDBBuiltGrid.vdbBuildFromDense(values, dims, options?)builds a Float grid from a dense array. By default the array is x-major ((x·ny + y)·nz + z, z fastest).layout: 'x-fastest'readsx + nx·(y + ny·z)instead, the order of a 3D texture and of most simulation arrays: the array is read in place (no transposed copy, 64 MB at 256³) and the image is the same, byte for byte. Other options:origin,voxelSize(a number, or[x, y, z]for anisotropic voxels: a diagonal Map, asvdbResampleGridwrites),worldOrigin,background,activeThreshold,gridName,gridClass,collapse.VDBBuiltGrid.voxelSizeis the x side;voxelSizeXYZhas all three. Withcollapse(the default) an 8³ block of one value is stored as a tile of its lower node, not as a leaf (a constant test grid has no leaves);collapse: falsekeeps the leaves.vdbBuildFromLeavesDetailed(leaves, codec, options?)is the general entry: your leaves and aVDBLeafCodec(VDB_FLOAT_LEAF_CODEC,VDB_FP4_LEAF_CODEC,VDB_FP8_LEAF_CODEC,VDB_FP16_LEAF_CODEC,VDB_HALF_LEAF_CODEC,VDB_VEC3F_LEAF_CODEC).
import { vdbBuildFromDense, vdbInspect, writeNvdb } from '@pmndrs/vdb/tools';
const dims: [number, number, number] = [64, 64, 64];
const values = new Float32Array(dims[0] * dims[1] * dims[2]); // x-major: z fastest
for (let z = 0; z < dims[2]; z++)
for (let y = 0; y < dims[1]; y++)
for (let x = 0; x < dims[0]; x++) {
const d = Math.hypot(x - 32, y - 32, z - 32);
values[(x * dims[1] + y) * dims[2] + z] = Math.max(0, 1 - d / 24);
}
// Zero voxels become inactive background; the rest go into a sparse 5-4-3 tree.
const image = vdbBuildFromDense(values, dims, { voxelSize: 0.1, gridName: 'density', gridClass: 'FogVolume' });
console.log(vdbInspect(image)); // { gridType, gridClass, voxelCount, nodeCounts, memoryBreakdown }
return writeNvdb([image], { codec: 'zip' });
vdbInspect(image) reports the grid type and class, the voxel count, node counts per level and a memory breakdown.
Data that is x fastest goes in as it is:
// density[x + nx * (y + ny * z)]: a 3D texture's order, a simulation's array. Read in place, no transposed copy.
const image = vdbBuildFromDense(density, [nx, ny, nz], { layout: 'x-fastest', gridName: 'density' });
Quantize
vdbQuantize(image, mode, tolerance?, options?) re-encodes a Float grid as Fp4, Fp8, Fp16 or FpN with a per-leaf minimum and
quantum, and packed codes. Fp4, Fp8 and Fp16 are fixed at 4, 8 and 16 bits per voxel (tolerance is ignored). FpN picks
its bit width per leaf from the absolute tolerance. All load on the GPU through the generated module (types: ['fp4'],
['fp8'], ['fp16'] or ['fpn']). Fp8 and FpN match nanovdb_convert byte for byte on the fixtures; Fp4 and Fp16 use
the same rules but have no native fixture to compare with.
'half' writes a NanoVDB Half grid (GridType::Half, implemented in NanoVDB 13.2): every value, tiles and the
background included, becomes the nearest IEEE binary16 (round to nearest even; beyond ±65504 it becomes infinity), 2 bytes
per voxel with no per-leaf range, so negative values and wide ranges keep about 3 significant digits everywhere. Min/max
statistics are binary16, average and standard deviation f32. Load it with types: ['half']. The values match native
NanoVDB v14 (createNanoGrid<FloatGrid, nanovdb::Half>) bit for bit on the fixtures. nanovdb_convert has no Half
option; vdbFloatToHalf / vdbHalfToFloat / vdbRoundToHalf are the conversions it uses.
Half grids are sources too: vdbQuantize(half, 'fp8') (or any mode) re-encodes one, vdbQuantize(half, 'float') widens it
to a Float grid exactly (Half → Float → Half gives the same bytes back), and vdbCollapseGrid and transformLeafValues
keep it Half. Quantized (Fp*) grids are not sources: quantize from the Float or Half grid.
import { vdbCollapseGrid, vdbQuantize } from '@pmndrs/vdb/tools';
// Per-leaf min and quantum plus packed codes. Fp8 is one byte per voxel ('fp4' and 'fp16' are 4 and
// 16 bits); FpN picks the bit width per leaf from the tolerance (an absolute error bound in value units).
const fp8 = vdbQuantize(image, 'fp8');
const fpn = vdbQuantize(image, 'fpn', 0.001);
// NanoVDB Half: every value as an IEEE binary16 (2 bytes per voxel, ~3 significant digits, no per-leaf range).
const half = vdbQuantize(image, 'half');
// vdbCollapseGrid() folds constant leaves into tiles (a smaller image, the same values).
// vdbQuantize() does the same when asked: vdbQuantize(image, 'fpn', 0.001, { collapse: true }).
const collapsed = vdbCollapseGrid(image).image;
On the smoke sample, FpN at equal resolution is smaller than the .wvdb bake (474 vs 790 KiB); see
formats.
Resample and transform
vdbResampleGrid(source, options) resamples a grid image, a parsed .vdb grid or a whole VdbFile over a world box to a
target resolution, with a 'point', 'box' (default) or 'trilinear' filter. The result is sparse and optionally
quantized. vdbResampleError reports how far a result is from its source, and vdbResampleDense returns the dense values.
vdbTransform(image, input) edits the grid's map only (a uniform voxel size and translation, or a 4x4), with no voxel
churn.
import { vdbResampleError, vdbResampleGrid, vdbTransform } from '@pmndrs/vdb/tools';
// Resample over a world box (default: the grid's own) to about 128 voxels on the longest side,
// with a box filter, and store the result as Fp8.
const baked = vdbResampleGrid(image, { resolution: 128, filter: 'box', quantize: 'fp8' });
console.log(baked.dimensions, baked.bbox, baked.voxelCount);
// How far is it from the source? The mean absolute error as a share of the mean value.
console.log(vdbResampleError(image, baked.image).percent);
// A metadata-only edit of the grid's map (uniform scale and translate), with no voxel churn.
return vdbTransform(baked.image, { voxelSize: 0.05, worldOrigin: [0, 0, 0] });
Majorants and a dense brick atlas
vdbBrickMajorant(image, options) builds the brick-majorant bounds the core's majorant WGSL profile walks (see
the WGSL module); the pool builds the same bounds on the GPU with pool.buildMajorant(id, options).
vdbValueRange(image, { outside }) is the exact range of the sampled field, for pool.setValueMax. The profile's CPU
twins are here too: vdbMajorantWalk (the walk's segments), vdbDeltaTrack and vdbRatioTrack (the tracking helpers),
in f32 in the shader's order, to check a kernel of your own or to tune brick sizes without a GPU.
vdbBrickAtlas(image, { resolution }) builds pmndrs/pathtracer's sparse brick atlas (its volumes.gridLayout: 'atlas'):
occupied 8³ bricks stored as 9³ texels (each brick plus its trilinear apron) in a near-cubic block, and an indirection
of one u32 per brick (bx | by << 10 | bz << 20, or 0xffffffff for an empty brick). It reads the grid's leaves and
tiles directly, with no dense copy, and gives the arrays the pathtracer's buildBrickAtlas(resolution, dense) gives for
the same field: the grid inside its index bbox, 0 outside it and outside [0, resolution). Use it to feed that atlas
path from a .nvdb file.