angular-three-soba 1.10.0 → 1.11.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.
Files changed (77) hide show
  1. package/abstractions/index.d.ts +1 -0
  2. package/abstractions/lib/edges/edges.d.ts +18 -0
  3. package/cameras/index.d.ts +1 -0
  4. package/cameras/lib/camera/camera-content.d.ts +1 -1
  5. package/cameras/lib/cube-camera/cube-camera.d.ts +27 -0
  6. package/esm2020/abstractions/index.mjs +2 -1
  7. package/esm2020/abstractions/lib/edges/edges.mjs +95 -0
  8. package/esm2020/abstractions/lib/gizmo-helper/gizmo-helper.mjs +8 -3
  9. package/esm2020/abstractions/lib/text-3d/text-3d.mjs +1 -1
  10. package/esm2020/cameras/index.mjs +2 -1
  11. package/esm2020/cameras/lib/camera/camera-content.mjs +1 -1
  12. package/esm2020/cameras/lib/cube-camera/cube-camera.mjs +130 -0
  13. package/esm2020/materials/index.mjs +3 -1
  14. package/esm2020/materials/lib/mesh-refraction-material/mesh-refraction-material.mjs +157 -0
  15. package/esm2020/materials/lib/mesh-transmission-material/mesh-transmission-material.mjs +237 -0
  16. package/esm2020/misc/lib/fbo/fbo.mjs +5 -4
  17. package/esm2020/performance/lib/detailed/detailed.mjs +6 -3
  18. package/esm2020/shaders/index.mjs +6 -1
  19. package/esm2020/shaders/lib/caustics-material/caustics-material.mjs +128 -0
  20. package/esm2020/shaders/lib/caustics-projection-material/caustics-projection-material.mjs +33 -0
  21. package/esm2020/shaders/lib/discard-material/discard-material.mjs +3 -0
  22. package/esm2020/shaders/lib/mesh-transmission-material/mesh-transmission-material.mjs +268 -0
  23. package/esm2020/shaders/lib/soft-shadow-material/soft-shadow-material.mjs +33 -0
  24. package/esm2020/staging/index.mjs +2 -1
  25. package/esm2020/staging/lib/accumulative-shadows/accumulative-shadows.mjs +7 -38
  26. package/esm2020/staging/lib/accumulative-shadows/progressive-light-map.mjs +2 -3
  27. package/esm2020/staging/lib/accumulative-shadows/randomized-lights.mjs +6 -6
  28. package/esm2020/staging/lib/caustics/caustics.mjs +363 -0
  29. package/esm2020/staging/lib/center/center.mjs +6 -6
  30. package/fesm2015/angular-three-soba-abstractions.mjs +98 -4
  31. package/fesm2015/angular-three-soba-abstractions.mjs.map +1 -1
  32. package/fesm2015/angular-three-soba-cameras.mjs +127 -4
  33. package/fesm2015/angular-three-soba-cameras.mjs.map +1 -1
  34. package/fesm2015/angular-three-soba-materials.mjs +386 -2
  35. package/fesm2015/angular-three-soba-materials.mjs.map +1 -1
  36. package/fesm2015/angular-three-soba-misc.mjs +4 -3
  37. package/fesm2015/angular-three-soba-misc.mjs.map +1 -1
  38. package/fesm2015/angular-three-soba-performance.mjs +5 -2
  39. package/fesm2015/angular-three-soba-performance.mjs.map +1 -1
  40. package/fesm2015/angular-three-soba-shaders.mjs +485 -31
  41. package/fesm2015/angular-three-soba-shaders.mjs.map +1 -1
  42. package/fesm2015/angular-three-soba-staging.mjs +376 -53
  43. package/fesm2015/angular-three-soba-staging.mjs.map +1 -1
  44. package/fesm2020/angular-three-soba-abstractions.mjs +98 -4
  45. package/fesm2020/angular-three-soba-abstractions.mjs.map +1 -1
  46. package/fesm2020/angular-three-soba-cameras.mjs +127 -4
  47. package/fesm2020/angular-three-soba-cameras.mjs.map +1 -1
  48. package/fesm2020/angular-three-soba-materials.mjs +384 -2
  49. package/fesm2020/angular-three-soba-materials.mjs.map +1 -1
  50. package/fesm2020/angular-three-soba-misc.mjs +4 -3
  51. package/fesm2020/angular-three-soba-misc.mjs.map +1 -1
  52. package/fesm2020/angular-three-soba-performance.mjs +5 -2
  53. package/fesm2020/angular-three-soba-performance.mjs.map +1 -1
  54. package/fesm2020/angular-three-soba-shaders.mjs +491 -34
  55. package/fesm2020/angular-three-soba-shaders.mjs.map +1 -1
  56. package/fesm2020/angular-three-soba-staging.mjs +375 -53
  57. package/fesm2020/angular-three-soba-staging.mjs.map +1 -1
  58. package/materials/index.d.ts +2 -0
  59. package/materials/lib/mesh-refraction-material/mesh-refraction-material.d.ts +28 -0
  60. package/materials/lib/mesh-transmission-material/mesh-transmission-material.d.ts +46 -0
  61. package/misc/lib/fbo/fbo.d.ts +8 -6
  62. package/package.json +3 -4
  63. package/performance/lib/detailed/detailed.d.ts +2 -1
  64. package/plugin/package.json +1 -1
  65. package/shaders/index.d.ts +5 -0
  66. package/shaders/lib/caustics-material/caustics-material.d.ts +4 -0
  67. package/shaders/lib/caustics-projection-material/caustics-projection-material.d.ts +4 -0
  68. package/shaders/lib/discard-material/discard-material.d.ts +3 -0
  69. package/shaders/lib/mesh-transmission-material/mesh-transmission-material.d.ts +25 -0
  70. package/shaders/lib/soft-shadow-material/soft-shadow-material.d.ts +10 -0
  71. package/staging/index.d.ts +1 -0
  72. package/staging/lib/accumulative-shadows/accumulative-shadows.d.ts +2 -8
  73. package/staging/lib/accumulative-shadows/randomized-lights.d.ts +1 -1
  74. package/staging/lib/caustics/caustics.d.ts +47 -0
  75. package/staging/lib/environment/environment-cube.d.ts +1 -1
  76. package/staging/lib/environment/environment-ground.d.ts +1 -1
  77. package/staging/lib/environment/utils.d.ts +1 -1
