three-gpu-pathtracer 0.0.1 → 0.0.4

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 (36) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +678 -386
  3. package/build/index.module.js +3166 -1690
  4. package/build/index.module.js.map +1 -1
  5. package/build/index.umd.cjs +3176 -1692
  6. package/build/index.umd.cjs.map +1 -1
  7. package/package.json +60 -57
  8. package/src/core/DynamicPathTracingSceneGenerator.js +106 -0
  9. package/src/core/MaterialReducer.js +256 -256
  10. package/src/core/PathTracingRenderer.js +125 -28
  11. package/src/core/PathTracingSceneGenerator.js +52 -46
  12. package/src/core/PhysicalCamera.js +28 -0
  13. package/src/index.js +25 -21
  14. package/src/materials/AlphaDisplayMaterial.js +48 -0
  15. package/src/materials/AmbientOcclusionMaterial.js +197 -197
  16. package/src/materials/BlendMaterial.js +67 -0
  17. package/src/materials/LambertPathTracingMaterial.js +285 -285
  18. package/src/materials/MaterialBase.js +56 -56
  19. package/src/materials/PhysicalPathTracingMaterial.js +684 -370
  20. package/src/shader/shaderEnvMapSampling.js +67 -0
  21. package/src/shader/shaderGGXFunctions.js +108 -107
  22. package/src/shader/shaderMaterialSampling.js +345 -333
  23. package/src/shader/shaderStructs.js +131 -30
  24. package/src/shader/shaderUtils.js +246 -140
  25. package/src/uniforms/EquirectHdrInfoUniform.js +263 -0
  26. package/src/uniforms/MaterialsTexture.js +251 -0
  27. package/src/uniforms/PhysicalCameraUniform.js +36 -0
  28. package/src/uniforms/RenderTarget2DArray.js +93 -80
  29. package/src/utils/BlurredEnvMapGenerator.js +113 -0
  30. package/src/utils/GeometryPreparationUtils.js +194 -172
  31. package/src/utils/UVUnwrapper.js +101 -101
  32. package/src/workers/PathTracingSceneWorker.js +40 -0
  33. package/src/uniforms/EquirectPdfUniform.js +0 -132
  34. package/src/uniforms/MaterialStructArrayUniform.js +0 -18
  35. package/src/uniforms/MaterialStructUniform.js +0 -94
  36. package/src/viewers/PathTracingViewer.js +0 -259
