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