three-gpu-pathtracer 0.0.5 → 0.0.6

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 (42) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +781 -728
  3. package/build/index.module.js +5223 -3956
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +5226 -3954
  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 +255 -255
  11. package/src/core/PathTracingSceneGenerator.js +68 -68
  12. package/src/index.js +33 -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/LambertPathTracingMaterial.js +285 -285
  17. package/src/materials/MaterialBase.js +56 -56
  18. package/src/materials/PhysicalPathTracingMaterial.js +922 -848
  19. package/src/{core → objects}/EquirectCamera.js +13 -13
  20. package/src/{core → objects}/PhysicalCamera.js +28 -28
  21. package/src/objects/PhysicalSpotLight.js +14 -0
  22. package/src/objects/ShapedAreaLight.js +12 -0
  23. package/src/shader/shaderEnvMapSampling.js +59 -59
  24. package/src/shader/shaderGGXFunctions.js +108 -108
  25. package/src/shader/shaderIridescenceFunctions.js +130 -0
  26. package/src/shader/shaderLightSampling.js +231 -87
  27. package/src/shader/shaderMaterialSampling.js +546 -501
  28. package/src/shader/shaderSheenFunctions.js +98 -0
  29. package/src/shader/shaderStructs.js +307 -191
  30. package/src/shader/shaderUtils.js +350 -287
  31. package/src/uniforms/EquirectHdrInfoUniform.js +259 -263
  32. package/src/uniforms/IESProfilesTexture.js +100 -0
  33. package/src/uniforms/LightsInfoUniformStruct.js +162 -0
  34. package/src/uniforms/MaterialsTexture.js +406 -319
  35. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  36. package/src/uniforms/RenderTarget2DArray.js +93 -93
  37. package/src/utils/BlurredEnvMapGenerator.js +113 -113
  38. package/src/utils/GeometryPreparationUtils.js +194 -194
  39. package/src/utils/IESLoader.js +325 -0
  40. package/src/utils/UVUnwrapper.js +101 -101
  41. package/src/workers/PathTracingSceneWorker.js +42 -41
  42. package/src/uniforms/LightsTexture.js +0 -83