@@ -1,197 +1,197 @@
1
- import { TangentSpaceNormalMap, Vector2 } from 'three';
2
- import { MaterialBase } from './MaterialBase.js';
3
- import { MeshBVHUniformStruct, shaderStructs, shaderIntersectFunction } from 'three-mesh-bvh';
4
- import { shaderMaterialStructs } from '../shader/shaderStructs.js';
5
- import { shaderUtils } from '../shader/shaderUtils.js';
6
-
7
- export class AmbientOcclusionMaterial extends MaterialBase {
8
-
9
- get normalMap() {
10
-
11
- return this.uniforms.normalMap.value;
12
-
13
- }
14
-
15
- set normalMap( v ) {
16
-
17
- this.uniforms.normalMap.value = v;
18
- this.setDefine( 'USE_NORMALMAP', v ? null : '' );
19
-
20
- }
21
-
22
- get normalMapType() {
23
-
24
- return TangentSpaceNormalMap;
25
-
26
- }
27
-
28
- set normalMapType( v ) {
29
-
30
- if ( v !== TangentSpaceNormalMap ) {
31
-
32
- throw new Error( 'AmbientOcclusionMaterial: Only tangent space normal map are supported' );
33
-
34
- }
35
-
36
- }
37
-
38
- constructor( parameters ) {
39
-
40
- super( {
41
-
42
- defines: {
43
- SAMPLES: 10,
44
- },
45
-
46
- uniforms: {
47
- bvh: { value: new MeshBVHUniformStruct() },
48
- radius: { value: 1.0 },
49
- seed: { value: 0 },
50
-
51
- normalMap: { value: null },
52
- normalScale: { value: new Vector2( 1, 1 ) },
53
- },
54
-
55
- vertexShader: /* glsl */`
56
-
57
- varying vec3 vNorm;
58
- varying vec3 vPos;
59
-
60
- #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
61
-
62
- varying vec2 vUv;
63
- varying vec4 vTan;
64
-
65
- #endif
66
-
67
- void main() {
68
-
69
- vec4 mvPosition = vec4( position, 1.0 );
70
- mvPosition = modelViewMatrix * mvPosition;
71
- gl_Position = projectionMatrix * mvPosition;
72
-
73
- mat3 modelNormalMatrix = transpose( inverse( mat3( modelMatrix ) ) );
74
- vNorm = normalize( modelNormalMatrix * normal );
75
- vPos = ( modelMatrix * vec4( position, 1.0 ) ).xyz;
76
-
77
- #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
78
-
79
- vUv = uv;
80
- vTan = tangent;
81
-
82
- #endif
83
-
84
- }
85
-
86
- `,
87
-
88
- fragmentShader: /* glsl */`
89
- #define RAY_OFFSET 1e-5
90
-
91
- precision highp isampler2D;
92
- precision highp usampler2D;
93
- precision highp sampler2DArray;
94
- #include <common>
95
- #include <cube_uv_reflection_fragment>
96
-
97
- ${ shaderStructs }
98
- ${ shaderIntersectFunction }
99
- ${ shaderMaterialStructs }
100
- ${ shaderUtils }
101
-
102
- uniform BVH bvh;
103
- uniform int seed;
104
- uniform float radius;
105
-
106
- varying vec3 vNorm;
107
- varying vec3 vPos;
108
-
109
- #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
110
-
111
- uniform sampler2D normalMap;
112
- uniform vec2 normalScale;
113
- varying vec2 vUv;
114
- varying vec4 vTan;
115
-
116
- #endif
117
-
118
- void main() {
119
-
120
- rng_initialize( gl_FragCoord.xy, seed );
121
-
122
- // compute the flat face surface normal
123
- vec3 fdx = vec3( dFdx( vPos.x ), dFdx( vPos.y ), dFdx( vPos.z ) );
124
- vec3 fdy = vec3( dFdy( vPos.x ), dFdy( vPos.y ), dFdy( vPos.z ) );
125
- vec3 faceNormal = normalize( cross( fdx, fdy ) );
126
-
127
- // find the max component to scale the offset to account for floating point error
128
- vec3 absPoint = abs( vPos );
129
- float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
130
- vec3 normal = vNorm;
131
-
132
- #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
133
-
134
- // some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
135
- // resulting in NaNs and slow path tracing.
136
- if ( length( vTan.xyz ) > 0.0 ) {
137
-
138
- vec2 uv = vUv;
139
- vec3 tangent = normalize( vTan.xyz );
140
- vec3 bitangent = normalize( cross( normal, tangent ) * vTan.w );
141
- mat3 vTBN = mat3( tangent, bitangent, normal );
142
-
143
- vec3 texNormal = texture2D( normalMap, uv ).xyz * 2.0 - 1.0;
144
- texNormal.xy *= normalScale;
145
- normal = vTBN * texNormal;
146
-
147
- }
148
-
149
- #endif
150
-
151
- normal *= gl_FrontFacing ? 1.0 : - 1.0;
152
-
153
- vec3 rayOrigin = vPos + faceNormal * ( maxPoint + 1.0 ) * RAY_OFFSET;
154
- float accumulated = 0.0;
155
- for ( int i = 0; i < SAMPLES; i ++ ) {
156
-
157
- // sample the cosine weighted hemisphere and discard the sample if it's below
158
- // the geometric surface
159
- vec3 rayDirection = getHemisphereSample( normalize( normal ), rand4().xy );
160
-
161
- // check if we hit the mesh and its within the specified radius
162
- float side = 1.0;
163
- float dist = 0.0;
164
- vec3 barycoord = vec3( 0.0 );
165
- vec3 outNormal = vec3( 0.0 );
166
- uvec4 faceIndices = uvec4( 0u );
167
-
168
- // if the ray is above the geometry surface, and it doesn't hit another surface within the specified radius then
169
- // we consider it lit
170
- if (
171
- dot( rayDirection, faceNormal ) > 0.0 &&
172
- (
173
- ! bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, outNormal, barycoord, side, dist ) ||
174
- dist > radius
175
- )
176
- ) {
177
-
178
- accumulated += 1.0;
179
-
180
- }
181
-
182
- }
183
-
184
- gl_FragColor.rgb = vec3( accumulated / float( SAMPLES ) );
185
- gl_FragColor.a = 1.0;
186
-
187
- }
188
-
189
- `
190
-
191
- } );
192
-
193
- this.setValues( parameters );
194
-
195
- }
196
-
197
- }
1
+ import { TangentSpaceNormalMap, Vector2 } from 'three';
2
+ import { MaterialBase } from './MaterialBase.js';
3
+ import { MeshBVHUniformStruct, shaderStructs, shaderIntersectFunction } from 'three-mesh-bvh';
4
+ import { shaderMaterialStructs } from '../shader/shaderStructs.js';
5
+ import { shaderUtils } from '../shader/shaderUtils.js';
6
+
7
+ export class AmbientOcclusionMaterial extends MaterialBase {
8
+
9
+ get normalMap() {
10
+
11
+ return this.uniforms.normalMap.value;
12
+
13
+ }
14
+
15
+ set normalMap( v ) {
16
+
17
+ this.uniforms.normalMap.value = v;
18
+ this.setDefine( 'USE_NORMALMAP', v ? null : '' );
19
+
20
+ }
21
+
22
+ get normalMapType() {
23
+
24
+ return TangentSpaceNormalMap;
25
+
26
+ }
27
+
28
+ set normalMapType( v ) {
29
+
30
+ if ( v !== TangentSpaceNormalMap ) {
31
+
32
+ throw new Error( 'AmbientOcclusionMaterial: Only tangent space normal map are supported' );
33
+
34
+ }
35
+
36
+ }
37
+
38
+ constructor( parameters ) {
39
+
40
+ super( {
41
+
42
+ defines: {
43
+ SAMPLES: 10,
44
+ },
45
+
46
+ uniforms: {
47
+ bvh: { value: new MeshBVHUniformStruct() },
48
+ radius: { value: 1.0 },
49
+ seed: { value: 0 },
50
+
51
+ normalMap: { value: null },
52
+ normalScale: { value: new Vector2( 1, 1 ) },
53
+ },
54
+
55
+ vertexShader: /* glsl */`
56
+
57
+ varying vec3 vNorm;
58
+ varying vec3 vPos;
59
+
60
+ #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
61
+
62
+ varying vec2 vUv;
63
+ varying vec4 vTan;
64
+
65
+ #endif
66
+
67
+ void main() {
68
+
69
+ vec4 mvPosition = vec4( position, 1.0 );
70
+ mvPosition = modelViewMatrix * mvPosition;
71
+ gl_Position = projectionMatrix * mvPosition;
72
+
73
+ mat3 modelNormalMatrix = transpose( inverse( mat3( modelMatrix ) ) );
74
+ vNorm = normalize( modelNormalMatrix * normal );
75
+ vPos = ( modelMatrix * vec4( position, 1.0 ) ).xyz;
76
+
77
+ #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
78
+
79
+ vUv = uv;
80
+ vTan = tangent;
81
+
82
+ #endif
83
+
84
+ }
85
+
86
+ `,
87
+
88
+ fragmentShader: /* glsl */`
89
+ #define RAY_OFFSET 1e-5
90
+
91
+ precision highp isampler2D;
92
+ precision highp usampler2D;
93
+ precision highp sampler2DArray;
94
+ #include <common>
95
+ #include <cube_uv_reflection_fragment>
96
+
97
+ ${ shaderStructs }
98
+ ${ shaderIntersectFunction }
99
+ ${ shaderMaterialStructs }
100
+ ${ shaderUtils }
101
+
102
+ uniform BVH bvh;
103
+ uniform int seed;
104
+ uniform float radius;
105
+
106
+ varying vec3 vNorm;
107
+ varying vec3 vPos;
108
+
109
+ #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
110
+
111
+ uniform sampler2D normalMap;
112
+ uniform vec2 normalScale;
113
+ varying vec2 vUv;
114
+ varying vec4 vTan;
115
+
116
+ #endif
117
+
118
+ void main() {
119
+
120
+ rng_initialize( gl_FragCoord.xy, seed );
121
+
122
+ // compute the flat face surface normal
123
+ vec3 fdx = vec3( dFdx( vPos.x ), dFdx( vPos.y ), dFdx( vPos.z ) );
124
+ vec3 fdy = vec3( dFdy( vPos.x ), dFdy( vPos.y ), dFdy( vPos.z ) );
125
+ vec3 faceNormal = normalize( cross( fdx, fdy ) );
126
+
127
+ // find the max component to scale the offset to account for floating point error
128
+ vec3 absPoint = abs( vPos );
129
+ float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
130
+ vec3 normal = vNorm;
131
+
132
+ #if defined(USE_NORMALMAP) && defined(USE_TANGENT)
133
+
134
+ // some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
135
+ // resulting in NaNs and slow path tracing.
136
+ if ( length( vTan.xyz ) > 0.0 ) {
137
+
138
+ vec2 uv = vUv;
139
+ vec3 tangent = normalize( vTan.xyz );
140
+ vec3 bitangent = normalize( cross( normal, tangent ) * vTan.w );
141
+ mat3 vTBN = mat3( tangent, bitangent, normal );
142
+
143
+ vec3 texNormal = texture2D( normalMap, uv ).xyz * 2.0 - 1.0;
144
+ texNormal.xy *= normalScale;
145
+ normal = vTBN * texNormal;
146
+
147
+ }
148
+
149
+ #endif
150
+
151
+ normal *= gl_FrontFacing ? 1.0 : - 1.0;
152
+
153
+ vec3 rayOrigin = vPos + faceNormal * ( maxPoint + 1.0 ) * RAY_OFFSET;
154
+ float accumulated = 0.0;
155
+ for ( int i = 0; i < SAMPLES; i ++ ) {
156
+
157
+ // sample the cosine weighted hemisphere and discard the sample if it's below
158
+ // the geometric surface
159
+ vec3 rayDirection = getHemisphereSample( normalize( normal ), rand4().xy );
160
+
161
+ // check if we hit the mesh and its within the specified radius
162
+ float side = 1.0;
163
+ float dist = 0.0;
164
+ vec3 barycoord = vec3( 0.0 );
165
+ vec3 outNormal = vec3( 0.0 );
166
+ uvec4 faceIndices = uvec4( 0u );
167
+
168
+ // if the ray is above the geometry surface, and it doesn't hit another surface within the specified radius then
169
+ // we consider it lit
170
+ if (
171
+ dot( rayDirection, faceNormal ) > 0.0 &&
172
+ (
173
+ ! bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, outNormal, barycoord, side, dist ) ||
174
+ dist > radius
175
+ )
176
+ ) {
177
+
178
+ accumulated += 1.0;
179
+
180
+ }
181
+
182
+ }
183
+
184
+ gl_FragColor.rgb = vec3( accumulated / float( SAMPLES ) );
185
+ gl_FragColor.a = 1.0;
186
+
187
+ }
188
+
189
+ `
190
+
191
+ } );
192
+
193
+ this.setValues( parameters );
194
+
195
+ }
196
+
197
+ }
@@ -0,0 +1,67 @@
1
+ import { NoBlending } from 'three';
2
+ import { MaterialBase } from './MaterialBase.js';
3
+
4
+ export class BlendMaterial extends MaterialBase {
5
+
6
+ constructor( parameters ) {
7
+
8
+ super( {
9
+
10
+ blending: NoBlending,
11
+
12
+ uniforms: {
13
+
14
+ target1: { value: null },
15
+ target2: { value: null },
16
+ opacity: { value: 1.0 },
17
+
18
+ },
19
+
20
+ vertexShader: /* glsl */`
21
+
22
+ varying vec2 vUv;
23
+
24
+ void main() {
25
+
26
+ vUv = uv;
27
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
28
+
29
+ }`,
30
+
31
+ fragmentShader: /* glsl */`
32
+
33
+ uniform float opacity;
34
+
35
+ uniform sampler2D target1;
36
+ uniform sampler2D target2;
37
+
38
+ varying vec2 vUv;
39
+
40
+ void main() {
41
+
42
+ vec4 color1 = texture2D( target1, vUv );
43
+ vec4 color2 = texture2D( target2, vUv );
44
+
45
+ float invOpacity = 1.0 - opacity;
46
+ float totalAlpha = color1.a * invOpacity + color2.a * opacity;
47
+
48
+ if ( color1.a != 0.0 || color2.a != 0.0 ) {
49
+
50
+ gl_FragColor.rgb = color1.rgb * ( invOpacity * color1.a / totalAlpha ) + color2.rgb * ( opacity * color2.a / totalAlpha );
51
+ gl_FragColor.a = totalAlpha;
52
+
53
+ } else {
54
+
55
+ gl_FragColor = vec4( 0.0 );
56
+
57
+ }
58
+
59
+ }`
60
+
61
+ } );
62
+
63
+ this.setValues( parameters );
64
+
65
+ }
66
+
67
+ }