three-gpu-pathtracer 0.0.7 → 0.0.9

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 (51) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +886 -815
  3. package/build/index.module.js +6374 -5613
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +6377 -5615
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +69 -68
  8. package/src/core/DynamicPathTracingSceneGenerator.js +119 -119
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +275 -270
  11. package/src/core/PathTracingSceneGenerator.js +1 -1
  12. package/src/index.js +39 -35
  13. package/src/materials/AlphaDisplayMaterial.js +48 -48
  14. package/src/materials/AmbientOcclusionMaterial.js +199 -197
  15. package/src/materials/BlendMaterial.js +67 -67
  16. package/src/materials/DenoiseMaterial.js +142 -142
  17. package/src/materials/GraphMaterial.js +243 -243
  18. package/src/materials/LambertPathTracingMaterial.js +285 -285
  19. package/src/materials/MaterialBase.js +56 -56
  20. package/src/materials/PhysicalPathTracingMaterial.js +982 -970
  21. package/src/objects/EquirectCamera.js +13 -13
  22. package/src/objects/PhysicalCamera.js +28 -28
  23. package/src/objects/PhysicalSpotLight.js +14 -14
  24. package/src/objects/ShapedAreaLight.js +12 -12
  25. package/src/shader/shaderEnvMapSampling.js +58 -59
  26. package/src/shader/shaderGGXFunctions.js +100 -100
  27. package/src/shader/shaderIridescenceFunctions.js +130 -130
  28. package/src/shader/shaderLayerTexelFetchFunctions.js +25 -0
  29. package/src/shader/shaderLightSampling.js +229 -231
  30. package/src/shader/shaderMaterialSampling.js +498 -542
  31. package/src/shader/shaderRandFunctions.js +57 -0
  32. package/src/shader/shaderSheenFunctions.js +98 -98
  33. package/src/shader/shaderSobolSampling.js +256 -0
  34. package/src/shader/shaderStructs.js +325 -321
  35. package/src/shader/shaderUtils.js +361 -364
  36. package/src/textures/GradientEquirectTexture.js +35 -0
  37. package/src/textures/ProceduralEquirectTexture.js +75 -0
  38. package/src/uniforms/AttributesTextureArray.js +35 -0
  39. package/src/uniforms/EquirectHdrInfoUniform.js +259 -259
  40. package/src/uniforms/FloatAttributeTextureArray.js +169 -0
  41. package/src/uniforms/IESProfilesTexture.js +100 -100
  42. package/src/uniforms/LightsInfoUniformStruct.js +207 -162
  43. package/src/uniforms/MaterialsTexture.js +426 -426
  44. package/src/uniforms/PhysicalCameraUniform.js +36 -36
  45. package/src/uniforms/RenderTarget2DArray.js +97 -93
  46. package/src/uniforms/utils.js +30 -0
  47. package/src/utils/BlurredEnvMapGenerator.js +116 -113
  48. package/src/utils/IESLoader.js +325 -325
  49. package/src/utils/SobolNumberMapGenerator.js +80 -0
  50. package/src/utils/UVUnwrapper.js +101 -101
  51. package/src/workers/PathTracingSceneWorker.js +42 -42
