VDBVolumeMaterial
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 densitydensityReference. The extinction per index voxel at density 1 isopticalDepth / (longest side × densityReference).densityReferenceis 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 followspool.replace, agridIdchange 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:shadowStepssteps, 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 defaultsunIntensityis 16 and the defaultambient0.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
densityReferenceto the constructor, or setmaterial.densityReferenceAuto = falseonce the first frame is in the pool. - Per-voxel units.
densityScalegreater than 0 is the extinction per index voxel at density 1, used as given (opticalDepthanddensityReferenceare then ignored). WithshadowReach: 0the shadow ray steps bystepSize. 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
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.
stepSizeis in voxels,opticalDepthis relative to the index box anddensityScaleintegrates per voxel, so scaling a grid withpool.setTransformor a NanoVDB map scales its footprint, not how opaque it looks. - The march is fixed-step.
maxSteps(512) steps ofstepSize(0.75 voxels) cross 384 voxels; raisemaxStepsorstepSizefor a larger grid seen along its length (the cloud demo setsmaxStepsfrom the grid's size). - The proxy mesh is not tied to the grid. After
pool.setTransform(id, ...)ormaterial.gridId.value = other, build a newvdbProxyGeometry(pool.info(id)), or fragments outside the old box are never rasterized. - One material, many grids. When you pass a number,
material.gridIdis auintuniform: set.valueto show another grid of the types theVDBGridNodecompiles. 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.