angular-three-soba 1.9.3 → 1.11.0

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 (123) 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/billboard/billboard.mjs +3 -3
  8. package/esm2020/abstractions/lib/catmull-rom-line/catmull-rom-line.mjs +3 -3
  9. package/esm2020/abstractions/lib/cubic-bezier-line/cubic-bezier-line.mjs +3 -3
  10. package/esm2020/abstractions/lib/edges/edges.mjs +95 -0
  11. package/esm2020/abstractions/lib/gizmo-helper/gizmo-helper.mjs +6 -6
  12. package/esm2020/abstractions/lib/gizmo-helper/gizmo-viewcube/gizmo-viewcube-edge.mjs +3 -3
  13. package/esm2020/abstractions/lib/gizmo-helper/gizmo-viewcube/gizmo-viewcube-face.mjs +6 -6
  14. package/esm2020/abstractions/lib/gizmo-helper/gizmo-viewcube/gizmo-viewcube-inputs.mjs +3 -3
  15. package/esm2020/abstractions/lib/gizmo-helper/gizmo-viewcube/gizmo-viewcube.mjs +3 -3
  16. package/esm2020/abstractions/lib/gizmo-helper/gizmo-viewport/gizmo-viewport-axis.mjs +6 -6
  17. package/esm2020/abstractions/lib/gizmo-helper/gizmo-viewport/gizmo-viewport.mjs +3 -3
  18. package/esm2020/abstractions/lib/line/line-input.mjs +3 -3
  19. package/esm2020/abstractions/lib/line/line.mjs +3 -3
  20. package/esm2020/abstractions/lib/quadratic-bezier-line/quadratic-bezier-line.mjs +3 -3
  21. package/esm2020/abstractions/lib/text/text.mjs +3 -3
  22. package/esm2020/abstractions/lib/text-3d/text-3d.mjs +3 -3
  23. package/esm2020/cameras/index.mjs +2 -1
  24. package/esm2020/cameras/lib/camera/camera-content.mjs +4 -4
  25. package/esm2020/cameras/lib/camera/camera.mjs +3 -3
  26. package/esm2020/cameras/lib/cube-camera/cube-camera.mjs +130 -0
  27. package/esm2020/cameras/lib/orthographic-camera/orthographic-camera.mjs +3 -3
  28. package/esm2020/cameras/lib/perspective-camera/perspective-camera.mjs +3 -3
  29. package/esm2020/controls/lib/orbit-controls/orbit-controls.mjs +3 -3
  30. package/esm2020/loaders/lib/loader/loader.mjs +3 -3
  31. package/esm2020/materials/index.mjs +3 -1
  32. package/esm2020/materials/lib/mesh-distort-material/mesh-distort-material.mjs +3 -3
  33. package/esm2020/materials/lib/mesh-reflector-material/mesh-reflector-material.mjs +3 -3
  34. package/esm2020/materials/lib/mesh-refraction-material/mesh-refraction-material.mjs +157 -0
  35. package/esm2020/materials/lib/mesh-transmission-material/mesh-transmission-material.mjs +240 -0
  36. package/esm2020/materials/lib/mesh-wobble-material/mesh-wobble-material.mjs +3 -3
  37. package/esm2020/misc/lib/bake-shadows/bake-shadows.mjs +3 -3
  38. package/esm2020/misc/lib/fbo/fbo.mjs +5 -4
  39. package/esm2020/performance/lib/adaptive/adaptive-dpr.mjs +3 -3
  40. package/esm2020/performance/lib/adaptive/adaptive-events.mjs +3 -3
  41. package/esm2020/performance/lib/detailed/detailed.mjs +3 -3
  42. package/esm2020/performance/lib/stats/stats.mjs +3 -3
  43. package/esm2020/shaders/index.mjs +6 -1
  44. package/esm2020/shaders/lib/caustics-material/caustics-material.mjs +128 -0
  45. package/esm2020/shaders/lib/caustics-projection-material/caustics-projection-material.mjs +33 -0
  46. package/esm2020/shaders/lib/discard-material/discard-material.mjs +3 -0
  47. package/esm2020/shaders/lib/mesh-refraction-material/mesh-refraction-material.mjs +169 -0
  48. package/esm2020/shaders/lib/mesh-transmission-material/mesh-transmission-material.mjs +268 -0
  49. package/esm2020/staging/index.mjs +2 -1
  50. package/esm2020/staging/lib/accumulative-shadows/accumulative-shadows.mjs +6 -6
  51. package/esm2020/staging/lib/accumulative-shadows/progressive-light-map.mjs +2 -3
  52. package/esm2020/staging/lib/accumulative-shadows/randomized-lights.mjs +6 -6
  53. package/esm2020/staging/lib/bounds/bounds.mjs +3 -3
  54. package/esm2020/staging/lib/camera-shake/camera-shake.mjs +3 -3
  55. package/esm2020/staging/lib/caustics/caustics.mjs +364 -0
  56. package/esm2020/staging/lib/center/center.mjs +3 -3
  57. package/esm2020/staging/lib/cloud/cloud.mjs +3 -3
  58. package/esm2020/staging/lib/contact-shadows/contact-shadows.mjs +3 -3
  59. package/esm2020/staging/lib/environment/environment-cube.mjs +3 -3
  60. package/esm2020/staging/lib/environment/environment-ground.mjs +3 -3
  61. package/esm2020/staging/lib/environment/environment-inputs.mjs +3 -3
  62. package/esm2020/staging/lib/environment/environment-map.mjs +3 -3
  63. package/esm2020/staging/lib/environment/environment-portal.mjs +3 -3
  64. package/esm2020/staging/lib/environment/environment.mjs +6 -6
  65. package/esm2020/staging/lib/float/float.mjs +3 -3
  66. package/esm2020/staging/lib/sky/sky.mjs +3 -3
  67. package/esm2020/staging/lib/sparkles/sparkles.mjs +3 -3
  68. package/esm2020/staging/lib/spot-light/shadow-mesh-input.mjs +3 -3
  69. package/esm2020/staging/lib/spot-light/spot-light-input.mjs +3 -3
  70. package/esm2020/staging/lib/spot-light/spot-light-shadow-no-shader.mjs +3 -3
  71. package/esm2020/staging/lib/spot-light/spot-light-shadow-shader.mjs +3 -3
  72. package/esm2020/staging/lib/spot-light/spot-light-shadow.mjs +3 -3
  73. package/esm2020/staging/lib/spot-light/spot-light.mjs +3 -3
  74. package/esm2020/staging/lib/spot-light/volumetric-mesh.mjs +3 -3
  75. package/esm2020/staging/lib/stage/stage.mjs +6 -6
  76. package/esm2020/staging/lib/stars/stars.mjs +3 -3
  77. package/fesm2015/angular-three-soba-abstractions.mjs +145 -56
  78. package/fesm2015/angular-three-soba-abstractions.mjs.map +1 -1
  79. package/fesm2015/angular-three-soba-cameras.mjs +139 -16
  80. package/fesm2015/angular-three-soba-cameras.mjs.map +1 -1
  81. package/fesm2015/angular-three-soba-controls.mjs +3 -3
  82. package/fesm2015/angular-three-soba-loaders.mjs +3 -3
  83. package/fesm2015/angular-three-soba-materials.mjs +398 -11
  84. package/fesm2015/angular-three-soba-materials.mjs.map +1 -1
  85. package/fesm2015/angular-three-soba-misc.mjs +7 -6
  86. package/fesm2015/angular-three-soba-misc.mjs.map +1 -1
  87. package/fesm2015/angular-three-soba-performance.mjs +12 -12
  88. package/fesm2015/angular-three-soba-shaders.mjs +619 -31
  89. package/fesm2015/angular-three-soba-shaders.mjs.map +1 -1
  90. package/fesm2015/angular-three-soba-staging.mjs +450 -96
  91. package/fesm2015/angular-three-soba-staging.mjs.map +1 -1
  92. package/fesm2020/angular-three-soba-abstractions.mjs +145 -56
  93. package/fesm2020/angular-three-soba-abstractions.mjs.map +1 -1
  94. package/fesm2020/angular-three-soba-cameras.mjs +139 -16
  95. package/fesm2020/angular-three-soba-cameras.mjs.map +1 -1
  96. package/fesm2020/angular-three-soba-controls.mjs +3 -3
  97. package/fesm2020/angular-three-soba-loaders.mjs +3 -3
  98. package/fesm2020/angular-three-soba-materials.mjs +396 -11
  99. package/fesm2020/angular-three-soba-materials.mjs.map +1 -1
  100. package/fesm2020/angular-three-soba-misc.mjs +7 -6
  101. package/fesm2020/angular-three-soba-misc.mjs.map +1 -1
  102. package/fesm2020/angular-three-soba-performance.mjs +12 -12
  103. package/fesm2020/angular-three-soba-shaders.mjs +625 -34
  104. package/fesm2020/angular-three-soba-shaders.mjs.map +1 -1
  105. package/fesm2020/angular-three-soba-staging.mjs +449 -96
  106. package/fesm2020/angular-three-soba-staging.mjs.map +1 -1
  107. package/materials/index.d.ts +2 -0
  108. package/materials/lib/mesh-refraction-material/mesh-refraction-material.d.ts +28 -0
  109. package/materials/lib/mesh-transmission-material/mesh-transmission-material.d.ts +47 -0
  110. package/misc/lib/fbo/fbo.d.ts +8 -6
  111. package/package.json +4 -3
  112. package/plugin/package.json +1 -1
  113. package/shaders/index.d.ts +5 -0
  114. package/shaders/lib/caustics-material/caustics-material.d.ts +4 -0
  115. package/shaders/lib/caustics-projection-material/caustics-projection-material.d.ts +4 -0
  116. package/shaders/lib/discard-material/discard-material.d.ts +3 -0
  117. package/shaders/lib/mesh-refraction-material/mesh-refraction-material.d.ts +4 -0
  118. package/shaders/lib/mesh-transmission-material/mesh-transmission-material.d.ts +25 -0
  119. package/staging/index.d.ts +1 -0
  120. package/staging/lib/caustics/caustics.d.ts +48 -0
  121. package/staging/lib/environment/environment-cube.d.ts +1 -1
  122. package/staging/lib/environment/environment-ground.d.ts +1 -1
  123. package/staging/lib/environment/utils.d.ts +1 -1
