Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

three-gpu-pathtracer

gkjohnson9.1kMIT0.0.23TypeScript support: included

Path tracing renderer and utilities for three.js built on top of three-mesh-bvh.

webgl, threejs, performance, geometry, graphics, mesh, renderer, raytracing, bvh, three-js, path-tracing, three-mesh-bvh, rtx, path-tracer, path, tracer

readme

three-gpu-pathtracer

npm version build github twitter sponsors

Path tracing project using three-mesh-bvh and WebGL 2 to accelerate high quality, physically based rendering on the GPU. Features include support for GGX surface model, material information, textures, normal maps, emission, environment maps, tiled rendering, and more!

More features and capabilities in progress!

Examples

Setup

Basic glTF Setup Example

Basic Primitive Geometry Example

Beauty Demos

Physically Based Materials

Lego Models

Interior Scene

Depth of Field

HDR Image

Features

Skinned Geometry Support

Morph Target Support

Area Light Support

Spot Light Support

Volumetric Fog Support

Test Scenes

Material Test Orb

Transmission Preset Orb

Model Viewer Fidelity Scene Comparisons

Physical Material Database

Tools

Animation Rendering

Ambient Occlusion Material

Running examples locally

To run and modify the examples locally, make sure you have Node and NPM installed. Check the supported versions in the test configuration.

In order to install dependencies, you will need make and a C++ compiler available.

On Debian or Ubuntu, run sudo apt install build-essential. It should just work on MacOS.

  • To install dependencies, run npm install
  • To start the demos run npm start
  • Visit http://localhost:1234/<demo-name.html>

Use

Basic Renderer

import * as THREE from 'three';
import { WebGLPathTracer } from 'three-gpu-pathtracer';

// init scene, camera, controls, etc

renderer = new THREE.WebGLRenderer();
renderer.toneMapping = THREE.ACESFilmicToneMapping;

pathTracer = new WebGLPathTracer( renderer );
pathTracer.setScene( scene, camera );

animate();

function animate() {

    requestAnimationFrame( animate );
    pathTracer.renderSample();

}

Blurred Environment Map

Using a pre blurred envioronment map can help improve frame convergence time at the cost of sharp environment reflections. If performance is concern then multiple importance sampling can be disabled and blurred environment map used.

import { BlurredEnvMapGenerator } from 'three-gpu-pathtracer';

// ...

const envMap = await new RGBELoader().setDataType( THREE.FloatType ).loadAsync( envMapUrl );
const generator = new BlurredEnvMapGenerator( renderer );
const blurredEnvMap = generator.generate( envMap, 0.35 );

// render!

Exports

WebGLPathTracer

constructor

constructor( renderer : WebGLRenderer )

.bounces

bounces = 10 : Number

Max number of lights bounces to trace.

.filteredGlossyFactor

filteredGlossyFactor = 0 : Number

Factor for alleviating bright pixels from rays that hit diffuse surfaces then specular surfaces. Setting this higher alleviates fireflies but will remove some specular caustics.

.tiles

tiles = ( 3, 3 ) : Vector2

Number of tiles on x and y to render to. Can be used to improve the responsiveness of a page while still rendering a high resolution target.

.renderDelay

renderDelay = 100 : Number

Number of milliseconds to delay rendering samples after the path tracer has been reset.

.fadeDuration

fadeDuration = 500 : Number

How long to take to fade the fully path traced scene in in milliseconds wen rendering to the canvas.

.minSamples

minSamples = 5 : Number

How many samples to render before displaying to the canvas.

.dynamicLowRes

dynamicLowRes = false : Boolean

Whether to render an extra low resolution of the scene while the full resolution renders. The scale is defined by lowResScale.

.lowResScale

lowResScale = 0.1 : Number

The scale to render the low resolution pass at.

.synchronizeRenderSize

synchronizeRenderSize = true : Boolean

Whether to automatically update the sie of the path traced buffer when the canvas size changes.

.renderScale

renderScale = 1 : Number

The scale to render the path traced image at. Only relevant if synchronizeRenderSize is true.

.renderToCanvas

renderToCanvas = true : Boolean

Whether to automatically render the path traced buffer to the canvas when renderSample is called.

.rasterizeScene

rasterizeScene = true : Boolean

Whether to automatically rasterize the scene with the three.js renderer while the path traced buffer is rendering.

