three-gpu-pathtracer 0.0.2 → 0.0.5
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 +21 -21
- package/README.md +728 -597
- package/build/index.module.js +3585 -1739
- package/build/index.module.js.map +1 -1
- package/build/index.umd.cjs +3589 -1739
- package/build/index.umd.cjs.map +1 -1
- package/package.json +60 -61
- package/src/core/DynamicPathTracingSceneGenerator.js +17 -11
- package/src/core/EquirectCamera.js +13 -0
- package/src/core/MaterialReducer.js +256 -256
- package/src/core/PathTracingRenderer.js +143 -28
- package/src/core/PathTracingSceneGenerator.js +68 -52
- package/src/core/PhysicalCamera.js +28 -28
- package/src/index.js +26 -23
- package/src/materials/AlphaDisplayMaterial.js +48 -0
- package/src/materials/AmbientOcclusionMaterial.js +197 -197
- package/src/materials/BlendMaterial.js +67 -0
- package/src/materials/LambertPathTracingMaterial.js +285 -285
- package/src/materials/MaterialBase.js +56 -56
- package/src/materials/PhysicalPathTracingMaterial.js +848 -420
- package/src/shader/shaderEnvMapSampling.js +59 -0
- package/src/shader/shaderGGXFunctions.js +108 -107
- package/src/shader/shaderLightSampling.js +87 -0
- package/src/shader/shaderMaterialSampling.js +501 -333
- package/src/shader/shaderStructs.js +191 -42
- package/src/shader/shaderUtils.js +287 -191
- package/src/uniforms/EquirectHdrInfoUniform.js +263 -0
- package/src/uniforms/LightsTexture.js +83 -0
- package/src/uniforms/MaterialsTexture.js +319 -0
- package/src/uniforms/PhysicalCameraUniform.js +36 -36
- package/src/uniforms/RenderTarget2DArray.js +93 -80
- package/src/utils/BlurredEnvMapGenerator.js +113 -0
- package/src/utils/GeometryPreparationUtils.js +2 -8
- package/src/utils/UVUnwrapper.js +101 -101
- package/src/workers/PathTracingSceneWorker.js +41 -40
- package/src/uniforms/EquirectPdfUniform.js +0 -132
- package/src/uniforms/MaterialStructArrayUniform.js +0 -18
- package/src/uniforms/MaterialStructUniform.js +0 -114
|
@@ -1,285 +1,285 @@
|
|
|
1
|
-
import { Matrix4, Color } from 'three';
|
|
2
|
-
import { MaterialBase } from './MaterialBase.js';
|
|
3
|
-
import {
|
|
4
|
-
MeshBVHUniformStruct, FloatVertexAttributeTexture, UIntVertexAttributeTexture,
|
|
5
|
-
shaderStructs, shaderIntersectFunction,
|
|
6
|
-
} from 'three-mesh-bvh';
|
|
7
|
-
import { shaderMaterialStructs } from '../shader/shaderStructs.js';
|
|
8
|
-
import { shaderUtils } from '../shader/shaderUtils.js';
|
|
9
|
-
import { MaterialStructArrayUniform } from '../uniforms/MaterialStructArrayUniform.js';
|
|
10
|
-
import { RenderTarget2DArray } from '../uniforms/RenderTarget2DArray.js';
|
|
11
|
-
|
|
12
|
-
export class LambertPathTracingMaterial extends MaterialBase {
|
|
13
|
-
|
|
14
|
-
// three.js relies on this field to add env map functions and defines
|
|
15
|
-
get envMap() {
|
|
16
|
-
|
|
17
|
-
return this.environmentMap;
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
constructor( parameters ) {
|
|
22
|
-
|
|
23
|
-
super( {
|
|
24
|
-
|
|
25
|
-
transparent: true,
|
|
26
|
-
depthWrite: false,
|
|
27
|
-
|
|
28
|
-
defines: {
|
|
29
|
-
BOUNCES: 3,
|
|
30
|
-
MATERIAL_LENGTH: 0,
|
|
31
|
-
GRADIENT_BG: 0,
|
|
32
|
-
DISPLAY_FLOOR: 1,
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
uniforms: {
|
|
36
|
-
bvh: { value: new MeshBVHUniformStruct() },
|
|
37
|
-
normalAttribute: { value: new FloatVertexAttributeTexture() },
|
|
38
|
-
tangentAttribute: { value: new FloatVertexAttributeTexture() },
|
|
39
|
-
uvAttribute: { value: new FloatVertexAttributeTexture() },
|
|
40
|
-
materialIndexAttribute: { value: new UIntVertexAttributeTexture() },
|
|
41
|
-
materials: { value: new MaterialStructArrayUniform() },
|
|
42
|
-
textures: { value: new RenderTarget2DArray().texture },
|
|
43
|
-
cameraWorldMatrix: { value: new Matrix4() },
|
|
44
|
-
invProjectionMatrix: { value: new Matrix4() },
|
|
45
|
-
environmentBlur: { value: 0.2 },
|
|
46
|
-
environmentIntensity: { value: 2.0 },
|
|
47
|
-
environmentMap: { value: null },
|
|
48
|
-
seed: { value: 0 },
|
|
49
|
-
opacity: { value: 1 },
|
|
50
|
-
|
|
51
|
-
gradientTop: { value: new Color( 0xbfd8ff ) },
|
|
52
|
-
gradientBottom: { value: new Color( 0xffffff ) },
|
|
53
|
-
|
|
54
|
-
bgGradientTop: { value: new Color( 0x111111 ) },
|
|
55
|
-
bgGradientBottom: { value: new Color( 0x000000 ) },
|
|
56
|
-
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
vertexShader: /* glsl */`
|
|
60
|
-
|
|
61
|
-
varying vec2 vUv;
|
|
62
|
-
void main() {
|
|
63
|
-
|
|
64
|
-
vec4 mvPosition = vec4( position, 1.0 );
|
|
65
|
-
mvPosition = modelViewMatrix * mvPosition;
|
|
66
|
-
gl_Position = projectionMatrix * mvPosition;
|
|
67
|
-
|
|
68
|
-
vUv = uv;
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
`,
|
|
73
|
-
|
|
74
|
-
fragmentShader: /* glsl */`
|
|
75
|
-
#define RAY_OFFSET 1e-5
|
|
76
|
-
|
|
77
|
-
precision highp isampler2D;
|
|
78
|
-
precision highp usampler2D;
|
|
79
|
-
precision highp sampler2DArray;
|
|
80
|
-
vec4 envMapTexelToLinear( vec4 a ) { return a; }
|
|
81
|
-
#include <common>
|
|
82
|
-
#include <cube_uv_reflection_fragment>
|
|
83
|
-
|
|
84
|
-
${ shaderStructs }
|
|
85
|
-
${ shaderIntersectFunction }
|
|
86
|
-
${ shaderMaterialStructs }
|
|
87
|
-
${ shaderUtils }
|
|
88
|
-
|
|
89
|
-
#ifdef USE_ENVMAP
|
|
90
|
-
|
|
91
|
-
uniform float environmentBlur;
|
|
92
|
-
uniform sampler2D environmentMap;
|
|
93
|
-
|
|
94
|
-
#else
|
|
95
|
-
|
|
96
|
-
uniform vec3 gradientTop;
|
|
97
|
-
uniform vec3 gradientBottom;
|
|
98
|
-
|
|
99
|
-
#endif
|
|
100
|
-
|
|
101
|
-
#if GRADIENT_BG
|
|
102
|
-
|
|
103
|
-
uniform vec3 bgGradientTop;
|
|
104
|
-
uniform vec3 bgGradientBottom;
|
|
105
|
-
|
|
106
|
-
#endif
|
|
107
|
-
|
|
108
|
-
uniform mat4 cameraWorldMatrix;
|
|
109
|
-
uniform mat4 invProjectionMatrix;
|
|
110
|
-
uniform sampler2D normalAttribute;
|
|
111
|
-
uniform sampler2D tangentAttribute;
|
|
112
|
-
uniform sampler2D uvAttribute;
|
|
113
|
-
uniform usampler2D materialIndexAttribute;
|
|
114
|
-
uniform BVH bvh;
|
|
115
|
-
uniform float environmentIntensity;
|
|
116
|
-
uniform int seed;
|
|
117
|
-
uniform float opacity;
|
|
118
|
-
uniform Material materials[ MATERIAL_LENGTH ];
|
|
119
|
-
uniform sampler2DArray textures;
|
|
120
|
-
varying vec2 vUv;
|
|
121
|
-
|
|
122
|
-
void main() {
|
|
123
|
-
|
|
124
|
-
rng_initialize( gl_FragCoord.xy, seed );
|
|
125
|
-
|
|
126
|
-
// get [-1, 1] normalized device coordinates
|
|
127
|
-
vec2 ndc = 2.0 * vUv - vec2( 1.0 );
|
|
128
|
-
vec3 rayOrigin, rayDirection;
|
|
129
|
-
ndcToCameraRay( ndc, cameraWorldMatrix, invProjectionMatrix, rayOrigin, rayDirection );
|
|
130
|
-
|
|
131
|
-
// Lambertian render
|
|
132
|
-
gl_FragColor = vec4( 0.0 );
|
|
133
|
-
|
|
134
|
-
vec3 throughputColor = vec3( 1.0 );
|
|
135
|
-
|
|
136
|
-
// hit results
|
|
137
|
-
uvec4 faceIndices = uvec4( 0u );
|
|
138
|
-
vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
|
|
139
|
-
vec3 barycoord = vec3( 0.0 );
|
|
140
|
-
float side = 1.0;
|
|
141
|
-
float dist = 0.0;
|
|
142
|
-
int i;
|
|
143
|
-
for ( i = 0; i < BOUNCES; i ++ ) {
|
|
144
|
-
|
|
145
|
-
if ( ! bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist ) ) {
|
|
146
|
-
|
|
147
|
-
#if GRADIENT_BG
|
|
148
|
-
|
|
149
|
-
if ( i == 0 ) {
|
|
150
|
-
|
|
151
|
-
rayDirection = normalize( rayDirection );
|
|
152
|
-
float value = ( rayDirection.y + 1.0 ) / 2.0;
|
|
153
|
-
|
|
154
|
-
value = pow( value, 2.0 );
|
|
155
|
-
|
|
156
|
-
gl_FragColor = vec4( mix( bgGradientBottom, bgGradientTop, value ), 1.0 );
|
|
157
|
-
break;
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
#endif
|
|
162
|
-
|
|
163
|
-
#ifdef USE_ENVMAP
|
|
164
|
-
|
|
165
|
-
vec3 skyColor = textureCubeUV( environmentMap, rayDirection, environmentBlur ).rgb;
|
|
166
|
-
|
|
167
|
-
#else
|
|
168
|
-
|
|
169
|
-
rayDirection = normalize( rayDirection );
|
|
170
|
-
float value = ( rayDirection.y + 1.0 ) / 2.0;
|
|
171
|
-
vec3 skyColor = mix( gradientBottom, gradientTop, value );
|
|
172
|
-
|
|
173
|
-
#endif
|
|
174
|
-
|
|
175
|
-
gl_FragColor += vec4( skyColor * throughputColor * environmentIntensity, 1.0 );
|
|
176
|
-
|
|
177
|
-
break;
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
uint materialIndex = uTexelFetch1D( materialIndexAttribute, faceIndices.x ).r;
|
|
183
|
-
Material material = materials[ materialIndex ];
|
|
184
|
-
|
|
185
|
-
if ( material.opacity < rand() ) {
|
|
186
|
-
|
|
187
|
-
vec3 point = rayOrigin + rayDirection * dist;
|
|
188
|
-
rayOrigin += rayDirection * dist - faceNormal * RAY_OFFSET;
|
|
189
|
-
throughputColor *= mix( vec3( 1.0 ), material.color, 0.5 * material.opacity );
|
|
190
|
-
|
|
191
|
-
i --;
|
|
192
|
-
continue;
|
|
193
|
-
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// fetch the interpolated smooth normal
|
|
197
|
-
vec3 normal = normalize( textureSampleBarycoord(
|
|
198
|
-
normalAttribute,
|
|
199
|
-
barycoord,
|
|
200
|
-
faceIndices.xyz
|
|
201
|
-
).xyz );
|
|
202
|
-
|
|
203
|
-
vec2 uv = textureSampleBarycoord( uvAttribute, barycoord, faceIndices.xyz ).xy;
|
|
204
|
-
|
|
205
|
-
// emission
|
|
206
|
-
vec3 emission = material.emissiveIntensity * material.emissive;
|
|
207
|
-
if ( material.emissiveMap != - 1 ) {
|
|
208
|
-
|
|
209
|
-
emission *= texture2D( textures, vec3( uv, material.emissiveMap ) ).xyz;
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
gl_FragColor.rgb += throughputColor * emission * max( side, 0.0 );
|
|
214
|
-
|
|
215
|
-
// 1 / PI attenuation for physically correct lambert model
|
|
216
|
-
// https://www.rorydriscoll.com/2009/01/25/energy-conservation-in-games/
|
|
217
|
-
throughputColor *= 1.0 / PI;
|
|
218
|
-
|
|
219
|
-
// albedo
|
|
220
|
-
throughputColor *= material.color;
|
|
221
|
-
if ( material.map != - 1 ) {
|
|
222
|
-
|
|
223
|
-
throughputColor *= texture2D( textures, vec3( uv, material.map ) ).xyz;
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// normal
|
|
228
|
-
if ( material.normalMap != - 1 ) {
|
|
229
|
-
|
|
230
|
-
vec4 tangentSample = textureSampleBarycoord(
|
|
231
|
-
tangentAttribute,
|
|
232
|
-
barycoord,
|
|
233
|
-
faceIndices.xyz
|
|
234
|
-
);
|
|
235
|
-
|
|
236
|
-
// some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
|
|
237
|
-
// resulting in NaNs and slow path tracing.
|
|
238
|
-
if ( length( tangentSample.xyz ) > 0.0 ) {
|
|
239
|
-
|
|
240
|
-
vec3 tangent = normalize( tangentSample.xyz );
|
|
241
|
-
vec3 bitangent = normalize( cross( normal, tangent ) * tangentSample.w );
|
|
242
|
-
mat3 vTBN = mat3( tangent, bitangent, normal );
|
|
243
|
-
|
|
244
|
-
vec3 texNormal = texture2D( textures, vec3( uv, material.normalMap ) ).xyz * 2.0 - 1.0;
|
|
245
|
-
texNormal.xy *= material.normalScale;
|
|
246
|
-
normal = vTBN * texNormal;
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
normal *= side;
|
|
253
|
-
|
|
254
|
-
// adjust the hit point by the surface normal by a factor of some offset and the
|
|
255
|
-
// maximum component-wise value of the current point to accommodate floating point
|
|
256
|
-
// error as values increase.
|
|
257
|
-
vec3 point = rayOrigin + rayDirection * dist;
|
|
258
|
-
vec3 absPoint = abs( point );
|
|
259
|
-
float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
|
|
260
|
-
rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * RAY_OFFSET;
|
|
261
|
-
rayDirection = getHemisphereSample( normal, rand2() );
|
|
262
|
-
|
|
263
|
-
// if the surface normal is skewed such that the outgoing vector can wind up underneath
|
|
264
|
-
// the triangle surface then just consider it absorbed.
|
|
265
|
-
if ( dot( rayDirection, faceNormal ) < 0.0 ) {
|
|
266
|
-
|
|
267
|
-
break;
|
|
268
|
-
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
gl_FragColor.a = opacity;
|
|
274
|
-
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
`
|
|
278
|
-
|
|
279
|
-
} );
|
|
280
|
-
|
|
281
|
-
this.setValues( parameters );
|
|
282
|
-
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
}
|
|
1
|
+
import { Matrix4, Color } from 'three';
|
|
2
|
+
import { MaterialBase } from './MaterialBase.js';
|
|
3
|
+
import {
|
|
4
|
+
MeshBVHUniformStruct, FloatVertexAttributeTexture, UIntVertexAttributeTexture,
|
|
5
|
+
shaderStructs, shaderIntersectFunction,
|
|
6
|
+
} from 'three-mesh-bvh';
|
|
7
|
+
import { shaderMaterialStructs } from '../shader/shaderStructs.js';
|
|
8
|
+
import { shaderUtils } from '../shader/shaderUtils.js';
|
|
9
|
+
import { MaterialStructArrayUniform } from '../uniforms/MaterialStructArrayUniform.js';
|
|
10
|
+
import { RenderTarget2DArray } from '../uniforms/RenderTarget2DArray.js';
|
|
11
|
+
|
|
12
|
+
export class LambertPathTracingMaterial extends MaterialBase {
|
|
13
|
+
|
|
14
|
+
// three.js relies on this field to add env map functions and defines
|
|
15
|
+
get envMap() {
|
|
16
|
+
|
|
17
|
+
return this.environmentMap;
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
constructor( parameters ) {
|
|
22
|
+
|
|
23
|
+
super( {
|
|
24
|
+
|
|
25
|
+
transparent: true,
|
|
26
|
+
depthWrite: false,
|
|
27
|
+
|
|
28
|
+
defines: {
|
|
29
|
+
BOUNCES: 3,
|
|
30
|
+
MATERIAL_LENGTH: 0,
|
|
31
|
+
GRADIENT_BG: 0,
|
|
32
|
+
DISPLAY_FLOOR: 1,
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
uniforms: {
|
|
36
|
+
bvh: { value: new MeshBVHUniformStruct() },
|
|
37
|
+
normalAttribute: { value: new FloatVertexAttributeTexture() },
|
|
38
|
+
tangentAttribute: { value: new FloatVertexAttributeTexture() },
|
|
39
|
+
uvAttribute: { value: new FloatVertexAttributeTexture() },
|
|
40
|
+
materialIndexAttribute: { value: new UIntVertexAttributeTexture() },
|
|
41
|
+
materials: { value: new MaterialStructArrayUniform() },
|
|
42
|
+
textures: { value: new RenderTarget2DArray().texture },
|
|
43
|
+
cameraWorldMatrix: { value: new Matrix4() },
|
|
44
|
+
invProjectionMatrix: { value: new Matrix4() },
|
|
45
|
+
environmentBlur: { value: 0.2 },
|
|
46
|
+
environmentIntensity: { value: 2.0 },
|
|
47
|
+
environmentMap: { value: null },
|
|
48
|
+
seed: { value: 0 },
|
|
49
|
+
opacity: { value: 1 },
|
|
50
|
+
|
|
51
|
+
gradientTop: { value: new Color( 0xbfd8ff ) },
|
|
52
|
+
gradientBottom: { value: new Color( 0xffffff ) },
|
|
53
|
+
|
|
54
|
+
bgGradientTop: { value: new Color( 0x111111 ) },
|
|
55
|
+
bgGradientBottom: { value: new Color( 0x000000 ) },
|
|
56
|
+
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
vertexShader: /* glsl */`
|
|
60
|
+
|
|
61
|
+
varying vec2 vUv;
|
|
62
|
+
void main() {
|
|
63
|
+
|
|
64
|
+
vec4 mvPosition = vec4( position, 1.0 );
|
|
65
|
+
mvPosition = modelViewMatrix * mvPosition;
|
|
66
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
67
|
+
|
|
68
|
+
vUv = uv;
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
`,
|
|
73
|
+
|
|
74
|
+
fragmentShader: /* glsl */`
|
|
75
|
+
#define RAY_OFFSET 1e-5
|
|
76
|
+
|
|
77
|
+
precision highp isampler2D;
|
|
78
|
+
precision highp usampler2D;
|
|
79
|
+
precision highp sampler2DArray;
|
|
80
|
+
vec4 envMapTexelToLinear( vec4 a ) { return a; }
|
|
81
|
+
#include <common>
|
|
82
|
+
#include <cube_uv_reflection_fragment>
|
|
83
|
+
|
|
84
|
+
${ shaderStructs }
|
|
85
|
+
${ shaderIntersectFunction }
|
|
86
|
+
${ shaderMaterialStructs }
|
|
87
|
+
${ shaderUtils }
|
|
88
|
+
|
|
89
|
+
#ifdef USE_ENVMAP
|
|
90
|
+
|
|
91
|
+
uniform float environmentBlur;
|
|
92
|
+
uniform sampler2D environmentMap;
|
|
93
|
+
|
|
94
|
+
#else
|
|
95
|
+
|
|
96
|
+
uniform vec3 gradientTop;
|
|
97
|
+
uniform vec3 gradientBottom;
|
|
98
|
+
|
|
99
|
+
#endif
|
|
100
|
+
|
|
101
|
+
#if GRADIENT_BG
|
|
102
|
+
|
|
103
|
+
uniform vec3 bgGradientTop;
|
|
104
|
+
uniform vec3 bgGradientBottom;
|
|
105
|
+
|
|
106
|
+
#endif
|
|
107
|
+
|
|
108
|
+
uniform mat4 cameraWorldMatrix;
|
|
109
|
+
uniform mat4 invProjectionMatrix;
|
|
110
|
+
uniform sampler2D normalAttribute;
|
|
111
|
+
uniform sampler2D tangentAttribute;
|
|
112
|
+
uniform sampler2D uvAttribute;
|
|
113
|
+
uniform usampler2D materialIndexAttribute;
|
|
114
|
+
uniform BVH bvh;
|
|
115
|
+
uniform float environmentIntensity;
|
|
116
|
+
uniform int seed;
|
|
117
|
+
uniform float opacity;
|
|
118
|
+
uniform Material materials[ MATERIAL_LENGTH ];
|
|
119
|
+
uniform sampler2DArray textures;
|
|
120
|
+
varying vec2 vUv;
|
|
121
|
+
|
|
122
|
+
void main() {
|
|
123
|
+
|
|
124
|
+
rng_initialize( gl_FragCoord.xy, seed );
|
|
125
|
+
|
|
126
|
+
// get [-1, 1] normalized device coordinates
|
|
127
|
+
vec2 ndc = 2.0 * vUv - vec2( 1.0 );
|
|
128
|
+
vec3 rayOrigin, rayDirection;
|
|
129
|
+
ndcToCameraRay( ndc, cameraWorldMatrix, invProjectionMatrix, rayOrigin, rayDirection );
|
|
130
|
+
|
|
131
|
+
// Lambertian render
|
|
132
|
+
gl_FragColor = vec4( 0.0 );
|
|
133
|
+
|
|
134
|
+
vec3 throughputColor = vec3( 1.0 );
|
|
135
|
+
|
|
136
|
+
// hit results
|
|
137
|
+
uvec4 faceIndices = uvec4( 0u );
|
|
138
|
+
vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
|
|
139
|
+
vec3 barycoord = vec3( 0.0 );
|
|
140
|
+
float side = 1.0;
|
|
141
|
+
float dist = 0.0;
|
|
142
|
+
int i;
|
|
143
|
+
for ( i = 0; i < BOUNCES; i ++ ) {
|
|
144
|
+
|
|
145
|
+
if ( ! bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist ) ) {
|
|
146
|
+
|
|
147
|
+
#if GRADIENT_BG
|
|
148
|
+
|
|
149
|
+
if ( i == 0 ) {
|
|
150
|
+
|
|
151
|
+
rayDirection = normalize( rayDirection );
|
|
152
|
+
float value = ( rayDirection.y + 1.0 ) / 2.0;
|
|
153
|
+
|
|
154
|
+
value = pow( value, 2.0 );
|
|
155
|
+
|
|
156
|
+
gl_FragColor = vec4( mix( bgGradientBottom, bgGradientTop, value ), 1.0 );
|
|
157
|
+
break;
|
|
158
|
+
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#endif
|
|
162
|
+
|
|
163
|
+
#ifdef USE_ENVMAP
|
|
164
|
+
|
|
165
|
+
vec3 skyColor = textureCubeUV( environmentMap, rayDirection, environmentBlur ).rgb;
|
|
166
|
+
|
|
167
|
+
#else
|
|
168
|
+
|
|
169
|
+
rayDirection = normalize( rayDirection );
|
|
170
|
+
float value = ( rayDirection.y + 1.0 ) / 2.0;
|
|
171
|
+
vec3 skyColor = mix( gradientBottom, gradientTop, value );
|
|
172
|
+
|
|
173
|
+
#endif
|
|
174
|
+
|
|
175
|
+
gl_FragColor += vec4( skyColor * throughputColor * environmentIntensity, 1.0 );
|
|
176
|
+
|
|
177
|
+
break;
|
|
178
|
+
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
uint materialIndex = uTexelFetch1D( materialIndexAttribute, faceIndices.x ).r;
|
|
183
|
+
Material material = materials[ materialIndex ];
|
|
184
|
+
|
|
185
|
+
if ( material.opacity < rand() ) {
|
|
186
|
+
|
|
187
|
+
vec3 point = rayOrigin + rayDirection * dist;
|
|
188
|
+
rayOrigin += rayDirection * dist - faceNormal * RAY_OFFSET;
|
|
189
|
+
throughputColor *= mix( vec3( 1.0 ), material.color, 0.5 * material.opacity );
|
|
190
|
+
|
|
191
|
+
i --;
|
|
192
|
+
continue;
|
|
193
|
+
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// fetch the interpolated smooth normal
|
|
197
|
+
vec3 normal = normalize( textureSampleBarycoord(
|
|
198
|
+
normalAttribute,
|
|
199
|
+
barycoord,
|
|
200
|
+
faceIndices.xyz
|
|
201
|
+
).xyz );
|
|
202
|
+
|
|
203
|
+
vec2 uv = textureSampleBarycoord( uvAttribute, barycoord, faceIndices.xyz ).xy;
|
|
204
|
+
|
|
205
|
+
// emission
|
|
206
|
+
vec3 emission = material.emissiveIntensity * material.emissive;
|
|
207
|
+
if ( material.emissiveMap != - 1 ) {
|
|
208
|
+
|
|
209
|
+
emission *= texture2D( textures, vec3( uv, material.emissiveMap ) ).xyz;
|
|
210
|
+
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
gl_FragColor.rgb += throughputColor * emission * max( side, 0.0 );
|
|
214
|
+
|
|
215
|
+
// 1 / PI attenuation for physically correct lambert model
|
|
216
|
+
// https://www.rorydriscoll.com/2009/01/25/energy-conservation-in-games/
|
|
217
|
+
throughputColor *= 1.0 / PI;
|
|
218
|
+
|
|
219
|
+
// albedo
|
|
220
|
+
throughputColor *= material.color;
|
|
221
|
+
if ( material.map != - 1 ) {
|
|
222
|
+
|
|
223
|
+
throughputColor *= texture2D( textures, vec3( uv, material.map ) ).xyz;
|
|
224
|
+
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// normal
|
|
228
|
+
if ( material.normalMap != - 1 ) {
|
|
229
|
+
|
|
230
|
+
vec4 tangentSample = textureSampleBarycoord(
|
|
231
|
+
tangentAttribute,
|
|
232
|
+
barycoord,
|
|
233
|
+
faceIndices.xyz
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// some provided tangents can be malformed (0, 0, 0) causing the normal to be degenerate
|
|
237
|
+
// resulting in NaNs and slow path tracing.
|
|
238
|
+
if ( length( tangentSample.xyz ) > 0.0 ) {
|
|
239
|
+
|
|
240
|
+
vec3 tangent = normalize( tangentSample.xyz );
|
|
241
|
+
vec3 bitangent = normalize( cross( normal, tangent ) * tangentSample.w );
|
|
242
|
+
mat3 vTBN = mat3( tangent, bitangent, normal );
|
|
243
|
+
|
|
244
|
+
vec3 texNormal = texture2D( textures, vec3( uv, material.normalMap ) ).xyz * 2.0 - 1.0;
|
|
245
|
+
texNormal.xy *= material.normalScale;
|
|
246
|
+
normal = vTBN * texNormal;
|
|
247
|
+
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
normal *= side;
|
|
253
|
+
|
|
254
|
+
// adjust the hit point by the surface normal by a factor of some offset and the
|
|
255
|
+
// maximum component-wise value of the current point to accommodate floating point
|
|
256
|
+
// error as values increase.
|
|
257
|
+
vec3 point = rayOrigin + rayDirection * dist;
|
|
258
|
+
vec3 absPoint = abs( point );
|
|
259
|
+
float maxPoint = max( absPoint.x, max( absPoint.y, absPoint.z ) );
|
|
260
|
+
rayOrigin = point + faceNormal * ( maxPoint + 1.0 ) * RAY_OFFSET;
|
|
261
|
+
rayDirection = getHemisphereSample( normal, rand2() );
|
|
262
|
+
|
|
263
|
+
// if the surface normal is skewed such that the outgoing vector can wind up underneath
|
|
264
|
+
// the triangle surface then just consider it absorbed.
|
|
265
|
+
if ( dot( rayDirection, faceNormal ) < 0.0 ) {
|
|
266
|
+
|
|
267
|
+
break;
|
|
268
|
+
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
gl_FragColor.a = opacity;
|
|
274
|
+
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
`
|
|
278
|
+
|
|
279
|
+
} );
|
|
280
|
+
|
|
281
|
+
this.setValues( parameters );
|
|
282
|
+
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
}
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { ShaderMaterial } from 'three';
|
|
2
|
-
|
|
3
|
-
export class MaterialBase extends ShaderMaterial {
|
|
4
|
-
|
|
5
|
-
constructor( shader ) {
|
|
6
|
-
|
|
7
|
-
super( shader );
|
|
8
|
-
|
|
9
|
-
for ( const key in this.uniforms ) {
|
|
10
|
-
|
|
11
|
-
Object.defineProperty( this, key, {
|
|
12
|
-
|
|
13
|
-
get() {
|
|
14
|
-
|
|
15
|
-
return this.uniforms[ key ].value;
|
|
16
|
-
|
|
17
|
-
},
|
|
18
|
-
|
|
19
|
-
set( v ) {
|
|
20
|
-
|
|
21
|
-
this.uniforms[ key ].value = v;
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
} );
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// sets the given named define value and sets "needsUpdate" to true if it's different
|
|
32
|
-
setDefine( name, value = undefined ) {
|
|
33
|
-
|
|
34
|
-
if ( value === undefined || value === null ) {
|
|
35
|
-
|
|
36
|
-
if ( name in this.defines ) {
|
|
37
|
-
|
|
38
|
-
delete this.defines[ name ];
|
|
39
|
-
this.needsUpdate = true;
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
} else {
|
|
44
|
-
|
|
45
|
-
if ( this.defines[ name ] !== value ) {
|
|
46
|
-
|
|
47
|
-
this.defines[ name ] = value;
|
|
48
|
-
this.needsUpdate = true;
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
}
|
|
1
|
+
import { ShaderMaterial } from 'three';
|
|
2
|
+
|
|
3
|
+
export class MaterialBase extends ShaderMaterial {
|
|
4
|
+
|
|
5
|
+
constructor( shader ) {
|
|
6
|
+
|
|
7
|
+
super( shader );
|
|
8
|
+
|
|
9
|
+
for ( const key in this.uniforms ) {
|
|
10
|
+
|
|
11
|
+
Object.defineProperty( this, key, {
|
|
12
|
+
|
|
13
|
+
get() {
|
|
14
|
+
|
|
15
|
+
return this.uniforms[ key ].value;
|
|
16
|
+
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
set( v ) {
|
|
20
|
+
|
|
21
|
+
this.uniforms[ key ].value = v;
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
} );
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// sets the given named define value and sets "needsUpdate" to true if it's different
|
|
32
|
+
setDefine( name, value = undefined ) {
|
|
33
|
+
|
|
34
|
+
if ( value === undefined || value === null ) {
|
|
35
|
+
|
|
36
|
+
if ( name in this.defines ) {
|
|
37
|
+
|
|
38
|
+
delete this.defines[ name ];
|
|
39
|
+
this.needsUpdate = true;
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
} else {
|
|
44
|
+
|
|
45
|
+
if ( this.defines[ name ] !== value ) {
|
|
46
|
+
|
|
47
|
+
this.defines[ name ] = value;
|
|
48
|
+
this.needsUpdate = true;
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|