three-gpu-pathtracer 0.0.1

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Garrett Johnson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,386 @@
1
+ # three-gpu-pathtracer
2
+
3
+ [![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/)
4
+ [![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)
5
+
6
+ ![](https://user-images.githubusercontent.com/734200/162287477-96696b18-890b-4c1b-8a73-d662e577cc48.png)
7
+
8
+ Path tracing project using [three-mesh-bvh](https://github.com/gkjohnson/three-mesh-bvh) 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!
9
+
10
+ _More features and capabilities in progress!_
11
+
12
+ # Examples
13
+
14
+ [PBR demo here](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/index.html)!
15
+
16
+ [Lego demo here](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/lego.html)!
17
+
18
+ [Material demo here](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html)!
19
+
20
+ [Transmission preset demo here](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/materialBall.html#transmission)!
21
+
22
+ [Ambient Occlusion Material demo here](https://gkjohnson.github.io/three-gpu-pathtracer/example/bundle/aoRender.html)!
23
+
24
+ # Use
25
+
26
+ ```js
27
+ import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
28
+ import {
29
+ PathTracingSceneGenerator,
30
+ PathTracingRenderer,
31
+ PhysicalPathTracingMaterial,
32
+ } from 'three-gpu-pathtracer';
33
+
34
+ // init scene, renderer, camera, controls, etc
35
+
36
+ // initialize the path tracing material and renderer
37
+ const ptMaterial = new PhysicalPathTracingMaterial();
38
+ const ptRenderer = new PathTracingRenderer( renderer );
39
+ ptRenderer.camera = camera;
40
+ ptRenderer.material = ptMaterial;
41
+
42
+ // init quad for rendering to the canvas
43
+ const fsQuad = new FullScreenQuad( new THREE.MeshBasicMaterial( {
44
+ map: ptRenderer.target.texture,
45
+ } ) );
46
+
47
+ // initialize the scene and update the material properties with the bvh, materials, etc
48
+ const generator = new PathTracingSceneGenerator();
49
+ const { bvh, textures, materials } = await generator.generate( scene );
50
+
51
+ // update bvh and geometry attribute textures
52
+ ptMaterial.bvh.updateFrom( bvh );
53
+ ptMaterial.normalAttribute.updateFrom( geometry.attributes.normal );
54
+ ptMaterial.tangentAttribute.updateFrom( geometry.attributes.tangent );
55
+ ptMaterial.uvAttribute.updateFrom( geometry.attributes.uv );
56
+
57
+ // update materials and texture arrays
58
+ ptMaterial.materialIndexAttribute.updateFrom( geometry.attributes.materialIndex );
59
+ ptMaterial.textures.setTextures( renderer, 2048, 2048, textures );
60
+ ptMaterial.materials.updateFrom( materials, textures );
61
+ ptMaterial.setDefine( 'MATERIAL_LENGTH', materials.length );
62
+
63
+ // set the environment map
64
+ const texture = await new RGBELoader().loadAsync( envMapUrl );
65
+ const pmremGenerator = new THREE.PMREMGenerator( renderer );
66
+ pmremGenerator.compileCubemapShader();
67
+
68
+ const envMap = pmremGenerator.fromEquirectangular( texture );
69
+ ptRenderer.material.environmentMap = envMap.texture;
70
+
71
+ animate();
72
+
73
+ // ...
74
+
75
+ function animate() {
76
+
77
+ // if the camera position changes call "ptRenderer.reset()"
78
+
79
+ // update the camera and render one sample
80
+ camera.updateMatrixWorld();
81
+ ptRenderer.update();
82
+
83
+ // copy the current state of the path tracer to canvas to display
84
+ renderer.autoClear = false;
85
+ fsQuad.material.map = ptRenderer.target.texture;
86
+ fsQuad.render( renderer );
87
+ renderer.autoClear = true;
88
+
89
+ }
90
+ ```
91
+
92
+ # Exports
93
+
94
+ ## PathTracingRenderer
95
+
96
+ Utility class for tracking and rendering a path traced scene to a render target.
97
+
98
+ ### .samples
99
+
100
+ ```js
101
+ readonly samples : Number
102
+ ```
103
+
104
+ Number of samples per pixel that have been rendered to the target.
105
+
106
+ ### .target
107
+
108
+ ```js
109
+ readonly target : WebGLRenderTarget
110
+ ```
111
+
112
+ The target being rendered to. The size of the target is updated with `setSize` and is initialized to a FloatType texture.
113
+
114
+ ### .camera
115
+
116
+ ```js
117
+ camera = null : Camera
118
+ ```
119
+
120
+ The camera to render with. The view offset of the camera will be updated every sample to enable anti aliasing.
121
+
122
+ ### .material
123
+
124
+ ```js
125
+ material = null : ShaderMaterial
126
+ ```
127
+
128
+ 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`.
129
+
130
+ ### .tiles
131
+
132
+ ```js
133
+ tiles = ( 1, 1 ) : Vector2
134
+ ```
135
+
136
+ 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.
137
+
138
+ ### .stableNoise
139
+
140
+ ```js
141
+ stableNoise = false
142
+ ```
143
+
144
+ 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.
145
+
146
+ ### constructor
147
+
148
+ ```js
149
+ constructor( renderer : WebGLRenderer )
150
+ ```
151
+
152
+ ### .setSize
153
+
154
+ ```js
155
+ setSize( size : Vector2 ) : void
156
+ ```
157
+
158
+ Sets the size of the target to render to.
159
+
160
+ ### .update
161
+
162
+ ```js
163
+ update()
164
+ ```
165
+
166
+ Renders a single sample to the target.
167
+
168
+ ### .reset
169
+
170
+ ```js
171
+ reset() : void
172
+ ```
173
+
174
+ Resets and restarts the render from scratch.
175
+
176
+ ## PathTracingSceneGenerator
177
+
178
+ Utility class for generating the set of data required for initializing the path tracing material with a bvh, geometry, materials, and textures.
179
+
180
+ ### .generate
181
+
182
+ ```js
183
+ async generate( scene : Object3D ) : {
184
+ bvh : MeshBVH,
185
+ materials : Array<Material>,
186
+ textures : Array<Texture>
187
+ }
188
+ ```
189
+
190
+ 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.
191
+
192
+ ## MaterialBase
193
+
194
+ _extends THREE.ShaderMaterial_
195
+
196
+ Convenience base class that adds additional functions and implicitly adds object definitions for all uniforms of the shader to the object.
197
+
198
+ ### .setDefine
199
+
200
+ ```js
201
+ setDefine( name : string, value = undefined : any ) : void
202
+ ```
203
+
204
+ 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`.
205
+
206
+ ## PhysicalPathTracingMaterial
207
+
208
+ _extends MaterialBase_
209
+
210
+ **Uniforms**
211
+
212
+ ```js
213
+ {
214
+ // Geometry and BVH information,
215
+ bvh: MeshBVHUniformStruct,
216
+ normalAttribute: FloatVertexAttributeTexture,
217
+ tangentAttribute: FloatVertexAttributeTexture,
218
+ uvAttribute: FloatVertexAttributeTexture,
219
+ materialIndexAttribute: UIntVertexAttributeTexture,
220
+ materials: MaterialStructArrayUniform,
221
+ textures: RenderTarget2DArray,
222
+
223
+ // PMREM-processed Environment Map,
224
+ environmentMap: Texture,
225
+ environmentRotaton: Matrix3,
226
+
227
+ // Environment Map information,
228
+ environmentBlur: Number,
229
+ environmentIntensity: Number,
230
+
231
+ // Factor for alleviating bright pixels from rays that hit diffuse surfaces then
232
+ // specular surfaces. Setting this higher alleviates fireflies but will remove some
233
+ // specular caustics.
234
+ filterGlossyFactor: Number,
235
+
236
+ // The colors to use for the gradient env lighting when no environment map is provided.
237
+ gradientTop: Color,
238
+ gradientBottom: Color,
239
+
240
+ // The colors to use for the gradient background when enabled.
241
+ bgGradientTop: Color,
242
+ bgGradientBottom: Color,
243
+ }
244
+ ```
245
+
246
+ **Defines**
247
+
248
+ ```js
249
+ {
250
+ // The number of ray bounces to test. Higher is better quality but slower performance.
251
+ BOUNCES = 3 : Number,
252
+
253
+ // The number of transparent pixels to allow on top of existing bounces for object transparency.
254
+ TRANSPARENT_TRAVERSALS = 5 : Number,
255
+
256
+ // Whether to use the "bg" gradient fields to sample for the backround
257
+ GRADIENT_BG = 0 : Number
258
+
259
+ // The number of materials provided in the "materials" uniform.
260
+ MATERIAL_LENGTH : Number,
261
+
262
+
263
+ }
264
+ ```
265
+
266
+ ## RenderTarget2DArray
267
+
268
+ _extends WebGLArrayRenderTarget_
269
+
270
+ A convenience extension from `WebGLArrayRenderTarget` that affords easily creating a uniform texture array from an array of textures.
271
+
272
+ ### .setTextures
273
+
274
+ ```js
275
+ setTextures(
276
+ renderer : WebGLRenderer,
277
+ width : Number,
278
+ height : Number,
279
+ textures : Array<Texture>
280
+ ) : void
281
+ ```
282
+
283
+ 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.
284
+
285
+ ## MaterialStructArrayUniform
286
+
287
+ _extends Array_
288
+
289
+ Array of `MaterialStructUniform` definitions for use as a Shader uniform.
290
+
291
+ ### .updateFrom
292
+
293
+ ```js
294
+ updateFrom( materials : Array<Material>, textures : Array<Texture> ) : void
295
+ ```
296
+
297
+ Updates the value of the uniform to align with the provided set of materials and textures.
298
+
299
+ ## MaterialStructUniform
300
+
301
+ Struct definiton for representing material information as a uniform. See the [implementation](https://github.com/gkjohnson/three-gpu-pathtracer/blob/main/src/shader/shaderStructs.js) for full struct definition information.
302
+
303
+ ### .updateFrom
304
+
305
+ ```js
306
+ updateFrame( material : Material, textures : Array<Texture> ) : void
307
+ ```
308
+
309
+ Updates the uniform with the information from the passed material. Texture fields are set to the index of the texture in the provided textures array.
310
+
311
+ ## Functions
312
+
313
+ ### mergeMeshes
314
+
315
+ ```js
316
+ mergeMeshes( meshes : Array<Mesh> ) : {
317
+ materials : Array<Material>,
318
+ textures : Array<Textures>,
319
+ geometry : BufferGeometry
320
+ }
321
+ ```
322
+
323
+ 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.
324
+
325
+ ## Shader Chunks
326
+
327
+ **shaderMaterialSampling**
328
+
329
+ 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.
330
+
331
+ **shaderStructs**
332
+
333
+ 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.
334
+
335
+ **shaderUtils**
336
+
337
+ 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.
338
+
339
+ # Caveats
340
+
341
+ - All textures must use the same wrap and interpolation flags.
342
+
343
+ # Screenshots
344
+
345
+ ![](./docs/interior-scene-cropped.png)
346
+
347
+ <p align="center">
348
+ <i>"Interior Scene" model by <a href="https://sketchfab.com/3d-models/interior-scene-45ddbbc4c2dc4f8ca9ed99da9a78326a">Allay Design</a></i>
349
+ </p>
350
+
351
+ ![](https://user-images.githubusercontent.com/734200/161820794-df0da371-ee5c-4368-9e7b-5e7daf6cf3c7.png)
352
+
353
+ <p align="center">
354
+ <i>Perseverance Rover model by <a href="https://mars.nasa.gov/resources/25042/mars-perseverance-rover-3d-model/">NASA / JPL-Caltech</a></i>
355
+ </p>
356
+
357
+ ![](https://user-images.githubusercontent.com/734200/161877900-566652e4-c799-4940-bccb-0c8f4cea5387.png)
358
+
359
+ <p align="center">
360
+ <i>Gelatinous Cube model by <a href="https://sketchfab.com/3d-models/gelatinous-cube-e08385238f4d4b59b012233a9fbdca21">glenatron</a></i>
361
+ </p>
362
+
363
+ ![](https://user-images.githubusercontent.com/734200/161822206-c27bf594-d648-4735-868e-4baf4e414802.png)
364
+
365
+ ![](https://user-images.githubusercontent.com/734200/161822214-eace4297-03c4-4adc-b472-efe29a862685.png)
366
+
367
+ <p align="center">
368
+ <i>Lego models courtesy of the <a href="https://omr.ldraw.org/">LDraw Official Model Repository</a></i>
369
+ </p>
370
+
371
+ ![](https://user-images.githubusercontent.com/734200/161877196-7ae2769e-7e54-4694-9ca8-e8f5219d1c2d.png)
372
+
373
+ <p align="center">
374
+ <i>Octopus Tea model by <a href="https://sketchfab.com/3d-models/cartoon-octopus-takes-a-tea-bath-107260cf0fd24202a67eb037a6c760a5
375
+ ">AzTiZ</a></i>
376
+ </p>
377
+
378
+ ### Resources
379
+
380
+ [Raytracing in One Weekend Book](https://raytracing.github.io/)
381
+
382
+ [PBR Book](https://pbr-book.org/)
383
+
384
+ [knightcrawler25/GLSL-PathTracer](https://github.com/knightcrawler25/GLSL-PathTracer/)
385
+
386
+