VDBVolumeMaterial

A fragment raymarch of one pooled grid, with a sun, a shadow march and live uniforms.

VDBVolumeMaterial is a NodeMaterial that raymarches one grid of a VDBGridPool through a VDBGridNode. The grid node needs the rayClipSupport helper.

import * as THREE from 'three/webgpu';
import { VDBGridPool } from '@pmndrs/vdb';
import { NvdbFile } from '@pmndrs/vdb/tools';
import { VDBGridNode, VDBVolumeMaterial, getDevice, vdbProxyGeometry } from '@pmndrs/vdb/three';

const image = (await NvdbFile.fromURL(url)).gridImage(0);

const pool = await VDBGridPool.create(getDevice(renderer));
const id = pool.add(image);
pool.flush(); // the grid is on the GPU after this
await pool.settled();

// One VDBGridNode per renderer. The ray clip the material needs is an opt-in helper.
const grids = new VDBGridNode(renderer, pool, { helpers: ['rayClipSupport'] });
const material = new VDBVolumeMaterial(renderer, grids, id); // the defaults are sized to the grid
scene.add(new THREE.Mesh(vdbProxyGeometry(pool.info(id)), material)); // a box around the grid's index bbox

renderer.setAnimationLoop(() => {
    grids.update(); // rebinds only if the pool replaced its buffer
    renderer.render(scene, camera);
});

The cloud demo runs it on the WDAS cloud.

It renders on a proxy box with side: BackSide, so the camera can be inside. Entry and exit come from the analytic ray clip, not the rasterized faces. The material is transparent, writes no depth, and uses premultiplied alpha. It does not composite against scene depth.

The default look

A material made without options draws a lit volume on a grid of any resolution and value range, because its density and its shadow ray are relative to the grid:

  • opticalDepth (default 6) is the optical depth of a ray that crosses the grid's longest index side at the density densityReference. The extinction per index voxel at density 1 is opticalDepth / (longest side × densityReference).
  • densityReference is the mean of the grid's active values (the root node's average statistic, pool.info(id).rootAverage). The material reads it from the pool at each render, so it follows pool.replace, a gridId change and a coarse-first swap. A grid without statistics uses 1.
  • shadowReach (default 0.3) is the length of the shadow ray as a fraction of the longest side: shadowSteps steps, sampled at their midpoints.
  • The sun term is sunColor × sunIntensity × phase × shadow. A phase function integrates to 1 over the sphere (about 1 / 4π ≈ 0.08), so the default sunIntensity is 16 and the default ambient 0.15.

Two cases need a choice:

  • Sequences. A grid that is replaced each frame is normalised by each frame's own mean, so a frame with less smoke is scaled back up. Pin the reference: pass densityReference to the constructor, or set material.densityReferenceAuto = false once the first frame is in the pool.
  • Per-voxel units. densityScale greater than 0 is the extinction per index voxel at density 1, used as given (opticalDepth and densityReference are then ignored). With shadowReach: 0 the shadow ray steps by stepSize. 0.1's look is { densityScale: 40, sunIntensity: 3, shadowReach: 0 }.

Parameters

Every parameter is a live TSL uniform: change .value between frames and nothing recompiles.

import { Color, Vector3 } from 'three/webgpu';

// The parameters are uniforms: change them between frames, nothing recompiles.
material.opticalDepth.value = 10; // optical depth across the grid's longest side at its mean density
material.stepSize.value = 0.5; // march step, in voxels
material.maxSteps.value = 512;
material.sunDirection.value.copy(new Vector3(0.3, 1, 0.2).normalize());
material.sunColor.value.set(new Color(1, 0.95, 0.85));
material.sunIntensity.value = 20; // the sun term is sunIntensity x phase (about 1 / 4π)
material.shadowSteps.value = 8; // 0 turns the sun shadow march off
material.shadowReach.value = 0.5; // the shadow ray spans half of the longest side
material.anisotropy.value = 0.3; // Henyey-Greenstein g of the sun's phase
material.jitter.value = 1; // per-pixel start jitter; 0 for a deterministic image
UniformMeaning
opticalDepthOptical depth across the grid's longest index side at densityReference (default 6)
densityReferenceThe density opticalDepth refers to: the grid's mean, read from the pool while densityReferenceAuto is true
densityScaleExtinction per index voxel at value 1. Default 0: opticalDepth is used instead
stepSizeMarch step, in index voxels
maxSteps, shadowStepsCaps on the march and on the shadow march toward the sun (0 turns shadows off)
shadowReachLength of the shadow ray as a fraction of the longest side (default 0.3); 0 steps it by stepSize
sunDirection, sunColor, sunIntensity, anisotropy, shadowDensity, ambientThe sun, its Henyey-Greenstein phase, and the ambient floor
jitterPer-pixel start jitter, 1 or 0

The constructor's maxStepsCap, shadowStepsCap and sampleBudgetCap bound the per-fragment cost (VDB_VOLUME_MAX_STEPS_CAP 1024, VDB_VOLUME_SHADOW_STEPS_CAP 128, VDB_VOLUME_SAMPLE_BUDGET_CAP 16384).

Things to know

  • The march runs in index space. stepSize is in voxels, opticalDepth is relative to the index box and densityScale integrates per voxel, so scaling a grid with pool.setTransform or a NanoVDB map scales its footprint, not how opaque it looks.
  • The march is fixed-step. maxSteps (512) steps of stepSize (0.75 voxels) cross 384 voxels; raise maxSteps or stepSize for a larger grid seen along its length (the cloud demo sets maxSteps from the grid's size).
  • The proxy mesh is not tied to the grid. After pool.setTransform(id, ...) or material.gridId.value = other, build a new vdbProxyGeometry(pool.info(id)), or fragments outside the old box are never rasterized.
  • One material, many grids. When you pass a number, material.gridId is a uint uniform: set .value to show another grid of the types the VDBGridNode compiles. Pass a TSL node instead to drive the id from a shader.
  • Perspective cameras only. The view ray is positionWorld - cameraPosition. An orthographic camera renders wrongly.