@@ -1,162 +1,207 @@
1
- import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, Vector3, Quaternion, Matrix4 } from 'three';
2
-
3
- const LIGHT_PIXELS = 6;
4
- const RECT_AREA_LIGHT = 0;
5
- const CIRC_AREA_LIGHT = 1;
6
- const SPOT_LIGHT = 2;
7
- export class LightsInfoUniformStruct {
8
-
9
- constructor() {
10
-
11
- const tex = new DataTexture( new Float32Array( 4 ), 1, 1 );
12
- tex.format = RGBAFormat;
13
- tex.type = FloatType;
14
- tex.wrapS = ClampToEdgeWrapping;
15
- tex.wrapT = ClampToEdgeWrapping;
16
- tex.generateMipmaps = false;
17
-
18
- this.tex = tex;
19
- this.count = 0;
20
-
21
- }
22
-
23
- updateFrom( lights, iesTextures = [] ) {
24
-
25
- const tex = this.tex;
26
- const pixelCount = Math.max( lights.length * LIGHT_PIXELS, 1 );
27
- const dimension = Math.ceil( Math.sqrt( pixelCount ) );
28
-
29
- if ( tex.image.width !== dimension ) {
30
-
31
- tex.dispose();
32
-
33
- tex.image.data = new Float32Array( dimension * dimension * 4 );
34
- tex.image.width = dimension;
35
- tex.image.height = dimension;
36
-
37
- }
38
-
39
- const floatArray = tex.image.data;
40
-
41
- const u = new Vector3();
42
- const v = new Vector3();
43
- const m = new Matrix4();
44
- const worldQuaternion = new Quaternion();
45
- const eye = new Vector3();
46
- const target = new Vector3();
47
- const up = new Vector3();
48
-
49
- for ( let i = 0, l = lights.length; i < l; i ++ ) {
50
-
51
- const l = lights[ i ];
52
-
53
- const baseIndex = i * LIGHT_PIXELS * 4;
54
- let index = 0;
55
-
56
- // sample 1
57
- // position
58
- l.getWorldPosition( v );
59
- floatArray[ baseIndex + ( index ++ ) ] = v.x;
60
- floatArray[ baseIndex + ( index ++ ) ] = v.y;
61
- floatArray[ baseIndex + ( index ++ ) ] = v.z;
62
-
63
- // type
64
- let type = RECT_AREA_LIGHT;
65
- if ( l.isRectAreaLight && l.isCircular ) type = CIRC_AREA_LIGHT;
66
- else if ( l.isSpotLight ) type = SPOT_LIGHT;
67
- floatArray[ baseIndex + ( index ++ ) ] = type;
68
-
69
- // sample 2
70
- // color
71
- floatArray[ baseIndex + ( index ++ ) ] = l.color.r;
72
- floatArray[ baseIndex + ( index ++ ) ] = l.color.g;
73
- floatArray[ baseIndex + ( index ++ ) ] = l.color.b;
74
-
75
- // intensity
76
- floatArray[ baseIndex + ( index ++ ) ] = l.intensity;
77
-
78
- l.getWorldQuaternion( worldQuaternion );
79
-
80
- if ( l.isRectAreaLight ) {
81
-
82
- // sample 3
83
- // u vector
84
- u.set( l.width, 0, 0 ).applyQuaternion( worldQuaternion );
85
-
86
- floatArray[ baseIndex + ( index ++ ) ] = u.x;
87
- floatArray[ baseIndex + ( index ++ ) ] = u.y;
88
- floatArray[ baseIndex + ( index ++ ) ] = u.z;
89
- index ++;
90
-
91
- // sample 4
92
- // v vector
93
- v.set( 0, l.height, 0 ).applyQuaternion( worldQuaternion );
94
-
95
- floatArray[ baseIndex + ( index ++ ) ] = v.x;
96
- floatArray[ baseIndex + ( index ++ ) ] = v.y;
97
- floatArray[ baseIndex + ( index ++ ) ] = v.z;
98
-
99
- // area
100
- floatArray[ baseIndex + ( index ++ ) ] = u.cross( v ).length() * ( l.isCircular ? ( Math.PI / 4.0 ) : 1.0 );
101
-
102
- } else if ( l.isSpotLight ) {
103
-
104
- const radius = l.radius;
105
- eye.setFromMatrixPosition( l.matrixWorld );
106
- target.setFromMatrixPosition( l.target.matrixWorld );
107
- m.lookAt( eye, target, up );
108
- worldQuaternion.setFromRotationMatrix( m );
109
-
110
- // sample 3
111
- // u vector
112
- u.set( 1, 0, 0 ).applyQuaternion( worldQuaternion );
113
-
114
- floatArray[ baseIndex + ( index ++ ) ] = u.x;
115
- floatArray[ baseIndex + ( index ++ ) ] = u.y;
116
- floatArray[ baseIndex + ( index ++ ) ] = u.z;
117
- index ++;
118
-
119
- // sample 4
120
- // v vector
121
- v.set( 0, 1, 0 ).applyQuaternion( worldQuaternion );
122
-
123
- floatArray[ baseIndex + ( index ++ ) ] = v.x;
124
- floatArray[ baseIndex + ( index ++ ) ] = v.y;
125
- floatArray[ baseIndex + ( index ++ ) ] = v.z;
126
-
127
- // area
128
- floatArray[ baseIndex + ( index ++ ) ] = Math.PI * radius * radius;
129
-
130
- // sample 5
131
- // radius
132
- floatArray[ baseIndex + ( index ++ ) ] = radius;
133
-
134
- // near
135
- floatArray[ baseIndex + ( index ++ ) ] = l.shadow.camera.near;
136
-
137
- // decay
138
- floatArray[ baseIndex + ( index ++ ) ] = l.decay;
139
-
140
- // distance
141
- floatArray[ baseIndex + ( index ++ ) ] = l.distance;
142
-
143
- // sample 6
144
- // coneCos
145
- floatArray[ baseIndex + ( index ++ ) ] = Math.cos( l.angle );
146
-
147
- // penumbraCos
148
- floatArray[ baseIndex + ( index ++ ) ] = Math.cos( l.angle * ( 1 - l.penumbra ) );
149
-
150
- // iesProfile
151
- floatArray[ baseIndex + ( index ++ ) ] = iesTextures.indexOf( l.iesTexture );
152
-
153
- }
154
-
155
- }
156
-
157
- tex.needsUpdate = true;
158
- this.count = lights.length;
159
-
160
- }
161
-
162
- }
1
+ import { DataTexture, RGBAFormat, ClampToEdgeWrapping, FloatType, Vector3, Quaternion, Matrix4 } from 'three';
2
+
3
+ const LIGHT_PIXELS = 6;
4
+ const RECT_AREA_LIGHT = 0;
5
+ const CIRC_AREA_LIGHT = 1;
6
+ const SPOT_LIGHT = 2;
7
+ const DIR_LIGHT = 3;
8
+ const POINT_LIGHT = 4;
9
+ export class LightsInfoUniformStruct {
10
+
11
+ constructor() {
12
+
13
+ const tex = new DataTexture( new Float32Array( 4 ), 1, 1 );
14
+ tex.format = RGBAFormat;
15
+ tex.type = FloatType;
16
+ tex.wrapS = ClampToEdgeWrapping;
17
+ tex.wrapT = ClampToEdgeWrapping;
18
+ tex.generateMipmaps = false;
19
+
20
+ this.tex = tex;
21
+ this.count = 0;
22
+
23
+ }
24
+
25
+ updateFrom( lights, iesTextures = [] ) {
26
+
27
+ const tex = this.tex;
28
+ const pixelCount = Math.max( lights.length * LIGHT_PIXELS, 1 );
29
+ const dimension = Math.ceil( Math.sqrt( pixelCount ) );
30
+
31
+ if ( tex.image.width !== dimension ) {
32
+
33
+ tex.dispose();
34
+
35
+ tex.image.data = new Float32Array( dimension * dimension * 4 );
36
+ tex.image.width = dimension;
37
+ tex.image.height = dimension;
38
+
39
+ }
40
+
41
+ const floatArray = tex.image.data;
42
+
43
+ const u = new Vector3();
44
+ const v = new Vector3();
45
+ const m = new Matrix4();
46
+ const worldQuaternion = new Quaternion();
47
+ const eye = new Vector3();
48
+ const target = new Vector3();
49
+ const up = new Vector3();
50
+
51
+ for ( let i = 0, l = lights.length; i < l; i ++ ) {
52
+
53
+ const l = lights[ i ];
54
+
55
+ const baseIndex = i * LIGHT_PIXELS * 4;
56
+ let index = 0;
57
+
58
+ // sample 1
59
+ // position
60
+ l.getWorldPosition( v );
61
+ floatArray[ baseIndex + ( index ++ ) ] = v.x;
62
+ floatArray[ baseIndex + ( index ++ ) ] = v.y;
63
+ floatArray[ baseIndex + ( index ++ ) ] = v.z;
64
+
65
+ // type
66
+ let type = RECT_AREA_LIGHT;
67
+ if ( l.isRectAreaLight && l.isCircular ) {
68
+
69
+ type = CIRC_AREA_LIGHT;
70
+
71
+ } else if ( l.isSpotLight ) {
72
+
73
+ type = SPOT_LIGHT;
74
+
75
+ } else if ( l.isDirectionalLight ) {
76
+
77
+ type = DIR_LIGHT;
78
+
79
+ } else if ( l.isPointLight ) {
80
+
81
+ type = POINT_LIGHT;
82
+
83
+ }
84
+
85
+ floatArray[ baseIndex + ( index ++ ) ] = type;
86
+
87
+ // sample 2
88
+ // color
89
+ floatArray[ baseIndex + ( index ++ ) ] = l.color.r;
90
+ floatArray[ baseIndex + ( index ++ ) ] = l.color.g;
91
+ floatArray[ baseIndex + ( index ++ ) ] = l.color.b;
92
+
93
+ // intensity
94
+ floatArray[ baseIndex + ( index ++ ) ] = l.intensity;
95
+
96
+ l.getWorldQuaternion( worldQuaternion );
97
+
98
+ if ( l.isRectAreaLight ) {
99
+
100
+ // sample 3
101
+ // u vector
102
+ u.set( l.width, 0, 0 ).applyQuaternion( worldQuaternion );
103
+
104
+ floatArray[ baseIndex + ( index ++ ) ] = u.x;
105
+ floatArray[ baseIndex + ( index ++ ) ] = u.y;
106
+ floatArray[ baseIndex + ( index ++ ) ] = u.z;
107
+ index ++;
108
+
109
+ // sample 4
110
+ // v vector
111
+ v.set( 0, l.height, 0 ).applyQuaternion( worldQuaternion );
112
+
113
+ floatArray[ baseIndex + ( index ++ ) ] = v.x;
114
+ floatArray[ baseIndex + ( index ++ ) ] = v.y;
115
+ floatArray[ baseIndex + ( index ++ ) ] = v.z;
116
+
117
+ // area
118
+ floatArray[ baseIndex + ( index ++ ) ] = u.cross( v ).length() * ( l.isCircular ? ( Math.PI / 4.0 ) : 1.0 );
119
+
120
+ } else if ( l.isSpotLight ) {
121
+
122
+ const radius = l.radius;
123
+ eye.setFromMatrixPosition( l.matrixWorld );
124
+ target.setFromMatrixPosition( l.target.matrixWorld );
125
+ m.lookAt( eye, target, up );
126
+ worldQuaternion.setFromRotationMatrix( m );
127
+
128
+ // sample 3
129
+ // u vector
130
+ u.set( 1, 0, 0 ).applyQuaternion( worldQuaternion );
131
+
132
+ floatArray[ baseIndex + ( index ++ ) ] = u.x;
133
+ floatArray[ baseIndex + ( index ++ ) ] = u.y;
134
+ floatArray[ baseIndex + ( index ++ ) ] = u.z;
135
+ index ++;
136
+
137
+ // sample 4
138
+ // v vector
139
+ v.set( 0, 1, 0 ).applyQuaternion( worldQuaternion );
140
+
141
+ floatArray[ baseIndex + ( index ++ ) ] = v.x;
142
+ floatArray[ baseIndex + ( index ++ ) ] = v.y;
143
+ floatArray[ baseIndex + ( index ++ ) ] = v.z;
144
+
145
+ // area
146
+ floatArray[ baseIndex + ( index ++ ) ] = Math.PI * radius * radius;
147
+
148
+ // sample 5
149
+ // radius
150
+ floatArray[ baseIndex + ( index ++ ) ] = radius;
151
+
152
+ // near
153
+ floatArray[ baseIndex + ( index ++ ) ] = l.shadow.camera.near;
154
+
155
+ // decay
156
+ floatArray[ baseIndex + ( index ++ ) ] = l.decay;
157
+
158
+ // distance
159
+ floatArray[ baseIndex + ( index ++ ) ] = l.distance;
160
+
161
+ // sample 6
162
+ // coneCos
163
+ floatArray[ baseIndex + ( index ++ ) ] = Math.cos( l.angle );
164
+
165
+ // penumbraCos
166
+ floatArray[ baseIndex + ( index ++ ) ] = Math.cos( l.angle * ( 1 - l.penumbra ) );
167
+
168
+ // iesProfile
169
+ floatArray[ baseIndex + ( index ++ ) ] = iesTextures.indexOf( l.iesTexture );
170
+
171
+ } else if ( l.isPointLight ) {
172
+
173
+ const worldPosition = l.getWorldPosition( u );
174
+ floatArray[ baseIndex + ( index ++ ) ] = worldPosition.x;
175
+ floatArray[ baseIndex + ( index ++ ) ] = worldPosition.y;
176
+ floatArray[ baseIndex + ( index ++ ) ] = worldPosition.z;
177
+ index ++;
178
+
179
+ // sample 4
180
+ index += 4;
181
+
182
+ // sample 5
183
+ index += 2;
184
+
185
+ floatArray[ baseIndex + ( index ++ ) ] = l.decay;
186
+ floatArray[ baseIndex + ( index ++ ) ] = l.distance;
187
+
188
+ } else if ( l.isDirectionalLight ) {
189
+
190
+ const worldPosition = l.getWorldPosition( u );
191
+ const targetPosition = l.target.getWorldPosition( v );
192
+
193
+ target.subVectors( worldPosition, targetPosition ).normalize();
194
+ floatArray[ baseIndex + ( index ++ ) ] = target.x;
195
+ floatArray[ baseIndex + ( index ++ ) ] = target.y;
196
+ floatArray[ baseIndex + ( index ++ ) ] = target.z;
197
+
198
+ }
199
+
200
+ }
201
+
202
+ tex.needsUpdate = true;
203
+ this.count = lights.length;
204
+
205
+ }
206
+
207
+ }