Rendering without three
Live: rendering without three (demo 17, source in
examples/src/demos/17-raw-webgpu; locally, pnpm dev) renders the WDAS cloud, an emissive EmberGen explosion and a
level set with no three.js: the page's own passes draw the sky, the ground and an obelisk, read the volumes' shadow map
and fire light, and hand their own depth map back so the obelisk shadows the cloud.
Everything on this page is public in @pmndrs/vdb since 0.2.0: vdbCreateFrameRenderer, vdbSparseSource, the
receiver WGSL (vdbVolumeShadowWgsl, vdbEmissionProbeWgsl, vdbFrameIntegrateWgsl) and the level-set and point
passes. @pmndrs/vdb/three's froxel renderer is a thin adapter over the same renderer.
The froxel renderer (the same lighting as Froxel renderer, ported from mjurczyk's
FrameRenderer) runs in the core as WGSL compute kernels on 3D textures (D34). It takes the host's GPUDevice, records
into the host's command encoder, allocates its own textures and reads only plain data: a camera, lights, an environment
texture and depth maps. Nothing in it knows about an engine, so a raw WebGPU page, a three.js adapter, the cloud system
and the pathtracer all drive it the same way.
The frame loop
One frame is one command encoder, in this order:
- Your pool:
pool.update(encoder)uploads staged grids (a growth replacespool.buffer). - The core source:
source.update(encoder)builds pyramids and writes the volume table, after the pool's update in the same encoder. - Your depth maps of the lights, if meshes should shadow the volumes (mesh → volume), recorded before the core reads them.
- The core:
frame.update(encoder, input)records every kernel the frame needs: the 'frame' lighting shadow grid and transmittance, the environment reduction, the volume shadow maps, the emission probes, the froxel gather and the per-column scan. It returnsoutputs: textures and uniform buffers your shaders read. - Your opaque passes, reading the volume shadow maps (volume → mesh) and the emission probes.
- The composite:
frame.render(encoder, targets)draws the volumes over your colour target up to your depth, or your own shader callsvdbFrameIntegrate(the host-side seam). - Present (tone mapping, sRGB) and
device.queue.submit. The contract is oneupdate()per submitted encoder, submitted in the task that recorded it (noawaitbetweenupdate()andsubmit): textures the renderer or a dense source replaces are destroyed in a later task, so a submit in the same task never names a destroyed texture.
Set up
A sparse source serves every volume of one pool through one table, so a cloud and an explosion in the same pool are one
bind group and one dispatch. A volume's matrixWorld maps the unit box [-0.5, 0.5]³ to the world; its other
parameters are those of VDBFrameVolume (extinction, colours, emission, phase overrides).
// The device is yours; the pool and the renderer record into encoders you submit.
const device = await adapter.requestDevice({ requiredLimits: VDBGridPool.requiredLimits(adapter) });
const pool = await VDBGridPool.create(device);
// One sparse source serves every volume of one pool: one bind group, one dispatch per frame.
const source = vdbSparseSource(pool, { types: ['fp8', 'float'] });
const cloudSlot = source.volume({ density: pool.add(cloudImage) }, { image: cloudImage });
const fireSlot = source.volume({ density: pool.add(fireDensity), emission: pool.add(fireTemperature) });
// The renderer's composite pass draws into your colour target and reads your depth.
const frame = vdbCreateFrameRenderer(device, { composite: { colorFormat: 'rgba16float', depthFormat: 'depth32float' } });
Object.assign(frame.settings, { lighting: 'frame', castShadows: true, width: 256, height: 256, depth: 24 });
// matrixWorld maps the unit box [-0.5, 0.5]^3 to the world (column-major).
const cloud = frame.addVolume(cloudSlot, { matrixWorld: [200, 0, 0, 0, 0, 120, 0, 0, 0, 0, 160, 0, 0, 140, 0, 1], extinction: 40 });
const fire = frame.addVolume(fireSlot, {
matrixWorld: [60, 0, 0, 0, 0, 80, 0, 0, 0, 0, 60, 0, -120, 40, 0, 1],
temperatureRange: [0, 4500],
emissiveIntensity: 150,
emissionColor: { spectrum: 'planck' },
});
await frame.prepare(); // optional: builds every pipeline up front, with the async creators
frame.settings has mjurczyk's names and defaults (lighting, width / height / depth froxels, subSteps,
multipleScattering, castShadows, temporal, jitter, …) and is read at the next update(). A dense .wvdb source
(vdbDenseSource(device, wvdbDenseVolume(readWvdb(bytes)))) is its own slot.
Each frame
const encoder = device.createCommandEncoder();
pool.update(encoder); // 1. your pool first (uploads, growth)
source.update(encoder); // 2. the core source after it: pyramids, the volume table (same encoder)
// 3. the core: shadow grid, transmittance, environment, volume shadow maps, emission probes, froxels
const input: VDBFrameInput = {
frameId,
camera: { view: camera.view, projection: camera.projection, near: camera.near, far: camera.far },
viewport: { drawingBuffer: size, canvas: size },
lights: [{ id: 'sun', kind: 'directional', direction: [-0.6, -0.7, -0.4], color: [2.4, 2.3, 2.1], shadow: { size: 1024, view: 'fit' } }],
environment: { id: 'sky', revision: 1, texture: sky, projection: 'equirect', intensity: 1 },
occluders: [{ light: 'sun', depth: sunDepth.createView({ dimension: '2d-array' }), viewProjection: [sunViewProjection], revision: sunRevision }],
emissionLight: { resolution: 16 },
};
const u = frame.update(encoder, input);
// 4. your opaque passes, reading u.outputs (volume shadow maps, emission probes) and writing colour + depth
drawOpaque(encoder, u);
// 5. the volumes over them: the core's composite pass (his sRGB blend reads a copy of the colour)
encoder.copyTextureToTexture({ texture: targets.color }, { texture: targets.sceneColor }, size);
frame.render(encoder, {
color: targets.color.createView(),
size,
depth: targets.depth.createView(),
sceneColor: targets.sceneColor.createView(),
clampColors: false, // you tone map afterwards
});
present(encoder); // tone map + sRGB into the canvas
device.queue.submit([encoder.finish()]); // one update() per submitted encoder
Conventions: matrices are column-major (three's elements order), the projection has WebGPU depth ([0, 1]; set
camera.reversedDepth for a reversed one), colours are linear and already multiplied by intensity, and a light's
direction is the way the light travels. frameId must grow by one per frame: a skipped id drops the temporal
history. With settings.srgbBlending (his default) the composite reads targets.sceneColor, a copy of the colour
target made before it; turn it off for a premultiplied blend that needs no copy. frame.render() is single-sample:
an MSAA host composites after its resolve.
Volume → mesh shadows and the fire light
Give a light a shadow request and the core draws a Beer shadow map of every volume for it
(settings.castShadows): 'fit' cascades around the volumes for a directional light, the host's own camera for a spot
light, a cube for a point light. outputs.volumeShadows.get(lightId) holds the map and a uniform buffer of the view it
was drawn with, so a receiver never projects with a newer camera than the map's. With emissionLight in the input,
the emissive volumes fill irradiance probes, outputs.emissionProbes.
Your surfaces read both through two WGSL functions the core gives you:
// A receiver's WGSL: the core's two receiver functions (they declare their structs) and your bindings.
const receiverWgsl = /* wgsl */ `
${vdbVolumeShadowWgsl()}
${vdbEmissionProbeWgsl()}
@group(1) @binding(0) var volumeShadowMap: texture_2d_array<f32>;
@group(1) @binding(1) var<uniform> volumeShadowView: VdbVolumeShadowView;
@group(1) @binding(2) var linearSampler: sampler;
@group(1) @binding(3) var emissionProbes: texture_3d<f32>;
@group(1) @binding(4) var<uniform> emissionGrid: VdbEmissionGrid;
fn lit(world: vec3f, n: vec3f, albedo: vec3f, sun: vec3f, toSun: vec3f, viewDepth: f32, fireIntensity: f32) -> vec3f {
// volume -> mesh: the Beer map, read with the view the core drew it with
let shadow = vdbVolumeShadow(volumeShadowMap, linearSampler, volumeShadowView, world, viewDepth);
// the volumes' emission as irradiance (a Lambert surface: albedo * E / pi)
let fire = vdbEmissionIrradiance(emissionProbes, linearSampler, emissionGrid, world, n) * fireIntensity / 3.14159265;
return albedo * (sun * max(dot(n, toSun), 0.0) * shadow + fire);
}`;
// Rebuild when u.outputs.version moves (a texture is a new object). Before the first map: bind placeholders
// (a zeroed view buffer reads 1, a zeroed grid 0).
const map = u.outputs.volumeShadows.get('sun');
const probes = u.outputs.emissionProbes;
if (!map || !probes) return null;
return device.createBindGroup({
layout,
entries: [
{ binding: 0, resource: map.texture.createView({ dimension: '2d-array' }) },
{ binding: 1, resource: { buffer: map.view } },
{ binding: 2, resource: linear },
{ binding: 3, resource: probes.texture.createView({ dimension: '3d' }) },
{ binding: 4, resource: { buffer: probes.grid } },
],
});
viewDepth is the receiver's distance in front of the camera (it picks the cascade). A point light's map is read
through a cube view with vdbVolumeShadowCube.
Mesh → volume shadows
The other direction needs only a depth map you already have: draw your meshes from the light into a depth texture and
pass it as an occluder (input.occluders), with the view-projection it was drawn with. The core tests each lit
position against it, as volumetric fog does with a light's shadow map: the 'frame' transmittance keeps the direct part
unoccluded and occludes the scattered part per grid voxel, the gather occludes the direct part at each froxel, and
'volume' lighting stops the march. Pass a '2d-array' view (cascades or cube faces as layers, up to 6; an r32float
texture works too), and bump revision when the map is redrawn: without one the transmittance is redrawn every frame.
At most 4 maps are bound. Demo 17 draws its obelisk and level set into the sun's map, and the obelisk's shadow crosses
the cloud.
The environment
input.environment takes any linear float texture with TEXTURE_BINDING: an equirect (u = atan2(d.z, d.x) / 2π + ½
along x, row 0 the zenith) or a cube (6 layers). The core reduces it on the GPU, with no readback, to spherical harmonics
and up to 12 lights (settings.environmentLighting), so a compute PMREM, a sky model or an HDR loader's output plugs
straight in. A new id or revision re-runs the reduction; rotation (3 × 3, world → map) turns it. Demo 17 writes a
procedural sky into an rgba16float equirect and draws the same texture as its background.
Composite in your own shader
frame.render() is one fullscreen pass. If the volumes should land inside your own pass (a post chain, a deferred
resolve, a custom blend), call the integrate function there instead:
// Composite in your own fragment shader instead of frame.render(): bind the integrated froxels at a slot of
// your choice and call vdbFrameIntegrate with the pixel's screen uv (bottom-left) and its view depth.
const code = /* wgsl */ `
${vdbFrameIntegrateWgsl({ slot: { group: 1, firstBinding: 0 } })}
fn overScene(sceneColor: vec3f, screenUv: vec2f, viewDepth: f32) -> vec3f {
let i = vdbFrameIntegrate(screenUv, viewDepth);
if (i.transmittance.y >= 1.0) {
return sceneColor; // no volume in front of this pixel
}
return vdbFrameComposite(i, vec4f(sceneColor, 1.0), true).rgb; // his sRGB blend
}`;
const layout = device.createBindGroupLayout({ entries: vdbFrameIntegrateEntries({ group: 1, firstBinding: 0 }) });
const group = device.createBindGroup({
layout,
entries: [
{ binding: 0, resource: u.outputs.froxels!.integrated.createView({ dimension: '3d' }) },
{ binding: 1, resource: linear },
{ binding: 2, resource: { buffer: u.outputs.uniforms } },
],
});
Demo 17 has both: its "composite" control switches between frame.render() and its present shader, and its e2e checks
that the two give the same image (within one 8-bit step).
Level sets and points
Level sets are surfaces, not media, so they are drawn in your render pass: vdbCreateLevelSetPass(device, pool, { colorFormat, depthFormat, output }) rasterizes each grid's box and traces the pixel-centre ray, writing frag_depth
(so it depth-tests against your meshes and the volumes composite in front of or behind it) and either a Lambert shade or
a G-buffer texel (world normal, ray distance) for your own lighting. Call record(renderPass, { view, projection, viewport }, [{ grid }]) after the pool's update. The same pass with a light's orthographic camera draws the level set
into that light's depth map. vdbCreatePointPass draws point grids as sprites the same way. See
Level sets and points for the three.js materials over the same WGSL.
import { vdbCreateLevelSetPass, vdbCreatePointPass, type VDBGridPool } from '@pmndrs/vdb';
import { vdbPointRenderData, type VdbPointDataGrid } from '@pmndrs/vdb/tools';
// Once: one pass per attachment format pair. 'shade' is a Lambert surface; 'gbuffer' writes normal and distance.
const levelSets = vdbCreateLevelSetPass(device, pool, { colorFormat: 'rgba16float', depthFormat: 'depth32float' });
const points = vdbCreatePointPass(device, { colorFormat: 'rgba16float', depthFormat: 'depth32float' });
const data = vdbPointRenderData(pointGrid, { maxPoints: 1_000_000 }); // world positions, Cd colours
points.setPoints({ positions: data.positions, colors: data.colors, count: data.count });
// Every frame, inside your render pass, after pool.update(encoder). Matrices are column-major, depth in [0, 1].
const frame = { view: camera.view, projection: camera.projection, viewport: [0, 0, width, height] as const };
levelSets.record(renderPass, { ...frame, light: { direction: [-0.4, -0.8, -0.45], color: [1, 1, 1] } }, [
{ grid: levelSetId, color: [0.8, 0.75, 0.7] }, // traced per pixel; writes frag_depth
]);
points.record(renderPass, { ...frame, diameter: data.spacing }); // round sprites, depth-tested
A pass decodes the grid types the pool held when it was made; pass types when the pool fills later. Two passes
with the same formats share one pipeline. To shade in your own shader instead, vdbLevelSetSurfaceWgsl() is the trace
(vdbLevelSetSurface, composed after a march module with the 'levelSet' helper) and vdbPointSpriteWgsl() the
sprite's size and vertex maths; the passes above are built from them.
Settings
frame.settings is a plain object (VDBFrameSettings), read at each update(). The field list, with defaults, is in
Froxel renderer: @pmndrs/vdb/three's frameRenderer.settings is the same object.
vdbFrameDefaultSettings() returns the defaults and vdbFrameDefaultVolumeParams() a volume's.
import { vdbCreateFrameRenderer, vdbFrameDefaultSettings, type VDBFrameSettings } from '@pmndrs/vdb';
// The defaults as plain data (mjurczyk's, plus occlusion and ambientOcclusion on).
const defaults: VDBFrameSettings = vdbFrameDefaultSettings();
console.log(defaults.lighting, defaults.multipleScattering); // 'frame', 'neural'
// Pass a part of them at creation, or write frame.settings at any time: the next update() reads it.
const frame = vdbCreateFrameRenderer(device, { settings: { lighting: 'volume', multipleScattering: 'two-sided' } });
frame.settings.temporal = false; // frees the history texture at the next update
frame.settings.occlusion = false; // your depth maps no longer shadow the volumes
Values out of the device's range (shadowResolution, castShadowMapSize) clamp with a one-time console warning.
multipleScattering: 'neural' (the default) and 'mlp' run a small network through frame.mlp; that data shape
(VDBFrameMlp) is experimental and may change before 1.0, the modes' names are not.
Things to know
- Build bind groups that read
pool.bufferor the core's outputs after the updates of the frame, and rebuild them whenpool.versionoroutputs.versionmoves: a growth or a resize makes new objects, and the old ones stay alive for two frames, so a stale bind group draws the previous frame's data without a validation error. dispose()frees what the renderer made (textures, buffers, pipelines' resources); the device, the pools, the sources and your textures stay yours.- Turning a feature off frees its textures at the next
update(): the temporal history whentemporalgoes off, 'frame' lighting's transmittance and environment grids in 'volume' lighting (the shadow grid too withoutcastShadows), and the 'neural' or 'multi-scale' mips in other modes. Turning it on again makes them again and redraws them. Textures the scene stops needing (the extra spectral slot groups, the partial sums of a second source) go after 120 updates in a row without them, so a camera that alternates does not reallocate every frame. The froxel textures themselves go after 120 updates with no visible volume in view:outputs.froxelsis null then (andoutputs.versionmoves), so a host composite that binds them must bind a placeholder, as demo 17 does. They come back, empty, at the next update that draws a volume. - Every kernel has a CPU twin in
@pmndrs/vdb/toolsand matches it within 2 f16 ulp. Through the same kernels, demo 17's frame loop and the three adapter give byte-identical frames for mjurczyk's reference scenes (K6.1c).