three-gpu-pathtracer 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +815 -728
  3. package/build/index.module.js +5474 -3706
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +5479 -3704
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +68 -60
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -111
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +270 -255
  11. package/src/core/PathTracingSceneGenerator.js +4 -3
  12. package/src/index.js +35 -26
  13. package/src/materials/AlphaDisplayMaterial.js +48 -48
  14. package/src/materials/AmbientOcclusionMaterial.js +197 -197
  15. package/src/materials/BlendMaterial.js +67 -67
  16. package/src/materials/DenoiseMaterial.js +142 -0
  17. package/src/materials/GraphMaterial.js +243 -0
  18. package/src/materials/LambertPathTracingMaterial.js +285 -285
  19. package/src/materials/MaterialBase.js +56 -56
  20. package/src/materials/PhysicalPathTracingMaterial.js +970 -848
  21. package/src/{core → objects}/EquirectCamera.js +13 -13
  22. package/src/{core → objects}/PhysicalCamera.js +28 -28
  23. package/src/objects/PhysicalSpotLight.js +14 -0
  24. package/src/objects/ShapedAreaLight.js +12 -0
  25. package/src/shader/shaderEnvMapSampling.js +59 -59
  26. package/src/shader/shaderGGXFunctions.js +100 -108
  27. package/src/shader/shaderIridescenceFunctions.js +130 -0
  28. package/src/shader/shaderLightSampling.js +231 -87
  29. package/src/shader/shaderMaterialSampling.js +542 -501
  30. package/src/shader/shaderSheenFunctions.js +98 -0
  31. package/src/shader/shaderStructs.js +321 -191
  32. package/src/shader/shaderUtils.js +364 -287
  33. package/src/uniforms/EquirectHdrInfoUniform.js +259 -263
  34. package/src/uniforms/IESProfilesTexture.js +100 -0
  35. package/src/uniforms/LightsInfoUniformStruct.js +162 -0
  36. package/src/uniforms/MaterialsTexture.js +426 -319
  37. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  38. package/src/uniforms/RenderTarget2DArray.js +93 -93
  39. package/src/utils/BlurredEnvMapGenerator.js +113 -113
  40. package/src/utils/GeometryPreparationUtils.js +21 -1
  41. package/src/utils/IESLoader.js +325 -0
  42. package/src/utils/UVUnwrapper.js +101 -101
  43. package/src/workers/PathTracingSceneWorker.js +42 -41
  44. package/src/uniforms/LightsTexture.js +0 -83
