@needle-tools/three-mesh-effects 0.3.1 → 0.3.2

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 (50) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +22 -6
  3. package/SKILL.md +7 -5
  4. package/build/box-impostor-runtime.js +212 -87
  5. package/build/box-impostor-runtime.js.map +4 -4
  6. package/build/impostor-runtime.js +112 -46
  7. package/build/impostor-runtime.js.map +4 -4
  8. package/build/index.js +4 -0
  9. package/build/index.js.map +2 -2
  10. package/build/internal/box-impostor/box-impostor-display.js +178 -81
  11. package/build/internal/box-impostor/box-impostor-display.js.map +4 -4
  12. package/build/internal/box-impostor/box-impostor-material.js +163 -71
  13. package/build/internal/box-impostor/box-impostor-material.js.map +4 -4
  14. package/build/internal/box-impostor/box-impostor-projection.js +38 -18
  15. package/build/internal/box-impostor/box-impostor-projection.js.map +4 -4
  16. package/build/internal/box-impostor/box-impostor-runtime.js +193 -76
  17. package/build/internal/box-impostor/box-impostor-runtime.js.map +4 -4
  18. package/build/internal/impostor/impostor-node-material.js +112 -46
  19. package/build/internal/impostor/impostor-node-material.js.map +4 -4
  20. package/build/internal/pixel-impostor/pixel-impostor-display.js +84 -38
  21. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +4 -4
  22. package/build/internal/rendering/captured-surface-depth.js +35 -0
  23. package/build/internal/rendering/captured-surface-depth.js.map +7 -0
  24. package/build/internal/rendering/captured-surface-material.js +28 -0
  25. package/build/internal/rendering/captured-surface-material.js.map +7 -0
  26. package/build/internal/rendering/renderer-support.js +31 -0
  27. package/build/internal/rendering/renderer-support.js.map +7 -0
  28. package/build/pixel-impostor-runtime.js +84 -38
  29. package/build/pixel-impostor-runtime.js.map +4 -4
  30. package/build/types/box-impostor/box-impostor-display.d.ts +9 -1
  31. package/build/types/box-impostor/box-impostor-material.d.ts +4 -0
  32. package/build/types/box-impostor/box-impostor-runtime.d.ts +5 -11
  33. package/build/types/impostor/impostor-node-material.d.ts +2 -15
  34. package/build/types/rendering/captured-surface-depth.d.ts +5 -0
  35. package/build/types/rendering/captured-surface-depth.js +1 -0
  36. package/build/types/rendering/captured-surface-material.d.ts +8 -0
  37. package/build/types/rendering/captured-surface-material.js +1 -0
  38. package/build/types/rendering/renderer-support.d.ts +27 -0
  39. package/build/types/rendering/renderer-support.js +1 -0
  40. package/package.json +3 -3
  41. package/src/box-impostor/box-impostor-display.js +23 -11
  42. package/src/box-impostor/box-impostor-material.js +94 -44
  43. package/src/box-impostor/box-impostor-projection.js +3 -14
  44. package/src/box-impostor/box-impostor-runtime.js +6 -4
  45. package/src/impostor/impostor-node-material.js +11 -40
  46. package/src/pixel-impostor/pixel-impostor-display.js +5 -13
  47. package/src/public/index.js +5 -0
  48. package/src/rendering/captured-surface-depth.js +55 -0
  49. package/src/rendering/captured-surface-material.js +27 -0
  50. package/src/rendering/renderer-support.js +50 -0
@@ -1,5 +1,35 @@
1
1
  // @ts-nocheck
2
2
 