.textureSize

textureSize = ( 1024, 1024 ) : Vector2

The dimensions to expand or shrink all textures to so all scene textures can be packed into a single texture array.

.samples

readonly samples : Number

The number of samples that have been rendered.

.target

readonly target : WebGLRenderTarget

The path traced render target. This potentially changes every call to renderSample.

.setScene

setScene( scene : Scene, camera : Camera ) : void

Sets the scene and camera to render. Must be called again when the camera object changes, the geometry in the scene changes, or new materials are assigned.

While only changed data is updated it is still a relatively expensive function. Prefer to use the other "update" functions where possible.

.setSceneAsync

setSceneAsync(
    scene : Scene,
    camera : Camera,
    options = {
        onProgress = null : value => void,
    } : Object
) : void

Asynchronous version of setScene. Requires calling setBVHWorker first.

.updateCamera

updateCamera() : void

Updates the camera parameters. Must be called if any of the parameters on the previously set camera change.

.updateMaterials

updateMaterials() : void

Updates the material properties. Must be called when properties change for any materials already being used.

Note that materials used with WebGLPathTracer support the following additional properties:

// Whether to render the object as completely transparent against the rest
// of the environment so other objects can be composited later
matte = false : Boolean;

// Whether the object should cast a shadow
castShadow = true : Boolean;

.updateEnvironment

updateEnvironment() : void

Updates lighting from the scene environment and background properties. Must be called if any associated scene settings change on the set scene object.

.updateLights

updateLights() : void

Updates lights used in path tracing. Must be called if any lights are added or removed or properties change.

.renderSample

renderSample() : void

Render a single sample to the path tracer target. If renderToCanvas is true then the image is rendered to the canvas.

.reset

reset() : void

Restart the rendering.

.dispose

dispose() : void

Dispose the path tracer assets. Any materials or textures used must be disposed separately.

PhysicalCamera

extends THREE.PerspectiveCamera

An extension of the three.js PerspectiveCamera with some other parameters associated with depth of field. These parameters otherwise do not affect the camera behavior are are for convenience of use with the PhysicalCameraUniform and pathtracer.

.focusDistance

focusDistance = 25 : Number

The distance from the camera in meters that everything is is perfect focus.

.fStop

fStop = 1.4 : Number

The fstop value of the camera. If this is changed then the bokehSize field is implicitly updated.

.bokehSize

bokehSize : Number

The bokeh size as derived from the fStop and focal length in millimeters. If this is set then the fStop is implicitly updated.

.apertureBlades

apertureBlades = 0 : Number

The number of sides / blades on the aperture.

.apertureRotation

apertureRotation = 0 : Number

The rotation of the aperture shape in radians.

.anamorphicRatio

anamorphicRatio = 1 : Number

The anamorphic ratio of the lens. A higher value will stretch the bokeh effect horizontally.

EquirectCamera

extends THREE.Camera

A class indicating that the path tracer should render an equirectangular view. Does not work with three.js raster rendering.

PhysicalSpotLight

extends THREE.SpotLight

.radius

radius = 0 : Number

The radius of the spotlight surface. Increase this value to add softness to shadows.

.iesMap

iesMap = null : Texture

The loaded IES texture describing directional light intensity. These can be loaded with the IESLoader.

Premade IES profiles can be downloaded from [ieslibrary.com]. And custom profiles can be generated using CNDL.

ShapedAreaLight

extends THREE.RectAreaLight

.isCircular

isCircular = false : Boolean

Whether the area light should be rendered as a circle or a rectangle.

IESLoader

extends Loader

Loader for loading and parsing IES profile data. Load and parse functions return a DataTexture with the profile contents.

BlurredEnvMapGenerator

Utility for generating a PMREM blurred environment map that can be used with the path tracer.

constructor

constructor( renderer : WebGLRenderer )

.generate

generate( texture : Texture, blur : Number ) : DataTexture

Takes a texture to blur and the amount to blur it. Returns a new DataTexture that has been PMREM blurred environment map that can have distribution data generated for importance sampling.

.dispose

dispose() : void

Disposes of the temporary files and textures for generation.

GradientEquirectTexture

.exponent

exponent = 2 : Number

.topColor

topColor = 0xffffff : Color

.bottomColor

