Compute and streaming

Grid statistics and dense atlases through the renderer, and coarse-first loading into a pool id.

Compute wrappers

vdbGridStats(renderer, pool, id, options?) and vdbDecodeToAtlas(renderer, pool, id, options?) are the core's compute tools through the renderer's device, returning promises. The grid must be resident (a pool.flush() ran after its add).

import { vdbDecodeToAtlas, vdbGridStats } from '@pmndrs/vdb/three';

// The core's vdbGridStats and vdbDecodeToAtlas through the renderer's device; the grid must be resident.
const stats = await vdbGridStats(renderer, pool, id, { histogramBins: 128 });
console.log(stats.min, stats.max, stats.mean, stats.activeVoxelCount);

// The atlas also comes back as a three Data3DTexture, ready for a material.
const atlas = await vdbDecodeToAtlas(renderer, pool, id, { maxDim: 128, format: 'float32', normalize: false });
console.log(atlas.dims, atlas.texture);

vdbDecodeToAtlas returns the core's atlas plus a Data3DTexture. Texel i samples voxel min + i at full resolution (index position min + (i + 0.5) * (max - min + 1) / n - 0.5 in general), so the texture spans the voxel-centred box [min - 0.5, max + 0.5] of the index bbox. The value transform is the core's vdbApplyValueTransform (a CPU edit that stages pool.replace).

Coarse-first streaming

Two demos show it: coarse-first (a 12 KiB preview, then the full grid, with a live byte counter) and streaming (the same into the froxel renderer).

vdbStreamCoarseFirst(pool, response, options?) streams a file baked with vdbBakeCoarseFirst into one pool id. It adds the preview as soon as its bytes are in, with the full grid's size reserved, then replaces it in place with each finer grid. The id, its bind-group slot and any instance transform stay.

That describes a file baked with the default segments: 'one'. A segments: 'per-grid' file (for native tools that read grids by index) streams too, but announces the full grid late: no reserved size (the swap moves the grid), voxelRatio 1 while the preview shows, and a first event whose types lacks the full grid's type. See segment layout.

import { vdbStreamCoarseFirst } from '@pmndrs/vdb/three';

// A file baked with vdbBakeCoarseFirst() holds the preview grid first. The preview is added with the
// full size reserved, then replaced in place when the full grid arrives: the same pool id throughout.
const { id } = await vdbStreamCoarseFirst(pool, await fetch(url), {
    // After each swap: pool.flush() + grids.update() and the proxy box from pool.info(id). A material with an
    // explicit densityScale (per index voxel) also gets it x voxelRatio while a preview shows.
    bind: { grids, material, mesh },
    onGrid: ({ action, final }) => console.log(action, final ? 'full grid' : 'preview'),
});

A swap changes what the id holds, so three things need redoing. The opt-in bind option does them after each swap, before onGrid runs; every member is optional:

  • bind.material: only for a VDBVolumeMaterial with an explicit densityScale. It is per index voxel, so a preview with k-times larger voxels has 1/k the optical depth at the same value. bind sets densityScale = base × voxelRatio (the base is bind.densityScale, else the material's value at the first swap), which is the base again at the full grid. A material on its defaults needs no scale: its opticalDepth is relative to the grid's box and mean density, so bind leaves it alone (its densityScale is 0).
  • bind.mesh: the preview's box is the full box grown to whole preview voxels, so the mesh gets a new vdbProxyGeometry(pool.info(id)) (proxyPad sets its pad) and the old geometry is disposed.
  • bind.majorant(event): replace drops a brick majorant. Return the one for the grid just shown (or null for none) and pool.setMajorant is called for you.
  • bind.grids: a VDBGridNode; pool.flush() then grids.update().

Into the froxel renderer

A VDBFrameVolume needs no bind member of its own. Make the VDBGridNode, a sparse source with followImage: true and the volume once, at the first grid. event.types lists every grid type of the family (the file lists the full grid's metadata before any data), so the one module decodes both a baked FpN preview and an Fp8 full grid. After a swap, once the new image is resident, the source re-reads the grid's index box, world box and transform from pool.info, rebuilds its coarse pyramid at the new size and bumps its version, and the volume moves its box to follow. Boxes, sizes and LOD levels are uniforms on a following source, so the swap builds no shader or pipeline. Froxels integrate in world space, so a preview needs no voxelRatio scale.

import { VDBFrameVolume, VDBGridNode, vdbSparseSource, vdbStreamCoarseFirst, type VDBCoarseFirstBind } from '@pmndrs/vdb/three';

// Into the froxel renderer: one node, source and volume, made at the first grid (the preview).
const bind: VDBCoarseFirstBind = {}; // later swaps: pool.flush() + grids.update()
const { id } = await vdbStreamCoarseFirst(pool, await fetch(url), {
    bind,
    onGrid: (event) => {
        if (event.action !== 'add') return; // a swap: the source and the volume follow by themselves
        pool.flush();
        // Every grid type of the family (a baked FpN preview, an Fp8 full grid): one module reads both.
        const grids = new VDBGridNode(renderer, pool, {
            profile: 'march',
            helpers: ['cellBoundTrilinear', 'hddaCellEnd', 'fastTrilinear'],
            types: event.types,
        });
        bind.grids = grids;
        // followImage: after a swap the source re-reads the grid's boxes and rebuilds its pyramid, and the
        // volume moves its box. No new node, source, volume or shader.
        const source = vdbSparseSource(grids, { density: event.id }, { images: { density: event.grid.image }, followImage: true });
        scene.add(new VDBFrameVolume(source, { extinction: 100 })); // froxels: no voxelRatio scale
    },
});

The source's density mean (normalizeDensity) is read back from the pool after a swap, as for a sequence frame; await source.refreshStats() after pool.flush() if the first frame after the swap must use the new mean.

The members are read at every swap, so they can be assigned in onGrid once the first grid exists. Code that handles a swap asynchronously can call vdbCoarseFirstApply(pool, event, bind) itself. Without bind the loader touches only pool.add and pool.replace, so it works with any pool host. It lives in ./three because tools may not import the core. Pass signal to abort, and reader for maxGridBytes limits.