3
+ // src/rendering/captured-surface-depth.js
4
+ import {
5
+ Fn,
6
+ cameraFar,
7
+ cameraNear,
8
+ cameraProjectionMatrix,
9
+ modelViewMatrix,
10
+ vec4,
11
+ viewZToLogarithmicDepth,
12
+ viewZToOrthographicDepth,
13
+ viewZToPerspectiveDepth,
14
+ viewZToReversedOrthographicDepth,
15
+ viewZToReversedPerspectiveDepth
16
+ } from "three/tsl";
17
+ function capturedSurfaceDepth(position, shadowBias = 0) {
18
+ return Fn(
19
+ (_, builder) => {
20
+ const z = modelViewMatrix.mul(vec4(position, 1)).z.toVar("capturedSurfaceViewZ");
21
+ const reversed = builder.renderer.reversedDepthBuffer === true;
22
+ const perspective = cameraProjectionMatrix.mul(vec4(0, 0, 0, 1)).w.equal(0);
23
+ const perspectiveDepth = builder.renderer.logarithmicDepthBuffer === true ? viewZToLogarithmicDepth(z, cameraNear, cameraFar) : (reversed ? viewZToReversedPerspectiveDepth : viewZToPerspectiveDepth)(z, cameraNear, cameraFar);
24
+ const result = perspective.select(
25
+ perspectiveDepth,
26
+ (reversed ? viewZToReversedOrthographicDepth : viewZToOrthographicDepth)(z, cameraNear, cameraFar)
27
+ );
28
+ return shadowBias && "isShadowPassMaterial" in builder.material && builder.material.isShadowPassMaterial ? result.add(reversed ? -shadowBias : shadowBias).clamp(0, 1) : result;
29
+ }
30
+ )();
31
+ }
32
+
3
33
  // src/box-impostor/box-impostor-material.js