bottomColor = 0x000000 : Color

constructor

constructor( resolution = 512 : Number )

.update

update() : void

MaterialBase

extends THREE.ShaderMaterial

Convenience base class that adds additional functions and implicitly adds object definitions for all uniforms of the shader to the object.

.setDefine

setDefine( name : string, value = undefined : any ) : void

Sets the define of the given name to the provided value. If the value is set to null or undefined then it is deleted from the defines of the material. If the define changed from the previous value then Material.needsUpdate is set to true.

FogVolumeMaterial

extends MeshStandardMaterial

A material used for rendering fog-like volumes within the scene. The color, emissive, and emissiveIntensity fields are all used in the render.

NOTE Since fog models many particles throughout the scene and cause many extra bounces fog materials can dramatically impact render time.

.density

The particulate density of the volume.

DenoiseMaterial

extends MaterialBase

Denoise material based on BrutPitt/glslSmartDeNoise intended to be the final pass to the screen. Includes tonemapping and color space conversions.

Uniforms

{

    // sigma - sigma Standard Deviation
    // kSigma - sigma coefficient
    // kSigma * sigma = radius of the circular kernel
    sigma = 5.0 : Number,
    kSigma = 1.0 : Number,

    // edge sharpening threshold
    threshold = 0.03 : Number,

}

Gotchas

  • The project requires use of WebGL2.
  • All textures must use the same wrap and interpolation flags.
  • SpotLights, DirectionalLights, and PointLights are only supported with MIS.
  • Only MeshStandardMaterial and MeshPhysicalMaterial are supported.
  • Instanced geometry and interleaved buffers are not supported.
  • Emissive materials are supported but do not take advantage of MIS.

Screenshots

Sample materials

"SD Macross City Standoff Diorama" scene by tipatat

"Interior Scene" model by Allay Design

Perseverance Rover, Ingenuity Helicopter models by NASA / JPL-Caltech

Gelatinous Cube model by glenatron

Lego models courtesy of the LDraw Official Model Repository

Octopus Tea model by AzTiZ

Botanists Study model by riikkakilpelainen

Japanese Bridge Garden model by kristenlee

Resources

Raytracing in One Weekend Book

PBR Book

knightcrawler25/GLSL-PathTracer

DassaultSystemes-Technology/dspbr-pt

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

Added

  • Support for CubeTexture environments and backgrounds.
  • Compilation happens asynchronously to avoid blocking the browser.

Fixed

  • Corner case where marking a mesh as non-visible would not remove the geometry.
  • Meshes not updating when replacing the geometry with a new one.

[0.0.22] - 2024.05.03

Fixed

  • Non visible meshes not being hidden when path traced.

[0.0.21] - 2024.04.29

Fixed

  • Reverted transmission BRDF function changes resulting in black artifacts.

Added

  • WebGLPathTracer class for more easily intializing a path tracer.
  • Typescript definitions.

Changed

  • PathTracingRenderer, DynamicPathTracingSceneGenerator, and more classes have been deprecated in favor of WebGLPathTracer. See new README for API.
  • Remove IESLoader in favor of three.js' version.

[0.0.20] - 2024.02.21

Fixed

  • Adjust peer dependency semver for three-mesh-bvh.

[0.0.19] - 2024.02.20

Fixed

  • Ensure materials texture is never a degenerate dimension.
  • Handle completely black environment maps so they do not corrupt the image.

[0.0.18] - 2024.02.20

Fixed

  • Transparent textures no longer have the color incorrectly premultiplied by the alpha.
  • Fix rounding error issue in tiled rendering logic causing some columns and rows of pixels to not be rendered to.
  • Improve hdr map info to be able to handle any texture type.
  • Path tracing scene generators no longer crash when an empty scene is provided.
  • Native three.js spot light not working correctly.
  • Env map clamping which was causing an incorrect circle of color to display at the poles.

Added

  • Support for stratified sampling in addition to PCG and Sobol under the RANDOM_TYPE flag.

Changed

  • Rendering objects with negative scales now requires three-mesh-bvh v0.7.2

[0.0.17] - 2024.01.18

Added

  • Support for rendering geometry with inverted scales.

Changed

  • Upgrade to three-mesh-bvh v0.7.0.
  • AreaLights no longer render the light surface.
  • Disabled sobol sampling functionality related to MacOS crashes. It can be re-enabled with the FEATURE_SOBOL define flag.

