three-gpu-pathtracer 0.0.20 → 0.0.21

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 (75) hide show
  1. package/README.md +111 -464
  2. package/build/index.module.js +5691 -5312
  3. package/build/index.module.js.map +1 -1
  4. package/build/index.umd.cjs +5369 -5003
  5. package/build/index.umd.cjs.map +1 -1
  6. package/package.json +12 -6
  7. package/src/core/PathTracingRenderer.js +59 -46
  8. package/src/core/PathTracingSceneGenerator.js +245 -10
  9. package/src/core/WebGLPathTracer.js +472 -0
  10. package/src/core/utils/BakedGeometry.js +35 -0
  11. package/src/core/utils/BufferAttributeUtils.js +64 -0
  12. package/src/{utils → core/utils}/GeometryPreparationUtils.js +35 -35
  13. package/src/core/utils/MeshDiff.js +102 -0
  14. package/src/core/utils/StaticGeometryGenerator.js +285 -0
  15. package/src/core/utils/convertToStaticGeometry.js +344 -0
  16. package/src/core/utils/mergeGeometries.js +218 -0
  17. package/src/core/utils/sceneUpdateUtils.js +96 -0
  18. package/src/index.d.ts +274 -0
  19. package/src/index.js +4 -20
  20. package/src/materials/MaterialBase.js +4 -0
  21. package/src/materials/fullscreen/ClampedInterpolationMaterial.js +112 -0
  22. package/src/materials/fullscreen/DenoiseMaterial.js +4 -0
  23. package/src/materials/pathtracing/PhysicalPathTracingMaterial.js +73 -76
  24. package/src/materials/pathtracing/glsl/{attenuateHit.glsl.js → attenuate_hit_function.glsl.js} +1 -1
  25. package/src/materials/pathtracing/glsl/{cameraUtils.glsl.js → camera_util_functions.glsl.js} +1 -1
  26. package/src/materials/pathtracing/glsl/{directLightContribution.glsl.js → direct_light_contribution_function.glsl.js} +1 -1
  27. package/src/materials/pathtracing/glsl/{getSurfaceRecord.glsl.js → get_surface_record_function.glsl.js} +1 -1
  28. package/src/materials/pathtracing/glsl/index.js +6 -0
  29. package/src/materials/pathtracing/glsl/{renderStructs.glsl.js → render_structs.glsl.js} +1 -1
  30. package/src/materials/pathtracing/glsl/{traceScene.glsl.js → trace_scene_function.glsl.js} +1 -3
  31. package/src/materials/surface/AmbientOcclusionMaterial.js +8 -8
  32. package/src/objects/PhysicalSpotLight.js +2 -2
  33. package/src/shader/bsdf/{bsdfSampling.glsl.js → bsdf_functions.glsl.js} +19 -72
  34. package/src/shader/bsdf/{fog.glsl.js → fog_functions.glsl.js} +1 -1
  35. package/src/shader/bsdf/{ggx.glsl.js → ggx_functions.glsl.js} +1 -1
  36. package/src/shader/bsdf/index.js +5 -0
  37. package/src/shader/bsdf/{iridescence.glsl.js → iridescence_functions.glsl.js} +1 -1
  38. package/src/shader/bsdf/{sheen.glsl.js → sheen_functions.glsl.js} +1 -1
  39. package/src/shader/bvh/index.js +2 -0
  40. package/src/shader/{structs/fogMaterialBvh.glsl.js → bvh/inside_fog_volume_function.glsl.js} +1 -1
  41. package/src/shader/{common/bvhAnyHit.glsl.js → bvh/ray_any_hit_function.glsl.js} +1 -1
  42. package/src/shader/common/{fresnel.glsl.js → fresnel_functions.glsl.js} +1 -1
  43. package/src/shader/common/index.js +5 -0
  44. package/src/shader/common/{math.glsl.js → math_functions.glsl.js} +1 -1
  45. package/src/shader/common/{intersectShapes.glsl.js → shape_intersection_functions.glsl.js} +1 -1
  46. package/src/shader/common/{arraySamplerTexelFetch.glsl.js → texture_sample_functions.glsl.js} +1 -1
  47. package/src/shader/common/{utils.glsl.js → util_functions.glsl.js} +1 -1
  48. package/src/shader/rand/index.js +3 -0
  49. package/src/shader/rand/pcg.glsl.js +1 -1
  50. package/src/shader/rand/sobol.glsl.js +4 -4
  51. package/src/shader/rand/{stratifiedTexture.glsl.js → stratified.glsl.js} +7 -2
  52. package/src/shader/sampling/{equirectSampling.glsl.js → equirect_sampling_functions.glsl.js} +1 -2
  53. package/src/shader/sampling/index.js +3 -0
  54. package/src/shader/sampling/{lightSampling.glsl.js → light_sampling_functions.glsl.js} +3 -3
  55. package/src/shader/sampling/{shapeSampling.glsl.js → shape_sampling_functions.glsl.js} +1 -1
  56. package/src/shader/structs/{cameraStruct.glsl.js → camera_struct.glsl.js} +1 -1
  57. package/src/shader/structs/{equirectStruct.glsl.js → equirect_struct.glsl.js} +1 -1
  58. package/src/shader/structs/index.js +5 -0
  59. package/src/shader/structs/{lightsStruct.glsl.js → lights_struct.glsl.js} +1 -1
  60. package/src/shader/structs/{materialStruct.glsl.js → material_struct.glsl.js} +2 -2
  61. package/src/shader/structs/surface_record_struct.glsl.js +63 -0
  62. package/src/uniforms/EquirectHdrInfoUniform.js +16 -11
  63. package/src/uniforms/LightsInfoUniformStruct.js +21 -10
  64. package/src/uniforms/MaterialsTexture.js +27 -86
  65. package/src/uniforms/RenderTarget2DArray.js +60 -20
  66. package/src/utils/BlurredEnvMapGenerator.js +12 -5
  67. package/src/utils/SobolNumberMapGenerator.js +3 -3
  68. package/src/utils/bufferToHash.js +22 -0
  69. package/src/core/DynamicPathTracingSceneGenerator.js +0 -164
  70. package/src/core/MaterialReducer.js +0 -256
  71. package/src/materials/pathtracing/LambertPathTracingMaterial.js +0 -297
  72. package/src/uniforms/IESProfilesTexture.js +0 -100
  73. package/src/uniforms/utils.js +0 -30
  74. package/src/utils/IESLoader.js +0 -327
  75. package/src/workers/PathTracingSceneWorker.js +0 -52
