@needle-tools/three-mesh-effects 0.2.2 → 0.3.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 (100) hide show
  1. package/CHANGELOG.md +28 -6
  2. package/README.md +76 -1
  3. package/SKILL.md +9 -1
  4. package/build/box-impostor-runtime.js +1298 -0
  5. package/build/box-impostor-runtime.js.map +7 -0
  6. package/build/deformation-runtime.js +85 -31
  7. package/build/deformation-runtime.js.map +4 -4
  8. package/build/hull-effects.js +60 -28
  9. package/build/hull-effects.js.map +3 -3
  10. package/build/impostor-runtime.js +235 -116
  11. package/build/impostor-runtime.js.map +4 -4
  12. package/build/index.js +4 -0
  13. package/build/index.js.map +2 -2
  14. package/build/internal/box-impostor/box-impostor-display-settings.js +50 -0
  15. package/build/internal/box-impostor/box-impostor-display-settings.js.map +7 -0
  16. package/build/internal/box-impostor/box-impostor-display.js +1005 -0
  17. package/build/internal/box-impostor/box-impostor-display.js.map +7 -0
  18. package/build/internal/box-impostor/box-impostor-face-axes.js +15 -0
  19. package/build/internal/box-impostor/box-impostor-face-axes.js.map +7 -0
  20. package/build/internal/box-impostor/box-impostor-faces.js +125 -0
  21. package/build/internal/box-impostor/box-impostor-faces.js.map +7 -0
  22. package/build/internal/box-impostor/box-impostor-geometry.js +230 -0
  23. package/build/internal/box-impostor/box-impostor-geometry.js.map +7 -0
  24. package/build/internal/box-impostor/box-impostor-material.js +964 -0
  25. package/build/internal/box-impostor/box-impostor-material.js.map +7 -0
  26. package/build/internal/box-impostor/box-impostor-metadata.js +108 -0
  27. package/build/internal/box-impostor/box-impostor-metadata.js.map +7 -0
  28. package/build/internal/box-impostor/box-impostor-projection.js +256 -0
  29. package/build/internal/box-impostor/box-impostor-projection.js.map +7 -0
  30. package/build/internal/box-impostor/box-impostor-runtime.js +1252 -0
  31. package/build/internal/box-impostor/box-impostor-runtime.js.map +7 -0
  32. package/build/internal/box-impostor/box-impostor-surface-search.js +21 -0
  33. package/build/internal/box-impostor/box-impostor-surface-search.js.map +7 -0
  34. package/build/internal/deformation/cloth-surface.js +23 -17
  35. package/build/internal/deformation/cloth-surface.js.map +3 -3
  36. package/build/internal/deformation/fur-surface.js +55 -12
  37. package/build/internal/deformation/fur-surface.js.map +3 -3
  38. package/build/internal/deformation/pin-cloth.js +7 -2
  39. package/build/internal/deformation/pin-cloth.js.map +2 -2
  40. package/build/internal/deformation/rig-runtime.js +85 -31
  41. package/build/internal/deformation/rig-runtime.js.map +4 -4
  42. package/build/internal/impostor/impostor-node-material.js +235 -116
  43. package/build/internal/impostor/impostor-node-material.js.map +4 -4
  44. package/build/internal/rendering/hull-effect-runtime.js +59 -27
  45. package/build/internal/rendering/hull-effect-runtime.js.map +3 -3
  46. package/build/internal/rendering/texture-slot.js +26 -0
  47. package/build/internal/rendering/texture-slot.js.map +7 -0
  48. package/build/internal/rendering/wireframe.js +41 -9
  49. package/build/internal/rendering/wireframe.js.map +2 -2
  50. package/build/internal/rendering/wrap-preview.js +59 -27
  51. package/build/internal/rendering/wrap-preview.js.map +3 -3
  52. package/build/types/box-impostor/box-impostor-display-settings.d.ts +30 -0
  53. package/build/types/box-impostor/box-impostor-display-settings.js +1 -0
  54. package/build/types/box-impostor/box-impostor-display.d.ts +5 -0
  55. package/build/types/box-impostor/box-impostor-display.js +1 -0
  56. package/build/types/box-impostor/box-impostor-face-axes.d.ts +5 -0
  57. package/build/types/box-impostor/box-impostor-face-axes.js +1 -0
  58. package/build/types/box-impostor/box-impostor-faces.d.ts +11 -0
  59. package/build/types/box-impostor/box-impostor-faces.js +1 -0
  60. package/build/types/box-impostor/box-impostor-geometry.d.ts +5 -0
  61. package/build/types/box-impostor/box-impostor-geometry.js +1 -0
  62. package/build/types/box-impostor/box-impostor-material.d.ts +10 -0
  63. package/build/types/box-impostor/box-impostor-material.js +1 -0
  64. package/build/types/box-impostor/box-impostor-metadata.d.ts +31 -0
  65. package/build/types/box-impostor/box-impostor-metadata.js +1 -0
  66. package/build/types/box-impostor/box-impostor-projection.d.ts +19 -0
  67. package/build/types/box-impostor/box-impostor-projection.js +1 -0
  68. package/build/types/box-impostor/box-impostor-runtime.d.ts +25 -0
  69. package/build/types/box-impostor/box-impostor-runtime.js +1 -0
  70. package/build/types/box-impostor/box-impostor-surface-search.d.ts +13 -0
  71. package/build/types/box-impostor/box-impostor-surface-search.js +1 -0
  72. package/build/types/deformation/cloth-surface.d.ts +1 -0
  73. package/build/types/deformation/pin-cloth.d.ts +1 -0
  74. package/build/types/public/box-impostor-runtime.d.ts +6 -0
  75. package/build/types/public/box-impostor-runtime.js +1 -0
  76. package/build/types/public/hull-effects.d.ts +1 -0
  77. package/build/types/public/index.d.ts +1 -1
  78. package/build/types/rendering/texture-slot.d.ts +10 -0
  79. package/build/types/rendering/texture-slot.js +1 -0
  80. package/build/types/rendering/wireframe.d.ts +2 -2
  81. package/package.json +27 -6
  82. package/src/box-impostor/box-impostor-display-settings.js +54 -0
  83. package/src/box-impostor/box-impostor-display.js +38 -0
  84. package/src/box-impostor/box-impostor-face-axes.js +11 -0
  85. package/src/box-impostor/box-impostor-faces.js +118 -0
  86. package/src/box-impostor/box-impostor-geometry.js +140 -0
  87. package/src/box-impostor/box-impostor-material.js +721 -0
  88. package/src/box-impostor/box-impostor-metadata.js +122 -0
  89. package/src/box-impostor/box-impostor-projection.js +287 -0
  90. package/src/box-impostor/box-impostor-runtime.js +122 -0
  91. package/src/box-impostor/box-impostor-surface-search.js +11 -0
  92. package/src/deformation/cloth-surface.ts +28 -17
  93. package/src/deformation/fur-surface.ts +22 -12
  94. package/src/deformation/pin-cloth.ts +9 -3
  95. package/src/impostor/impostor-node-material.js +238 -150
  96. package/src/public/box-impostor-runtime.js +14 -0
  97. package/src/public/hull-effects.js +1 -0
  98. package/src/public/index.js +5 -1
  99. package/src/rendering/texture-slot.js +27 -0
  100. package/src/rendering/wireframe.js +51 -13
