.wvdb files
.wvdb is mjurczyk's WebVDB delivery format (spec v0.5): dense bricks at several LODs, coarsest first, in one file.
It is supported for delivery (D14), read and written by @pmndrs/vdb/tools. The logic is ported from
mjurczyk/openvdb (MIT); see credits.
Read
readWvdb(bytes, options?) decodes the whole file. { maxLod } stops at a LOD, and { partial: true } accepts a
truncated file. A volume grid has levels (dimensions, values as floats, brick bounds); a points grid has positions
and optional colours.
import { readWvdb, wvdbDenseVolume } from '@pmndrs/vdb/tools';
const file = readWvdb(bytes); // the whole file; { maxLod } or { partial: true } read a prefix
for (const grid of file.grids) console.log(grid.name, grid.kind, grid.class, grid.levels.length);
// The finest decoded LOD of the density grid as 8-bit texels with a mip atlas: what the dense
// frame-renderer source uploads.
const volume = wvdbDenseVolume(file, { density: 'density' });
console.log(volume.dimensions, volume.meanDensity);
wvdbDenseVolume(file, options?) turns the density grid (and optional mask, albedo and emission grids) into
8-bit texels with a mip atlas. That is the input of the dense source of the froxel renderer.
Stream
WvdbStreamReader is the push parser. Each complete LOD is an event, coarsest first, so a viewer can show LOD 0 while
the finer ones download. readWvdbHeader and wvdbLodByteRange support range requests for one LOD.
import { WvdbStreamReader } from '@pmndrs/vdb/tools';
const reader = new WvdbStreamReader();
const response = await fetch(url);
const body = response.body;
if (!body) return;
const stream = body.getReader();
for (;;) {
const { done, value } = await stream.read();
if (done) break;
// Each complete LOD is an event, coarsest first.
for (const event of reader.push(value)) console.log('lod', event.lod, 'complete', event.complete);
}
reader.end();
Show a LOD while its pages stream
A LOD is stored in pages of bricks. With partialLods: true the reader also emits an event after every page
(complete: false), so a LOD can be shown before it is whole. wvdbDensePartialVolume(reader.file, event, options?)
turns any event into a dense volume. Its options are those of wvdbDenseVolume, plus fill:
'upsample'(default): bricks that are stored but not decoded yet show the coarser LOD, upsampled as mjurczyk's loader does (trilinear, texel centres aligned). The picture only gains detail as pages land.'background': those bricks stay empty until their page lands.
Give the volume to the dense source with replace, as any LOD. The frame renderer's dense source (vdbDenseSource)
fills the pending bricks on the GPU with one compute pass, and rebuilds the mip atlas from the result. In that atlas,
level 1 of a pending brick is the coarser LOD itself (fillMips: 'coarse', the default), so a far view of a brick that
has not arrived reads exactly the coarser LOD. fillMips: 'box' builds every level from the upsample, as his renderer
does. Without the core, wvdbDenseResolve(volume) gives the same bytes on the CPU.
import { WvdbStreamReader, wvdbDensePartialVolume } from '@pmndrs/vdb/tools';
import { vdbDenseTextures, type VDBThreeDenseSource } from '@pmndrs/vdb/three';
// A partial event after every page; the levels are live, so build the volume before the next push.
const reader = new WvdbStreamReader({ partialLods: true });
let source: VDBThreeDenseSource | null = null;
const response = await fetch(url);
const stream = response.body!.getReader();
for (;;) {
const { done, value } = await stream.read();
if (done) break;
const event = reader.push(value).at(-1);
if (!event) continue;
// Bricks not decoded yet are upsampled from the coarser LOD (on the GPU, by the frame renderer's dense source).
const volume = wvdbDensePartialVolume(reader.file!, event, { fill: 'upsample' });
if (source) source.replace(volume);
else show((source = vdbDenseTextures(volume))); // e.g. new VDBFrameVolume(source, { extinction: 100 })
}
reader.end();
Events share the reader's live levels: a page decoded later in the same push is already in an earlier event's
levels, so build each volume before the next push (the texels are copied then). A completed LOD is byte-identical
whatever was shown while it streamed. Gallery demo 16 (?format=wvdb) streams smoke.vdb this way and compares the
modes. On SwiftShader at low quality, the mean PSNR of the partial pages against the final frame was 49.4 dB for
upsample, 42.4 dB with his mips, 42.9 dB for whole LODs only and 34.2 dB for empty bricks.
Write and bake
writeWvdb(grids, options) writes dense grids you made. vdbToWvdb(vdb, options) and nvdbToWvdb(images, options)
bake from a .vdb or a NanoVDB grid: they sample over the bbox to a target resolution and downsample the LODs.
import { nvdbToWvdb, vdbToWvdb, writeWvdb } from '@pmndrs/vdb/tools';
// From a .vdb (all float grids) or a NanoVDB grid image: point-sampled over the bbox, like his CLI.
const fromVdb = vdbToWvdb(vdbBytes, { resolution: 128, lods: 3 });
const fromNano = nvdbToWvdb(image, { resolution: 128 });
// Or write dense grids you made yourself.
const dims: [number, number, number] = [32, 32, 32];
const written = writeWvdb(
[{ name: 'density', background: 0, bbox: null, finest: { dimensions: dims, values: new Float32Array(32 * 32 * 32) } }],
{ lods: 2 },
);
Sampling defaults to 'nearest', which reproduces his CLI byte for byte (D27). Use sampling: 'box' when the source
is much finer than the texel grid, or when you read mass quantitatively; box files are 7 to 24% larger.
Level sets and points
Level-set sources use levelSetSampler: 'spec' (the format spec) or 'surface-mean' (his CLI's sampler). A .wvdb points
grid is read and written (writeWvdb with a type: 'points' grid, maxPoints, per-LOD subsets from wvdbPointLods).
vdbToWvdb bakes a .vdb's point data grids into points grids as his CLI does: world positions and Cd, every n-th
point above maxPoints (default 4,000,000), in file order beside the float grids (the volume box ignores the points).
LOD 0 holds every 64th point, LOD 1 every 8th of the rest, LOD 2 the others, so a streamed file draws an even subsample
first: WvdbStreamReader with partialLods and VDBPoints.fromWvdbGrid / addWvdbLevel in
three.js (demo 14's ?source=wvdb). Positions are stored as 16-bit codes over the
points' box.