@@ -1,297 +0,0 @@
1
- import { Matrix4, Color } from 'three';
2
- import { MaterialBase } from '../MaterialBase.js';
3
- import {
4
- MeshBVHUniformStruct, FloatVertexAttributeTexture, UIntVertexAttributeTexture,
5
- BVHShaderGLSL,
6
- } from 'three-mesh-bvh';
7
-
8
- // uniforms
9
- import { MaterialStructArrayUniform } from '../uniforms/MaterialStructArrayUniform.js';
10
- import { RenderTarget2DArray } from '../../uniforms/RenderTarget2DArray.js';
11
-
12
- import { materialStructGLSL } from '../../shader/structs/materialStruct.glsl.js';
13
- import { shapeSamplingGLSL } from '../../shader/sampling/shapeSampling.glsl.js';
14
- import { pcgGLSL } from '../../shader/rand/pcg.glsl.js';
15
-
16
- export class LambertPathTracingMaterial extends MaterialBase {
17
-
18
- // three.js relies on this field to add env map functions and defines
19
- get envMap() {
20
-
21
- return this.environmentMap;
22
-
23
- }
24
-
25
- constructor( parameters ) {
26
-
27
- super( {
28
-
29
- transparent: true,
30
- depthWrite: false,
31
-
32
- defines: {
33
- BOUNCES: 3,
34
- MATERIAL_LENGTH: 0,
35
- GRADIENT_BG: 0,
36
- DISPLAY_FLOOR: 1,
37
- },
38
-
39
- uniforms: {
40
- bvh: { value: new MeshBVHUniformStruct() },
41
- normalAttribute: { value: new FloatVertexAttributeTexture() },
42
- tangentAttribute: { value: new FloatVertexAttributeTexture() },
43
- uvAttribute: { value: new FloatVertexAttributeTexture() },
44
- materialIndexAttribute: { value: new UIntVertexAttributeTexture() },
45
- materials: { value: new MaterialStructArrayUniform() },
46
- textures: { value: new RenderTarget2DArray().texture },
47
- cameraWorldMatrix: { value: new Matrix4() },
48
- invProjectionMatrix: { value: new Matrix4() },
49
- environmentBlur: { value: 0.2 },
50
- environmentIntensity: { value: 2.0 },
51
- environmentMap: { value: null },
52
- seed: { value: 0 },
53
- opacity: { value: 1 },
54
-
55
- gradientTop: { value: new Color( 0xbfd8ff ) },
56
- gradientBottom: { value: new Color( 0xffffff ) },
57
-
58
- bgGradientTop: { value: new Color( 0x111111 ) },
59
- bgGradientBottom: { value: new Color( 0x000000 ) },
60
-
61
- },
62
-
63
- vertexShader: /* glsl */`
64
-
65
- varying vec2 vUv;
66
- void main() {
67
-
68
- vec4 mvPosition = vec4( position, 1.0 );
69
- mvPosition = modelViewMatrix * mvPosition;
70
- gl_Position = projectionMatrix * mvPosition;
71
-
72
- vUv = uv;
73
-
74
- }
75
-
76
- `,
77
-
78
- fragmentShader: /* glsl */`
79
- #define RAY_OFFSET 1e-5
80
-
81
- precision highp isampler2D;
82
- precision highp usampler2D;
83
- precision highp sampler2DArray;
84
- vec4 envMapTexelToLinear( vec4 a ) { return a; }
85
- #include <common>
86
- #include <cube_uv_reflection_fragment>
87
-
88
- ${ BVHShaderGLSL.common_functions }
89
- ${ BVHShaderGLSL.bvh_struct_definitions }
90
- ${ BVHShaderGLSL.bvh_ray_functions }
91
-
92
- // uniform structs
93
- ${ materialStructGLSL }
94
-
95
- // rand
96
- ${ pcgGLSL }
97
-
98
- // common
99
- ${ shapeSamplingGLSL }
100
-
101
- #ifdef USE_ENVMAP
102
-
103
- uniform float environmentBlur;
104
- uniform sampler2D environmentMap;
105
-
106
- #else
107
-
108
- uniform vec3 gradientTop;
109
- uniform vec3 gradientBottom;
110
-
111
- #endif
112
-
113
- #if GRADIENT_BG
114
-
115
- uniform vec3 bgGradientTop;
116
- uniform vec3 bgGradientBottom;
117
-
118
- #endif
119
-
120
- uniform mat4 cameraWorldMatrix;
121
- uniform mat4 invProjectionMatrix;
122
- uniform sampler2D normalAttribute;
123
- uniform sampler2D tangentAttribute;
124
- uniform sampler2D uvAttribute;
125
- uniform usampler2D materialIndexAttribute;
126
- uniform BVH bvh;
127
- uniform float environmentIntensity;
128
- uniform int seed;
129
- uniform float opacity;
130
- uniform Material materials[ MATERIAL_LENGTH ];
131
- uniform sampler2DArray textures;
132
- varying vec2 vUv;
133
-
134
- void main() {
135
-
136
- rng_initialize( gl_FragCoord.xy, seed );
137
-
138
- // get [-1, 1] normalized device coordinates
139
- vec2 ndc = 2.0 * vUv - vec2( 1.0 );
140
- vec3 rayOrigin, rayDirection;
141
- ndcToCameraRay( ndc, cameraWorldMatrix, invProjectionMatrix, rayOrigin, rayDirection );
142
-
143
- // Lambertian render
144
- gl_FragColor = vec4( 0.0 );
145
-
146
- vec3 throughputColor = vec3( 1.0 );
147
-
148
- // hit results
149
- uvec4 faceIndices = uvec4( 0u );
150
- vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
151
- vec3 barycoord = vec3( 0.0 );
152
- float side = 1.0;
153
- float dist = 0.0;
154
- int i;
155
- for ( i = 0; i < BOUNCES; i ++ ) {
156
-
157
- if ( ! bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist ) ) {
158
-
159
- #if GRADIENT_BG
160
-
161
- if ( i == 0 ) {
162
-
163
- rayDirection = normalize( rayDirection );
164
- float value = ( rayDirection.y + 1.0 ) / 2.0;
165
-
166
- value = pow( value, 2.0 );
167
-
168
- gl_FragColor = vec4( mix( bgGradientBottom, bgGradientTop, value ), 1.0 );
169
- break;
170
-
171
- }
172
-
173
- #endif
174
-
175
- #ifdef USE_ENVMAP
176
-
177
- vec3 skyColor = textureCubeUV( environmentMap, rayDirection, environmentBlur ).rgb;
178
-
179
- #else
180
-
181
- rayDirection = normalize( rayDirection );
182
- float value = ( rayDirection.y + 1.0 ) / 2.0;
183
- vec3 skyColor = mix( gradientBottom, gradientTop, value );
184
-
185
- #endif
186
-
187
- gl_FragColor += vec4( skyColor * throughputColor * environmentIntensity, 1.0 );
188
-
189
- break;
190
-
191
- }
192
-
193
-
194
- uint materialIndex = uTexelFetch1D( materialIndexAttribute, faceIndices.x ).r;
195
- Material material = materials[ materialIndex ];
196
-
197
- if ( material.opacity < pcgRand() ) {
198
-
199
- vec3 point = rayOrigin + rayDirection * dist;
200
- rayOrigin += rayDirection * dist - faceNormal * RAY_OFFSET;
201
- throughputColor *= mix( vec3( 1.0 ), material.color, 0.5 * material.opacity );
202
-
203
- i --;
204
- continue;
205
-
206
- }
207
-
208
- // fetch the interpolated smooth normal
209
- vec3 normal = normalize( textureSampleBarycoord(
210
- normalAttribute,
211
- barycoord,
212
- faceIndices.xyz
213
- ).xyz );
214
-
215
- vec2 uv = textureSampleBarycoord( uvAttribute, barycoord, faceIndices.xyz ).xy;
216
-
217
- // emission
218
- vec3 emission = material.emissiveIntensity * material.emissive;
219
- if ( material.emissiveMap != - 1 ) {
220
-
221
- emission *= texture2D( textures, vec3( uv, material.emissiveMap ) ).xyz;
222
-
223
- }
224
-
225
- gl_FragColor.rgb += throughputColor * emission * max( side, 0.0 );
226
-
227
- // 1 / PI attenuation for physically correct lambert model
228
- // https://www.rorydriscoll.com/2009/01/25/energy-conservation-in-games/
229
- throughputColor *= 1.0 / PI;
230
-
231
- // albedo
232
- throughputColor *= material.color;
233
- if ( material.map != - 1 ) {
234
-
235
- throughputColor *= texture2D( textures, vec3( uv, material.map ) ).xyz;
236
-
237
- }
238
-
239
- // normal
240
- if ( material.normalMap != - 1 ) {
241
-
242
- vec4 tangentSample = textureSampleBarycoord(
243
- tangentAttribute,
244
- barycoord,
245
- faceIndices.xyz
246
- );
247
-
248
- // some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
249
- // resulting in NaNs and slow path tracing.
250
- if ( length( tangentSample.xyz ) > 0.0 ) {
251
-
252
- vec3 tangent = normalize( tangentSample.xyz );
253
- vec3 bitangent = normalize( cross( normal, tangent ) * tangentSample.w );
254
- mat3 vTBN = mat3( tangent, bitangent, normal );
255
-
256
- vec3 texNormal = texture2D( textures, vec3( uv, material.normalMap ) ).xyz * 2.0 - 1.0;
257
- texNormal.xy *= material.normalScale;
258
- normal = vTBN * texNormal;
259
-
260
- }
261
-
262
- }
263
-
264
- normal *= side;
265
-
266
- // adjust the hit point by the surface normal by a factor of some offset and the
267
- // maximum component-wise value of the current point to accommodate floating point
268
- // error as values increase.
269
- vec3 point = rayOrigin + rayDirection * dist;
270
- vec3 absPoint = abs( point );
271
- float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
272
- rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * RAY_OFFSET;
273
- rayDirection = sampleHemisphere( normal, rand2() );
274
-
275
- // if the surface normal is skewed such that the outgoing vector can wind up underneath
276
- // the triangle surface then just consider it absorbed.
277
- if ( dot( rayDirection, faceNormal ) < 0.0 ) {
278
-
279
- break;
280
-
281
- }
282
-
283
- }
284
-
285
- gl_FragColor.a = opacity;
286
-
287
- }
288
-
289
- `
290
-
291
- } );
292
-
293
- this.setValues( parameters );
294
-
295
- }
296
-
297
- }
@@ -1,100 +0,0 @@
1
- import {
2
- ClampToEdgeWrapping,
3
- Color,
4
- HalfFloatType,
5
- LinearFilter,
6
- MeshBasicMaterial,
7
- NoToneMapping,
8
- RGBAFormat,
9
- WebGLArrayRenderTarget,
10
- } from 'three';
11
- import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
12
- import { IESLoader } from '../utils/IESLoader.js';
13
-
14
- const prevColor = new Color();
15
- export class IESProfilesTexture extends WebGLArrayRenderTarget {
16
-
17
- constructor( ...args ) {
18
-
19
- super( ...args );
20
-
21
- const tex = this.texture;
22
- tex.format = RGBAFormat;
23
- tex.type = HalfFloatType;
24
- tex.minFilter = LinearFilter;
25
- tex.magFilter = LinearFilter;
26
- tex.wrapS = ClampToEdgeWrapping;
27
- tex.wrapT = ClampToEdgeWrapping;
28
- tex.generateMipmaps = false;
29
-
30
- tex.updateFrom = ( ...args ) => {
31
-
32
- this.updateFrom( ...args );
33
-
34
- };
35
-
36
- const fsQuad = new FullScreenQuad( new MeshBasicMaterial() );
37
- this.fsQuad = fsQuad;
38
-
39
- this.iesLoader = new IESLoader();
40
-
41
- }
42
-
43
- async updateFrom( renderer, textures ) {
44
-
45
- // save previous renderer state
46
- const prevRenderTarget = renderer.getRenderTarget();
47
- const prevToneMapping = renderer.toneMapping;
48
- const prevAlpha = renderer.getClearAlpha();
49
- renderer.getClearColor( prevColor );
50
-
51
- // resize the render target and ensure we don't have an empty texture
52
- // render target depth must be >= 1 to avoid unbound texture error on android devices
53
- const depth = textures.length || 1;
54
- this.setSize( 360, 180, depth );
55
- renderer.setClearColor( 0, 0 );
56
- renderer.toneMapping = NoToneMapping;
57
-
58
- // render each texture into each layer of the target
59
- const fsQuad = this.fsQuad;
60
- for ( let i = 0, l = depth; i < l; i ++ ) {
61
-
62
- const texture = textures[ i ];
63
- if ( texture ) {
64
-
65
- // revert to default texture transform before rendering
66
- texture.matrixAutoUpdate = false;
67
- texture.matrix.identity();
68
-
69
- fsQuad.material.map = texture;
70
- fsQuad.material.transparent = true;
71
-
72
- renderer.setRenderTarget( this, i );
73
- fsQuad.render( renderer );
74
-
75
- // restore custom texture transform
76
- texture.updateMatrix();
77
- texture.matrixAutoUpdate = true;
78
-
79
- }
80
-
81
- }
82
-
83
- // reset the renderer
84
- fsQuad.material.map = null;
85
- renderer.setClearColor( prevColor, prevAlpha );
86
- renderer.setRenderTarget( prevRenderTarget );
87
- renderer.toneMapping = prevToneMapping;
88
-
89
- fsQuad.dispose();
90
-
91
- }
92
-
93
- dispose() {
94
-
95
- super.dispose();
96
- this.fsQuad.dispose();
97
-
98
- }
99
-
100
- }
@@ -1,30 +0,0 @@
1
- // we must hash the texture to determine uniqueness using the encoding, as well, because the
2
- // when rendering each texture to the texture array they must have a consistent color space.
3
- export function getTextureHash( t ) {
4
-
5
- return `${ t.source.uuid }:${ t.colorSpace }`;
6
-
7
- }
8
-
9
- // reduce the set of textures to just those with a unique source while retaining
10
- // the order of the textures.
11
- export function reduceTexturesToUniqueSources( textures ) {
12
-
13
- const sourceSet = new Set();
14
- const result = [];
15
- for ( let i = 0, l = textures.length; i < l; i ++ ) {
16
-
17
- const tex = textures[ i ];
18
- const hash = getTextureHash( tex );
19
- if ( ! sourceSet.has( hash ) ) {
20
-
21
- sourceSet.add( hash );
22
- result.push( tex );
23
-
24
- }
25
-
26
- }
27
-
28
- return result;
29
-
30
- }