package/README.md CHANGED
@@ -1,728 +1,815 @@
1
- # three-gpu-pathtracer
2
-
3
- [![npm version](https://img.shields.io/npm/v/three-gpu-pathtracer.svg?style=flat-square)](https://www.npmjs.com/package/three-gpu-pathtracer)
4
- [![lgtm code quality](https://img.shields.io/lgtm/grade/javascript/g/gkjohnson/three-gpu-pathtracer.svg?style=flat-square&label=code-quality)](https://lgtm.com/projects/g/gkjohnson/three-gpu-pathtracer/)
5
- [![build](https://img.shields.io/github/workflow/status/gkjohnson/three-gpu-pathtracer/Node.js%20CI?style=flat-square&label=build)](https://github.com/gkjohnson/three-gpu-pathtracer/actions)
6
- [![github](https://flat.badgen.net/badge/icon/github?icon=github&label)](https://github.com/gkjohnson/three-gpu-pathtracer/)
7
- [![twitter](https://flat.badgen.net/twitter/follow/garrettkjohnson)](https://twitter.com/garrettkjohnson)
8
- [![sponsors](https://img.shields.io/github/sponsors/gkjohnson?style=flat-square&color=1da1f2)](https://github.com/sponsors/gkjohnson/)
9
-
10
- ![](https://user-images.githubusercontent.com/734200/162287477-96696b18-890b-4c1b-8a73-d662e577cc48.png)
11
-
12
- Path tracing project using [three-mesh-bvh](https://github.com/gkjohnson/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!
13
-
14
- _More features and capabilities in progress!_
15
-
16
- # Examples
17
-
18
- **Beauty Demos**
19
-
20
- [Physically Based Materials](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/index.html)
21
-
22
- [Lego Models](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/lego.html)
23
-
24
- [Interior Scene](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/interior.html)
25
-
26
- [Depth of Field](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/depthOfField.html)
27
-
28
- **Features**
29
-
30
- [Skinned Geometry Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/skinnedMesh.html)
31
-
32
- [Morph Target Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/skinnedMesh.html#morphtarget)
33
-
34
- [Area Light Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/areaLight.html)
35
-
36
- **Test Scenes**
37
-
38
- [Material Test Orb](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html)
39
-
40
- [Transmission Preset Orb](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html#transmission)
41
-
42
- [Model Viewer Fidelity Scene Comparisons](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/viewerTest.html#khronos-DragonAttenuation)
43
-
44
- **Light Baking**
45
-
46
- [Ambient Occlusion Material](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/aoRender.html)
47
-
48
- # Use
49
-
50
- **Basic Renderer**
51
-
52
- ```js
53
- import * as THREE from 'three';
54
- import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
55
- import {
56
- PathTracingSceneGenerator,
57
- PathTracingRenderer,
58
- PhysicalPathTracingMaterial,
59
- } from 'three-gpu-pathtracer';
60
-
61
- // init scene, renderer, camera, controls, etc
62
-
63
- renderer.outputEncoding = THREE.sRGBEncoding;
64
- renderer.toneMapping = THREE.ACESFilmicToneMapping;
65
-
66
- // initialize the path tracing material and renderer
67
- const ptMaterial = new PhysicalPathTracingMaterial();
68
- const ptRenderer = new PathTracingRenderer( renderer );
69
- ptRenderer.setSize( window.innerWidth, window.innerHeight );
70
- ptRenderer.camera = camera;
71
- ptRenderer.material = ptMaterial;
72
-
73
- // if rendering transparent background
74
- ptRenderer.alpha = true;
75
-
76
- // init quad for rendering to the canvas
77
- const fsQuad = new FullScreenQuad( new THREE.MeshBasicMaterial( {
78
- map: ptRenderer.target.texture,
79
-
80
- // if rendering transparent background
81
- blending: THREE.CustomBlending,
82
- } ) );
83
-
84
- // ensure scene matrices are up to date
85
- scene.updateMatrixWorld();
86
-
87
- // initialize the scene and update the material properties with the bvh, materials, etc
88
- const generator = new PathTracingSceneGenerator();
89
- const { bvh, textures, materials, lights } = generator.generate( scene );
90
-
91
- // update bvh and geometry attribute textures
92
- ptMaterial.bvh.updateFrom( bvh );
93
- ptMaterial.normalAttribute.updateFrom( geometry.attributes.normal );
94
- ptMaterial.tangentAttribute.updateFrom( geometry.attributes.tangent );
95
- ptMaterial.uvAttribute.updateFrom( geometry.attributes.uv );
96
-
97
- // update materials and texture arrays
98
- ptMaterial.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
99
- ptMaterial.textures.setTextures( renderer, 2048, 2048, textures );
100
- ptMaterial.materials.updateFrom( materials, textures );
101
-
102
- // update the lights
103
- ptMaterial.lights.updateFrom( lights );
104
- ptMaterial.lightCount = lights.length;
105
-
106
- // set the environment map
107
- const texture = await new RGBELoader().loadAsync( envMapUrl );
108
- ptRenderer.material.envMapInfo.updateFrom( texture );
109
-
110
- animate();
111
-
112
- // ...
113
-
114
- function animate() {
115
-
116
- // if the camera position changes call "ptRenderer.reset()"
117
-
118
- // update the camera and render one sample
119
- camera.updateMatrixWorld();
120
- ptRenderer.update();
121
-
122
- // if using alpha = true then the target texture will change every frame
123
- // so we must retrieve it before render.
124
- fsQuad.material.map = ptRenderer.target.texture;
125
-
126
- // copy the current state of the path tracer to canvas to display
127
- fsQuad.render( renderer );
128
-
129
- }
130
- ```
131
-
132
- **Blurred Environment Map**
133
-
134
- 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.
135
-
136
- ```js
137
- import { BlurredEnvMapGenerator } from 'three-gpu-pathtracer';
138
-
139
- // ...
140
-
141
- const envMap = await new RGBELoader().loadAsync( envMapUrl );
142
- const generator = new BlurredEnvMapGenerator( renderer );
143
- const blurredEnvMap = generator.generate( envMap, 0.35 );
144
-
145
- // render!
146
-
147
- ```
148
-
149
- ## Dynamic Scenes
150
-
151
- Using the dynamic scene generator the same, frequently updated scene can be converted into a single reusable geometry multiple times and BVH refit which greatly improves subsequent scene updates. See `DynamicPathTracingSceneGenerator` docs for more info.
152
-
153
- ```js
154
- import { DynamicPathTracingSceneGenerator } from 'three-gpu-pathtracer';
155
-
156
- // ... initialize scene etc
157
-
158
- const generator = new DynamicPathTracingSceneGenerator( scene );
159
- const { bvh, textures, materials } = generator.generate( scene );
160
-
161
- // ... update path tracer and render
162
- ```
163
-
164
- ## Asynchronous Scene Generation
165
-
166
- _NOTE WebWorker syntax is inconsistently supported across bundlers and sometimes not supported at all so the PathTracingSceneWorker class is not exported from the package root. If needed the code from src/worker can be copied and modified to accomodate a particular build process._
167
-
168
- ```js
169
- import { PathTracingSceneWorker } from 'three-gpu-pathtracer/src/workers/PathTracingSceneWorker.js';
170
-
171
- // ...
172
-
173
- // initialize the scene and update the material properties with the bvh, materials, etc
174
- const generator = new PathTracingSceneWorker();
175
- const { bvh, textures, materials, lights } = await generator.generate( scene );
176
-
177
- // ...
178
- ```
179
-
180
- # Exports
181
-
182
- ## PathTracingRenderer
183
-
184
- Utility class for tracking and rendering a path traced scene to a render target.
185
-
186
- ### .samples
187
-
188
- ```js
189
- readonly samples : Number
190
- ```
191
-
192
- Number of samples per pixel that have been rendered to the target.
193
-
194
- ### .target
195
-
196
- ```js
197
- readonly target : WebGLRenderTarget
198
- ```
199
-
200
- The target being rendered to. The size of the target is updated with `setSize` and is initialized to a FloatType texture.
201
-
202
- ### .camera
203
-
204
- ```js
205
- camera = null : Camera
206
- ```
207
-
208
- The camera to render with. The view offset of the camera will be updated every sample to enable anti aliasing.
209
-
210
- ### .material
211
-
212
- ```js
213
- material = null : ShaderMaterial
214
- ```
215
-
216
- The Path Tracing material to render. This is expected to be a full screen quad material that respects the "opacity" field for every pixel so samples can be accumulated over time. The material is also expected to have `cameraWorldMatrix` and `invProjectionMatrix` fields of type `Matrix4`.
217
-
218
- ### .tiles
219
-
220
- ```js
221
- tiles = ( 1, 1 ) : Vector2
222
- ```
223
-
224
- 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.
225
-
226
- ### .stableNoise
227
-
228
- ```js
229
- stableNoise = false : Boolean
230
- ```
231
-
232
- Whether to reset the random seed to `0` when restarting the render. If true then a consistent random sample pattern will appear when moving the camera, for example.
233
-
234
- ### .alpha
235
-
236
- ```js
237
- alpha = false : Boolean
238
- ```
239
-
240
- Whether to support rendering scenes with transparent backgrounds. When transparent backgrounds are used two extra render targets are used, custom blending is performed, and PathTracingRenderer.target will change on every completed sample.
241
-
242
- ### constructor
243
-
244
- ```js
245
- constructor( renderer : WebGLRenderer )
246
- ```
247
-
248
- ### .setSize
249
-
250
- ```js
251
- setSize( size : Vector2 ) : void
252
- ```
253
-
254
- Sets the size of the target to render to.
255
-
256
- ### .update
257
-
258
- ```js
259
- update()
260
- ```
261
-
262
- Renders a single sample to the target.
263
-
264
- ### .reset
265
-
266
- ```js
267
- reset() : void
268
- ```
269
-
270
- Resets and restarts the render from scratch.
271
-
272
- ## PathTracingSceneGenerator
273
-
274
- Utility class for generating the set of data required for initializing the path tracing material with a bvh, geometry, materials, and textures.
275
-
276
- ### .generate
277
-
278
- ```js
279
- generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
280
- bvh : MeshBVH,
281
- materials : Array<Material>,
282
- textures : Array<Texture>,
283
- lights : Array<Light>
284
- }
285
- ```
286
-
287
- Merges the geometry in the given scene with an additional "materialIndex" attribute that references the associated material array. Also produces a set of textures referenced by the scene materials.
288
-
289
- ## PathTracingSceneWorker
290
-
291
- _extends PathTracingSceneGenerator_
292
-
293
- See note in [Asyncronous Generation](#asynchronous-generation) use snippet.
294
-
295
- ### .generate
296
-
297
- ```js
298
- async generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
299
- bvh : MeshBVH,
300
- materials : Array<Material>,
301
- textures : Array<Texture>,
302
- lights : Array<Light>
303
- }
304
- ```
305
-
306
- ### .dispose
307
-
308
- ```js
309
- dispose() : void
310
- ```
311
-
312
- ## PhysicalCamera
313
-
314
- _extends THREE.PerspectiveCamera_
315
-
316
- 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.
317
-
318
- ### .focusDistance
319
-
320
- ```js
321
- focusDistance = 25 : Number
322
- ```
323
-
324
- The distance from the camera in meters that everything is is perfect focus.
325
-
326
- ### .fStop
327
-
328
- ```js
329
- fStop = 1.4 : Number
330
- ```
331
-
332
- The fstop value of the camera. If this is changed then the `bokehSize` field is implicitly updated.
333
-
334
- ### .bokehSize
335
-
336
- ```js
337
- bokehSize : Number
338
- ```
339
-
340
- The bokeh size as derived from the fStop and focal length in millimeters. If this is set then the fStop is implicitly updated.
341
-
342
- ### .apertureBlades
343
-
344
- ```js
345
- apertureBlades = 0 : Number
346
- ```
347
-
348
- The number of sides / blades on the aperture.
349
-
350
- ### .apertureRotation
351
-
352
- ```js
353
- apertureRotation = 0 : Number
354
- ```
355
-
356
- The rotation of the aperture shape in radians.
357
-
358
- ### .anamorphicRatio
359
-
360
- ```js
361
- anamorphicRatio = 1 : Number
362
- ```
363
-
364
- The anamorphic ratio of the lens. A higher value will stretch the bokeh effect horizontally.
365
-
366
- ## EquirectCamera
367
-
368
- _extends THREE.Camera_
369
-
370
- A class indicating that the path tracer should render an equirectangular view. Does not work with three.js raster rendering.
371
-
372
- ## DynamicPathTracingSceneGenerator
373
-
374
- A variation of the path tracing scene generator intended for quickly regenerating a scene BVH representation that updates frequently. Ie those with animated objects or animated skinned geometry.
375
-
376
- In order to quickly update a dynamic scene the same BVH is reused across updates by refitting rather than regenerating. This is significantly faster but also results in a less optimal BVH after significant changes.
377
-
378
- If geometry or materials are added or removed from the scene then `reset` must be called.
379
-
380
- ### constructor
381
-
382
- ```js
383
- constructor( scene : Object3D | Array<Object3D> )
384
- ```
385
-
386
- Takes the scene to convert.
387
-
388
- ### .generate
389
-
390
- ```js
391
- generate() : {
392
- bvh : MeshBVH,
393
- materials : Array<Material>,
394
- textures : Array<Texture>
395
- }
396
- ```
397
-
398
- Generates and refits the bvh to the current scene state. The same bvh, materials, and textures objects are returns after the initial call until `reset` is called.
399
-
400
- ### .reset
401
-
402
- ```js
403
- reset() : void
404
- ```
405
-
406
- Resets the generator so a new BVH is generated. This must be called when geometry, objects, or materials are added or removed from the scene.
407
-
408
- ## BlurredEnvMapGenerator
409
-
410
- Utility for generating a PMREM blurred environment map that can be used with the path tracer.
411
-
412
- ### constructor
413
-
414
- ```js
415
- constructor( renderer : WebGLRenderer )
416
- ```
417
-
418
- ### .generate
419
-
420
- ```js
421
- generate( texture : Texture, blur : Number ) : DataTexture
422
- ```
423
-
424
- 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.
425
-
426
- ### .dispose
427
-
428
- ```js
429
- dispose() : void
430
- ```
431
-
432
- Disposes of the temporary files and textures for generation.
433
-
434
- ## MaterialBase
435
-
436
- _extends THREE.ShaderMaterial_
437
-
438
- Convenience base class that adds additional functions and implicitly adds object definitions for all uniforms of the shader to the object.
439
-
440
- ### .setDefine
441
-
442
- ```js
443
- setDefine( name : string, value = undefined : any ) : void
444
- ```
445
-
446
- 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`.
447
-
448
- ## PhysicalPathTracingMaterial
449
-
450
- _extends MaterialBase_
451
-
452
- **Uniforms**
453
-
454
- ```js
455
- {
456
- // The number of ray bounces to test. Higher is better quality but slower performance.
457
- bounces = 3 : Number,
458
-
459
- // The physical camera parameters to use
460
- physicalCamera : PhysicalCameraUniform,
461
-
462
- // Geometry and BVH information
463
- bvh: MeshBVHUniformStruct,
464
- normalAttribute: FloatVertexAttributeTexture,
465
- tangentAttribute: FloatVertexAttributeTexture,
466
- uvAttribute: FloatVertexAttributeTexture,
467
- materialIndexAttribute: UIntVertexAttributeTexture,
468
- materials: MaterialsTexture,
469
- textures: RenderTarget2DArray,
470
-
471
- // Light information
472
- lights: LightsTexture,
473
- lightCount = 0: Number,
474
-
475
- // Environment Map information
476
- envMapInfo: EquirectHdrInfoUniform,
477
- environmentRotation: Matrix3,
478
- environmentIntensity = 1: Number,
479
-
480
- // background blur
481
- backgroundBlur = 0: Number,
482
-
483
- // Factor for alleviating bright pixels from rays that hit diffuse surfaces then
484
- // specular surfaces. Setting this higher alleviates fireflies but will remove some
485
- // specular caustics.
486
- filterGlossyFactor = 0: Number,
487
-
488
- // The colors to use for the gradient background when enabled.
489
- bgGradientTop: Color,
490
- bgGradientBottom: Color,
491
-
492
- // The transparency to render the background with. Note that the "alpha" option
493
- // must be set to true on PathTracingRenderer for this field to work properly.
494
- backgroundAlpha: 1.0,
495
- }
496
- ```
497
-
498
- **Defines**
499
-
500
- ```js
501
- {
502
-
503
- // Whether to use multiple importance sampling to help the image converge more quickly
504
- FEATURE_MIS = 1 : Number,
505
-
506
- // Whether to use the "bg" gradient fields to sample for the background
507
- FEATURE_GRADIENT_BG = 0 : Number
508
-
509
- // The number of transparent pixels to allow on top of existing bounces for object transparency.
510
- TRANSPARENT_TRAVERSALS = 5 : Number,
511
-
512
- }
513
- ```
514
-
515
- ## RenderTarget2DArray
516
-
517
- _extends WebGLArrayRenderTarget_
518
-
519
- A convenience extension from `WebGLArrayRenderTarget` that affords easily creating a uniform texture array from an array of textures.
520
-
521
- ### .setTextures
522
-
523
- ```js
524
- setTextures(
525
- renderer : WebGLRenderer,
526
- width : Number,
527
- height : Number,
528
- textures : Array<Texture>
529
- ) : void
530
- ```
531
-
532
- Takes the rendering context to updateh the target for, the target dimensions of the texture array, and the array of textures to render into the 2D texture array. Every texture is stretched to the dimensions of the texture array at the same index they are provided in.
533
-
534
- ## PhysicalCameraUniform
535
-
536
- Uniform for storing the camera parameters for use with the shader.
537
-
538
- ### .updateFrom
539
-
540
- ```js
541
- updateFrom( camera : PerspectiveCamera | PhysicalCamera ) : void
542
- ```
543
-
544
- Copies all fields from the passed PhysicalCamera if available otherwise the defaults are used.
545
-
546
- ## MaterialsTexture
547
-
548
- _extends DataTexture_
549
-
550
- Helper texture uniform for encoding materials as texture data.
551
-
552
- ### .threeCompatibilityTransforms
553
-
554
- ```js
555
- threeCompatibilityTransforms = false : Boolean
556
- ```
557
-
558
- Three.js materials support only a single set of UV transforms in a certain fallback priority while the pathtracer supports a unique set of transforms per texture. Set this field to true in order to use the same uv transform handling as three.js materials.
559
-
560
- See fallback order documentation [here](https://threejs.org/docs/#api/en/textures/Texture.offset).
561
-
562
- ### .setSide
563
-
564
- ```js
565
- setSide( index : Number, side : FrontSide | BackSide | DoubleSide ) : void
566
- ```
567
-
568
- Sets the side to render for the given material.
569
-
570
- ### .setMatte
571
-
572
- ```js
573
- setMatte( index : Number, matte : Boolean ) : void
574
- ```
575
-
576
- Sets whether or not the material of the given index is matte or not. When "true" the background is rendered in place of the material.
577
-
578
- ### .setCastShadow
579
-
580
- ```js
581
- setCastShadow( index : Number, enabled : Boolean ) : void
582
- ```
583
-
584
- Sets whether or not the material of the given index will cast shadows. When "false" materials will not cast shadows on diffuse surfaces but will still be reflected. This is a good setting for lighting enclosed interiors with environment lighting.
585
-
586
- ### .updateFrom
587
-
588
- ```js
589
- updateFrom( materials : Array<Material>, textures : Array<Texture> ) : void
590
- ```
591
-
592
- Updates the size and values of the texture to align with the provided set of materials and textures.
593
-
594
- The "matte" and "side" values must be updated explicitly.
595
-
596
- ## LightsTexture
597
-
598
- _extends DataTexture_
599
-
600
- Helper texture uniform for encoding lights as texture data.
601
-
602
- ### .updateFrom
603
-
604
- ```js
605
- updateFrom( lights : Array<Light> ) : void
606
- ```
607
-
608
- Updates the size and values of the texture to align with the provided set of lights.
609
-
610
- ## EquirectHdrInfoUniform
611
-
612
- Stores the environment map contents along with the intensity distribution information to support multiple importance sampling.
613
-
614
- ### .updateFrom
615
-
616
- ```js
617
- updateFrom( environmentMap : Texture ) : void
618
- ```
619
-
620
- Takes an environment map to process into something usable by the path tracer. Is expected to be a DataTexture so the data can be read.
621
-
622
- ## Functions
623
-
624
- ### mergeMeshes
625
-
626
- ```js
627
- mergeMeshes( meshes : Array<Mesh> ) : {
628
- materials : Array<Material>,
629
- textures : Array<Textures>,
630
- geometry : BufferGeometry
631
- }
632
- ```
633
-
634
- Merges the set of meshes into a single geometry with a `materialIndex` vertex attribute included on the geometry identifying the associated material in the returned `materials` array.
635
-
636
- ## Shader Chunks
637
-
638
- **shaderMaterialSampling**
639
-
640
- Set of functions for performing material scatter and PDF sampling. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderMaterialSampling.js) for full list of functions.
641
-
642
- **shaderLightSampling**
643
-
644
- Set of functions for performing light sampling. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderLightSampling.js) for full list of functions.
645
-
646
- **shaderStructs**
647
-
648
- Material and light struct definition for use with uniforms. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderStructs.js) for full list of functions.
649
-
650
- **shaderUtils**
651
-
652
- Set of randomness and other light transport utilities for use in a shader. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderUtils.js) for full list of functions.
653
-
654
- # Gotchas
655
-
656
- - The project requires use of WebGL2.
657
- - All textures must use the same wrap and interpolation flags.
658
-
659
- # Screenshots
660
-
661
- ![](https://user-images.githubusercontent.com/734200/162584469-68e6df38-92da-4a13-b352-ca0bdea14548.png)
662
-
663
- <p align="center">
664
- <i>Sample materials</i>
665
- </p>
666
-
667
- ![](https://user-images.githubusercontent.com/734200/163835927-be75d2c0-f27b-4e4b-a3eb-2371043fa5e1.png)
668
-
669
- ![](https://user-images.githubusercontent.com/734200/163839431-ed75e64d-9ae4-4423-afca-55162a44873e.png)
670
-
671
- <p align="center">
672
- <i>"SD Macross City Standoff Diorama" scene by <a href="https://sketchfab.com/3d-models/sd-macross-city-standoff-diorama-b154220f7e7441799d6be2f7ff9658c7">tipatat</a></i>
673
- </p>
674
-
675
- ![](./docs/interior-scene-cropped.png)
676
-
677
- <p align="center">
678
- <i>"Interior Scene" model by <a href="https://sketchfab.com/3d-models/interior-scene-45ddbbc4c2dc4f8ca9ed99da9a78326a">Allay Design</a></i>
679
- </p>
680
-
681
- ![](https://user-images.githubusercontent.com/734200/161820794-df0da371-ee5c-4368-9e7b-5e7daf6cf3c7.png)
682
-
683
- ![](https://user-images.githubusercontent.com/734200/162550315-3cdabf40-3dea-4d7d-bcfc-eb543eea2d93.png)
684
-
685
- <p align="center">
686
- <i>Perseverance Rover, Ingenuity Helicopter models by <a href="https://mars.nasa.gov/resources/25042/mars-perseverance-rover-3d-model/">NASA / JPL-Caltech</a></i>
687
- </p>
688
-
689
- ![](https://user-images.githubusercontent.com/734200/161877900-566652e4-c799-4940-bccb-0c8f4cea5387.png)
690
-
691
- <p align="center">
692
- <i>Gelatinous Cube model by <a href="https://sketchfab.com/3d-models/gelatinous-cube-e08385238f4d4b59b012233a9fbdca21">glenatron</a></i>
693
- </p>
694
-
695
- ![](https://user-images.githubusercontent.com/734200/161822206-c27bf594-d648-4735-868e-4baf4e414802.png)
696
-
697
- ![](https://user-images.githubusercontent.com/734200/161822214-eace4297-03c4-4adc-b472-efe29a862685.png)
698
-
699
- <p align="center">
700
- <i>Lego models courtesy of the <a href="https://omr.ldraw.org/">LDraw Official Model Repository</a></i>
701
- </p>
702
-
703
- ![](https://user-images.githubusercontent.com/734200/161877196-7ae2769e-7e54-4694-9ca8-e8f5219d1c2d.png)
704
-
705
- <p align="center">
706
- <i>Octopus Tea model by <a href="https://sketchfab.com/3d-models/cartoon-octopus-takes-a-tea-bath-107260cf0fd24202a67eb037a6c760a5
707
- ">AzTiZ</a></i>
708
- </p>
709
-
710
- ![](https://user-images.githubusercontent.com/734200/173212652-de6a83e5-dd2c-49b5-8ed7-484ff8969b5b.png)
711
- <p align="center">
712
- <i>Botanists Study model by <a href="https://sketchfab.com/3d-models/the-botanists-study-8b7b5743b1c848ed8ea58f5518c44e7e">riikkakilpelainen</a></i>
713
- </p>
714
-
715
- ![](https://user-images.githubusercontent.com/734200/173170459-849b9343-efe3-4635-8719-346511472965.png)
716
- <p align="center">
717
- <i>Japanese Bridge Garden model by <a href="https://sketchfab.com/3d-models/japanese-bridge-garden-d122e17593eb4012913cde927486d15a">kristenlee</a></i>
718
- </p>
719
-
720
- ### Resources
721
-
722
- [Raytracing in One Weekend Book](https://raytracing.github.io/)
723
-
724
- [PBR Book](https://pbr-book.org/)
725
-
726
- [knightcrawler25/GLSL-PathTracer](https://github.com/knightcrawler25/GLSL-PathTracer/)
727
-
728
-
1
+ # three-gpu-pathtracer
2
+
3
+ [![npm version](https://img.shields.io/npm/v/three-gpu-pathtracer.svg?style=flat-square)](https://www.npmjs.com/package/three-gpu-pathtracer)
4
+ [![lgtm code quality](https://img.shields.io/lgtm/grade/javascript/g/gkjohnson/three-gpu-pathtracer.svg?style=flat-square&label=code-quality)](https://lgtm.com/projects/g/gkjohnson/three-gpu-pathtracer/)
5
+ [![build](https://img.shields.io/github/workflow/status/gkjohnson/three-gpu-pathtracer/Node.js%20CI?style=flat-square&label=build)](https://github.com/gkjohnson/three-gpu-pathtracer/actions)
6
+ [![github](https://flat.badgen.net/badge/icon/github?icon=github&label)](https://github.com/gkjohnson/three-gpu-pathtracer/)
7
+ [![twitter](https://flat.badgen.net/twitter/follow/garrettkjohnson)](https://twitter.com/garrettkjohnson)
8
+ [![sponsors](https://img.shields.io/github/sponsors/gkjohnson?style=flat-square&color=1da1f2)](https://github.com/sponsors/gkjohnson/)
9
+
10
+ ![](https://user-images.githubusercontent.com/734200/162287477-96696b18-890b-4c1b-8a73-d662e577cc48.png)
11
+
12
+ Path tracing project using [three-mesh-bvh](https://github.com/gkjohnson/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!
13
+
14
+ _More features and capabilities in progress!_
15
+
16
+ # Examples
17
+
18
+ **Beauty Demos**
19
+
20
+ [Physically Based Materials](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/index.html)
21
+
22
+ [Lego Models](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/lego.html)
23
+
24
+ [Interior Scene](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/interior.html)
25
+
26
+ [Depth of Field](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/depthOfField.html)
27
+
28
+ **Features**
29
+
30
+ [Skinned Geometry Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/skinnedMesh.html)
31
+
32
+ [Morph Target Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/skinnedMesh.html#morphtarget)
33
+
34
+ [Area Light Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/areaLight.html)
35
+
36
+ [Spot Light Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/spotLights.html)
37
+
38
+ **Test Scenes**
39
+
40
+ [Material Test Orb](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html)
41
+
42
+ [Transmission Preset Orb](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html#transmission)
43
+
44
+ [Model Viewer Fidelity Scene Comparisons](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/viewerTest.html#khronos-DragonAttenuation)
45
+
46
+ **Light Baking**
47
+
48
+ [Ambient Occlusion Material](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/aoRender.html)
49
+
50
+
51
+ ## Running examples locally
52
+
53
+ To run and modify the examples locally, make sure you have Node and NPM installed. Check the supported versions in [the test configuration](./.github/workflows/node.js.yml).
54
+
55
+ In order to install dependencies, you will need `make` and a C++ compiler available.
56
+
57
+ On Debian or Ubuntu, run `sudo apt install build-essential`. It should just work on MacOS.
58
+
59
+ - To install dependencies, run `npm install`
60
+ - To start the demos run `npm start`
61
+ - Visit `http://localhost:1234/<demo-name.html>`
62
+
63
+ # Use
64
+
65
+ **Basic Renderer**
66
+
67
+ ```js
68
+ import * as THREE from 'three';
69
+ import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
70
+ import {
71
+ PathTracingSceneGenerator,
72
+ PathTracingRenderer,
73
+ PhysicalPathTracingMaterial,
74
+ } from 'three-gpu-pathtracer';
75
+
76
+ // init scene, renderer, camera, controls, etc
77
+
78
+ renderer.outputEncoding = THREE.sRGBEncoding;
79
+ renderer.toneMapping = THREE.ACESFilmicToneMapping;
80
+
81
+ // initialize the path tracing material and renderer
82
+ const ptMaterial = new PhysicalPathTracingMaterial();
83
+ const ptRenderer = new PathTracingRenderer( renderer );
84
+ ptRenderer.setSize( window.innerWidth, window.innerHeight );
85
+ ptRenderer.camera = camera;
86
+ ptRenderer.material = ptMaterial;
87
+
88
+ // if rendering transparent background
89
+ ptRenderer.alpha = true;
90
+
91
+ // init quad for rendering to the canvas
92
+ const fsQuad = new FullScreenQuad( new THREE.MeshBasicMaterial( {
93
+ map: ptRenderer.target.texture,
94
+
95
+ // if rendering transparent background
96
+ blending: THREE.CustomBlending,
97
+ } ) );
98
+
99
+ // ensure scene matrices are up to date
100
+ scene.updateMatrixWorld();
101
+
102
+ // initialize the scene and update the material properties with the bvh, materials, etc
103
+ const generator = new PathTracingSceneGenerator();
104
+ const { bvh, textures, materials, lights } = generator.generate( scene );
105
+
106
+ // update bvh and geometry attribute textures
107
+ ptMaterial.bvh.updateFrom( bvh );
108
+ ptMaterial.normalAttribute.updateFrom( geometry.attributes.normal );
109
+ ptMaterial.tangentAttribute.updateFrom( geometry.attributes.tangent );
110
+ ptMaterial.uvAttribute.updateFrom( geometry.attributes.uv );
111
+
112
+ // update materials and texture arrays
113
+ ptMaterial.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
114
+ ptMaterial.textures.setTextures( renderer, 2048, 2048, textures );
115
+ ptMaterial.materials.updateFrom( materials, textures );
116
+
117
+ // update the lights
118
+ ptMaterial.lights.updateFrom( lights );
119
+
120
+ // set the environment map
121
+ const texture = await new RGBELoader().loadAsync( envMapUrl );
122
+ ptRenderer.material.envMapInfo.updateFrom( texture );
123
+
124
+ animate();
125
+
126
+ // ...
127
+
128
+ function animate() {
129
+
130
+ // if the camera position changes call "ptRenderer.reset()"
131
+
132
+ // update the camera and render one sample
133
+ camera.updateMatrixWorld();
134
+ ptRenderer.update();
135
+
136
+ // if using alpha = true then the target texture will change every frame
137
+ // so we must retrieve it before render.
138
+ fsQuad.material.map = ptRenderer.target.texture;
139
+
140
+ // copy the current state of the path tracer to canvas to display
141
+ fsQuad.render( renderer );
142
+
143
+ }
144
+ ```
145
+
146
+ **Blurred Environment Map**
147
+
148
+ 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.
149
+
150
+ ```js
151
+ import { BlurredEnvMapGenerator } from 'three-gpu-pathtracer';
152
+
153
+ // ...
154
+
155
+ const envMap = await new RGBELoader().loadAsync( envMapUrl );
156
+ const generator = new BlurredEnvMapGenerator( renderer );
157
+ const blurredEnvMap = generator.generate( envMap, 0.35 );
158
+
159
+ // render!
160
+
161
+ ```
162
+
163
+ ## Dynamic Scenes
164
+
165
+ Using the dynamic scene generator the same, frequently updated scene can be converted into a single reusable geometry multiple times and BVH refit which greatly improves subsequent scene updates. See `DynamicPathTracingSceneGenerator` docs for more info.
166
+
167
+ ```js
168
+ import { DynamicPathTracingSceneGenerator } from 'three-gpu-pathtracer';
169
+
170
+ // ... initialize scene etc
171
+
172
+ const generator = new DynamicPathTracingSceneGenerator( scene );
173
+ const { bvh, textures, materials } = generator.generate( scene );
174
+
175
+ // ... update path tracer and render
176
+ ```
177
+
178
+ ## Asynchronous Scene Generation
179
+
180
+ _NOTE WebWorker syntax is inconsistently supported across bundlers and sometimes not supported at all so the PathTracingSceneWorker class is not exported from the package root. If needed the code from src/worker can be copied and modified to accomodate a particular build process._
181
+
182
+ ```js
183
+ import { PathTracingSceneWorker } from 'three-gpu-pathtracer/src/workers/PathTracingSceneWorker.js';
184
+
185
+ // ...
186
+
187
+ // initialize the scene and update the material properties with the bvh, materials, etc
188
+ const generator = new PathTracingSceneWorker();
189
+ const { bvh, textures, materials, lights } = await generator.generate( scene );
190
+
191
+ // ...
192
+ ```
193
+
194
+ # Exports
195
+
196
+ ## PathTracingRenderer
197
+
198
+ Utility class for tracking and rendering a path traced scene to a render target.
199
+
200
+ ### .samples
201
+
202
+ ```js
203
+ readonly samples : Number
204
+ ```
205
+
206
+ Number of samples per pixel that have been rendered to the target.
207
+
208
+ ### .target
209
+
210
+ ```js
211
+ readonly target : WebGLRenderTarget
212
+ ```
213
+
214
+ The target being rendered to. The size of the target is updated with `setSize` and is initialized to a FloatType texture.
215
+
216
+ ### .camera
217
+
218
+ ```js
219
+ camera = null : Camera
220
+ ```
221
+
222
+ The camera to render with. The view offset of the camera will be updated every sample to enable anti aliasing.
223
+
224
+ ### .material
225
+
226
+ ```js
227
+ material = null : ShaderMaterial
228
+ ```
229
+
230
+ The Path Tracing material to render. This is expected to be a full screen quad material that respects the "opacity" field for every pixel so samples can be accumulated over time. The material is also expected to have `cameraWorldMatrix` and `invProjectionMatrix` fields of type `Matrix4`.
231
+
232
+ ### .tiles
233
+
234
+ ```js
235
+ tiles = ( 1, 1 ) : Vector2
236
+ ```
237
+
238
+ 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.
239
+
240
+ ### .stableNoise
241
+
242
+ ```js
243
+ stableNoise = false : Boolean
244
+ ```
245
+
246
+ Whether to reset the random seed to `0` when restarting the render. If true then a consistent random sample pattern will appear when moving the camera, for example.
247
+
248
+ ### .stableTiles
249
+
250
+ ```js
251
+ stableTiles = true : Boolean
252
+ ```
253
+
254
+ Whether the initial tile is reset to the top left tile when moving the camera or if it should continue to shift every frame.
255
+
256
+ ### .alpha
257
+
258
+ ```js
259
+ alpha = false : Boolean
260
+ ```
261
+
262
+ Whether to support rendering scenes with transparent backgrounds. When transparent backgrounds are used two extra render targets are used, custom blending is performed, and PathTracingRenderer.target will change on every completed sample.
263
+
264
+ > **Note**
265
+ > When a transparent background is used then the material used for the final render to the canvas must use the same "premultipliedAlpha" setting as the canvas the final image may look incorrect.
266
+
267
+ ### constructor
268
+
269
+ ```js
270
+ constructor( renderer : WebGLRenderer )
271
+ ```
272
+
273
+ ### .setSize
274
+
275
+ ```js
276
+ setSize( size : Vector2 ) : void
277
+ ```
278
+
279
+ Sets the size of the target to render to.
280
+
281
+ ### .update
282
+
283
+ ```js
284
+ update()
285
+ ```
286
+
287
+ Renders a single sample to the target.
288
+
289
+ ### .reset
290
+
291
+ ```js
292
+ reset() : void
293
+ ```
294
+
295
+ Resets and restarts the render from scratch.
296
+
297
+ ## PathTracingSceneGenerator
298
+
299
+ Utility class for generating the set of data required for initializing the path tracing material with a bvh, geometry, materials, and textures.
300
+
301
+ ### .generate
302
+
303
+ ```js
304
+ generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
305
+ bvh : MeshBVH,
306
+ materials : Array<Material>,
307
+ textures : Array<Texture>,
308
+ lights : Array<Light>
309
+ }
310
+ ```
311
+
312
+ Merges the geometry in the given scene with an additional "materialIndex" attribute that references the associated material array. Also produces a set of textures referenced by the scene materials.
313
+
314
+ ## PathTracingSceneWorker
315
+
316
+ _extends PathTracingSceneGenerator_
317
+
318
+ See note in [Asyncronous Generation](#asynchronous-generation) use snippet.
319
+
320
+ ### .generate
321
+
322
+ ```js
323
+ async generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
324
+ bvh : MeshBVH,
325
+ materials : Array<Material>,
326
+ textures : Array<Texture>,
327
+ lights : Array<Light>
328
+ }
329
+ ```
330
+
331
+ ### .dispose
332
+
333
+ ```js
334
+ dispose() : void
335
+ ```
336
+
337
+ ## PhysicalCamera
338
+
339
+ _extends THREE.PerspectiveCamera_
340
+
341
+ 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.
342
+
343
+ ### .focusDistance
344
+
345
+ ```js
346
+ focusDistance = 25 : Number
347
+ ```
348
+
349
+ The distance from the camera in meters that everything is is perfect focus.
350
+
351
+ ### .fStop
352
+
353
+ ```js
354
+ fStop = 1.4 : Number
355
+ ```
356
+
357
+ The fstop value of the camera. If this is changed then the `bokehSize` field is implicitly updated.
358
+
359
+ ### .bokehSize
360
+
361
+ ```js
362
+ bokehSize : Number
363
+ ```
364
+
365
+ The bokeh size as derived from the fStop and focal length in millimeters. If this is set then the fStop is implicitly updated.
366
+
367
+ ### .apertureBlades
368
+
369
+ ```js
370
+ apertureBlades = 0 : Number
371
+ ```
372
+
373
+ The number of sides / blades on the aperture.
374
+
375
+ ### .apertureRotation
376
+
377
+ ```js
378
+ apertureRotation = 0 : Number
379
+ ```
380
+
381
+ The rotation of the aperture shape in radians.
382
+
383
+ ### .anamorphicRatio
384
+
385
+ ```js
386
+ anamorphicRatio = 1 : Number
387
+ ```
388
+
389
+ The anamorphic ratio of the lens. A higher value will stretch the bokeh effect horizontally.
390
+
391
+ ## EquirectCamera
392
+
393
+ _extends THREE.Camera_
394
+
395
+ A class indicating that the path tracer should render an equirectangular view. Does not work with three.js raster rendering.
396
+
397
+ ## PhysicalSpotLight
398
+
399
+ _extends THREE.SpotLight_
400
+
401
+ ### .radius
402
+
403
+ ```js
404
+ radius = 0 : Number
405
+ ```
406
+
407
+ The radius of the spotlight surface. Increase this value to add softness to shadows.
408
+
409
+ ### .iesTexture
410
+
411
+ ```js
412
+ iesTexture = null : Texture
413
+ ```
414
+
415
+ The loaded IES texture describing directional light intensity. These can be loaded with the `IESLoader`.
416
+
417
+ Premade IES profiles can be downloaded from [ieslibrary.com]. And custom profiles can be generated using [CNDL](https://cndl.io/).
418
+
419
+ ## ShapedAreaLight
420
+
421
+ _extends THREE.RectAreaLight_
422
+
423
+ ### .isCircular
424
+
425
+ ```js
426
+ isCircular = false : Boolean
427
+ ```
428
+
429
+ Whether the area light should be rendered as a circle or a rectangle.
430
+
431
+ ## DynamicPathTracingSceneGenerator
432
+
433
+ A variation of the path tracing scene generator intended for quickly regenerating a scene BVH representation that updates frequently. Ie those with animated objects or animated skinned geometry.
434
+
435
+ In order to quickly update a dynamic scene the same BVH is reused across updates by refitting rather than regenerating. This is significantly faster but also results in a less optimal BVH after significant changes.
436
+
437
+ If geometry or materials are added or removed from the scene then `reset` must be called.
438
+
439
+ ### constructor
440
+
441
+ ```js
442
+ constructor( scene : Object3D | Array<Object3D> )
443
+ ```
444
+
445
+ Takes the scene to convert.
446
+
447
+ ### .generate
448
+
449
+ ```js
450
+ generate() : {
451
+ bvh : MeshBVH,
452
+ materials : Array<Material>,
453
+ textures : Array<Texture>
454
+ }
455
+ ```
456
+
457
+ Generates and refits the bvh to the current scene state. The same bvh, materials, and textures objects are returns after the initial call until `reset` is called.
458
+
459
+ ### .reset
460
+
461
+ ```js
462
+ reset() : void
463
+ ```
464
+
465
+ Resets the generator so a new BVH is generated. This must be called when geometry, objects, or materials are added or removed from the scene.
466
+
467
+ ## IESLoader
468
+
469
+ _extends Loader_
470
+
471
+ Loader for loading and parsing IES profile data. Load and parse functions return a `DataTexture` with the profile contents.
472
+
473
+ ## BlurredEnvMapGenerator
474
+
475
+ Utility for generating a PMREM blurred environment map that can be used with the path tracer.
476
+
477
+ ### constructor
478
+
479
+ ```js
480
+ constructor( renderer : WebGLRenderer )
481
+ ```
482
+
483
+ ### .generate
484
+
485
+ ```js
486
+ generate( texture : Texture, blur : Number ) : DataTexture
487
+ ```
488
+
489
+ 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.
490
+
491
+ ### .dispose
492
+
493
+ ```js
494
+ dispose() : void
495
+ ```
496
+
497
+ Disposes of the temporary files and textures for generation.
498
+
499
+ ## MaterialBase
500
+
501
+ _extends THREE.ShaderMaterial_
502
+
503
+ Convenience base class that adds additional functions and implicitly adds object definitions for all uniforms of the shader to the object.
504
+
505
+ ### .setDefine
506
+
507
+ ```js
508
+ setDefine( name : string, value = undefined : any ) : void
509
+ ```
510
+
511
+ 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`.
512
+
513
+ ## PhysicalPathTracingMaterial
514
+
515
+ _extends MaterialBase_
516
+
517
+ **Uniforms**
518
+
519
+ ```js
520
+ {
521
+ // The number of ray bounces to test. Higher is better quality but slower performance.
522
+ bounces = 3 : Number,
523
+
524
+ // The physical camera parameters to use
525
+ physicalCamera : PhysicalCameraUniform,
526
+
527
+ // Geometry and BVH information
528
+ bvh: MeshBVHUniformStruct,
529
+ normalAttribute: FloatVertexAttributeTexture,
530
+ tangentAttribute: FloatVertexAttributeTexture,
531
+ uvAttribute: FloatVertexAttributeTexture,
532
+ materialIndexAttribute: UIntVertexAttributeTexture,
533
+ materials: MaterialsTexture,
534
+ textures: RenderTarget2DArray,
535
+
536
+ // Light information
537
+ lights: LightsInfoUniformStruct,
538
+ iesProfiles: IESProfilesTexture,
539
+
540
+ // Environment Map information
541
+ envMapInfo: EquirectHdrInfoUniform,
542
+ environmentRotation: Matrix3,
543
+ environmentIntensity = 1: Number,
544
+
545
+ // background blur
546
+ backgroundBlur = 0: Number,
547
+
548
+ // Factor for alleviating bright pixels from rays that hit diffuse surfaces then
549
+ // specular surfaces. Setting this higher alleviates fireflies but will remove some
550
+ // specular caustics.
551
+ filterGlossyFactor = 0: Number,
552
+
553
+ // The colors to use for the gradient background when enabled.
554
+ bgGradientTop: Color,
555
+ bgGradientBottom: Color,
556
+
557
+ // The transparency to render the background with. Note that the "alpha" option
558
+ // must be set to true on PathTracingRenderer for this field to work properly.
559
+ backgroundAlpha: 1.0,
560
+ }
561
+ ```
562
+
563
+ **Defines**
564
+
565
+ ```js
566
+ {
567
+
568
+ // Whether to use multiple importance sampling to help the image converge more quickly
569
+ FEATURE_MIS = 1 : Number,
570
+
571
+ // Whether to use the "bg" gradient fields to sample for the background
572
+ FEATURE_GRADIENT_BG = 0 : Number
573
+
574
+ // The number of transparent pixels to allow on top of existing bounces for object transparency.
575
+ TRANSPARENT_TRAVERSALS = 5 : Number,
576
+
577
+ }
578
+ ```
579
+
580
+ ## DenoiseMaterial
581
+
582
+ _extends MaterialBase_
583
+
584
+ Denoise material based on [BrutPitt/glslSmartDeNoise](https://github.com/BrutPitt/glslSmartDeNoise) intended to be the final pass to the screen. Includes tonemapping and color space conversions.
585
+
586
+ **Uniforms**
587
+
588
+ ```js
589
+ {
590
+
591
+ // sigma - sigma Standard Deviation
592
+ // kSigma - sigma coefficient
593
+ // kSigma * sigma = radius of the circular kernel
594
+ sigma = 5.0 : Number,
595
+ kSigma = 1.0 : Number,
596
+
597
+ // edge sharpening threshold
598
+ threshold = 0.03 : Number,
599
+
600
+ }
601
+ ```
602
+
603
+ ## RenderTarget2DArray
604
+
605
+ _extends WebGLArrayRenderTarget_
606
+
607
+ A convenience extension from `WebGLArrayRenderTarget` that affords easily creating a uniform texture array from an array of textures.
608
+
609
+ ### .setTextures
610
+
611
+ ```js
612
+ setTextures(
613
+ renderer : WebGLRenderer,
614
+ width : Number,
615
+ height : Number,
616
+ textures : Array<Texture>
617
+ ) : void
618
+ ```
619
+
620
+ Takes the rendering context to updateh the target for, the target dimensions of the texture array, and the array of textures to render into the 2D texture array. Every texture is stretched to the dimensions of the texture array at the same index they are provided in.
621
+
622
+ ## PhysicalCameraUniform
623
+
624
+ Uniform for storing the camera parameters for use with the shader.
625
+
626
+ ### .updateFrom
627
+
628
+ ```js
629
+ updateFrom( camera : PerspectiveCamera | PhysicalCamera ) : void
630
+ ```
631
+
632
+ Copies all fields from the passed PhysicalCamera if available otherwise the defaults are used.
633
+
634
+ ## MaterialsTexture
635
+
636
+ _extends DataTexture_
637
+
638
+ Helper texture uniform for encoding materials as texture data.
639
+
640
+ ### .threeCompatibilityTransforms
641
+
642
+ ```js
643
+ threeCompatibilityTransforms = false : Boolean
644
+ ```
645
+
646
+ Three.js materials support only a single set of UV transforms in a certain fallback priority while the pathtracer supports a unique set of transforms per texture. Set this field to true in order to use the same uv transform handling as three.js materials.
647
+
648
+ See fallback order documentation [here](https://threejs.org/docs/#api/en/textures/Texture.offset).
649
+
650
+ ### .setSide
651
+
652
+ ```js
653
+ setSide( index : Number, side : FrontSide | BackSide | DoubleSide ) : void
654
+ ```
655
+
656
+ Sets the side to render for the given material.
657
+
658
+ ### .setMatte
659
+
660
+ ```js
661
+ setMatte( index : Number, matte : Boolean ) : void
662
+ ```
663
+
664
+ Sets whether or not the material of the given index is matte or not. When "true" the background is rendered in place of the material.
665
+
666
+ ### .setCastShadow
667
+
668
+ ```js
669
+ setCastShadow( index : Number, enabled : Boolean ) : void
670
+ ```
671
+
672
+ Sets whether or not the material of the given index will cast shadows. When "false" materials will not cast shadows on diffuse surfaces but will still be reflected. This is a good setting for lighting enclosed interiors with environment lighting.
673
+
674
+ ### .updateFrom
675
+
676
+ ```js
677
+ updateFrom( materials : Array<Material>, textures : Array<Texture> ) : void
678
+ ```
679
+
680
+ Updates the size and values of the texture to align with the provided set of materials and textures.
681
+
682
+ The "matte" and "side" values must be updated explicitly.
683
+
684
+ ## LightsInfoUniformStruct
685
+
686
+ Helper uniform for encoding lights as texture data with count.
687
+
688
+ ### .updateFrom
689
+
690
+ ```js
691
+ updateFrom( lights : Array<Light>, iesTextures = [] : Array<Texture> ) : void
692
+ ```
693
+
694
+ Updates the size and values of the texture to align with the provided set of lights and IES textures.
695
+
696
+ ## EquirectHdrInfoUniform
697
+
698
+ Stores the environment map contents along with the intensity distribution information to support multiple importance sampling.
699
+
700
+ ### .updateFrom
701
+
702
+ ```js
703
+ updateFrom( environmentMap : Texture ) : void
704
+ ```
705
+
706
+ Takes an environment map to process into something usable by the path tracer. Is expected to be a DataTexture so the data can be read.
707
+
708
+ ## Functions
709
+
710
+ ### mergeMeshes
711
+
712
+ ```js
713
+ mergeMeshes( meshes : Array<Mesh> ) : {
714
+ materials : Array<Material>,
715
+ textures : Array<Textures>,
716
+ geometry : BufferGeometry
717
+ }
718
+ ```
719
+
720
+ Merges the set of meshes into a single geometry with a `materialIndex` vertex attribute included on the geometry identifying the associated material in the returned `materials` array.
721
+
722
+ ## Shader Chunks
723
+
724
+ **shaderMaterialSampling**
725
+
726
+ Set of functions for performing material scatter and PDF sampling. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderMaterialSampling.js) for full list of functions.
727
+
728
+ **shaderLightSampling**
729
+
730
+ Set of functions for performing light sampling. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderLightSampling.js) for full list of functions.
731
+
732
+ **shaderStructs**
733
+
734
+ Material and light struct definition for use with uniforms. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderStructs.js) for full list of functions.
735
+
736
+ **shaderUtils**
737
+
738
+ Set of randomness and other light transport utilities for use in a shader. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderUtils.js) for full list of functions.
739
+
740
+ # Gotchas
741
+
742
+ - The project requires use of WebGL2.
743
+ - All textures must use the same wrap and interpolation flags.
744
+ - Spotlights are not supported in non-MIS rendering currently.
745
+
746
+ # Screenshots
747
+
748
+ ![](https://user-images.githubusercontent.com/734200/162584469-68e6df38-92da-4a13-b352-ca0bdea14548.png)
749
+
750
+ <p align="center">
751
+ <i>Sample materials</i>
752
+ </p>
753
+
754
+ ![](https://user-images.githubusercontent.com/734200/163835927-be75d2c0-f27b-4e4b-a3eb-2371043fa5e1.png)
755
+
756
+ ![](https://user-images.githubusercontent.com/734200/163839431-ed75e64d-9ae4-4423-afca-55162a44873e.png)
757
+
758
+ <p align="center">
759
+ <i>"SD Macross City Standoff Diorama" scene by <a href="https://sketchfab.com/3d-models/sd-macross-city-standoff-diorama-b154220f7e7441799d6be2f7ff9658c7">tipatat</a></i>
760
+ </p>
761
+
762
+ ![](./docs/interior-scene-cropped.png)
763
+
764
+ <p align="center">
765
+ <i>"Interior Scene" model by <a href="https://sketchfab.com/3d-models/interior-scene-45ddbbc4c2dc4f8ca9ed99da9a78326a">Allay Design</a></i>
766
+ </p>
767
+
768
+ ![](https://user-images.githubusercontent.com/734200/161820794-df0da371-ee5c-4368-9e7b-5e7daf6cf3c7.png)
769
+
770
+ ![](https://user-images.githubusercontent.com/734200/162550315-3cdabf40-3dea-4d7d-bcfc-eb543eea2d93.png)
771
+
772
+ <p align="center">
773
+ <i>Perseverance Rover, Ingenuity Helicopter models by <a href="https://mars.nasa.gov/resources/25042/mars-perseverance-rover-3d-model/">NASA / JPL-Caltech</a></i>
774
+ </p>
775
+
776
+ ![](https://user-images.githubusercontent.com/734200/161877900-566652e4-c799-4940-bccb-0c8f4cea5387.png)
777
+
778
+ <p align="center">
779
+ <i>Gelatinous Cube model by <a href="https://sketchfab.com/3d-models/gelatinous-cube-e08385238f4d4b59b012233a9fbdca21">glenatron</a></i>
780
+ </p>
781
+
782
+ ![](https://user-images.githubusercontent.com/734200/161822206-c27bf594-d648-4735-868e-4baf4e414802.png)
783
+
784
+ ![](https://user-images.githubusercontent.com/734200/161822214-eace4297-03c4-4adc-b472-efe29a862685.png)
785
+
786
+ <p align="center">
787
+ <i>Lego models courtesy of the <a href="https://omr.ldraw.org/">LDraw Official Model Repository</a></i>
788
+ </p>
789
+
790
+ ![](https://user-images.githubusercontent.com/734200/161877196-7ae2769e-7e54-4694-9ca8-e8f5219d1c2d.png)
791
+
792
+ <p align="center">
793
+ <i>Octopus Tea model by <a href="https://sketchfab.com/3d-models/cartoon-octopus-takes-a-tea-bath-107260cf0fd24202a67eb037a6c760a5
794
+ ">AzTiZ</a></i>
795
+ </p>
796
+
797
+ ![](https://user-images.githubusercontent.com/734200/173212652-de6a83e5-dd2c-49b5-8ed7-484ff8969b5b.png)
798
+ <p align="center">
799
+ <i>Botanists Study model by <a href="https://sketchfab.com/3d-models/the-botanists-study-8b7b5743b1c848ed8ea58f5518c44e7e">riikkakilpelainen</a></i>
800
+ </p>
801
+
802
+ ![](https://user-images.githubusercontent.com/734200/173170459-849b9343-efe3-4635-8719-346511472965.png)
803
+ <p align="center">
804
+ <i>Japanese Bridge Garden model by <a href="https://sketchfab.com/3d-models/japanese-bridge-garden-d122e17593eb4012913cde927486d15a">kristenlee</a></i>
805
+ </p>
806
+
807
+ ### Resources
808
+
809
+ [Raytracing in One Weekend Book](https://raytracing.github.io/)
810
+
811
+ [PBR Book](https://pbr-book.org/)
812
+
813
+ [knightcrawler25/GLSL-PathTracer](https://github.com/knightcrawler25/GLSL-PathTracer/)
814
+
815
+