4
34
  import {
5
35
  DataTexture as DataTexture3,
@@ -9,12 +39,12 @@ import {
9
39
  UnsignedIntType,
10
40
  NearestFilter as NearestFilter3,
11
41
  LinearFilter,
12
- MeshStandardNodeMaterial,
13
42
  DoubleSide,
43
+ BackSide,
14
44
  FrontSide
15
45
  } from "three/webgpu";
16
46
  import {
17
- Fn as Fn3,
47
+ Fn as Fn4,
18
48
  If as If3,
19
49
  Loop,
20
50
  Break,
@@ -24,10 +54,10 @@ import {
24
54
  modelWorldMatrixInverse,
25
55
  cameraPosition,
26
56
  cameraWorldMatrix,
27
- cameraProjectionMatrix,
57
+ cameraProjectionMatrix as cameraProjectionMatrix2,
28
58
  vec2 as vec23,
29
59
  vec3 as vec33,
30
- vec4 as vec42,
60
+ vec4 as vec44,
31
61
  float as float3,
32
62
  int as int3,
33
63
  uint,
@@ -41,17 +71,37 @@ import {
41
71
  ivec2 as ivec23,
42
72
  normalize as normalize2,
43
73
  floor,
44
- modelViewMatrix as modelViewMatrix2,
45
- modelWorldMatrix as modelWorldMatrix2,
74
+ fract,
75
+ modelWorldMatrix as modelWorldMatrix3,
46
76
  cameraNear as cameraNear2,
47
- cameraFar as cameraFar2,
48
- viewZToPerspectiveDepth as viewZToPerspectiveDepth2,
49
- viewZToOrthographicDepth as viewZToOrthographicDepth2,
50
77
  transformNormalToView as transformNormalToView3,
51
78
  struct as struct2,
52
79
  uniform
53
80
  } from "three/tsl";
54
81
 
82
+ // src/rendering/captured-surface-material.js
83
+ import { MeshStandardNodeMaterial } from "three/webgpu";
84
+ import { context, modelViewMatrix as modelViewMatrix2, modelWorldMatrix, vec4 as vec42 } from "three/tsl";
85
+ var CapturedSurfaceMaterial = class extends MeshStandardNodeMaterial {
86
+ /** @type {import('three/webgpu').Node<'vec3'> | null} */
87
+ capturedPositionNode = null;
88
+ /**
89
+ * @override
90
+ * @param {import('three/webgpu').NodeBuilder} builder
91
+ */
92
+ setupLighting(builder) {
93
+ const lighting = super.setupLighting(builder);
94
+ if (!this.capturedPositionNode) return lighting;
95
+ const world = modelWorldMatrix.mul(vec42(this.capturedPositionNode, 1)).xyz;
96
+ const view = modelViewMatrix2.mul(vec42(this.capturedPositionNode, 1)).xyz;
97
+ return context(lighting, {
98
+ positionView: view,
99
+ positionWorld: world,
100
+ shadowPositionWorld: world
101
+ });
102
+ }
103
+ };
104
+
55
105
  // src/box-impostor/box-impostor-surface-search.js
56
106
  function refineSurfaceCrossing({ float: float4, If: If4, Loop: Loop2 }, { lo, hi, loValue, hiValue, height, steps, secant }) {
57
107
  const a = lo.toVar(), b = hi.toVar(), fa = loValue.toVar(), fb = secant ? hiValue.toVar() : null;
@@ -71,7 +121,7 @@ function refineSurfaceCrossing({ float: float4, If: If4, Loop: Loop2 }, { lo, hi
71
121
  // src/box-impostor/box-impostor-faces.js
72
122
  import { DataTexture, FloatType, RGBAFormat, NearestFilter } from "three";
73
123
  import {
74
- Fn,
124
+ Fn as Fn2,
75
125
  If,
76
126
  attribute,
77
127
  positionLocal,
@@ -188,7 +238,7 @@ function applyBoxFaces({
188
238
  const base = texture(maps.baseColor, uv()).level(0).toVar("boxFaceColor");
189
239
  const orm = texture(maps.orm, uv()).level(0);
190
240
  const coverage = max(orm.a, 1e-10);
191
- const normal = Fn(() => {
241
+ const normal = Fn2(() => {
192
242
  const packed = textureLoad(
193
243
  maps.normalDepth,
194
244
  ivec2(uv().mul(descriptor.atlasResolution))
@@ -233,11 +283,11 @@ function applyBoxFaces({
233
283
  // src/box-impostor/box-impostor-projection.js
234
284
  import { DataTexture as DataTexture2, FloatType as FloatType2, RGBAFormat as RGBAFormat2, NearestFilter as NearestFilter2 } from "three";
235
285
  import {
236
- Fn as Fn2,
286
+ Fn as Fn3,
237
287
  If as If2,
238
288
  vec2 as vec22,
239
289
  vec3 as vec32,
240
- vec4,
290
+ vec4 as vec43,
241
291
  float as float2,
242
292
  int as int2,
243
293
  ivec2 as ivec22,
@@ -250,12 +300,7 @@ import {
250
300
  textureLoad as textureLoad2,
251
301
  attribute as attribute2,
252
302
  struct,
253
- modelViewMatrix,
254
- modelWorldMatrix,
255
- cameraNear,
256
- cameraFar,
257
- viewZToPerspectiveDepth,
258
- viewZToOrthographicDepth,
303
+ modelWorldMatrix as modelWorldMatrix2,
259
304
  transformNormalToView as transformNormalToView2
260
305
  } from "three/tsl";
261
306
  function applyBoxProjection({
@@ -319,7 +364,7 @@ function applyBoxProjection({
319
364
  right.mul(decoded.x).add(up.mul(decoded.y)).add(n.mul(decoded.z))
320
365
  ).normalize();
321
366
  };
322
- const surface = Fn2(() => {
367
+ const surface = Fn3(() => {
323
368
  const ray = perspective.select(
324
369
  normalize(raster.sub(eye)),
325
370
  viewAxis.negate()
@@ -342,8 +387,8 @@ function applyBoxProjection({
342
387
  const weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
343
388
  const candidate = settings.interpolation === "none" ? null : {
344
389
  uv: vec22(0).toVar(),
345
- base: vec4(0).toVar(),
346
- orm: vec4(0).toVar(),
390
+ base: vec43(0).toVar(),
391
+ orm: vec43(0).toVar(),
347
392
  t: float2(1e20).toVar(),
348
393
  tolerance: float2(0).toVar(),
349
394
  right,
@@ -442,6 +487,7 @@ function applyBoxProjection({
442
487
  positions
443
488
  );
444
489
  })().toVar("boxProjectedSurface");
490
+ material.capturedPositionNode = surface.get("position");
445
491
  material.colorNode = surface.get("color");
446
492
  material.normalNode = transformNormalToView2(surface.get("normal"));
447
493
  material.roughnessNode = surface.get("orm").g;
@@ -451,15 +497,9 @@ function applyBoxProjection({
451
497
  material.maskNode = surface.get("alpha").greaterThanEqual(transparency === "cutout" ? alphaCutoff : float2(1 / 255));
452
498
  material.maskShadowNode = material.maskNode;
453
499
  if (transparency !== "cutout") material.opacityNode = surface.get("alpha");
454
- material.depthNode = Fn2(() => {
455
- const z = modelViewMatrix.mul(vec4(surface.get("position"), 1)).z.toVar("boxProjectionDepthZ");
456
- return perspective.select(
457
- viewZToPerspectiveDepth(z, cameraNear, cameraFar),
458
- viewZToOrthographicDepth(z, cameraNear, cameraFar)
459
- );
460
- })();
461
- material.receivedShadowPositionNode = modelWorldMatrix.mul(
462
- vec4(surface.get("position"), 1)
500
+ material.depthNode = capturedSurfaceDepth(surface.get("position"));
501
+ material.receivedShadowPositionNode = modelWorldMatrix2.mul(
502
+ vec43(surface.get("position"), 1)
463
503
  ).xyz;
464
504
  material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
465
505
  return material;
@@ -467,10 +507,48 @@ function applyBoxProjection({
467
507
 
468
508
  // src/box-impostor/box-impostor-material.js
469
509
  function createBoxImpostorMaterial(descriptor, maps) {
510
+ return createBoxImpostorMaterialVariant(descriptor, maps, { filtering: true });
511
+ }
512
+ function createBoxImpostorMaterialVariant(descriptor, maps, { filtering = false } = {}) {
470
513
  const settings = normalizeBoxImpostorDisplay(descriptor);
471
514
  const alphaCutoff = uniform(settings.alphaCutoff);
472
515
  const transparency = boxTransparency(descriptor, settings);
473
516
  const minimumAlpha = transparency === "cutout" ? alphaCutoff : float3(1 / 255);
517
+ const surfaceCoverage = (ormAlpha, baseAlpha) => filtering && descriptor.alphaMode !== "blend" ? min(ormAlpha, baseAlpha) : ormAlpha;
518
+ const decodeNormal = (encoded) => {
519
+ const packed = encoded.mul(2).sub(1);
520
+ const normal2 = vec33(packed, float3(1).sub(abs3(packed.x)).sub(abs3(packed.y))).toVar();
521
+ If3(normal2.z.lessThan(0), () => {
522
+ normal2.xy.assign(vec23(1).sub(abs3(normal2.yx)).mul(sign3(normal2.xy.add(1e-20))));
523
+ });
524
+ return normal2.normalize();
525
+ };
526
+ const filteredNormal = (uv2, depth, tolerance, nearest, alpha, sampled = texture3(maps.normalDepth, uv2).level(0), ormAlpha = texture3(maps.orm, uv2).level(0).a) => {
527
+ sampled = sampled.toVar();
528
+ const coverage2 = max3(descriptor.alphaMode === "blend" ? ormAlpha : alpha, 1e-10);
529
+ const nearestNormal = decodeNormal(nearest.rg), result = decodeNormal(sampled.rg.div(coverage2)).toVar();
530
+ const sampledDepth = sampled.b.mul(65280).add(sampled.a.mul(255)).div(65535);
531
+ const nearestDepth = nearest.b.mul(65280).add(nearest.a.mul(255)).div(65535);
532
+ If3(nearestNormal.z.lessThan(0).or(dot3(result, nearestNormal).lessThan(0.75)).or(coverage2.lessThan(0.9999)).or(abs3(sampledDepth.sub(nearestDepth)).greaterThan(tolerance)), () => {
533
+ const p = uv2.mul(descriptor.atlasResolution).sub(0.5), lo = floor(p), f = fract(p);
534
+ const normal2 = vec33(0).toVar(), weights = float3(0).toVar();
535
+ for (const [x, y] of [[0, 0], [1, 0], [0, 1], [1, 1]]) {
536
+ const texel = ivec23(lo.add(vec23(x, y)).clamp(0, descriptor.atlasResolution - 1));
537
+ const packed = textureLoad3(maps.normalDepth, texel).toVar();
538
+ const alpha2 = textureLoad3(maps.baseColor, texel).a;
539
+ const coverage3 = descriptor.alphaMode === "blend" ? textureLoad3(maps.orm, texel).a : alpha2;
540
+ const capturedDepth = packed.b.mul(65280).add(packed.a.mul(255)).div(65535);
541
+ const supported = coverage3.greaterThan(1e-4).and(alpha2.greaterThanEqual(minimumAlpha.mul(coverage3))).and(abs3(capturedDepth.sub(depth)).lessThanEqual(tolerance));
542
+ If3(supported, () => {
543
+ const weight = (x ? f.x : float3(1).sub(f.x)).mul(y ? f.y : float3(1).sub(f.y)).mul(coverage3);
544
+ normal2.addAssign(decodeNormal(packed.rg.div(max3(coverage3, 1e-10))).mul(weight));
545
+ weights.addAssign(weight);
546
+ });
547
+ }
548
+ result.assign(weights.greaterThan(1e-10).select(normal2.normalize(), nearestNormal));
549
+ });
550
+ return result;
551
+ };
474
552
  for (const map of Object.values(maps)) {
475
553
  if (map.minFilter !== LinearFilter || map.magFilter !== LinearFilter) {
476
554
  map.minFilter = map.magFilter = LinearFilter;
@@ -478,9 +556,9 @@ function createBoxImpostorMaterial(descriptor, maps) {
478
556
  map.needsUpdate = true;
479
557
  }
480
558
  }
481
- const material = new MeshStandardNodeMaterial({
559
+ const material = new CapturedSurfaceMaterial({
482
560
  name: "Box impostor",
483
- side: descriptor.mode !== "fitted" ? DoubleSide : FrontSide,
561
+ side: descriptor.mode !== "fitted" ? DoubleSide : BackSide,
484
562
  alphaHash: transparency === "dithered",
485
563
  transparent: transparency === "blended",
486
564
  depthWrite: transparency !== "blended",
@@ -514,25 +592,27 @@ function createBoxImpostorMaterial(descriptor, maps) {
514
592
  const ax = attribute3("boxAxisX", "vec3"), ay = attribute3("boxAxisY", "vec3"), az = attribute3("boxAxisZ", "vec3");
515
593
  const toBox = (p) => vec33(dot3(p, ax), dot3(p, ay), dot3(p, az));
516
594
  const fromBox = (p) => ax.mul(p.x).add(ay.mul(p.y)).add(az.mul(p.z));
517
- const eye = modelWorldMatrixInverse.mul(vec42(cameraPosition, 1)).xyz;
518
- const viewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec42(0, 0, 1, 0))).xyz.normalize();
519
- const perspective = cameraProjectionMatrix.element(3).w.equal(0);
595
+ const eye = modelWorldMatrixInverse.mul(vec44(cameraPosition, 1)).xyz;
596
+ const viewAxis = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec44(0, 0, 1, 0))).xyz.normalize();
597
+ const perspective = cameraProjectionMatrix2.element(3).w.equal(0);
520
598
  let proxy = positionLocal2;
521
599
  if (descriptor.mode !== "fitted") {
522
- proxy = Fn3((_, builder) => {
523
- const forward = perspective.select(normalize2(eye.sub(center)), viewAxis);
524
- const right = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec42(0, 1, 0, 0))).xyz.cross(forward).normalize();
600
+ proxy = Fn4((_, builder) => {
601
+ const delta = eye.sub(center);
602
+ const forward = perspective.and(delta.length().greaterThan(1e-6)).select(normalize2(delta), viewAxis);
603
+ const right = modelWorldMatrixInverse.mul(cameraWorldMatrix.mul(vec44(0, 1, 0, 0))).xyz.cross(forward).normalize();
525
604
  const up = forward.cross(right).normalize();
526
605
  const radius = half.length();
527
606
  const distance = dot3(eye.sub(center), forward);
528
- const plane = center.add(forward.mul(radius));
607
+ const planeOffset = perspective.select(min(radius, distance.sub(cameraNear2.mul(2))), radius);
608
+ const plane = center.add(forward.mul(planeOffset));
529
609
  const lo = vec23(1e20).toVar(), hi = vec23(-1e20).toVar();
530
610
  for (const x of [-1, 1])
531
611
  for (const y of [-1, 1])
532
612
  for (const z of [-1, 1]) {
533
613
  const corner2 = fromBox(half.mul(vec33(x, y, z)));
534
614
  const scale = perspective.select(
535
- max3(distance.sub(radius), 1e-5).div(
615
+ max3(distance.sub(planeOffset), 1e-5).div(
536
616
  max3(distance.sub(dot3(corner2, forward)), 1e-5)
537
617
  ),
538
618
  float3(1)
@@ -549,7 +629,8 @@ function createBoxImpostorMaterial(descriptor, maps) {
549
629
  material.castShadowPositionNode = proxy;
550
630
  }
551
631
  const raster = varying(proxy, "boxRasterPosition");
552
- if (boxDepthMode(descriptor, settings) === "projection" && descriptor.mode === "fitted" && settings.interpolation === "none")
632
+ if (boxDepthMode(descriptor, settings) === "projection" && descriptor.mode === "fitted" && settings.interpolation === "none") {
633
+ material.side = FrontSide;
553
634
  return applyBoxFaces({
554
635
  material,
555
636
  descriptor,
@@ -560,6 +641,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
560
641
  toBox,
561
642
  fromBox
562
643
  });
644
+ }
563
645
  if (boxDepthMode(descriptor, settings) === "projection")
564
646
  return applyBoxProjection({
565
647
  material,
@@ -603,7 +685,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
603
685
  { uv: "vec2", position: "vec3", normal: "vec3", valid: "float" },
604
686
  "BoxImpostorHit"
605
687
  );
606
- const raycast = Fn3((_, builder) => {
688
+ const raycast = Fn4((_, builder) => {
607
689
  const ray = perspective.select(
608
690
  normalize2(raster.sub(eye)),
609
691
  viewAxis.negate()
@@ -714,6 +796,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
714
796
  const point2 = o.add(r.mul(hitT)), uv2 = sampleUv(point2);
715
797
  const texel = ivec23(uv2.mul(descriptor.atlasResolution));
716
798
  const nearest = textureLoad3(maps.normalDepth, texel);
799
+ const capturedBase = texture3(maps.baseColor, uv2).level(0);
717
800
  const occupied = textureLoad3(maps.orm, texel).a.greaterThan(0.5);
718
801
  const residual = abs3(
719
802
  dot3(point2, n).sub(float3(1).sub(decode(nearest).mul(2)))
@@ -724,7 +807,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
724
807
  ).div(dot3(abs3(n), half)).mul(4).add(4 / 65535);
725
808
  If3(
726
809
  residual.lessThanEqual(tolerance).and(occupied).and(
727
- texture3(maps.baseColor, uv2).level(0).a.greaterThanEqual(minimumAlpha)
810
+ capturedBase.a.greaterThanEqual(minimumAlpha)
728
811
  ),
729
812
  () => {
730
813
  valid.assign(1);
@@ -732,7 +815,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
732
815
  bestT.assign(hitT);
733
816
  bestUv.assign(uv2);
734
817
  found.assign(1);
735
- const packed = texture3(maps.normalDepth, uv2).level(0).rg.div(max3(texture3(maps.orm, uv2).level(0).a, 1e-10)).mul(2).sub(1);
818
+ const packed = texture3(maps.normalDepth, uv2).level(0).rg.div(max3(surfaceCoverage(texture3(maps.orm, uv2).level(0).a, capturedBase.a), 1e-10)).mul(2).sub(1);
736
819
  const normal2 = vec33(
737
820
  packed.x,
738
821
  packed.y,
@@ -847,8 +930,9 @@ function createBoxImpostorMaterial(descriptor, maps) {
847
930
  );
848
931
  });
849
932
  const hit = raycast().toVar("boxHit");
933
+ hit.toStack();
850
934
  const base = texture3(maps.baseColor, hit.get("uv")).level(0), orm = texture3(maps.orm, hit.get("uv")).level(0);
851
- const coverage = max3(orm.a, 1e-10);
935
+ const coverage = max3(surfaceCoverage(orm.a, base.a), 1e-10);
852
936
  let color = base.div(coverage), surfaceOrm = orm.rgb.div(coverage), emission = texture3(maps.emissive, hit.get("uv")).level(0).rgb.div(coverage), normal = hit.get("normal");
853
937
  if (settings.interpolation !== "none") {
854
938
  const fallbackColor = color, fallbackOrm = surfaceOrm, fallbackEmission = emission, fallbackNormal = normal;
@@ -856,12 +940,12 @@ function createBoxImpostorMaterial(descriptor, maps) {
856
940
  { color: "vec4", orm: "vec3", emission: "vec3", normal: "vec3" },
857
941
  "BoxSurfaceBlend"
858
942
  );
859
- const blended = Fn3(() => {
943
+ const blended = Fn4(() => {
860
944
  const point = toBox(hit.get("position").sub(center)).div(half);
861
945
  const look = toBox(
862
946
  perspective.select(normalize2(eye.sub(hit.get("position"))), viewAxis)
863
947
  );
864
- const colors = vec42(0).toVar(), materials = vec33(0).toVar(), emissions = vec33(0).toVar(), normals = vec33(0).toVar(), weights = float3(0).toVar();
948
+ const colors = vec44(0).toVar(), materials = vec33(0).toVar(), emissions = vec33(0).toVar(), normals = vec33(0).toVar(), weights = float3(0).toVar();
865
949
  for (let axis = 0; axis < 3; axis++) {
866
950
  const positive = look.element(axis).greaterThanEqual(0);
867
951
  const front = BOX_FACE_AXES[axis * 2], back = BOX_FACE_AXES[axis * 2 + 1];
@@ -897,26 +981,38 @@ function createBoxImpostorMaterial(descriptor, maps) {
897
981
  ),
898
982
  () => {
899
983
  const angle = abs3(look.element(axis));
900
- const weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
901
- const coverage2 = max3(sampledOrm.a, 1e-10);
984
+ const filtered = filtering ? texture3(maps.normalDepth, uv2).level(0).toVar() : captured;
985
+ let weight = settings.interpolation === "smooth" ? angle.mul(angle) : angle;
986
+ if (filtering) {
987
+ const error = abs3(dot3(point, n).sub(float3(1).sub(
988
+ filtered.b.mul(65280).add(filtered.a.mul(255)).div(65535).mul(2)
989
+ )));
990
+ weight = weight.mul(float3(1).sub(error.div(tolerance)).clamp(0, 1));
991
+ }
992
+ const coverage2 = max3(surfaceCoverage(sampledOrm.a, sampled.a), 1e-10);
902
993
  colors.addAssign(sampled.div(coverage2).mul(weight));
903
994
  materials.addAssign(sampledOrm.rgb.div(coverage2).mul(weight));
904
995
  emissions.addAssign(
905
996
  texture3(maps.emissive, uv2).level(0).rgb.div(coverage2).mul(weight)
906
997
  );
907
- const packed = captured.rg.mul(2).sub(1);
908
- const decoded = vec33(
909
- packed,
910
- float3(1).sub(abs3(packed.x)).sub(abs3(packed.y))
911
- ).toVar();
912
- If3(decoded.z.lessThan(0), () => {
913
- decoded.xy.assign(
914
- vec23(1).sub(abs3(decoded.yx)).mul(sign3(decoded.xy.add(1e-20)))
915
- );
916
- });
998
+ let shadingNormal;
999
+ if (filtering) shadingNormal = filteredNormal(uv2, float3(1).sub(dot3(point, n)).mul(0.5), tolerance.mul(0.5), captured, sampled.a, filtered, sampledOrm.a);
1000
+ else {
1001
+ const packed = captured.rg.mul(2).sub(1);
1002
+ const decoded = vec33(
1003
+ packed,
1004
+ float3(1).sub(abs3(packed.x)).sub(abs3(packed.y))
1005
+ ).toVar();
1006
+ If3(decoded.z.lessThan(0), () => {
1007
+ decoded.xy.assign(
1008
+ vec23(1).sub(abs3(decoded.yx)).mul(sign3(decoded.xy.add(1e-20)))
1009
+ );
1010
+ });
1011
+ shadingNormal = decoded;
1012
+ }
917
1013
  normals.addAssign(
918
1014
  fromBox(
919
- right.mul(decoded.x).add(up.mul(decoded.y)).add(n.mul(decoded.z))
1015
+ right.mul(shadingNormal.x).add(up.mul(shadingNormal.y)).add(n.mul(shadingNormal.z))
920
1016
  ).normalize().mul(weight)
921
1017
  );
922
1018
  weights.addAssign(weight);
@@ -936,6 +1032,7 @@ function createBoxImpostorMaterial(descriptor, maps) {
936
1032
  emission = blended.get("emission");
937
1033
  normal = blended.get("normal");
938
1034
  }
1035
+ material.capturedPositionNode = hit.get("position");
939
1036
  material.colorNode = color.rgb;
940
1037
  material.normalNode = transformNormalToView3(normal);
941
1038
  material.roughnessNode = surfaceOrm.g;
@@ -945,20 +1042,15 @@ function createBoxImpostorMaterial(descriptor, maps) {
945
1042
  if (transparency !== "cutout") material.opacityNode = color.a.clamp(0, 1);
946
1043
  material.maskNode = hit.get("valid").greaterThan(0.5);
947
1044
  material.maskShadowNode = material.maskNode;
948
- material.depthNode = Fn3((_, builder) => {
949
- const z = modelViewMatrix2.mul(vec42(hit.get("position"), 1)).z.toVar("boxHitDepthZ");
950
- return perspective.select(
951
- viewZToPerspectiveDepth2(z, cameraNear2, cameraFar2),
952
- viewZToOrthographicDepth2(z, cameraNear2, cameraFar2)
953
- );
954
- })();
955
- material.receivedShadowPositionNode = modelWorldMatrix2.mul(
956
- vec42(hit.get("position"), 1)
1045
+ material.depthNode = capturedSurfaceDepth(hit.get("position"));
1046
+ material.receivedShadowPositionNode = modelWorldMatrix3.mul(
1047
+ vec44(hit.get("position"), 1)
957
1048
  ).xyz;
958
1049
  material.userData.boxImpostor = { descriptor, maps, alphaCutoff };
959
1050
  return material;
960
1051
  }
961
1052
  export {
962
- createBoxImpostorMaterial
1053
+ createBoxImpostorMaterial,
1054
+ createBoxImpostorMaterialVariant
963
1055
  };
964
1056
  //# sourceMappingURL=box-impostor-material.js.map