@@ -155,6 +155,195 @@ class BlurPass {
155
155
  }
156
156
  }
157
157
 
158
+ function shaderMaterial(uniforms, vertexShader, fragmentShader, onInit) {
159
+ const material = class extends THREE.ShaderMaterial {
160
+ constructor(parameters = {}) {
161
+ const entries = Object.entries(uniforms);
162
+ // Create unforms and shaders
163
+ super({
164
+ uniforms: entries.reduce((acc, [name, value]) => {
165
+ const uniform = THREE.UniformsUtils.clone({ [name]: { value } });
166
+ return Object.assign(Object.assign({}, acc), uniform);
167
+ }, {}),
168
+ vertexShader,
169
+ fragmentShader,
170
+ });
171
+ this.key = '';
172
+ // Create getter/setters
173
+ entries.forEach(([name]) => Object.defineProperty(this, name, {
174
+ get: () => this.uniforms[name].value,
175
+ set: (v) => (this.uniforms[name].value = v),
176
+ }));
177
+ // Assign parameters, this might include uniforms
178
+ Object.assign(this, parameters);
179
+ // Call onInit
180
+ if (onInit)
181
+ onInit(this);
182
+ }
183
+ };
184
+ material.key = THREE.MathUtils.generateUUID();
185
+ return material;
186
+ }
187
+
188
+ const CausticsMaterial = shaderMaterial({
189
+ cameraMatrixWorld: new THREE.Matrix4(),
190
+ cameraProjectionMatrixInv: new THREE.Matrix4(),
191
+ normalTexture: null,
192
+ depthTexture: null,
193
+ lightDir: new THREE.Vector3(0, 1, 0),
194
+ lightPlaneNormal: new THREE.Vector3(0, 1, 0),
195
+ lightPlaneConstant: 0,
196
+ near: 0.1,
197
+ far: 100,
198
+ modelMatrix: new THREE.Matrix4(),
199
+ worldRadius: 1 / 40,
200
+ ior: 1.1,
201
+ bounces: 0,
202
+ resolution: 1024,
203
+ size: 10,
204
+ intensity: 0.5,
205
+ },
206
+ /* glsl */ `varying vec2 vUv;
207
+ void main() {
208
+ vUv = uv;
209
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
210
+ }`,
211
+ /* glsl */ `uniform mat4 cameraMatrixWorld;
212
+ uniform mat4 cameraProjectionMatrixInv;
213
+ uniform vec3 lightDir;
214
+ uniform vec3 lightPlaneNormal;
215
+ uniform float lightPlaneConstant;
216
+ uniform float near;
217
+ uniform float far;
218
+ uniform float time;
219
+ uniform float worldRadius;
220
+ uniform float resolution;
221
+ uniform float size;
222
+ uniform float intensity;
223
+ uniform float ior;
224
+ precision highp isampler2D;
225
+ precision highp usampler2D;
226
+ uniform sampler2D normalTexture;
227
+ uniform sampler2D depthTexture;
228
+ uniform float bounces;
229
+ varying vec2 vUv;
230
+ vec3 WorldPosFromDepth(float depth, vec2 coord) {
231
+ float z = depth * 2.0 - 1.0;
232
+ vec4 clipSpacePosition = vec4(coord * 2.0 - 1.0, z, 1.0);
233
+ vec4 viewSpacePosition = cameraProjectionMatrixInv * clipSpacePosition;
234
+ // Perspective division
235
+ viewSpacePosition /= viewSpacePosition.w;
236
+ vec4 worldSpacePosition = cameraMatrixWorld * viewSpacePosition;
237
+ return worldSpacePosition.xyz;
238
+ }
239
+ float sdPlane( vec3 p, vec3 n, float h ) {
240
+ // n must be normalized
241
+ return dot(p,n) + h;
242
+ }
243
+ float planeIntersect( vec3 ro, vec3 rd, vec4 p ) {
244
+ return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz);
245
+ }
246
+ vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 pos, vec3 normal, float ior, out vec3 rayOrigin, out vec3 rayDirection) {
247
+ rayOrigin = ro;
248
+ rayDirection = rd;
249
+ rayDirection = refract(rayDirection, normal, 1.0 / ior);
250
+ rayOrigin = pos + rayDirection * 0.1;
251
+ return rayDirection;
252
+ }
253
+ void main() {
254
+ // Each sample consists of random offset in the x and y direction
255
+ float caustic = 0.0;
256
+ float causticTexelSize = (1.0 / resolution) * size * 2.0;
257
+ float texelsNeeded = worldRadius / causticTexelSize;
258
+ float sampleRadius = texelsNeeded / resolution;
259
+ float sum = 0.0;
260
+ if (texture2D(depthTexture, vUv).x == 1.0) {
261
+ gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
262
+ return;
263
+ }
264
+ vec2 offset1 = vec2(-0.5, -0.5);//vec2(rand() - 0.5, rand() - 0.5);
265
+ vec2 offset2 = vec2(-0.5, 0.5);//vec2(rand() - 0.5, rand() - 0.5);
266
+ vec2 offset3 = vec2(0.5, 0.5);//vec2(rand() - 0.5, rand() - 0.5);
267
+ vec2 offset4 = vec2(0.5, -0.5);//vec2(rand() - 0.5, rand() - 0.5);
268
+ vec2 uv1 = vUv + offset1 * sampleRadius;
269
+ vec2 uv2 = vUv + offset2 * sampleRadius;
270
+ vec2 uv3 = vUv + offset3 * sampleRadius;
271
+ vec2 uv4 = vUv + offset4 * sampleRadius;
272
+ vec3 normal1 = texture2D(normalTexture, uv1, -10.0).rgb * 2.0 - 1.0;
273
+ vec3 normal2 = texture2D(normalTexture, uv2, -10.0).rgb * 2.0 - 1.0;
274
+ vec3 normal3 = texture2D(normalTexture, uv3, -10.0).rgb * 2.0 - 1.0;
275
+ vec3 normal4 = texture2D(normalTexture, uv4, -10.0).rgb * 2.0 - 1.0;
276
+ float depth1 = texture2D(depthTexture, uv1, -10.0).x;
277
+ float depth2 = texture2D(depthTexture, uv2, -10.0).x;
278
+ float depth3 = texture2D(depthTexture, uv3, -10.0).x;
279
+ float depth4 = texture2D(depthTexture, uv4, -10.0).x;
280
+ // Sanity check the depths
281
+ if (depth1 == 1.0 || depth2 == 1.0 || depth3 == 1.0 || depth4 == 1.0) {
282
+ gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
283
+ return;
284
+ }
285
+ vec3 pos1 = WorldPosFromDepth(depth1, uv1);
286
+ vec3 pos2 = WorldPosFromDepth(depth2, uv2);
287
+ vec3 pos3 = WorldPosFromDepth(depth3, uv3);
288
+ vec3 pos4 = WorldPosFromDepth(depth4, uv4);
289
+ vec3 originPos1 = WorldPosFromDepth(0.0, uv1);
290
+ vec3 originPos2 = WorldPosFromDepth(0.0, uv2);
291
+ vec3 originPos3 = WorldPosFromDepth(0.0, uv3);
292
+ vec3 originPos4 = WorldPosFromDepth(0.0, uv4);
293
+ vec3 endPos1, endPos2, endPos3, endPos4;
294
+ vec3 endDir1, endDir2, endDir3, endDir4;
295
+ totalInternalReflection(originPos1, lightDir, pos1, normal1, ior, endPos1, endDir1);
296
+ totalInternalReflection(originPos2, lightDir, pos2, normal2, ior, endPos2, endDir2);
297
+ totalInternalReflection(originPos3, lightDir, pos3, normal3, ior, endPos3, endDir3);
298
+ totalInternalReflection(originPos4, lightDir, pos4, normal4, ior, endPos4, endDir4);
299
+ float lightPosArea = length(cross(originPos2 - originPos1, originPos3 - originPos1)) + length(cross(originPos3 - originPos1, originPos4 - originPos1));
300
+ float t1 = planeIntersect(endPos1, endDir1, vec4(lightPlaneNormal, lightPlaneConstant));
301
+ float t2 = planeIntersect(endPos2, endDir2, vec4(lightPlaneNormal, lightPlaneConstant));
302
+ float t3 = planeIntersect(endPos3, endDir3, vec4(lightPlaneNormal, lightPlaneConstant));
303
+ float t4 = planeIntersect(endPos4, endDir4, vec4(lightPlaneNormal, lightPlaneConstant));
304
+ vec3 finalPos1 = endPos1 + endDir1 * t1;
305
+ vec3 finalPos2 = endPos2 + endDir2 * t2;
306
+ vec3 finalPos3 = endPos3 + endDir3 * t3;
307
+ vec3 finalPos4 = endPos4 + endDir4 * t4;
308
+ float finalArea = length(cross(finalPos2 - finalPos1, finalPos3 - finalPos1)) + length(cross(finalPos3 - finalPos1, finalPos4 - finalPos1));
309
+ caustic += intensity * (lightPosArea / finalArea);
310
+ // Calculate the area of the triangle in light spaces
311
+ gl_FragColor = vec4(vec3(max(caustic, 0.0)), 1.0);
312
+ }`);
313
+
314
+ const CausticsProjectionMaterial = shaderMaterial({
315
+ causticsTexture: null,
316
+ causticsTextureB: null,
317
+ color: new THREE.Color(),
318
+ lightProjMatrix: new THREE.Matrix4(),
319
+ lightViewMatrix: new THREE.Matrix4(),
320
+ },
321
+ /* glsl */ `varying vec3 vWorldPosition;
322
+ void main() {
323
+ gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.);
324
+ vec4 worldPosition = modelMatrix * vec4(position, 1.);
325
+ vWorldPosition = worldPosition.xyz;
326
+ }`,
327
+ /* glsl */ `varying vec3 vWorldPosition;
328
+ uniform vec3 color;
329
+ uniform sampler2D causticsTexture;
330
+ uniform sampler2D causticsTextureB;
331
+ uniform mat4 lightProjMatrix;
332
+ uniform mat4 lightViewMatrix;
333
+ void main() {
334
+ // Apply caustics
335
+ vec4 lightSpacePos = lightProjMatrix * lightViewMatrix * vec4(vWorldPosition, 1.0);
336
+ lightSpacePos.xyz /= lightSpacePos.w;
337
+ lightSpacePos.xyz = lightSpacePos.xyz * 0.5 + 0.5;
338
+ vec3 front = texture2D(causticsTexture, lightSpacePos.xy).rgb;
339
+ vec3 back = texture2D(causticsTextureB, lightSpacePos.xy).rgb;
340
+ gl_FragColor = vec4((front + back) * color, 1.0);
341
+ #include <tonemapping_fragment>
342
+ #include <encodings_fragment>
343
+ }`);
344
+
345
+ const DiscardMaterial = shaderMaterial({}, 'void main() { }', 'void main() { gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); discard; }');
346
+
158
347
  const NGTS_DISTORT_MATERIAL_SHADER = new InjectionToken('DistortMaterialShader');
