@needle-tools/three-mesh-effects 0.2.3 → 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 (92) hide show
  1. package/CHANGELOG.md +25 -8
  2. package/README.md +68 -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 +30 -19
  7. package/build/deformation-runtime.js.map +3 -3
  8. package/build/hull-effects.js +60 -28
  9. package/build/hull-effects.js.map +3 -3
  10. package/build/impostor-runtime.js +152 -87
  11. package/build/impostor-runtime.js.map +3 -3
  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/pin-cloth.js +7 -2
  37. package/build/internal/deformation/pin-cloth.js.map +2 -2
  38. package/build/internal/deformation/rig-runtime.js +30 -19
  39. package/build/internal/deformation/rig-runtime.js.map +3 -3
  40. package/build/internal/impostor/impostor-node-material.js +152 -87
  41. package/build/internal/impostor/impostor-node-material.js.map +3 -3
  42. package/build/internal/rendering/hull-effect-runtime.js +59 -27
  43. package/build/internal/rendering/hull-effect-runtime.js.map +3 -3
  44. package/build/internal/rendering/wireframe.js +41 -9
  45. package/build/internal/rendering/wireframe.js.map +2 -2
  46. package/build/internal/rendering/wrap-preview.js +59 -27
  47. package/build/internal/rendering/wrap-preview.js.map +3 -3
  48. package/build/types/box-impostor/box-impostor-display-settings.d.ts +30 -0
  49. package/build/types/box-impostor/box-impostor-display-settings.js +1 -0
  50. package/build/types/box-impostor/box-impostor-display.d.ts +5 -0
  51. package/build/types/box-impostor/box-impostor-display.js +1 -0
  52. package/build/types/box-impostor/box-impostor-face-axes.d.ts +5 -0
  53. package/build/types/box-impostor/box-impostor-face-axes.js +1 -0
  54. package/build/types/box-impostor/box-impostor-faces.d.ts +11 -0
  55. package/build/types/box-impostor/box-impostor-faces.js +1 -0
  56. package/build/types/box-impostor/box-impostor-geometry.d.ts +5 -0
  57. package/build/types/box-impostor/box-impostor-geometry.js +1 -0
  58. package/build/types/box-impostor/box-impostor-material.d.ts +10 -0
  59. package/build/types/box-impostor/box-impostor-material.js +1 -0
  60. package/build/types/box-impostor/box-impostor-metadata.d.ts +31 -0
  61. package/build/types/box-impostor/box-impostor-metadata.js +1 -0
  62. package/build/types/box-impostor/box-impostor-projection.d.ts +19 -0
  63. package/build/types/box-impostor/box-impostor-projection.js +1 -0
  64. package/build/types/box-impostor/box-impostor-runtime.d.ts +25 -0
  65. package/build/types/box-impostor/box-impostor-runtime.js +1 -0
  66. package/build/types/box-impostor/box-impostor-surface-search.d.ts +13 -0
  67. package/build/types/box-impostor/box-impostor-surface-search.js +1 -0
  68. package/build/types/deformation/cloth-surface.d.ts +1 -0
  69. package/build/types/deformation/pin-cloth.d.ts +1 -0
  70. package/build/types/public/box-impostor-runtime.d.ts +6 -0
  71. package/build/types/public/box-impostor-runtime.js +1 -0
  72. package/build/types/public/hull-effects.d.ts +1 -0
  73. package/build/types/public/index.d.ts +1 -1
  74. package/build/types/rendering/wireframe.d.ts +2 -2
  75. package/package.json +27 -6
  76. package/src/box-impostor/box-impostor-display-settings.js +54 -0
  77. package/src/box-impostor/box-impostor-display.js +38 -0
  78. package/src/box-impostor/box-impostor-face-axes.js +11 -0
  79. package/src/box-impostor/box-impostor-faces.js +118 -0
  80. package/src/box-impostor/box-impostor-geometry.js +140 -0
  81. package/src/box-impostor/box-impostor-material.js +721 -0
  82. package/src/box-impostor/box-impostor-metadata.js +122 -0
  83. package/src/box-impostor/box-impostor-projection.js +287 -0
  84. package/src/box-impostor/box-impostor-runtime.js +122 -0
  85. package/src/box-impostor/box-impostor-surface-search.js +11 -0
  86. package/src/deformation/cloth-surface.ts +28 -17
  87. package/src/deformation/pin-cloth.ts +9 -3
  88. package/src/impostor/impostor-node-material.js +177 -117
  89. package/src/public/box-impostor-runtime.js +14 -0
  90. package/src/public/hull-effects.js +1 -0
  91. package/src/public/index.js +5 -1
  92. 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,
@@ -918,57 +919,161 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
918
919
  const rootFound = bool(false).toVar();
919
920
  const rootLower = float(0).toVar();
920
921
  const rootUpper = float(1).toVar();