@@ -1,6 +1,7 @@
1
1
  import * as THREE from 'three';
2
2
  import { ShaderMaterial, Uniform, Vector2, NoBlending, WebGLRenderTarget, LinearFilter, Scene, Camera, BufferGeometry, BufferAttribute, Mesh, MeshStandardMaterial, Vector3, Color } from 'three';
3
3
  import { InjectionToken } from '@angular/core';
4
+ import { MeshBVHUniformStruct, shaderStructs, shaderIntersectFunction } from 'three-mesh-bvh';
4
5
 
5
6
  class ConvolutionMaterial extends ShaderMaterial {
6
7
  constructor(texelSize = new Vector2()) {
@@ -154,6 +155,195 @@ class BlurPass {
154
155
  }
155
156
  }
156
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
+
157
347
  const NGTS_DISTORT_MATERIAL_SHADER = new InjectionToken('DistortMaterialShader');
158
348
  function provideNgtsMeshDistortMaterialShader(distortShader) {
159
349
  return {
@@ -430,6 +620,434 @@ class MeshReflectorMaterial extends MeshStandardMaterial {
430
620
  }
431
621
  }
432
622
 
623
+ // Author: N8Programs
624
+ const MeshRefractionMaterial = shaderMaterial({
625
+ envMap: null,
626
+ bounces: 3,
627
+ ior: 2.4,
628
+ correctMips: true,
629
+ aberrationStrength: 0.01,
630
+ fresnel: 0,
631
+ bvh: new MeshBVHUniformStruct(),
632
+ color: new THREE.Color('white'),
633
+ resolution: new THREE.Vector2(),
634
+ viewMatrixInverse: new THREE.Matrix4(),
635
+ projectionMatrixInverse: new THREE.Matrix4(),
636
+ },
637
+ /*glsl*/ `
638
+ uniform mat4 viewMatrixInverse;
639
+
640
+ varying vec3 vWorldPosition;
641
+ varying vec3 vNormal;
642
+ varying mat4 vModelMatrixInverse;
643
+
644
+ #ifdef USE_INSTANCING_COLOR
645
+ varying vec3 vInstanceColor;
646
+ #endif
647
+
648
+ void main() {
649
+ vec4 transformedNormal = vec4(normal, 0.0);
650
+ vec4 transformedPosition = vec4(position, 1.0);
651
+ #ifdef USE_INSTANCING
652
+ transformedNormal = instanceMatrix * transformedNormal;
653
+ transformedPosition = instanceMatrix * transformedPosition;
654
+ #endif
655
+
656
+ #ifdef USE_INSTANCING
657
+ vModelMatrixInverse = inverse(modelMatrix * instanceMatrix);
658
+ #else
659
+ vModelMatrixInverse = inverse(modelMatrix);
660
+ #endif
661
+
662
+ #ifdef USE_INSTANCING_COLOR
663
+ vInstanceColor = instanceColor.rgb;
664
+ #endif
665
+
666
+ vWorldPosition = (modelMatrix * transformedPosition).xyz;
667
+ vNormal = normalize((viewMatrixInverse * vec4(normalMatrix * transformedNormal.xyz, 0.0)).xyz);
668
+ gl_Position = projectionMatrix * viewMatrix * modelMatrix * transformedPosition;
669
+ }`,
670
+ /*glsl*/ `
671
+ #define ENVMAP_TYPE_CUBE_UV
672
+ precision highp isampler2D;
673
+ precision highp usampler2D;
674
+ varying vec3 vWorldPosition;
675
+ varying vec3 vNormal;
676
+ varying mat4 vModelMatrixInverse;
677
+
678
+ #ifdef USE_INSTANCING_COLOR
679
+ varying vec3 vInstanceColor;
680
+ #endif
681
+
682
+ #ifdef ENVMAP_TYPE_CUBEM
683
+ uniform samplerCube envMap;
684
+ #else
685
+ uniform sampler2D envMap;
686
+ #endif
687
+
688
+ uniform float bounces;
689
+ ${shaderStructs}
690
+ ${shaderIntersectFunction}
691
+ uniform BVH bvh;
692
+ uniform float ior;
693
+ uniform bool correctMips;
694
+ uniform vec2 resolution;
695
+ uniform float fresnel;
696
+ uniform mat4 modelMatrix;
697
+ uniform mat4 projectionMatrixInverse;
698
+ uniform mat4 viewMatrixInverse;
699
+ uniform float aberrationStrength;
700
+ uniform vec3 color;
701
+
702
+ float fresnelFunc(vec3 viewDirection, vec3 worldNormal) {
703
+ return pow( 1.0 + dot( viewDirection, worldNormal), 10.0 );
704
+ }
705
+
706
+ vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 normal, float ior, mat4 modelMatrixInverse) {
707
+ vec3 rayOrigin = ro;
708
+ vec3 rayDirection = rd;
709
+ rayDirection = refract(rayDirection, normal, 1.0 / ior);
710
+ rayOrigin = vWorldPosition + rayDirection * 0.001;
711
+ rayOrigin = (modelMatrixInverse * vec4(rayOrigin, 1.0)).xyz;
712
+ rayDirection = normalize((modelMatrixInverse * vec4(rayDirection, 0.0)).xyz);
713
+ for(float i = 0.0; i < bounces; i++) {
714
+ uvec4 faceIndices = uvec4( 0u );
715
+ vec3 faceNormal = vec3( 0.0, 0.0, 1.0 );
716
+ vec3 barycoord = vec3( 0.0 );
717
+ float side = 1.0;
718
+ float dist = 0.0;
719
+ bvhIntersectFirstHit( bvh, rayOrigin, rayDirection, faceIndices, faceNormal, barycoord, side, dist );
720
+ vec3 hitPos = rayOrigin + rayDirection * max(dist - 0.001, 0.0);
721
+ vec3 tempDir = refract(rayDirection, faceNormal, ior);
722
+ if (length(tempDir) != 0.0) {
723
+ rayDirection = tempDir;
724
+ break;
725
+ }
726
+ rayDirection = reflect(rayDirection, faceNormal);
727
+ rayOrigin = hitPos + rayDirection * 0.01;
728
+ }
729
+ rayDirection = normalize((modelMatrix * vec4(rayDirection, 0.0)).xyz);
730
+ return rayDirection;
731
+ }
732
+
733
+ #include <common>
734
+ #include <cube_uv_reflection_fragment>
735
+
736
+ #ifdef ENVMAP_TYPE_CUBEM
737
+ vec4 textureGradient(samplerCube envMap, vec3 rayDirection, vec3 directionCamPerfect) {
738
+ return textureGrad(envMap, rayDirection, dFdx(correctMips ? directionCamPerfect: rayDirection), dFdy(correctMips ? directionCamPerfect: rayDirection));
739
+ }
740
+ #else
741
+ vec4 textureGradient(sampler2D envMap, vec3 rayDirection, vec3 directionCamPerfect) {
742
+ vec2 uvv = equirectUv( rayDirection );
743
+ vec2 smoothUv = equirectUv( directionCamPerfect );
744
+ return textureGrad(envMap, uvv, dFdx(correctMips ? smoothUv : uvv), dFdy(correctMips ? smoothUv : uvv));
745
+ }
746
+ #endif
747
+
748
+ void main() {
749
+ vec2 uv = gl_FragCoord.xy / resolution;
750
+ vec3 directionCamPerfect = (projectionMatrixInverse * vec4(uv * 2.0 - 1.0, 0.0, 1.0)).xyz;
751
+ directionCamPerfect = (viewMatrixInverse * vec4(directionCamPerfect, 0.0)).xyz;
752
+ directionCamPerfect = normalize(directionCamPerfect);
753
+ vec3 normal = vNormal;
754
+ vec3 rayOrigin = cameraPosition;
755
+ vec3 rayDirection = normalize(vWorldPosition - cameraPosition);
756
+ vec3 finalColor;
757
+ #ifdef CHROMATIC_ABERRATIONS
758
+ vec3 rayDirectionG = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
759
+ #ifdef FAST_CHROMA
760
+ vec3 rayDirectionR = normalize(rayDirectionG + 1.0 * vec3(aberrationStrength / 2.0));
761
+ vec3 rayDirectionB = normalize(rayDirectionG - 1.0 * vec3(aberrationStrength / 2.0));
762
+ #else
763
+ vec3 rayDirectionR = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior * (1.0 - aberrationStrength), 1.0), vModelMatrixInverse);
764
+ vec3 rayDirectionB = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior * (1.0 + aberrationStrength), 1.0), vModelMatrixInverse);
765
+ #endif
766
+ float finalColorR = textureGradient(envMap, rayDirectionR, directionCamPerfect).r;
767
+ float finalColorG = textureGradient(envMap, rayDirectionG, directionCamPerfect).g;
768
+ float finalColorB = textureGradient(envMap, rayDirectionB, directionCamPerfect).b;
769
+ finalColor = vec3(finalColorR, finalColorG, finalColorB);
770
+ #else
771
+ rayDirection = totalInternalReflection(rayOrigin, rayDirection, normal, max(ior, 1.0), vModelMatrixInverse);
772
+ finalColor = textureGradient(envMap, rayDirection, directionCamPerfect).rgb;
773
+ #endif
774
+
775
+ finalColor *= color;
776
+ #ifdef USE_INSTANCING_COLOR
777
+ finalColor *= vInstanceColor;
778
+ #endif
779
+
780
+ vec3 viewDirection = normalize(vWorldPosition - cameraPosition);
781
+ float nFresnel = fresnelFunc(viewDirection, normal) * fresnel;
782
+ gl_FragColor = vec4(mix(finalColor, vec3(1.0), nFresnel), 1.0);
783
+ #include <tonemapping_fragment>
784
+ #include <encodings_fragment>
785
+ }`);
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
+
433
1051
  class MeshWobbleMaterial extends THREE.MeshStandardMaterial {
434
1052
  constructor(parameters = {}) {
435
1053
  super(parameters);
@@ -466,36 +1084,6 @@ class MeshWobbleMaterial extends THREE.MeshStandardMaterial {
466
1084
  }
467
1085
  }
468
1086
 
469
- function shaderMaterial(uniforms, vertexShader, fragmentShader, onInit) {
470
- const material = class extends THREE.ShaderMaterial {
471
- constructor(parameters = {}) {
472
- const entries = Object.entries(uniforms);
473
- // Create unforms and shaders
474
- super({
475
- uniforms: entries.reduce((acc, [name, value]) => {
476
- const uniform = THREE.UniformsUtils.clone({ [name]: { value } });
477
- return Object.assign(Object.assign({}, acc), uniform);
478
- }, {}),
479
- vertexShader,
480
- fragmentShader,
481
- });
482
- this.key = '';
483
- // Create getter/setters
484
- entries.forEach(([name]) => Object.defineProperty(this, name, {
485
- get: () => this.uniforms[name].value,
486
- set: (v) => (this.uniforms[name].value = v),
487
- }));
488
- // Assign parameters, this might include uniforms
489
- Object.assign(this, parameters);
490
- // Call onInit
491
- if (onInit)
492
- onInit(this);
493
- }
494
- };
495
- material.key = THREE.MathUtils.generateUUID();
496
- return material;
497
- }
498
-
499
1087
  class SpotLightMaterial extends ShaderMaterial {
500
1088
  constructor() {
501
1089
  super({
@@ -585,5 +1173,5 @@ class SpotLightMaterial extends ShaderMaterial {
585
1173
  * Generated bundle index. Do not edit.
586
1174
  */
587
1175
 
588
- export { BlurPass, ConvolutionMaterial, MeshReflectorMaterial, MeshWobbleMaterial, NGTS_DISTORT_MATERIAL_SHADER, SpotLightMaterial, provideNgtsMeshDistortMaterialShader, shaderMaterial };
1176
+ export { BlurPass, CausticsMaterial, CausticsProjectionMaterial, ConvolutionMaterial, DiscardMaterial, MeshReflectorMaterial, MeshRefractionMaterial, MeshTransmissionMaterial, MeshWobbleMaterial, NGTS_DISTORT_MATERIAL_SHADER, SpotLightMaterial, provideNgtsMeshDistortMaterialShader, shaderMaterial };
589
1177
  //# sourceMappingURL=angular-three-soba-shaders.mjs.map