Fixed

  • Models with a negative scale not rendering correctly.
  • Renderer crashing on MacOS devices.
  • Renderer crashing on Android devices.
  • Rendering not working at all on iOS devices due to lacking support for linearly interpolated Float32 textures.

[0.0.16] - 2023-07-21

Fixed

  • Reverted change that caused NaN values on some hardware.

[0.0.15] - 2023-05-20

Fixed

  • Missing file extension.

Added

  • CompatibilityDetector to determine whether the path tracer can run on the target platform.
  • DEBUG_MODE define to PhysicalPathTracingMaterial to render out ray depth.
  • GradientMapMaterial to map texture values to a color ramp.
  • Support for copy function to ShapedAreaLight, PhysicalCamera, and PhysicalSpotLight.

Changed

  • Fog hits no longer count as transparent surface bounces.
  • Remove precision workaround for Equirect map.
  • Significant refactoring to make more effective use of structs.

[0.0.14] - 2023-03-05

Added

  • Support for volumetric fog material.
  • Disable sampling of the environment map if env intensity is set to 0.0 to improve direct light sampling rate.

Changed

  • Base color is now applied both on the way in and out of a transmissive material.
  • Improved performance of env map CDF processing.

Fixed

  • Area light shapes now consistently cast shadows in MIS and non MIS mode

[0.0.13] - 2023-02-13

Changed

  • TRANSPARENT_TRAVERSALS define to transmissiveBounces uniform.
  • EquirectUniformInfo now defaults to a white environment map so lighting is present by default.
  • Add "stepRayOrigin" function for reuse in the path tracer functions.

Added

  • Transmissive materials now traverse more bounces than non transmissive materials for improved quality. See transmissiveBounces uniform.
  • Support for russian roulette path termination after 3 bounces. See the FEATURE_RUSSIAN_ROULETTE flag.
  • QuiltPathTracingRenderer to enable rending for the Looking Glass Display.

Fixed

  • PathTracingSceneGenerator / Worker: include point lights and directional lights in the result.
  • Translucent and transparent meshes incorrectly completely blocking area and punctual lights.
  • Respect the Material "sheen" field.
  • Incorrectly squaring the sheen term.
  • Iridescence being incorrectly applied to materials.

[0.0.12] - 2023-01-29

Fixed

  • Added workaround for Windows machines to address case where the shader compilation would fail due to arrays being passed as function arguments.

[0.0.11] - 2023-01-05

Fixed

  • Incorrect import statement extension.

[0.0.10] - 2023-01-04

Fixed

  • Equirect sampling CDF offset values causing env maps with 1 bright pixel to be most noticeably incorrect.
  • Clearcoat roughness map values not being respected.

[0.0.9] - 2022-12-31

Added

  • Support for Material.flatShading to render flat-shaded materials.
  • Support for randomization using Owen-scrambled and shuffled Sobol values enabling sample stratification and image in fewer samples.
  • Support for directional lights, point lights.

Fixed

  • Roughness and metalness maps not being assigned correctly.
  • Case where textures using shared "Source" with different encodings were not treated as unique.
  • Spot Lights no longer have a dark hot spot.

Changed

  • Move "random" functions around.

[0.0.8] - 2022-12-11

Fixed

  • Three.js semver package version.
  • Removed 3 texture sampler units to add room for future features, background map.
  • Texture memory leak in BlurredEnvMapGenerator.
  • PathtracingSceneGenerator / DynamicPathTracingSceneGenerator: both generators now only include visible geometry in the result.

Added

  • GradientEquirectTexture class for generating an equirect background texture with a gradient.
  • AttributesTextureArray class for storing multiple vertex attribute buffers in a sampler array to save texture units.

Removed

  • Removed FEATURE_GRADIENT_BG define and bgGradientTop, bgGradientBottom uniforms. Use the new GradientEquirectTexture class instead.
  • PhysicalPathTracingMaterial: Removed normalAttribute, tangentAttribute, uvAttribute, and colorAttribute uniforms. Use attributesArray to store those parameters, instead.
  • MaterialsTexture.setSide function.

