Parsing .vdb

parseVdb reads OpenVDB files, including Blosc, vector grids and point data, with no native code.

@pmndrs/vdb/tools is pure TypeScript (D3). It runs in the browser and in Node, and needs no GPU. parseVdb(buffer) reads an OpenVDB .vdb container and returns a VdbFile with its grids.

What it reads:

  • 5-4-3 trees of float grids, half grids (OpenVDB 13+ HalfGrid, Tree_half_5_4_3), vec3 grids (vec3s, vec3d, vec3i) and point data grids (ptdataidx32).
  • Uncompressed, zlib and Blosc (LZ4, BloscLZ and zlib) data, and half-float storage (saveFloatAsHalf).
  • Active tiles, and the inactive leaf and tile values OpenVDB keeps.
  • Files up to version 225, the version OpenVDB v14 still writes. A newer file throws VDBUnsupportedError before any grid is read (OpenVDB itself warns for 226 to 299 and rejects 300 and up).

A half grid parses as a float grid: its valueType is 'float' (every half widens to f32 exactly), and its gridType stays Tree_half_5_4_3 if you need to tell. buildFromVdb makes a NanoVDB Float grid from it.

Errors are VdbFormatError (a malformed file) or VDBUnsupportedError (a feature the parser does not read). Every error the tools throw on bad input is a VDBToolsError with a code; see errors.

import { buildFromVdb, parseVdb } from '@pmndrs/vdb/tools';

const file = parseVdb(bytes); // uncompressed, zlib or Blosc (LZ4 / zlib); half-float storage; active tiles

const images: Uint32Array[] = [];
for (const grid of file.grids) {
    console.log(grid.name, grid.valueType, grid.activeVoxelCount, grid.indexBBox);
    grid.readValue([0, 0, 0]); // { value, active }

    // A float grid, or a vec3 grid (Vec3f), becomes a NanoVDB grid image. The streaming build never
    // makes a dense array, and the .vdb's uniform scale and translate go into the NanoVDB map.
    if (grid.valueType !== 'ptdataidx32') images.push(buildFromVdb(grid));
}

Every grid has vdbReadValueCpu(ijk), iterLeaves() (each leaf with its origin, values and valueMask), iterTiles() and iterInactiveTiles(), plus the transform and metadata from the file.

Listing grids and decoding some

parseVdb(buffer, { select }) calls select(descriptor) for every grid after its header (name, type, metadata, transform) is read and before its tree. Return false to skip decoding that grid. VdbFile.descriptors lists every grid in file order, decoded or not, with its name, gridType, valueType, gridClass, transform, indexBBox, compression (zip, blosc, activeMask) and unsupported. A descriptor's valueType is the on-disk type, so an OpenVDB 13+ HalfGrid lists as half and decodes to a grid whose valueType is float (halves widen exactly).

Passing select also turns an undecodable grid (a value type, tree layout, transform or instancing the parser does not read) into a descriptor with unsupported set to the reason, instead of a thrown VDBUnsupportedError. select is not called for it. Without select, behaviour is as before: every grid is decoded and an unsupported one throws. select: () => false is a cheap listing pass.

// List the grids without decoding any tree (no decompression, so it is cheap on a large file).
const listing = parseVdb(bytes, { select: () => false });
for (const d of listing.descriptors) {
    console.log(d.name, d.valueType, d.gridClass, d.compression, d.indexBBox, d.unsupported);
}

// Decode only chosen grids. An unsupported grid is reported in `descriptors`, not thrown.
const file = parseVdb(bytes, { select: (d) => d.name === 'density' });

Point data

A ptdataidx32 grid is a point-data grid. points() decodes it into positions, the named attribute arrays and the group masks. Attributes the parser cannot decode are listed in skipped.

import { parseVdb } from '@pmndrs/vdb/tools';

const file = parseVdb(bytes);
for (const grid of file.grids) {
    if (grid.valueType !== 'ptdataidx32') continue; // a point-data grid
    const points = grid.points();
    console.log(grid.pointCount, grid.attributes.map((a) => a.name), grid.groupNames);
    return points.positions; // Float32Array of xyz; points.attributes holds the named attribute arrays
}

From a .vdb to the GPU

buildFromVdb(grid) returns a NanoVDB grid image (a Uint32Array) that VDBGridPool.add takes directly. See building grids for options and for what to do next.