159
348
  function provideNgtsMeshDistortMaterialShader(distortShader) {
160
349
  return {
@@ -431,36 +620,6 @@ class MeshReflectorMaterial extends MeshStandardMaterial {
431
620
  }
432
621
  }
433
622
 
434
- function shaderMaterial(uniforms, vertexShader, fragmentShader, onInit) {
435
- const material = class extends THREE.ShaderMaterial {
436
- constructor(parameters = {}) {
437
- const entries = Object.entries(uniforms);
438
- // Create unforms and shaders
439
- super({
440
- uniforms: entries.reduce((acc, [name, value]) => {
441
- const uniform = THREE.UniformsUtils.clone({ [name]: { value } });
442
- return Object.assign(Object.assign({}, acc), uniform);
443
- }, {}),
444
- vertexShader,
445
- fragmentShader,
446
- });
447
- this.key = '';
448
- // Create getter/setters
449
- entries.forEach(([name]) => Object.defineProperty(this, name, {
450
- get: () => this.uniforms[name].value,
451
- set: (v) => (this.uniforms[name].value = v),
452
- }));
453
- // Assign parameters, this might include uniforms
454
- Object.assign(this, parameters);
455
- // Call onInit
456
- if (onInit)
457
- onInit(this);
458
- }
459
- };
460
- material.key = THREE.MathUtils.generateUUID();
461
- return material;
462
- }
463
-
464
623
  // Author: N8Programs
465
624
  const MeshRefractionMaterial = shaderMaterial({
466
625
  envMap: null,
@@ -625,6 +784,270 @@ const MeshRefractionMaterial = shaderMaterial({
625
784
  #include <encodings_fragment>
626
785
  }`);
627
786
 
787
+ class MeshTransmissionMaterial extends THREE.MeshPhysicalMaterial {
788
+ constructor(samples = 6, transmissionSampler = false) {
789
+ super();
790
+ this.uniforms = {
791
+ chromaticAberration: { value: 0.05 },
792
+ // Transmission must always be 0, unless transmissionSampler is being used
793
+ transmission: { value: 0 },
794
+ // Instead a workaround is used, see below for reasons why
795
+ _transmission: { value: 1 },
796
+ transmissionMap: { value: null },
797
+ // Roughness is 1 in THREE.MeshPhysicalMaterial but it makes little sense in a transmission material
798
+ roughness: { value: 0 },
799
+ thickness: { value: 0 },
800
+ thicknessMap: { value: null },
801
+ attenuationDistance: { value: Infinity },
802
+ attenuationColor: { value: new THREE.Color('white') },
803
+ anisotropy: { value: 0.1 },
804
+ time: { value: 0 },
805
+ distortion: { value: 0.0 },
806
+ distortionScale: { value: 0.5 },
807
+ temporalDistortion: { value: 0.0 },
808
+ buffer: { value: null },
809
+ };
810
+ this.onBeforeCompile = (shader) => {
811
+ shader.uniforms = Object.assign(Object.assign({}, shader.uniforms), this.uniforms);
812
+ // If the transmission sampler is active inject a flag
813
+ if (transmissionSampler)
814
+ shader.defines['USE_SAMPLER'] = '';
815
+ // Otherwise we do use use .transmission and must therefore force USE_TRANSMISSION
816
+ // because threejs won't inject it for us
817
+ else
818
+ shader.defines['USE_TRANSMISSION'] = '';
819
+ // Head
820
+ shader.fragmentShader =
821
+ /*glsl*/ `
822
+ uniform float chromaticAberration;
823
+ uniform float anisotropy;
824
+ uniform float time;
825
+ uniform float distortion;
826
+ uniform float distortionScale;
827
+ uniform float temporalDistortion;
828
+ uniform sampler2D buffer;
829
+
830
+ vec3 random3(vec3 c) {
831
+ float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));
832
+ vec3 r;
833
+ r.z = fract(512.0*j);
834
+ j *= .125;
835
+ r.x = fract(512.0*j);
836
+ j *= .125;
837
+ r.y = fract(512.0*j);
838
+ return r-0.5;
839
+ }
840
+
841
+ float seed = 0.0;
842
+ uint hash( uint x ) {
843
+ x += ( x << 10u );
844
+ x ^= ( x >> 6u );
845
+ x += ( x << 3u );
846
+ x ^= ( x >> 11u );
847
+ x += ( x << 15u );
848
+ return x;
849
+ }
850
+
851
+ // Compound versions of the hashing algorithm I whipped together.
852
+ uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y) ); }
853
+ uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ); }
854
+ uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }
855
+
856
+ // Construct a float with half-open range [0:1] using low 23 bits.
857
+ // All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.
858
+ float floatConstruct( uint m ) {
859
+ const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask
860
+ const uint ieeeOne = 0x3F800000u; // 1.0 in IEEE binary32
861
+ m &= ieeeMantissa; // Keep only mantissa bits (fractional part)
862
+ m |= ieeeOne; // Add fractional part to 1.0
863
+ float f = uintBitsToFloat( m ); // Range [1:2]
864
+ return f - 1.0; // Range [0:1]
865
+ }
866
+
867
+ // Pseudo-random value in half-open range [0:1].
868
+ float random( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
869
+ float random( vec2 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
870
+ float random( vec3 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
871
+ float random( vec4 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
872
+
873
+ float rand() {
874
+ float result = random(vec3(gl_FragCoord.xy, seed));
875
+ seed += 1.0;
876
+ return result;
877
+ }
878
+
879
+ const float F3 = 0.3333333;
880
+ const float G3 = 0.1666667;
881
+
882
+ float snoise(vec3 p) {
883
+ vec3 s = floor(p + dot(p, vec3(F3)));
884
+ vec3 x = p - s + dot(s, vec3(G3));
885
+ vec3 e = step(vec3(0.0), x - x.yzx);
886
+ vec3 i1 = e*(1.0 - e.zxy);
887
+ vec3 i2 = 1.0 - e.zxy*(1.0 - e);
888
+ vec3 x1 = x - i1 + G3;
889
+ vec3 x2 = x - i2 + 2.0*G3;
890
+ vec3 x3 = x - 1.0 + 3.0*G3;
891
+ vec4 w, d;
892
+ w.x = dot(x, x);
893
+ w.y = dot(x1, x1);
894
+ w.z = dot(x2, x2);
895
+ w.w = dot(x3, x3);
896
+ w = max(0.6 - w, 0.0);
897
+ d.x = dot(random3(s), x);
898
+ d.y = dot(random3(s + i1), x1);
899
+ d.z = dot(random3(s + i2), x2);
900
+ d.w = dot(random3(s + 1.0), x3);
901
+ w *= w;
902
+ w *= w;
903
+ d *= w;
904
+ return dot(d, vec4(52.0));
905
+ }
906
+
907
+ float snoiseFractal(vec3 m) {
908
+ return 0.5333333* snoise(m)
909
+ +0.2666667* snoise(2.0*m)
910
+ +0.1333333* snoise(4.0*m)
911
+ +0.0666667* snoise(8.0*m);
912
+ }\n` + shader.fragmentShader;
913
+ // Remove transmission
914
+ shader.fragmentShader = shader.fragmentShader.replace('#include <transmission_pars_fragment>',
915
+ /*glsl*/ `
916
+ #ifdef USE_TRANSMISSION
917
+ // Transmission code is based on glTF-Sampler-Viewer
918
+ // https://github.com/KhronosGroup/glTF-Sample-Viewer
919
+ uniform float _transmission;
920
+ uniform float thickness;
921
+ uniform float attenuationDistance;
922
+ uniform vec3 attenuationColor;
923
+ #ifdef USE_TRANSMISSIONMAP
924
+ uniform sampler2D transmissionMap;
925
+ #endif
926
+ #ifdef USE_THICKNESSMAP
927
+ uniform sampler2D thicknessMap;
928
+ #endif
929
+ uniform vec2 transmissionSamplerSize;
930
+ uniform sampler2D transmissionSamplerMap;
931
+ uniform mat4 modelMatrix;
932
+ uniform mat4 projectionMatrix;
933
+ varying vec3 vWorldPosition;
934
+ vec3 getVolumeTransmissionRay( const in vec3 n, const in vec3 v, const in float thickness, const in float ior, const in mat4 modelMatrix ) {
935
+ // Direction of refracted light.
936
+ vec3 refractionVector = refract( - v, normalize( n ), 1.0 / ior );
937
+ // Compute rotation-independant scaling of the model matrix.
938
+ vec3 modelScale;
939
+ modelScale.x = length( vec3( modelMatrix[ 0 ].xyz ) );
940
+ modelScale.y = length( vec3( modelMatrix[ 1 ].xyz ) );
941
+ modelScale.z = length( vec3( modelMatrix[ 2 ].xyz ) );
942
+ // The thickness is specified in local space.
943
+ return normalize( refractionVector ) * thickness * modelScale;
944
+ }
945
+ float applyIorToRoughness( const in float roughness, const in float ior ) {
946
+ // Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and
947
+ // an IOR of 1.5 results in the default amount of microfacet refraction.
948
+ return roughness * clamp( ior * 2.0 - 2.0, 0.0, 1.0 );
949
+ }
950
+ vec4 getTransmissionSample( const in vec2 fragCoord, const in float roughness, const in float ior ) {
951
+ float framebufferLod = log2( transmissionSamplerSize.x ) * applyIorToRoughness( roughness, ior );
952
+ #ifdef USE_SAMPLER
953
+ #ifdef texture2DLodEXT
954
+ return texture2DLodEXT(transmissionSamplerMap, fragCoord.xy, framebufferLod);
955
+ #else
956
+ return texture2D(transmissionSamplerMap, fragCoord.xy, framebufferLod);
957
+ #endif
958
+ #else
959
+ return texture2D(buffer, fragCoord.xy);
960
+ #endif
961
+ }
962
+ vec3 applyVolumeAttenuation( const in vec3 radiance, const in float transmissionDistance, const in vec3 attenuationColor, const in float attenuationDistance ) {
963
+ if ( isinf( attenuationDistance ) ) {
964
+ // Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.
965
+ return radiance;
966
+ } else {
967
+ // Compute light attenuation using Beer's law.
968
+ vec3 attenuationCoefficient = -log( attenuationColor ) / attenuationDistance;
969
+ vec3 transmittance = exp( - attenuationCoefficient * transmissionDistance ); // Beer's law
970
+ return transmittance * radiance;
971
+ }
972
+ }
973
+ vec4 getIBLVolumeRefraction( const in vec3 n, const in vec3 v, const in float roughness, const in vec3 diffuseColor,
974
+ const in vec3 specularColor, const in float specularF90, const in vec3 position, const in mat4 modelMatrix,
975
+ const in mat4 viewMatrix, const in mat4 projMatrix, const in float ior, const in float thickness,
976
+ const in vec3 attenuationColor, const in float attenuationDistance ) {
977
+ vec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
978
+ vec3 refractedRayExit = position + transmissionRay;
979
+ // Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
980
+ vec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 );
981
+ vec2 refractionCoords = ndcPos.xy / ndcPos.w;
982
+ refractionCoords += 1.0;
983
+ refractionCoords /= 2.0;
984
+ // Sample framebuffer to get pixel the refracted ray hits.
985
+ vec4 transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );
986
+ vec3 attenuatedColor = applyVolumeAttenuation( transmittedLight.rgb, length( transmissionRay ), attenuationColor, attenuationDistance );
987
+ // Get the specular component.
988
+ vec3 F = EnvironmentBRDF( n, v, specularColor, specularF90, roughness );
989
+ return vec4( ( 1.0 - F ) * attenuatedColor * diffuseColor, transmittedLight.a );
990
+ }
991
+ #endif\n`);
992
+ // Add refraction
993
+ shader.fragmentShader = shader.fragmentShader.replace('#include <transmission_fragment>',
994
+ /*glsl*/ `
995
+ // Improve the refraction to use the world pos
996
+ material.transmission = _transmission;
997
+ material.transmissionAlpha = 1.0;
998
+ material.thickness = thickness;
999
+ material.attenuationDistance = attenuationDistance;
1000
+ material.attenuationColor = attenuationColor;
1001
+ #ifdef USE_TRANSMISSIONMAP
1002
+ material.transmission *= texture2D( transmissionMap, vUv ).r;
1003
+ #endif
1004
+ #ifdef USE_THICKNESSMAP
1005
+ material.thickness *= texture2D( thicknessMap, vUv ).g;
1006
+ #endif
1007
+
1008
+ vec3 pos = vWorldPosition;
1009
+ vec3 v = normalize( cameraPosition - pos );
1010
+ vec3 n = inverseTransformDirection( normal, viewMatrix );
1011
+ vec3 transmission = vec3(0.0);
1012
+ float transmissionR, transmissionB, transmissionG;
1013
+ float randomCoords = rand();
1014
+ float thickness_smear = thickness * max(pow(roughnessFactor, 0.33), anisotropy);
1015
+ vec3 distortionNormal = vec3(0.0);
1016
+ vec3 temporalOffset = vec3(time, -time, -time) * temporalDistortion;
1017
+ if (distortion > 0.0) {
1018
+ distortionNormal = distortion * vec3(snoiseFractal(vec3((pos * distortionScale + temporalOffset))), snoiseFractal(vec3(pos.zxy * distortionScale - temporalOffset)), snoiseFractal(vec3(pos.yxz * distortionScale + temporalOffset)));
1019
+ }
1020
+ for (float i = 0.0; i < ${samples}.0; i ++) {
1021
+ vec3 sampleNorm = normalize(n + roughnessFactor * roughnessFactor * 2.0 * normalize(vec3(rand() - 0.5, rand() - 0.5, rand() - 0.5)) * pow(rand(), 0.33) + distortionNormal);
1022
+ transmissionR = getIBLVolumeRefraction(
1023
+ sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
1024
+ pos, modelMatrix, viewMatrix, projectionMatrix, material.ior, material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
1025
+ material.attenuationColor, material.attenuationDistance
1026
+ ).r;
1027
+ transmissionG = getIBLVolumeRefraction(
1028
+ sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
1029
+ pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + chromaticAberration * (i + randomCoords) / float(${samples})) , material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
1030
+ material.attenuationColor, material.attenuationDistance
1031
+ ).g;
1032
+ transmissionB = getIBLVolumeRefraction(
1033
+ sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
1034
+ pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + 2.0 * chromaticAberration * (i + randomCoords) / float(${samples})), material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
1035
+ material.attenuationColor, material.attenuationDistance
1036
+ ).b;
1037
+ transmission.r += transmissionR;
1038
+ transmission.g += transmissionG;
1039
+ transmission.b += transmissionB;
1040
+ }
1041
+ transmission /= ${samples}.0;
1042
+ totalDiffuse = mix( totalDiffuse, transmission.rgb, material.transmission );\n`);
1043
+ };
1044
+ Object.keys(this.uniforms).forEach((name) => Object.defineProperty(this, name, {
1045
+ get: () => this.uniforms[name].value,
1046
+ set: (v) => (this.uniforms[name].value = v),
1047
+ }));
1048
+ }
1049
+ }
1050
+
628
1051
  class MeshWobbleMaterial extends THREE.MeshStandardMaterial {
629
1052
  constructor(parameters = {}) {
630
1053
  super(parameters);
@@ -661,6 +1084,37 @@ class MeshWobbleMaterial extends THREE.MeshStandardMaterial {
661
1084
  }
662
1085
  }
663
1086
 
1087
+ const SoftShadowMaterial = shaderMaterial({
1088
+ color: new THREE.Color(),
1089
+ blend: 2.0,
1090
+ alphaTest: 0.75,
1091
+ opacity: 0,
1092
+ map: null,
1093
+ },
1094
+ // language=GLSL
1095
+ `
1096
+ varying vec2 vUv;
1097
+ void main() {
1098
+ gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.);
1099
+ vUv = uv;
1100
+ }
1101
+ `,
1102
+ // language=GLSL
1103
+ `
1104
+ varying vec2 vUv;
1105
+ uniform sampler2D map;
1106
+ uniform vec3 color;
1107
+ uniform float blend;
1108
+ uniform float opacity;
1109
+ uniform float alphaTest;
1110
+ void main() {
1111
+ vec4 sampledDiffuseColor = texture2D(map, vUv);
1112
+ gl_FragColor = vec4(color * sampledDiffuseColor.r * blend, max(0.0, (1.0 - (sampledDiffuseColor.r + sampledDiffuseColor.g + sampledDiffuseColor.b) / alphaTest)) * opacity);
1113
+ #include <tonemapping_fragment>
1114
+ #include <encodings_fragment>
1115
+ }
1116
+ `);
1117
+
664
1118
  class SpotLightMaterial extends ShaderMaterial {
665
1119
  constructor() {
666
1120
  super({
@@ -750,5 +1204,5 @@ class SpotLightMaterial extends ShaderMaterial {
750
1204
  * Generated bundle index. Do not edit.
751
1205
  */
752
1206
 
753
- export { BlurPass, ConvolutionMaterial, MeshReflectorMaterial, MeshRefractionMaterial, MeshWobbleMaterial, NGTS_DISTORT_MATERIAL_SHADER, SpotLightMaterial, provideNgtsMeshDistortMaterialShader, shaderMaterial };
1207
+ export { BlurPass, CausticsMaterial, CausticsProjectionMaterial, ConvolutionMaterial, DiscardMaterial, MeshReflectorMaterial, MeshRefractionMaterial, MeshTransmissionMaterial, MeshWobbleMaterial, NGTS_DISTORT_MATERIAL_SHADER, SoftShadowMaterial, SpotLightMaterial, provideNgtsMeshDistortMaterialShader, shaderMaterial };
754
1208
  //# sourceMappingURL=angular-three-soba-shaders.mjs.map