three-gpu-pathtracer 0.0.3 → 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 (32) hide show
  1. package/README.md +136 -15
  2. package/build/index.module.js +2706 -529
  3. package/build/index.module.js.map +1 -1
  4. package/build/index.umd.cjs +2713 -529
  5. package/build/index.umd.cjs.map +1 -1
  6. package/package.json +68 -60
  7. package/src/core/DynamicPathTracingSceneGenerator.js +24 -11
  8. package/src/core/PathTracingRenderer.js +22 -0
  9. package/src/core/PathTracingSceneGenerator.js +36 -20
  10. package/src/index.js +9 -1
  11. package/src/materials/PhysicalPathTracingMaterial.js +350 -60
  12. package/src/objects/EquirectCamera.js +13 -0
  13. package/src/{core → objects}/PhysicalCamera.js +0 -0
  14. package/src/objects/PhysicalSpotLight.js +14 -0
  15. package/src/objects/ShapedAreaLight.js +12 -0
  16. package/src/shader/shaderEnvMapSampling.js +59 -67
  17. package/src/shader/shaderGGXFunctions.js +3 -2
  18. package/src/shader/shaderIridescenceFunctions.js +130 -0
  19. package/src/shader/shaderLightSampling.js +231 -0
  20. package/src/shader/shaderMaterialSampling.js +259 -53
  21. package/src/shader/shaderSheenFunctions.js +98 -0
  22. package/src/shader/shaderStructs.js +307 -92
  23. package/src/shader/shaderUtils.js +122 -0
  24. package/src/uniforms/EquirectHdrInfoUniform.js +10 -14
  25. package/src/uniforms/IESProfilesTexture.js +100 -0
  26. package/src/uniforms/LightsInfoUniformStruct.js +162 -0
  27. package/src/uniforms/MaterialsTexture.js +266 -33
  28. package/src/uniforms/PhysicalCameraUniform.js +1 -1
  29. package/src/uniforms/RenderTarget2DArray.js +93 -80
  30. package/src/utils/GeometryPreparationUtils.js +1 -1
  31. package/src/utils/IESLoader.js +325 -0
  32. package/src/workers/PathTracingSceneWorker.js +3 -1
package/README.md CHANGED
@@ -31,16 +31,35 @@ _More features and capabilities in progress!_
31
31
 