package/README.md CHANGED
@@ -1,728 +1,781 @@
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
+ ### .alpha
249
+
250
+ ```js
251
+ alpha = false : Boolean
252
+ ```
253
+
254
+ 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.
255
+
256
+ ### constructor
257
+
258
+ ```js
259
+ constructor( renderer : WebGLRenderer )
260
+ ```
261
+
262
+ ### .setSize
263
+
264
+ ```js
265
+ setSize( size : Vector2 ) : void
266
+ ```
267
+
268
+ Sets the size of the target to render to.
269
+
270
+ ### .update
271
+
272
+ ```js
273
+ update()
274
+ ```
275
+
276
+ Renders a single sample to the target.
277
+
278
+ ### .reset
279
+
280
+ ```js
281
+ reset() : void
282
+ ```
283
+
284
+ Resets and restarts the render from scratch.
285
+
286
+ ## PathTracingSceneGenerator
287
+
288
+ Utility class for generating the set of data required for initializing the path tracing material with a bvh, geometry, materials, and textures.
289
+
290
+ ### .generate
291
+
292
+ ```js
293
+ generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
294
+ bvh : MeshBVH,
295
+ materials : Array<Material>,
296
+ textures : Array<Texture>,
297
+ lights : Array<Light>
298
+ }
299
+ ```
300
+
301
+ 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.
302
+
303
+ ## PathTracingSceneWorker
304
+
305
+ _extends PathTracingSceneGenerator_
306
+
307
+ See note in [Asyncronous Generation](#asynchronous-generation) use snippet.
308
+
309
+ ### .generate
310
+
311
+ ```js
312
+ async generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
313
+ bvh : MeshBVH,
314
+ materials : Array<Material>,
315
+ textures : Array<Texture>,
316
+ lights : Array<Light>
317
+ }
318
+ ```
319
+
320
+ ### .dispose
321
+
322
+ ```js
323
+ dispose() : void
324
+ ```
325
+
326
+ ## PhysicalCamera
327
+
328
+ _extends THREE.PerspectiveCamera_
329
+
330
+ 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.
331
+
332
+ ### .focusDistance
333
+
334
+ ```js
335
+ focusDistance = 25 : Number
336
+ ```
337
+
338
+ The distance from the camera in meters that everything is is perfect focus.
339
+
340
+ ### .fStop
341
+
342
+ ```js
343
+ fStop = 1.4 : Number
344
+ ```
345
+
346
+ The fstop value of the camera. If this is changed then the `bokehSize` field is implicitly updated.
347
+
348
+ ### .bokehSize
349
+
350
+ ```js
351
+ bokehSize : Number
352
+ ```
353
+
354
+ The bokeh size as derived from the fStop and focal length in millimeters. If this is set then the fStop is implicitly updated.
355
+
356
+ ### .apertureBlades
357
+
358
+ ```js
359
+ apertureBlades = 0 : Number
360
+ ```
361
+
362
+ The number of sides / blades on the aperture.
363
+
364
+ ### .apertureRotation
365
+
366
+ ```js
367
+ apertureRotation = 0 : Number
368
+ ```
369
+
370
+ The rotation of the aperture shape in radians.
371
+
372
+ ### .anamorphicRatio
373
+
374
+ ```js
375
+ anamorphicRatio = 1 : Number
376
+ ```
377
+
378
+ The anamorphic ratio of the lens. A higher value will stretch the bokeh effect horizontally.
379
+
380
+ ## EquirectCamera
381
+
382
+ _extends THREE.Camera_
383
+
384
+ A class indicating that the path tracer should render an equirectangular view. Does not work with three.js raster rendering.
385
+
386
+ ## PhysicalSpotLight
387
+
388
+ _extends THREE.SpotLight_
389
+
390
+ ### .radius
391
+
392
+ ```js
393
+ radius = 0 : Number
394
+ ```
395
+
396
+ The radius of the spotlight surface. Increase this value to add softness to shadows.
397
+
398
+ ### .iesTexture
399
+
400
+ ```js
401
+ iesTexture = null : Texture
402
+ ```
403
+
404
+ The loaded IES texture describing directional light intensity. These can be loaded with the `IESLoader`.
405
+
406
+ Premade IES profiles can be downloaded from [ieslibrary.com]. And custom profiles can be generated using [CNDL](https://cndl.io/).
407
+
408
+ ## ShapedAreaLight
409
+
410
+ _extends THREE.RectAreaLight_
411
+
412
+ ### .isCircular
413
+
414
+ ```js
415
+ isCircular = false : Boolean
416
+ ```
417
+
418
+ Whether the area light should be rendered as a circle or a rectangle.
419
+
420
+ ## DynamicPathTracingSceneGenerator
421
+
422
+ 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.
423
+
424
+ 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.
425
+
426
+ If geometry or materials are added or removed from the scene then `reset` must be called.
427
+
428
+ ### constructor
429
+
430
+ ```js
431
+ constructor( scene : Object3D | Array<Object3D> )
432
+ ```
433
+
434
+ Takes the scene to convert.
435
+
436
+ ### .generate
437
+
438
+ ```js
439
+ generate() : {
440
+ bvh : MeshBVH,
441
+ materials : Array<Material>,
442
+ textures : Array<Texture>
443
+ }
444
+ ```
445
+
446
+ 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.
447
+
448
+ ### .reset
449
+
450
+ ```js
451
+ reset() : void
452
+ ```
453
+
454
+ 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.
455
+
456
+ ## IESLoader
457
+
458
+ _extends Loader_
459
+
460
+ Loader for loading and parsing IES profile data. Load and parse functions return a `DataTexture` with the profile contents.
461
+
462
+ ## BlurredEnvMapGenerator
463
+
464
+ Utility for generating a PMREM blurred environment map that can be used with the path tracer.
465
+
466
+ ### constructor
467
+
468
+ ```js
469
+ constructor( renderer : WebGLRenderer )
470
+ ```
471
+
472
+ ### .generate
473
+
474
+ ```js
475
+ generate( texture : Texture, blur : Number ) : DataTexture
476
+ ```
477
+
478
+ 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.
479
+
480
+ ### .dispose
481
+
482
+ ```js
483
+ dispose() : void
484
+ ```
485
+
486
+ Disposes of the temporary files and textures for generation.
487
+
488
+ ## MaterialBase
489
+
490
+ _extends THREE.ShaderMaterial_
491
+
492
+ Convenience base class that adds additional functions and implicitly adds object definitions for all uniforms of the shader to the object.
493
+
494
+ ### .setDefine
495
+
496
+ ```js
497
+ setDefine( name : string, value = undefined : any ) : void
498
+ ```
499
+
500
+ 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`.
501
+
502
+ ## PhysicalPathTracingMaterial
503
+
504
+ _extends MaterialBase_
505
+
506
+ **Uniforms**
507
+
508
+ ```js
509
+ {
510
+ // The number of ray bounces to test. Higher is better quality but slower performance.
511
+ bounces = 3 : Number,
512
+
513
+ // The physical camera parameters to use
514
+ physicalCamera : PhysicalCameraUniform,
515
+
516
+ // Geometry and BVH information
517
+ bvh: MeshBVHUniformStruct,
518
+ normalAttribute: FloatVertexAttributeTexture,
519
+ tangentAttribute: FloatVertexAttributeTexture,
520
+ uvAttribute: FloatVertexAttributeTexture,
521
+ materialIndexAttribute: UIntVertexAttributeTexture,
522
+ materials: MaterialsTexture,
523
+ textures: RenderTarget2DArray,
524
+
525
+ // Light information
526
+ lights: LightsInfoUniformStruct,
527
+ iesProfiles: IESProfilesTexture,
528
+
529
+ // Environment Map information
530
+ envMapInfo: EquirectHdrInfoUniform,
531
+ environmentRotation: Matrix3,
532
+ environmentIntensity = 1: Number,
533
+
534
+ // background blur
535
+ backgroundBlur = 0: Number,
536
+
537
+ // Factor for alleviating bright pixels from rays that hit diffuse surfaces then
538
+ // specular surfaces. Setting this higher alleviates fireflies but will remove some
539
+ // specular caustics.
540
+ filterGlossyFactor = 0: Number,
541
+
542
+ // The colors to use for the gradient background when enabled.
543
+ bgGradientTop: Color,
544
+ bgGradientBottom: Color,
545
+
546
+ // The transparency to render the background with. Note that the "alpha" option
547
+ // must be set to true on PathTracingRenderer for this field to work properly.
548
+ backgroundAlpha: 1.0,
549
+ }
550
+ ```
551
+
552
+ **Defines**
553
+
554
+ ```js
555
+ {
556
+
557
+ // Whether to use multiple importance sampling to help the image converge more quickly
558
+ FEATURE_MIS = 1 : Number,
559
+
560
+ // Whether to use the "bg" gradient fields to sample for the background
561
+ FEATURE_GRADIENT_BG = 0 : Number
562
+
563
+ // The number of transparent pixels to allow on top of existing bounces for object transparency.
564
+ TRANSPARENT_TRAVERSALS = 5 : Number,
565
+
566
+ }
567
+ ```
568
+
569
+ ## RenderTarget2DArray
570
+
571
+ _extends WebGLArrayRenderTarget_
572
+
573
+ A convenience extension from `WebGLArrayRenderTarget` that affords easily creating a uniform texture array from an array of textures.
574
+
575
+ ### .setTextures
576
+
577
+ ```js
578
+ setTextures(
579
+ renderer : WebGLRenderer,
580
+ width : Number,
581
+ height : Number,
582
+ textures : Array<Texture>
583
+ ) : void
584
+ ```
585
+
586
+ 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.
587
+
588
+ ## PhysicalCameraUniform
589
+
590
+ Uniform for storing the camera parameters for use with the shader.
591
+
592
+ ### .updateFrom
593
+
594
+ ```js
595
+ updateFrom( camera : PerspectiveCamera | PhysicalCamera ) : void
596
+ ```
597
+
598
+ Copies all fields from the passed PhysicalCamera if available otherwise the defaults are used.
599
+
600
+ ## MaterialsTexture
601
+
602
+ _extends DataTexture_
603
+
604
+ Helper texture uniform for encoding materials as texture data.
605
+
606
+ ### .threeCompatibilityTransforms
607
+
608
+ ```js
609
+ threeCompatibilityTransforms = false : Boolean
610
+ ```
611
+
612
+ 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.
613
+
614
+ See fallback order documentation [here](https://threejs.org/docs/#api/en/textures/Texture.offset).
615
+
616
+ ### .setSide
617
+
618
+ ```js
619
+ setSide( index : Number, side : FrontSide | BackSide | DoubleSide ) : void
620
+ ```
621
+
622
+ Sets the side to render for the given material.
623
+
624
+ ### .setMatte
625
+
626
+ ```js
627
+ setMatte( index : Number, matte : Boolean ) : void
628
+ ```
629
+
630
+ 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.
631
+
632
+ ### .setCastShadow
633
+
634
+ ```js
635
+ setCastShadow( index : Number, enabled : Boolean ) : void
636
+ ```
637
+
638
+ 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.
639
+
640
+ ### .updateFrom
641
+
642
+ ```js
643
+ updateFrom( materials : Array<Material>, textures : Array<Texture> ) : void
644
+ ```
645
+
646
+ Updates the size and values of the texture to align with the provided set of materials and textures.
647
+
648
+ The "matte" and "side" values must be updated explicitly.
649
+
650
+ ## LightsInfoUniformStruct
651
+
652
+ Helper uniform for encoding lights as texture data with count.
653
+
654
+ ### .updateFrom
655
+
656
+ ```js
657
+ updateFrom( lights : Array<Light>, iesTextures = [] : Array<Texture> ) : void
658
+ ```
659
+
660
+ Updates the size and values of the texture to align with the provided set of lights and IES textures.
661
+
662
+ ## EquirectHdrInfoUniform
663
+
664
+ Stores the environment map contents along with the intensity distribution information to support multiple importance sampling.
665
+
666
+ ### .updateFrom
667
+
668
+ ```js
669
+ updateFrom( environmentMap : Texture ) : void
670
+ ```
671
+
672
+ 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.
673
+
674
+ ## Functions
675
+
676
+ ### mergeMeshes
677
+
678
+ ```js
679
+ mergeMeshes( meshes : Array<Mesh> ) : {
680
+ materials : Array<Material>,
681
+ textures : Array<Textures>,
682
+ geometry : BufferGeometry
683
+ }
684
+ ```
685
+
686
+ 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.
687
+
688
+ ## Shader Chunks
689
+
690
+ **shaderMaterialSampling**
691
+
692
+ 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.
693
+
694
+ **shaderLightSampling**
695
+
696
+ 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.
697
+
698
+ **shaderStructs**
699
+
700
+ 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.
701
+
702
+ **shaderUtils**
703
+
704
+ 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.
705
+
706
+ # Gotchas
707
+
708
+ - The project requires use of WebGL2.
709
+ - All textures must use the same wrap and interpolation flags.
710
+ - Spotlights are not supported in non-MIS rendering currently.
711
+
712
+ # Screenshots
713
+
714
+ ![](https://user-images.githubusercontent.com/734200/162584469-68e6df38-92da-4a13-b352-ca0bdea14548.png)
715
+
716
+ <p align="center">
717
+ <i>Sample materials</i>
718
+ </p>
719
+
720
+ ![](https://user-images.githubusercontent.com/734200/163835927-be75d2c0-f27b-4e4b-a3eb-2371043fa5e1.png)
721
+
722
+ ![](https://user-images.githubusercontent.com/734200/163839431-ed75e64d-9ae4-4423-afca-55162a44873e.png)
723
+
724
+ <p align="center">
725
+ <i>"SD Macross City Standoff Diorama" scene by <a href="https://sketchfab.com/3d-models/sd-macross-city-standoff-diorama-b154220f7e7441799d6be2f7ff9658c7">tipatat</a></i>
726
+ </p>
727
+
728
+ ![](./docs/interior-scene-cropped.png)
729
+
730
+ <p align="center">
731
+ <i>"Interior Scene" model by <a href="https://sketchfab.com/3d-models/interior-scene-45ddbbc4c2dc4f8ca9ed99da9a78326a">Allay Design</a></i>
732
+ </p>
733
+
734
+ ![](https://user-images.githubusercontent.com/734200/161820794-df0da371-ee5c-4368-9e7b-5e7daf6cf3c7.png)
735
+
736
+ ![](https://user-images.githubusercontent.com/734200/162550315-3cdabf40-3dea-4d7d-bcfc-eb543eea2d93.png)
737
+
738
+ <p align="center">
739
+ <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>
740
+ </p>
741
+
742
+ ![](https://user-images.githubusercontent.com/734200/161877900-566652e4-c799-4940-bccb-0c8f4cea5387.png)
743
+
744
+ <p align="center">
745
+ <i>Gelatinous Cube model by <a href="https://sketchfab.com/3d-models/gelatinous-cube-e08385238f4d4b59b012233a9fbdca21">glenatron</a></i>
746
+ </p>
747
+
748
+ ![](https://user-images.githubusercontent.com/734200/161822206-c27bf594-d648-4735-868e-4baf4e414802.png)
749
+
750
+ ![](https://user-images.githubusercontent.com/734200/161822214-eace4297-03c4-4adc-b472-efe29a862685.png)
751
+
752
+ <p align="center">
753
+ <i>Lego models courtesy of the <a href="https://omr.ldraw.org/">LDraw Official Model Repository</a></i>
754
+ </p>
755
+
756
+ ![](https://user-images.githubusercontent.com/734200/161877196-7ae2769e-7e54-4694-9ca8-e8f5219d1c2d.png)
757
+
758
+ <p align="center">
759
+ <i>Octopus Tea model by <a href="https://sketchfab.com/3d-models/cartoon-octopus-takes-a-tea-bath-107260cf0fd24202a67eb037a6c760a5
760
+ ">AzTiZ</a></i>
761
+ </p>
762
+
763
+ ![](https://user-images.githubusercontent.com/734200/173212652-de6a83e5-dd2c-49b5-8ed7-484ff8969b5b.png)
764
+ <p align="center">
765
+ <i>Botanists Study model by <a href="https://sketchfab.com/3d-models/the-botanists-study-8b7b5743b1c848ed8ea58f5518c44e7e">riikkakilpelainen</a></i>
766
+ </p>
767
+
768
+ ![](https://user-images.githubusercontent.com/734200/173170459-849b9343-efe3-4635-8719-346511472965.png)
769
+ <p align="center">
770
+ <i>Japanese Bridge Garden model by <a href="https://sketchfab.com/3d-models/japanese-bridge-garden-d122e17593eb4012913cde927486d15a">kristenlee</a></i>
771
+ </p>
772
+
773
+ ### Resources
774
+
775
+ [Raytracing in One Weekend Book](https://raytracing.github.io/)
776
+
777
+ [PBR Book](https://pbr-book.org/)
778
+
779
+ [knightcrawler25/GLSL-PathTracer](https://github.com/knightcrawler25/GLSL-PathTracer/)
780
+
781
+