@@ -19,6 +19,7 @@ import {
19
19
  Fn,
20
20
  If,
21
21
  Loop,
22
+ Break,
22
23
  abs,
23
24
  any,
24
25
  bool,
@@ -45,6 +46,7 @@ import {
45
46
  smoothstep,
46
47
  texture,
47
48
  textureLoad,
49
+ textureSize,
48
50
  transformNormalToView,
49
51
  uniformArray,
50
52
  vec2,
@@ -125,6 +127,27 @@ function isBlendZone(value) {
125
127
  value <= 1;
126
128
  }
127
129
 
130
+ // src/rendering/texture-slot.js
131
+ function bindTextureSlot(material, slot, initial, nodes, onChange) {
132
+ let current = initial;
133
+ Object.defineProperty(material, slot, {
134
+ configurable: true,
135
+ enumerable: true,
136
+ get: () => current,
137
+ set: (value) => {
138
+ value.channel = current.channel;
139
+ value.offset = current.offset;
140
+ value.repeat = current.repeat;
141
+ value.center = current.center;
142
+ value.rotation = current.rotation;
143
+ current = value;
144
+ for (const node of nodes) node.value = value;
145
+ onChange?.(value);
146
+ material.needsUpdate = true;
147
+ }
148
+ });
149
+ }
150
+
128
151
  // src/impostor/octahedral.js
129
152
  var PERSPECTIVE_RAY_PLANE_SIGN = -1;
130
153
 
@@ -569,6 +592,21 @@ function createCompatibleImpostorNodeMaterial(descriptor, maps) {
569
592
  float halfTexel = 0.5001 / frameResolution;
570
593
  return (frame + clamp(insideCell, vec2(halfTexel), vec2(1.0 - halfTexel))) / frames;
571
594
  }
595
+ vec4 sampleColorAtlas(vec2 coordinates) {
596
+ vec2 size = vec2(textureSize(baseColorAtlas, 0));
597
+ vec2 grid = vec2(frames);
598
+ #if COMPACT_COLOR && COMPACT_EMISSIVE
599
+ grid.x *= 2.0;
600
+ #endif
601
+ vec2 pixels = coordinates * size;
602
+ float footprint = max(length(dFdx(pixels)), length(dFdy(pixels)));
603
+ float gutter = padding * size.y / (frames * frameResolution);
604
+ float lod = clamp(log2(max(footprint, 1.0)), 0.0, floor(log2(max(gutter, 1.0))));
605
+ vec2 cell = floor(clamp(coordinates * grid, vec2(0.0), grid - 0.00001));
606
+ vec2 guard = vec2(0.5001 * exp2(lod)) / size;
607
+ vec2 safe = clamp(coordinates, cell / grid + guard, (cell + 1.0) / grid - guard);
608
+ return textureLod(baseColorAtlas, safe, lod);
609
+ }
572
610
  void main() {
573
611
  vec3 cameraLocal = (inverse(modelMatrix) * vec4(cameraPosition, 1.0)).xyz;
574
612
  vec3 cameraDirection = normalize(cameraLocal);
@@ -603,7 +641,7 @@ function createCompatibleImpostorNodeMaterial(descriptor, maps) {
603
641
  #if COMPACT_COLOR
604
642
  colorUv.x *= COMPACT_EMISSIVE == 1 ? 0.5 : 1.0;
605
643
  #endif
606
- vec4 base = texture(baseColorAtlas, colorUv);
644
+ vec4 base = sampleColorAtlas(colorUv);
607
645
  vec4 geometry = texture(geometryAtlas, atlas);
608
646
  float contour = base.a;
609
647
  float depthValue;
@@ -676,8 +714,24 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
676
714
  uvNode.x.mul(compactEmissive ? 0.5 : 1).add(emissive && compactEmissive ? 0.5 : 0),
677
715
  uvNode.y
678
716
  ) : uvNode;
679
- const sampleCapturedAlpha = (uvNode) => compressedGeometry ? texture(baseColorAtlas, colorAtlasUv(uvNode)).level(0).a : texture(baseColorAtlas, uvNode).level(0).a;
680
- const sampleContour = (uvNode) => compressedGeometry ? texture(geometryAtlas, uvNode).level(0).a : descriptor.coverageEncoding === "sdf-base-alpha" ? texture(baseColorAtlas, uvNode).level(0).a : texture(ormAtlas, uvNode).level(0).a;
717
+ const mapLod = (atlas, coordinates) => {
718
+ const size = vec2(textureSize(atlas));
719
+ const pixels = coordinates.mul(size);
720
+ const footprint = max(pixels.dFdx().length(), pixels.dFdy().length());
721
+ const gutter = padding.mul(size.y).div(frames.mul(frameResolution));
722
+ return max(footprint, 1).log2().clamp(0, max(gutter, 1).log2().floor());
723
+ };
724
+ const sampleAtlas = (atlas, coordinates, lod = float(0), columns = frames) => {
725
+ const size = vec2(textureSize(atlas));
726
+ const grid = vec2(columns, frames);
727
+ const cell = coordinates.mul(grid).clamp(0, grid.sub(1e-5)).floor();
728
+ const guard = float(0.5001).mul(float(2).pow(lod)).div(size);
729
+ const safe = coordinates.clamp(cell.div(grid).add(guard), cell.add(1).div(grid).sub(guard));
730
+ return texture(atlas, safe).level(lod);
731
+ };
732
+ const colorColumns = frames.mul(compactLayout && compactEmissive ? 2 : 1);
733
+ const sampleCapturedAlpha = (uvNode) => compressedGeometry ? sampleAtlas(baseColorAtlas, colorAtlasUv(uvNode), float(0), colorColumns).a : sampleAtlas(baseColorAtlas, uvNode).a;
734
+ const sampleContour = (uvNode) => compressedGeometry ? texture(geometryAtlas, uvNode).level(0).a : descriptor.coverageEncoding === "sdf-base-alpha" ? sampleAtlas(baseColorAtlas, uvNode).a : sampleAtlas(ormAtlas, uvNode).a;
681
735
  const viewBounds = uniformArray(
682
736
  Array.from({ length: descriptor.frames * descriptor.frames }, (_, index) => new Vector4(
683
737
  descriptor.viewBounds[index * 4],
@@ -688,10 +742,6 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
688
742
  "vec4"
689
743
  );
690
744
  const atlasResolution = float(descriptor.frames * descriptor.frameResolution);
691
- const materialAtlasResolution = float(
692
- descriptor.frames * (descriptor.compression?.materialFrameResolution ?? descriptor.frameResolution)
693
- );
694
- const maximumSafeAtlasLod = float(Math.max(0, Math.floor(Math.log2(Math.max(1, descriptor.padding)))));
695
745
  const FrameBasis = NAMED_STRUCT_FACTORY(
696
746
  { right: "vec3", up: "vec3" },
697
747
  "ImpostorFrameBasis"
@@ -769,22 +819,6 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
769
819
  const halfTexel = float(0.5001).div(frameResolution);
770
820
  return frame.add(insideCell.clamp(halfTexel, float(1).sub(halfTexel))).div(frames);
771
821
  }, "vec2");
772
- const atlasLod = Fn(([uvNode]) => {
773
- const texelCoordinate = uvNode.mul(atlasResolution);
774
- const footprint = max(
775
- texelCoordinate.dFdx().length(),
776
- texelCoordinate.dFdy().length()
777
- );
778
- return max(footprint, 1).log2().clamp(0, maximumSafeAtlasLod);
779
- }, "float");
780
- const materialAtlasLod = Fn(([uvNode]) => {
781
- const texelCoordinate = uvNode.mul(materialAtlasResolution);
782
- const footprint = max(
783
- texelCoordinate.dFdx().length(),
784
- texelCoordinate.dFdy().length()
785
- );
786
- return max(footprint, 1).log2().clamp(0, maximumSafeAtlasLod);
787
- }, "float");
788
822
  const decodeDepth16 = Fn(([packed]) => {
789
823
  if (descriptor.compression?.depthEncoding === "lossless-geometry10-rgba") {
790
824
  const bytes2 = floor(packed.rg.mul(255).add(0.5));
@@ -885,57 +919,161 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
885
919
  const rootFound = bool(false).toVar();
886
920
  const rootLower = float(0).toVar();
887
921
  const rootUpper = float(1).toVar();
888
- const previousHasSurface = bool(false).toVar();
889
- const previousSearchDepth = float(0).toVar();
890
- const previousSurfaceDepth = float(1).toVar();
891
- const coarseStep = float(1).div(coarseSearchSteps);
892
- Loop(coarseSearchSteps, ({ i }) => {
893
- const searchDepth = float(i).add(1).mul(coarseStep);
894
- const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
895
- const searchRayPosition = planePosition.add(rayDirection.mul(
896
- searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
897
- ));
898
- const searchUv = vec2(
899
- dot(searchRayPosition, right),
900
- dot(searchRayPosition, up)
901
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
902
- const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
903
- If(searchOutOfBounds, () => {
904
- previousHasSurface.assign(false);
922
+ const safeRay = vec3(
923
+ nonZeroSign(rayDirection.x),
924
+ nonZeroSign(rayDirection.y),
925
+ nonZeroSign(rayDirection.z)
926
+ ).mul(max(abs(rayDirection), vec3(1e-7)));
927
+ const boundsGuard = radius.mul(2).div(interiorResolution);
928
+ const boxT0 = vec3(...descriptor.boundsMin).sub(boundsGuard).sub(planePosition).div(safeRay);
929
+ const boxT1 = vec3(...descriptor.boundsMax).add(boundsGuard).sub(planePosition).div(safeRay);
930
+ const boxLow = min(boxT0, boxT1), boxHigh = max(boxT0, boxT1);
931
+ const boxStart = max(boxLow.x, max(boxLow.y, boxLow.z));
932
+ const boxEnd = min(boxHigh.x, min(boxHigh.y, boxHigh.z));
933
+ const intersectsBounds = boxStart.lessThanEqual(boxEnd);
934
+ const captureDepthAt = (t) => radius.sub(
935
+ dot(planePosition.add(rayDirection.mul(t)), frameDirection)
936
+ ).div(radius.mul(2));
937
+ const depth0 = captureDepthAt(boxStart), depth1 = captureDepthAt(boxEnd);
938
+ const uvDepthSlope = vec2(dot(rayDirection, right), dot(rayDirection, up)).mul(radius.mul(-2).div(safeRayPlaneDenominator)).div(bounds.zw.sub(bounds.xy));
939
+ const safeSlope = vec2(
940
+ nonZeroSign(uvDepthSlope.x),
941
+ nonZeroSign(uvDepthSlope.y)
942
+ ).mul(max(abs(uvDepthSlope), vec2(1e-7)));
943
+ const uvStart = projectedFrameUv.sub(uvDepthSlope.mul(0.5));
944
+ const t0 = vec2(minimumFrameUv).sub(uvStart).div(safeSlope);
945
+ const t1 = vec2(maximumFrameUv).sub(uvStart).div(safeSlope);
946
+ const lower = min(t0, t1), upper = max(t0, t1);
947
+ const searchStart = max(min(depth0, depth1), max(0, max(lower.x, lower.y)));
948
+ const searchEnd = min(max(depth0, depth1), min(1, min(upper.x, upper.y)));
949
+ const searchRange = max(searchEnd.sub(searchStart), 1e-6);
950
+ const coarseStep = searchRange.div(coarseSearchSteps);
951
+ const validateRoot = () => {
952
+ If(rootSolved, () => {
953
+ const at = atlasUv(frame, frameUv);
954
+ const surface = reconstructPosition(frameUv, bounds, frameDirection, right, up, sampleDepth16(at));
955
+ const closest = planePosition.add(rayDirection.mul(dot(surface.sub(planePosition), rayDirection)));
956
+ const footprint2 = max(bounds.z.sub(bounds.x), bounds.w.sub(bounds.y)).div(interiorResolution);
957
+ const nearestPixel = floor(at.mul(atlasResolution));
958
+ const nearestDepth = loadDepth16(nearestPixel);
959
+ const depthJump = abs(sampleDepth16(at).sub(nearestDepth)).mul(radius).mul(2);
960
+ const discontinuity = bool(false).toVar();
961
+ If(depthJump.greaterThan(footprint2.mul(4)), () => {
962
+ const neighbor = (offset) => loadDepth16(nearestPixel.add(offset).clamp(
963
+ frame.mul(frameResolution),
964
+ frame.add(1).mul(frameResolution).sub(1)
965
+ ));
966
+ const curvature = max(
967
+ abs(neighbor(vec2(-1, 0)).add(neighbor(vec2(1, 0))).sub(nearestDepth.mul(2))),
968
+ abs(neighbor(vec2(0, -1)).add(neighbor(vec2(0, 1))).sub(nearestDepth.mul(2)))
969
+ ).mul(radius).mul(2);
970
+ discontinuity.assign(curvature.greaterThan(footprint2.mul(4)));
971
+ });
972
+ If(surface.sub(closest).length().greaterThan(footprint2.mul(4)).or(discontinuity).or(sampleCapturedAlpha(at).lessThan(reprojectionSurfaceCutoff)), () => {
973
+ rootFound.assign(false);
974
+ rootSolved.assign(false);
975
+ strictRootSolved.assign(false);
976
+ });
905
977
  });
906
- If(searchOutOfBounds.not(), () => {
907
- const searchAtlasUv = atlasUv(frame, searchUv);
908
- const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
909
- const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
910
- const hasSurface = searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff);
911
- const depthDelta = searchDepth.sub(searchSurfaceDepth);
912
- const crossesDepth = previousHasSurface.and(previousSearchDepth.lessThan(previousSurfaceDepth)).and(depthDelta.greaterThanEqual(0));
913
- const continuousDepthStep = abs(searchSurfaceDepth.sub(previousSurfaceDepth)).lessThanEqual(coarseStep.mul(2));
914
- const crossesCoveredSurface = crossesDepth.and(continuousDepthStep);
915
- const entersAtSurface = previousHasSurface.not().and(depthDelta.greaterThanEqual(0)).and(depthDelta.lessThanEqual(coarseStep.mul(1.15)));
916
- const boundedRoot = crossesCoveredSurface.or(entersAtSurface);
917
- If(
918
- rootFound.not().and(hasSurface).and(features.boundedReprojection ? boundedRoot : depthDelta.greaterThanEqual(0)),
919
- () => {
920
- rootLower.assign(max(searchDepth.sub(coarseStep), 0));
978
+ };
979
+ const refineRoot = () => {
980
+ If(rootFound, () => {
981
+ Loop(profile.depthSamples, () => {
982
+ const searchDepth = rootLower.add(rootUpper).mul(0.5);
983
+ const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
984
+ const searchRayPosition = planePosition.add(rayDirection.mul(
985
+ searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
986
+ ));
987
+ const searchUv = vec2(
988
+ dot(searchRayPosition, right),
989
+ dot(searchRayPosition, up)
990
+ ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
991
+ const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
992
+ const midpointIsUpper = bool(false).toVar();
993
+ If(searchOutOfBounds.not(), () => {
994
+ const searchAtlasUv = atlasUv(frame, searchUv);
995
+ const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
996
+ const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
997
+ midpointIsUpper.assign(
998
+ searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff).and(searchDepth.greaterThanEqual(searchSurfaceDepth))
999
+ );
1000
+ });
1001
+ If(midpointIsUpper, () => {
921
1002
  rootUpper.assign(searchDepth);
922
- rootFound.assign(true);
923
- rootSolved.assign(true);
924
- strictRootSolved.assign(true);
925
- }
926
- );
927
- If(rootFound.not().and(hasSurface), () => {
928
- previousHasSurface.assign(true);
929
- previousSearchDepth.assign(searchDepth);
930
- previousSurfaceDepth.assign(searchSurfaceDepth);
1003
+ });
1004
+ If(midpointIsUpper.not(), () => {
1005
+ rootLower.assign(searchDepth);
1006
+ });
1007
+ });
1008
+ const residualAt = (depth) => {
1009
+ const planeDepth = radius.sub(depth.mul(radius).mul(2));
1010
+ const position = planePosition.add(rayDirection.mul(
1011
+ planeDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
1012
+ ));
1013
+ const address = vec2(dot(position, right), dot(position, up)).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1014
+ return depth.sub(sampleDepth16(atlasUv(frame, address)));
1015
+ };
1016
+ const lowResidual = residualAt(rootLower), highResidual = residualAt(rootUpper);
1017
+ const difference = lowResidual.sub(highResidual);
1018
+ const fraction = lowResidual.div(
1019
+ nonZeroSign(difference).mul(max(abs(difference), 1e-8))
1020
+ ).clamp(0, 1);
1021
+ const rootDepth = mix(rootLower, rootUpper, fraction);
1022
+ const rootPlaneDepth = radius.sub(rootDepth.mul(radius).mul(2));
1023
+ const rootRayPosition = planePosition.add(rayDirection.mul(
1024
+ rootPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
1025
+ ));
1026
+ frameUv.assign(vec2(
1027
+ dot(rootRayPosition, right),
1028
+ dot(rootRayPosition, up)
1029
+ ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy)).clamp(
1030
+ minimumFrameUv,
1031
+ maximumFrameUv
1032
+ ));
1033
+ });
1034
+ };
1035
+ const traceInterval = (steps, start = searchStart, end = searchEnd) => {
1036
+ const coarseStep2 = max(end.sub(start), 1e-6).div(float(steps));
1037
+ Loop({ start: 0, end: steps, type: "int" }, ({ i }) => {
1038
+ If(rootFound.or(intersectsBounds.not()).or(end.lessThan(start)), () => {
1039
+ Break();
931
1040
  });
932
- If(hasSurface.not(), () => {
933
- previousHasSurface.assign(false);
1041
+ const searchDepth = start.add(float(i).add(1).mul(coarseStep2));
1042
+ const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
1043
+ const searchRayPosition = planePosition.add(rayDirection.mul(
1044
+ searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
1045
+ ));
1046
+ const searchUv = vec2(
1047
+ dot(searchRayPosition, right),
1048
+ dot(searchRayPosition, up)
1049
+ ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1050
+ const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
1051
+ If(searchOutOfBounds.not(), () => {
1052
+ const searchAtlasUv = atlasUv(frame, searchUv);
1053
+ const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
1054
+ const hasSurface = searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff);
1055
+ If(hasSurface, () => {
1056
+ const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
1057
+ const depthDelta = searchDepth.sub(searchSurfaceDepth);
1058
+ If(
1059
+ rootFound.not().and(hasSurface).and(depthDelta.greaterThanEqual(0)),
1060
+ () => {
1061
+ rootLower.assign(max(searchDepth.sub(coarseStep2), 0));
1062
+ rootUpper.assign(searchDepth);
1063
+ rootFound.assign(true);
1064
+ rootSolved.assign(true);
1065
+ strictRootSolved.assign(true);
1066
+ refineRoot();
1067
+ if (features.boundedReprojection) validateRoot();
1068
+ }
1069
+ );
1070
+ });
934
1071
  });
935
1072
  });
936
- });
937
- If(rootFound.not(), () => {
938
- const iterativeUv = frameUv.toVar();
1073
+ };
1074
+ traceInterval(int(coarseSearchSteps));
1075
+ If(rootFound.not().and(intersectsBounds), () => {
1076
+ const iterativeUv = projectedFrameUv.clamp(minimumFrameUv, maximumFrameUv).toVar();
939
1077
  const iterativeDepth = sampleDepth16(atlasUv(frame, iterativeUv)).toVar();
940
1078
  const iterationResidual = float(1).toVar();
941
1079
  const iterationValid = bool(true).toVar();
@@ -971,54 +1109,12 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
971
1109
  () => {
972
1110
  frameUv.assign(iterativeUv);
973
1111
  rootSolved.assign(true);
1112
+ if (features.boundedReprojection) validateRoot();
974
1113
  }
975
1114
  );
976
1115
  });
977
- If(rootFound, () => {
978
- Loop(profile.depthSamples, () => {
979
- const searchDepth = rootLower.add(rootUpper).mul(0.5);
980
- const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
981
- const searchRayPosition = planePosition.add(rayDirection.mul(
982
- searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
983
- ));
984
- const searchUv = vec2(
985
- dot(searchRayPosition, right),
986
- dot(searchRayPosition, up)
987
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
988
- const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
989
- const midpointIsUpper = bool(false).toVar();
990
- If(searchOutOfBounds.not(), () => {
991
- const searchAtlasUv = atlasUv(frame, searchUv);
992
- const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
993
- const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
994
- midpointIsUpper.assign(
995
- searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff).and(searchDepth.greaterThanEqual(searchSurfaceDepth))
996
- );
997
- });
998
- If(midpointIsUpper, () => {
999
- rootUpper.assign(searchDepth);
1000
- });
1001
- If(midpointIsUpper.not(), () => {
1002
- rootLower.assign(searchDepth);
1003
- });
1004
- });
1005
- const rootDepth = rootLower.add(rootUpper).mul(0.5);
1006
- const rootPlaneDepth = radius.sub(rootDepth.mul(radius).mul(2));
1007
- const rootRayPosition = planePosition.add(rayDirection.mul(
1008
- rootPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
1009
- ));
1010
- frameUv.assign(vec2(
1011
- dot(rootRayPosition, right),
1012
- dot(rootRayPosition, up)
1013
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy)).clamp(
1014
- minimumFrameUv,
1015
- maximumFrameUv
1016
- ));
1017
- });
1018
1116
  }
1019
1117
  const uvNode = atlasUv(frame, frameUv);
1020
- const lod = atlasLod(uvNode);
1021
- const materialLod = compactLayout ? materialAtlasLod(uvNode) : lod;
1022
1118
  reconstructed.assign(reconstructPosition(
1023
1119
  frameUv,
1024
1120
  bounds,
@@ -1027,7 +1123,7 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1027
1123
  up,
1028
1124
  sampleDepth16(uvNode)
1029
1125
  ));
1030
- const shadingSample = texture(normalDepthAtlas, uvNode).level(materialLod);
1126
+ const shadingSample = sampleAtlas(normalDepthAtlas, uvNode, mapLod(normalDepthAtlas, uvNode));
1031
1127
  const geometrySample = compressedGeometry ? texture(geometryAtlas, uvNode).level(0) : null;
1032
1128
  const viewNormal = decodeOctahedral(shadingSample.rg);
1033
1129
  const normal = right.mul(viewNormal.x).add(up.mul(viewNormal.y)).add(frameDirection.mul(viewNormal.z)).normalize();
@@ -1060,9 +1156,10 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1060
1156
  const strictValidFloat = boundsConfidence.mul(
1061
1157
  withReprojection ? alignedConfidence.mul(strictRootSolved) : float(1)
1062
1158
  );
1063
- const baseColor = texture(baseColorAtlas, colorAtlasUv(uvNode)).level(materialLod);
1159
+ const colorUv = colorAtlasUv(uvNode);
1160
+ const baseColor = sampleAtlas(baseColorAtlas, colorUv, mapLod(baseColorAtlas, colorUv), colorColumns);
1064
1161
  const capturedAlpha = sampleCapturedAlpha(uvNode);
1065
- const orm = compactLayout ? vec4(geometrySample.b, shadingSample.b, shadingSample.a, 1) : ormAtlas ? texture(ormAtlas, uvNode).level(lod) : vec4(
1162
+ const orm = compactLayout ? vec4(geometrySample.b, shadingSample.b, shadingSample.a, 1) : ormAtlas ? sampleAtlas(ormAtlas, uvNode, mapLod(ormAtlas, uvNode)) : vec4(
1066
1163
  descriptor.material.occlusion,
1067
1164
  descriptor.material.roughness,
1068
1165
  descriptor.material.metalness,
@@ -1080,7 +1177,7 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1080
1177
  faceForwardNormal,
1081
1178
  reconstructed,
1082
1179
  orm.rgb,
1083
- emissiveAtlas ? texture(emissiveAtlas, colorAtlasUv(uvNode, true)).level(materialLod).rgb : vec3(0),
1180
+ emissiveAtlas ? sampleAtlas(emissiveAtlas, colorAtlasUv(uvNode, true), mapLod(emissiveAtlas, colorAtlasUv(uvNode, true)), colorColumns).rgb : vec3(0),
1084
1181
  coverage,
1085
1182
  resolvedValidity,
1086
1183
  strictValidFloat,
@@ -1316,6 +1413,28 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1316
1413
  material.receivedShadowPositionNode = reconstructedWorldPosition;
1317
1414
  material.toneMapped = true;
1318
1415
  material.userData.impostorShaderProfile = profile;
1416
+ bindTextureSlot(material, "baseColorAtlas", maps.baseColor, [baseColorAtlas, ...compactLayout && compactEmissive && emissiveAtlas ? [emissiveAtlas] : []], (value) => {
1417
+ maps.baseColor = value;
1418
+ if (compactLayout && compactEmissive) maps.emissive = value;
1419
+ if (material.userData.impostor) {
1420
+ material.userData.impostor.atlases.baseColor = value;
1421
+ if (compactLayout && compactEmissive) material.userData.impostor.atlases.emissive = value;
1422
+ }
1423
+ });
1424
+ bindTextureSlot(material, "normalDepthAtlas", maps.normalDepth, [normalDepthAtlas, ...compactLayout && ormAtlas ? [ormAtlas] : [], ...!compressedGeometry ? [geometryAtlas] : []], (value) => {
1425
+ maps.normalDepth = value;
1426
+ if (compactLayout) maps.orm = value;
1427
+ if (material.userData.impostor) {
1428
+ material.userData.impostor.atlases.normalDepth = value;
1429
+ if (compactLayout) material.userData.impostor.atlases.orm = value;
1430
+ }
1431
+ });
1432
+ if (!compactLayout && maps.orm && ormAtlas) bindTextureSlot(material, "ormAtlas", maps.orm, [ormAtlas], (value) => {
1433
+ maps.orm = value;
1434
+ });
1435
+ if (!compactLayout && maps.emissive && emissiveAtlas) bindTextureSlot(material, "emissiveAtlas", maps.emissive, [emissiveAtlas], (value) => {
1436
+ maps.emissive = value;
1437
+ });
1319
1438
  return material;
1320
1439
  }
1321
1440
  export {