32
32
  [Morph Target Support](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/skinnedMesh.html#morphtarget)
33
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
+
34
38
  **Test Scenes**
35
39
 
36
40
  [Material Test Orb](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html)
37
41
 
38
42
  [Transmission Preset Orb](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html#transmission)
39
43
 
44
+ [Model Viewer Fidelity Scene Comparisons](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/viewerTest.html#khronos-DragonAttenuation)
45
+
40
46
  **Light Baking**
41
47
 
42
48
  [Ambient Occlusion Material](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/aoRender.html)
43
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
+
44
63
  # Use
45
64
 
46
65
  **Basic Renderer**
@@ -56,6 +75,9 @@ import {
56
75
 
57
76
  // init scene, renderer, camera, controls, etc
58
77
 
78
+ renderer.outputEncoding = THREE.sRGBEncoding;
79
+ renderer.toneMapping = THREE.ACESFilmicToneMapping;
80
+
59
81
  // initialize the path tracing material and renderer
60
82
  const ptMaterial = new PhysicalPathTracingMaterial();
61
83
  const ptRenderer = new PathTracingRenderer( renderer );
@@ -69,7 +91,7 @@ ptRenderer.alpha = true;
69
91
  // init quad for rendering to the canvas
70
92
  const fsQuad = new FullScreenQuad( new THREE.MeshBasicMaterial( {
71
93
  map: ptRenderer.target.texture,
72
-
94
+
73
95
  // if rendering transparent background
74
96
  blending: THREE.CustomBlending,
75
97
  } ) );
@@ -79,7 +101,7 @@ scene.updateMatrixWorld();
79
101
 
80
102
  // initialize the scene and update the material properties with the bvh, materials, etc
81
103
  const generator = new PathTracingSceneGenerator();
82
- const { bvh, textures, materials } = generator.generate( scene );
104
+ const { bvh, textures, materials, lights } = generator.generate( scene );
83
105
 
84
106
  // update bvh and geometry attribute textures
85
107
  ptMaterial.bvh.updateFrom( bvh );
@@ -92,6 +114,9 @@ ptMaterial.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex
92
114
  ptMaterial.textures.setTextures( renderer, 2048, 2048, textures );
93
115
  ptMaterial.materials.updateFrom( materials, textures );
94
116
 
117
+ // update the lights
118
+ ptMaterial.lights.updateFrom( lights );
119
+
95
120
  // set the environment map
96
121
  const texture = await new RGBELoader().loadAsync( envMapUrl );
97
122
  ptRenderer.material.envMapInfo.updateFrom( texture );
@@ -108,11 +133,12 @@ function animate() {
108
133
  camera.updateMatrixWorld();
109
134
  ptRenderer.update();
110
135
 
111
- // copy the current state of the path tracer to canvas to display
112
- renderer.autoClear = false;
136
+ // if using alpha = true then the target texture will change every frame
137
+ // so we must retrieve it before render.
113
138
  fsQuad.material.map = ptRenderer.target.texture;
139
+
140
+ // copy the current state of the path tracer to canvas to display
114
141
  fsQuad.render( renderer );
115
- renderer.autoClear = true;
116
142
 
117
143
  }
118
144
  ```
@@ -160,7 +186,7 @@ import { PathTracingSceneWorker } from 'three-gpu-pathtracer/src/workers/PathTra
160
186
 
161
187
  // initialize the scene and update the material properties with the bvh, materials, etc
162
188
  const generator = new PathTracingSceneWorker();
163
- const { bvh, textures, materials } = await generator.generate( scene );
189
+ const { bvh, textures, materials, lights } = await generator.generate( scene );
164
190
 
165
191
  // ...
166
192
  ```
@@ -264,10 +290,11 @@ Utility class for generating the set of data required for initializing the path
264
290
  ### .generate
265
291
 
266
292
  ```js
267
- generate( scene : Object3D, options = {} : Object ) : {
293
+ generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
268
294
  bvh : MeshBVH,
269
295
  materials : Array<Material>,
270
- textures : Array<Texture>
296
+ textures : Array<Texture>,
297
+ lights : Array<Light>
271
298
  }
272
299
  ```
273
300
 
@@ -282,10 +309,11 @@ See note in [Asyncronous Generation](#asynchronous-generation) use snippet.
282
309
  ### .generate
283
310
 
284
311
  ```js
285
- async generate( scene : Object3D, options = {} : Object ) : {
312
+ async generate( scene : Object3D | Array<Object3D>, options = {} : Object ) : {
286
313
  bvh : MeshBVH,
287
314
  materials : Array<Material>,
288
- textures : Array<Texture>
315
+ textures : Array<Texture>,
316
+ lights : Array<Light>
289
317
  }
290
318
  ```
291
319
 
@@ -349,6 +377,46 @@ anamorphicRatio = 1 : Number
349
377
 
350
378
  The anamorphic ratio of the lens. A higher value will stretch the bokeh effect horizontally.
351
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
+
352
420
  ## DynamicPathTracingSceneGenerator
353
421
 
354
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.
@@ -360,7 +428,7 @@ If geometry or materials are added or removed from the scene then `reset` must b
360
428
  ### constructor
361
429
 
362
430
  ```js
363
- constructor( scene : Object3D )
431
+ constructor( scene : Object3D | Array<Object3D> )
364
432
  ```
365
433
 
366
434
  Takes the scene to convert.
@@ -385,6 +453,12 @@ reset() : void
385
453
 
386
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.
387
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
+
388
462
  ## BlurredEnvMapGenerator
389
463
 
390
464
  Utility for generating a PMREM blurred environment map that can be used with the path tracer.
@@ -448,6 +522,10 @@ _extends MaterialBase_
448
522
  materials: MaterialsTexture,
449
523
  textures: RenderTarget2DArray,
450
524
 
525
+ // Light information
526
+ lights: LightsInfoUniformStruct,
527
+ iesProfiles: IESProfilesTexture,
528
+
451
529
  // Environment Map information
452
530
  envMapInfo: EquirectHdrInfoUniform,
453
531
  environmentRotation: Matrix3,
@@ -485,7 +563,6 @@ _extends MaterialBase_
485
563
  // The number of transparent pixels to allow on top of existing bounces for object transparency.
486
564
  TRANSPARENT_TRAVERSALS = 5 : Number,
487
565
 
488
-
489
566
  }
490
567
  ```
491
568
 
@@ -526,6 +603,16 @@ _extends DataTexture_
526
603
 
527
604
  Helper texture uniform for encoding materials as texture data.
528
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
+
529
616
  ### .setSide
530
617
 
531
618
  ```js
@@ -542,6 +629,14 @@ setMatte( index : Number, matte : Boolean ) : void
542
629
 
543
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.
544
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
+
545
640
  ### .updateFrom
546
641
 
547
642
  ```js
@@ -552,6 +647,18 @@ Updates the size and values of the texture to align with the provided set of mat
552
647
 
553
648
  The "matte" and "side" values must be updated explicitly.
554
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
+
555
662
  ## EquirectHdrInfoUniform
556
663
 
557
664
  Stores the environment map contents along with the intensity distribution information to support multiple importance sampling.
@@ -584,19 +691,23 @@ Merges the set of meshes into a single geometry with a `materialIndex` vertex at
584
691
 
585
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.
586
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
+
587
698
  **shaderStructs**
588
699
 
589
- Material 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.
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.
590
701
 
591
702
  **shaderUtils**
592
703
 
593
- Set of randomness and other light transmport 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.
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.
594
705
 
595
706
  # Gotchas
596
707
 
597
708
  - The project requires use of WebGL2.
598
709
  - All textures must use the same wrap and interpolation flags.
599
- - Texture repeat, rotation, and center properties are not supported.
710
+ - Spotlights are not supported in non-MIS rendering currently.
600
711
 
601
712
  # Screenshots
602
713
 
@@ -649,6 +760,16 @@ Set of randomness and other light transmport utilities for use in a shader. See
649
760
  ">AzTiZ</a></i>
650
761
  </p>
651
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
+
652
773
  ### Resources
653
774
 
654
775
  [Raytracing in One Weekend Book](https://raytracing.github.io/)