Changed

  • MaterialsTexture automatically uses the specified material side unless the object is transmissive - in which case double-sided is used.
  • Used textures are now reduced to just those with unique sources.
  • PhysicalPathTracingMaterial.uniforms.environmentRotation from a Matrix3 to a Matrix4.
  • Updated three-mesh-bvh to v0.5.19.
  • Rework application of Fresnel based on Joe Shutte's Disney BSDF writeup resulting in improve handling of metalness brightness.
  • Use a 1.1 fresnel by default for plastics since it matches GlTF models more exactly.

[0.0.7] - 2022-10-15

Added

  • DenoiseMaterial based on "glslSmartDenoise" to smooth the final render.
  • Support for vertex colors.
  • Support for attenuated transmission.
  • PathTracingRenderer.alpha: Docs specifying premuliplied alpha behavior.
  • Support for thin film transmission.

Fixed

  • Diffuse materials looking too dark.
  • Specular sampling to use perceptual roughness.
  • Default specular and ior values to match three.js.

Changed

  • Opacity support now requires setting material.transparent to true.

[0.0.6] - 2022-08-06

Added

  • Support for sheen parameter support
  • Support for iridescence parameter support
  • Support for lights to the DynamicPathTracingGenerator
  • Support for circular area lights
  • Support for spot lights
  • Add support for specular color and intensity control
  • Support for IES Profiles on the new "PhysicalSpotLight" class
  • IESLoader for loading IES profiles as textures

Changed

  • PhysicalPathTracingMaterial: Default "environment intensity" from 2.0 to 1.0.

Fixed

  • White hotspots at some glancing angles.

[0.0.5] - 2022-07-16

Added

  • Support for equirect rendering with EquirectCamera.
  • Support for area lights.
  • Support for threejs compatible texture transforms.
  • Support for Clearcoat properties.
  • Support for arrays of objects passed to pathtracer scene generator.

Fixed

  • Black renders on M1 Safari devices.
  • Camera ray direction recision issues when scrolling far from the origin.

[0.0.4] - 2022-06-12

Fixed

  • Textures not working correctly on Pixel 6 due to an issue with floatBitsToInt.
  • PathTracingRenderer.alpha not being able to be changed after rendering.
  • Improved reflective behavior for perfectly smooth surfaces.
  • Case where partially transparent objects would cast full shadows.

Added

  • Support for material alpha map.
  • Ability to disable casting of shadows.
  • Support for rendering with Orthographic cameras.
  • Support for texture transform properties per texture.

[0.0.3] - 2022-05-22

Fixed

  • Some black artifacts when rendering with depth of field.
  • DynamicPathTracingSceneGenerator.reset not correctly resetting the class resulting in errors when calling "generate" again.

Changed

  • Materials to use a texture instead of uniforms to cut down on max uniform errors.
  • SUPPORT_DOF no longer needs to be explicitly set and will be toggled automatically based on the bokeh size parameter.
  • Removed direct support for environment blur with addition of MIS. Instead use BlurredEnvMapGenerator to preblur an environment map.
  • Antialiasing jitter is now performed per ray in the shader instead of via camera position jitter for improved AA.
  • GRADIENT_BG define option to FEATURE_GRADIENT_BG

Added

  • Support for "matte" material flag.
  • Support for Multiple Importance Sampling for the envionment map and an associated "FEATURE_MIS" flag.
  • BlurredEnvMapGenerator to blur environment maps.
  • Support for rendering transparent backgrounds.

Removed

  • Support for gradient environment colors. Use a DataTexture, instead.

[0.0.2] - 2022-04-26

Added

  • Support for material sidedness which must be set explicitly on the material uniforms. See MaterialStructUniform.side for more information.
  • DynamicPathTracingSceneGenerator to support skinned and morph target meshes.
  • A PhysicalCamera instance and associated shader uniforms and updates to support camera depth of field and shaped bokeh.
  • PathTracingSceneWorker as separate from the synchronous PathTracingSceneGenerator to support more build processes.
  • Support for morph target, skinned meshes to scene generators.

Changed

  • PhysicalPathTracingMaterial to have a "bounces" uniform rather than define.
  • PathTracingSceneGenerator is now synchronous.

Fixed

  • Case where material arrays did not work correctly.

[0.0.1] - 2022-04-08

Initial release with support for path tracing physically based materials with properties including metalness, transmission, roughness, opacity, alpha cutout, and more!