921
- const previousHasSurface = bool(false).toVar();
922
- const previousSearchDepth = float(0).toVar();
923
- const previousSurfaceDepth = float(1).toVar();
924
- const coarseStep = float(1).div(coarseSearchSteps);
925
- Loop(coarseSearchSteps, ({ i }) => {
926
- const searchDepth = float(i).add(1).mul(coarseStep);
927
- const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
928
- const searchRayPosition = planePosition.add(rayDirection.mul(
929
- searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
930
- ));
931
- const searchUv = vec2(
932
- dot(searchRayPosition, right),
933
- dot(searchRayPosition, up)
934
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
935
- const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
936
- If(searchOutOfBounds, () => {
937
- 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
+ });
938
977
  });
939
- If(searchOutOfBounds.not(), () => {
940
- const searchAtlasUv = atlasUv(frame, searchUv);
941
- const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
942
- const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
943
- const hasSurface = searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff);
944
- const depthDelta = searchDepth.sub(searchSurfaceDepth);
945
- const crossesDepth = previousHasSurface.and(previousSearchDepth.lessThan(previousSurfaceDepth)).and(depthDelta.greaterThanEqual(0));
946
- const continuousDepthStep = abs(searchSurfaceDepth.sub(previousSurfaceDepth)).lessThanEqual(coarseStep.mul(2));
947
- const crossesCoveredSurface = crossesDepth.and(continuousDepthStep);
948
- const entersAtSurface = previousHasSurface.not().and(depthDelta.greaterThanEqual(0)).and(depthDelta.lessThanEqual(coarseStep.mul(1.15)));
949
- const boundedRoot = crossesCoveredSurface.or(entersAtSurface);
950
- If(
951
- rootFound.not().and(hasSurface).and(features.boundedReprojection ? boundedRoot : depthDelta.greaterThanEqual(0)),
952
- () => {
953
- 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, () => {
954
1002
  rootUpper.assign(searchDepth);
955
- rootFound.assign(true);
956
- rootSolved.assign(true);
957
- strictRootSolved.assign(true);
958
- }
959
- );
960
- If(rootFound.not().and(hasSurface), () => {
961
- previousHasSurface.assign(true);
962
- previousSearchDepth.assign(searchDepth);
963
- previousSurfaceDepth.assign(searchSurfaceDepth);
1003
+ });
1004
+ If(midpointIsUpper.not(), () => {
1005
+ rootLower.assign(searchDepth);
1006
+ });
964
1007
  });
965
- If(hasSurface.not(), () => {
966
- previousHasSurface.assign(false);
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();
1040
+ });
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
+ });
967
1071
  });
968
1072
  });
969
- });
970
- If(rootFound.not(), () => {
971
- const iterativeUv = frameUv.toVar();
1073
+ };
1074
+ traceInterval(int(coarseSearchSteps));
1075
+ If(rootFound.not().and(intersectsBounds), () => {
1076
+ const iterativeUv = projectedFrameUv.clamp(minimumFrameUv, maximumFrameUv).toVar();
972
1077
  const iterativeDepth = sampleDepth16(atlasUv(frame, iterativeUv)).toVar();
973
1078
  const iterationResidual = float(1).toVar();
974
1079
  const iterationValid = bool(true).toVar();
@@ -1004,50 +1109,10 @@ function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures = {}, ren
1004
1109
  () => {
1005
1110
  frameUv.assign(iterativeUv);
1006
1111
  rootSolved.assign(true);
1112
+ if (features.boundedReprojection) validateRoot();
1007
1113
  }
1008
1114
  );
1009
1115
  });
1010
- If(rootFound, () => {
1011
- Loop(profile.depthSamples, () => {
1012
- const searchDepth = rootLower.add(rootUpper).mul(0.5);
1013
- const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
1014
- const searchRayPosition = planePosition.add(rayDirection.mul(
1015
- searchPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
1016
- ));
1017
- const searchUv = vec2(
1018
- dot(searchRayPosition, right),
1019
- dot(searchRayPosition, up)
1020
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1021
- const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv))).or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
1022
- const midpointIsUpper = bool(false).toVar();
1023
- If(searchOutOfBounds.not(), () => {
1024
- const searchAtlasUv = atlasUv(frame, searchUv);
1025
- const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
1026
- const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
1027
- midpointIsUpper.assign(
1028
- searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff).and(searchDepth.greaterThanEqual(searchSurfaceDepth))
1029
- );
1030
- });
1031
- If(midpointIsUpper, () => {
1032
- rootUpper.assign(searchDepth);
1033
- });
1034
- If(midpointIsUpper.not(), () => {
1035
- rootLower.assign(searchDepth);
1036
- });
1037
- });
1038
- const rootDepth = rootLower.add(rootUpper).mul(0.5);
1039
- const rootPlaneDepth = radius.sub(rootDepth.mul(radius).mul(2));
1040
- const rootRayPosition = planePosition.add(rayDirection.mul(
1041
- rootPlaneDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator)
1042
- ));
1043
- frameUv.assign(vec2(
1044
- dot(rootRayPosition, right),
1045
- dot(rootRayPosition, up)
1046
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy)).clamp(
1047
- minimumFrameUv,
1048
- maximumFrameUv
1049
- ));
1050
- });
1051
1116
  }
1052
1117
  const uvNode = atlasUv(frame, frameUv);
1053
1118
  reconstructed.assign(reconstructPosition(