v0.0.30
three-gpu-pathtracer
GPU-accelerated path tracing renderer for physically based rendering on the web. Supports PBR materials, area lights, depth of field, and volumetric fog for high-quality rendering visuals.
renderingvisualizationphysically-based
import { ACESFilmicToneMapping, Color, RectAreaLight } from 'three/webgpu';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { GenerateMeshBVHWorker } from 'three-mesh-bvh/worker';
import { GradientEquirectTexture } from 'three-gpu-pathtracer';
import { WebGPUPathTracer } from 'three-gpu-pathtracer/webgpu';
import { createFloor } from './floor.js';
// scene, camera and renderer are initialized here
const URL = 'https://raw.githubusercontent.com/gkjohnson/3d-demo-data/main/models/nasa-m2020/MER_static.glb';
const { scene: model } = await new GLTFLoader()
.setDRACOLoader( new DRACOLoader() )
.loadAsync( URL );
scene.background = new Color( 0xeeeeee );
renderer.toneMapping = ACESFilmicToneMapping;
scene.add( model, createFloor() );
camera.position.set( 1.65, 1.95, 2.9 );
controls.target.set( 0, 0.6, 0 );
controls.update();
// area lights: a warm key, a cool fill and a back light
const key = new RectAreaLight( 0xfff0dd, 9, 3.2, 3.2 );
key.position.set( 3.2, 3.4, 2.8 );
key.lookAt( 0, 1.35, 0 );
const fill = new RectAreaLight( 0xdfeaff, 2.25, 4, 4 );
fill.position.set( - 3.9, 2.1, 2.5 );
fill.lookAt( 0, 1.35, 0 );
const back = new RectAreaLight( 0xffffff, 13.5, 2.6, 2.6 );
back.position.set( - 1.9, 3.7, - 3.6 );
back.lookAt( 0, 1.35, 0 );
scene.add( key, fill, back );
// a dim gradient environment for soft fill light
const environment = new GradientEquirectTexture();
environment.topColor.set( 0x333333 );
environment.bottomColor.set( 0x111111 );
environment.update();
scene.environment = environment;
// BVHs build in workers and shaders compile in the background, so the page never stalls
const pathTracer = new WebGPUPathTracer( renderer );
pathTracer.setBVHWorker( GenerateMeshBVHWorker );
pathTracer.asyncCompile = true;
// mobile GPUs are slower: fewer paths per frame keep frames fast, and fewer pixels converge sooner
if ( matchMedia( '(pointer: coarse)' ).matches ) {
pathTracer.frameBudget = 50000;
renderer.setPixelRatio( window.devicePixelRatio / 2 );
}
// samples accumulate while the camera is still, up to 32, and restart when it moves
pathTracer.maxSamples = 32;
await pathTracer.setSceneAsync( scene, camera );
controls.addEventListener( 'change', () => pathTracer.updateCamera() );
window.addEventListener( 'resize', () => pathTracer.updateCamera() );
renderer.setAnimationLoop( () => pathTracer.renderSample() );Install
npm install three-gpu-pathtracerChangelog
0.0.30
Fixed
- WebGPUPathTracer: "adaptiveFrameBudget" not adapting with three.js r188, which replaces the renderer's "trackTimestamp" with "GPUTimer".
0.0.29
Added
- WebGPUPathTracer: Support for bump maps, with "bumpScale" as a height in object units.
- WebGPUPathTracer: "interpolateDisplay", set to false to show the render without interpolation when it's drawn at a different size than the canvas.
- WebGPUPathTracer: "renderTime" and "elapsedTime" in the sample counts, the milliseconds spent rendering and the milliseconds since rendering started.
- WebGPUPathTracer: Emissive surfaces are sampled as lights, picked with a light tree by their power and distance, so scenes lit by emissive geometry converge much faster. Node materials' "emissiveNode" graphs are sampled too.
- WebGPUPathTracer: "renderTo", which draws the render into a render target or the canvas, either as presented with the "beauty" output or as the "accumulated" samples alone.
- WebGPUPathTracer: Support for render target textures as the scene's environment, copied on "updateEnvironment".
- WebGPUPathTracer: "finished" in the sample counts, the share of pixels done sampling, which "complete" waits on.
- WebGPUPathTracer: Node materials' TSL graphs, including MaterialX materials, are path traced, with their textures sampled from the texture atlas. See the "WebGPUPathTracer" docs for the limitations.
- React Three Fiber components from "three-gpu-pathtracer/r3f": "WebGPUPathTracer", which renders the fiber scene and takes the path tracer's settings and events as props, "Denoiser" and "Upscaler", which attach a denoiser or upscaler instance the app owns, and the "usePathTracer" hook.
Changed
- WebGPUPathTracer: "setScene" returns a promise that resolves once the scene renders, since three-mesh-bvh writes the scene data asynchronously. Rendering pauses on the last frame until then. It builds the mesh BVHs in the workers set with "setBVHWorker", replacing "setSceneAsync".
- WebGPUPathTracer: A camera's "getCameraRay" function takes the pixel and a [0, 1) random sample for the point within it, "( pixel, sample, resolution, ray )", in place of the jittered uv, so a camera knows exactly which pixel it renders.
- WebGPUPathTracer: Renamed the material models to shading models, "ShadingModel" and "GLTFShadingModel", set with "setShadingModel".
- ShadingModel: The ray traversal, medium stack and kernels read hit opacity, volumes, dispersion, matte and the denoiser guides through the shading model's functions, so a custom shading model's material and surface record structs only need the fields its own functions read. The base "ShadingModel" is a minimal diffuse model, and the glTF material lives in "GLTFShadingModel".
- WebGPUPathTracer: Requires three-mesh-bvh 0.10.0. Shadow rays end their traversal at the first opaque hit.
- WebGPUPathTracer: "setSize" updates the camera, so camera ray functions that depend on the render size follow it.
- WebGPUPathTracer: Vertex colors are only stored on the GPU when the scene has them, reducing memory use.
- WebGPUPathTracer: Russian roulette starts at the second vertex, as in Cycles, so paths end sooner and samples are cheaper.
- WebGPUPathTracer: The wavefront path tracer rebuilds its pixel queue after each pass, skipping finished pixels and the ones the camera declines and sampling the pixels that fall behind first, so renders with "maxSamples" finish sooner and more evenly. The queue shrinks with the render size, except during the low res preview.
- WebGPUPathTracer: The render fades in over the low res preview, which shows through the pixels the render hasn't reached yet. With a "minSamples" of 0 the preview stays until every pixel has a sample.
- WebGPUPathTracer: The samples accumulate in place in a single float array texture rather than two RGBA float targets, halving the memory they use.
- WebGPUPathTracer: The environment's importance sampling is built on the GPU, which is much faster.
- WebGPUPathTracer: The wavefront path tracer sizes its path slots to the pixels left to sample, so renders with "maxSamples" finish up to 20% sooner.
- WebGPUPathTracer: The megakernel renders several samples per "renderSample" call when the whole frame fits in "frameBudget".
- WebGPUPathTracer: The megakernel indexes each pixel's random sequence by its own sample count, as the wavefront does.
- WebGPUPathTracer: The megakernel measures its sample counts on the GPU, as the wavefront does.
- WebGPUPathTracer: Random samples only compute the Sobol dimensions they use, so the megakernel renders up to 5% faster.
- WebGPUPathTracer: The fog, subsurface, clearcoat, sheen, iridescence, anisotropy and transmission code, including thin walls and dispersion, and the emissive light sampling, is only compiled into the kernels when a material uses it, so scenes without them render faster.
- WebGPUPathTracer: The wavefront path tracer sorts its paths by material graph before shading, so scenes with many graphs render up to 1.7x faster.
- WebGPUPathTracer: Dispersion samples its wavelengths over 380 to 780 nm in proportion to their visible brightness, as Cycles does, reducing color noise.
- WebGPUPathTracer: "dispose" no longer disposes the attached denoiser and upscaler.
- WebGPUPathTracer: Adding or removing textures and IES profiles no longer recompiles the kernels in "updateMaterials" and "updateLights".
- WebGPUPathTracer: Materials are kept in scene order, so a scene builds the same kernels on every load and the browser's shader cache skips recompiling them.
Removed
- WebGPUPathTracer: "setSceneAsync". Use "setScene", which uses the workers when they're set.
- WebGPUPathTracer: "lowResScale" and "adaptiveDynamicRes". The low res preview is always sized to the pixels a frame of "frameBudget" can render.
- WebGPUPathTracer: "asyncCompile". The kernels always compile asynchronously, so nothing new renders while "compiling" is true.
- WebGPUPathTracer: "target". Use "renderTo" to draw the render into a render target.
- WebGLPathTracer, along with "PathTracingRenderer", "QuiltPathTracingRenderer", "PathTracingSceneGenerator", "PhysicalPathTracingMaterial", "BlurredEnvMapGenerator", "DenoiseMaterial" and "AmbientOcclusionMaterial" from "three-gpu-pathtracer". Use "WebGPUPathTracer" and "BlurredEnvMapGenerator" from "three-gpu-pathtracer/webgpu".
- The "xatlas-web" peer dependency.
- WebGPUPathTracer: The material "castShadow" field, which had no effect. Use "visibleToShadowRays" on the object.
Fixed
- WebGPUPathTracer: Cube map environments and backgrounds throwing an error.
- WebGPUPathTracer: Setting an ArrayCamera throwing an error.
- WebGPUPathTracer: Changing "transmissiveBackground" not restarting a finished render.
- WebGPUPathTracer: "getRenderTime" and "samplesPerSecond" counting the time spent paused, finished, with the page hidden or between "renderSample" calls, such as after "maxSamples" is reached and then raised.
- WebGPUPathTracer: Renders with "stableNoise" disabled reusing nearly the same random sequence after each reset.
- WebGPUPathTracer: Rays parallel to an axis missing bounds they lie on the face of.
- WebGPUPathTracer: "reset" restoring an old "frameBudget" when "adaptiveFrameBudget" is off.
- WebGPUPathTracer: Alpha test not working correctly when "transparent" is not set.
- WebGPUPathTracer: The wavefront path tracer failing to compile in Safari.
- WebGPUPathTracer: Materials without a "color" field, such as NodeMaterial and ShaderMaterial, throwing an error.
- WebGPUPathTracer: "sheenRoughnessMap" being read from the red channel instead of alpha.
- WebGPUPathTracer: Dark background and clear colors rendering darker than set, quantized to 8 bits.
- WebGPUPathTracer: Fireflies around small bright areas of the environment map, which is now importance sampled with the bilinear filtering it's read with.
- WebGPUPathTracer: Pixels staying flagged as active after a smaller frame budget drops their path.
- WebGPUPathTracer: The megakernel's adaptive frame budget overshooting "targetFrameTime" in heavy scenes and alternating between one and two samples per frame in light ones.
- WebGPUPathTracer: The triangles of a BatchedMesh's geometries being mixed together.
- WebGPUPathTracer: "stableNoise" not always rendering the same noise.
- WebGPUPathTracer: Never rendering with "dynamicLowRes" off and a "renderDelay".
- WebGPUPathTracer: Anisotropic materials losing energy and rendering darker as "anisotropy" increases.
- WebGPUPathTracer: An error thrown when it's disposed right after being created, such as in React's StrictMode.
- WebGPUPathTracer: Dark colors in sRGB textures banding, since the texture atlas stored them decoded to linear in 8 bits.
- WebGPUPathTracer: Calling "updateMaterials" while "setScene" is building hiding every object in the new scene.
- WebGPUPathTracer: Light through transmissive objects hidden with "visibleToShadowRays" being counted twice.
0.0.28
Added
- WebGPUPathTracer: "setBVHWorker" and "setSceneAsync" to build the mesh BVHs in workers without blocking the page.
- WebGPUPathTracer: A "complete" event and flag once every pixel has reached "maxSamples" and the denoiser, if any, has run.
- WebGPUPathTracer: A "progress" event with the stage the render is in, "building", "compiling", "rendering" or "denoising", and how far along it is.
- WebGPUPathTracer: "renderToCanvas" flag to skip drawing the result to the render target.
- OIDNDenoiser: An "error" event when a pass started by "update" fails, such as the weights failing to download, and a "progress" event as each tile finishes.
- WebGPUPathTracer: The scene data is available to kernels and camera ray functions through the TSL context as "bvhData".
- WebGPUPathTracer: "renderAuxBuffer" draws the albedo or normal auxiliary buffer given to the denoiser.
- WebGPUPathTracer: "adaptiveFrameBudget" and "targetFrameTime" to tune "frameBudget" toward a GPU frame time, measured as "frameTime". Enabled by default, and requires a renderer created with "trackTimestamp". A reset restores the budget from before the last pixels of a capped render finished.
- WebGPUPathTracer: "adaptiveDynamicRes" adjusts "lowResScale" to the pixels a frame of "frameBudget" can render.
- WebGPUPathTracer: Subsurface scattering through volumes, with "scatterStrength", "multiscatterColor" and "scatterAnisotropy" material properties following the "KHR_materials_scatter" proposal, and "maxSubsurfaceBounces".
- WebGPUPathTracer: A medium stack, so nested and overlapping volumes, like liquid in a glass, absorb and scatter together, including when the camera is inside one.
- WebGPUPathTracer: "FogVolumeMaterial" support, with light sampled inside the fog, shadows through it, a "scatterAnisotropy" phase setting, and "maxFogBounces", single scattering by default as in Cycles.
- FogVolumeNodeMaterial: A fog volume material for the WebGPU path tracer that also renders as haze when rasterized.
Changed
- WebGPUPathTracer: Requires three.js r187, which binds every storage buffer as an array.
- WebGPUPathTracer: Sobol is the default random strategy instead of Blue Dither.
- WebGPUPathTracer: "renderDelay" defaults to 16 and "fadeDuration" to 250, and the fade starts once the average pixel reaches "minSamples", so the full render shows sooner after the camera stops.
- OIDNDenoiser: Uses "oidn-webgpu" 0.6.0, the renamed "oidn-web" package, and paces denoising tiles to display frames.
- OIDNDenoiser: The albedo and normal auxiliary buffers are accumulated by the path tracer from the first scatter of every sample instead of rasterized, so they match the image under depth of field and transparency. "setScene" is gone and "update" takes the auxiliary buffer.
- AtlasTexture: Pages grow to the device texture size limit instead of 4096, so larger textures are no longer downsampled.
- WebGPUPathTracer: Pixels a camera declines count as finished once "maxSamples" is set.
- WebGPUPathTracer: Kernels are compiled asynchronously before the first render, so the page no longer freezes while they compile.
- WebGPUPathTracer: "updateEnvironment" no longer rebuilds an unchanged environment map on every call.
- WebGPUPathTracer: Shadow rays pass through partially transparent surfaces, attenuated by their opacity, and stop at the first opaque hit.
Fixed
- WebGPUPathTracer: Rough transmission at the critical angle producing a NaN direction, which could stall the GPU for minutes inside subsurface scattering volumes.
- WebGPUPathTracer: Diffuse surfaces losing energy when "specularIntensity" or "specularColor" is lowered.
- WebGPUPathTracer: Light through partially transparent objects hidden with "visibleToShadowRays" no longer being counted twice.
- WebGPUPathTracer: Environment map importance sampling accuracy.
- WebGPUPathTracer: Nearest filtered environment and background textures no longer fail to compile.
- WebGPUPathTracer: "setSceneAsync" no longer leaves geometry unusable when its attributes share a buffer, as in glTF files.
- WebGPUPathTracer: Path buffers being reallocated on every reset for the low resolution preview.
- WebGPUPathTracer: NaN pixels next to thin area lights.
- WebGPUPathTracer: Diffuse surfaces losing indirect light when viewed almost head-on.
- WebGPUPathTracer: Area and environment light being lost at the last bounce.
- WebGPUPathTracer: Transmissive volumes being tinted by the square of their color, now matching Blender.
- PathTracingSceneGenerator: ReferenceError when regenerating a scene whose geometry hasn't changed.