@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
@@ -22,6 +22,7 @@ import {
22
22
  Fn,
23
23
  If,
24
24
  Loop,
25
+ Break,
25
26
  abs,
26
27
  any,
27
28
  bool,
@@ -984,83 +985,186 @@ export function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures =
984
985
  const rootFound = bool(false).toVar();
985
986
  const rootLower = float(0).toVar();
986
987
  const rootUpper = float(1).toVar();
987
- const previousHasSurface = bool(false).toVar();
988
- const previousSearchDepth = float(0).toVar();
989
- const previousSurfaceDepth = float(1).toVar();
990
- const coarseStep = float(1).div(coarseSearchSteps);
991
- Loop(coarseSearchSteps, ({ i }) => {
992
- const searchDepth = float(i).add(1).mul(coarseStep);
993
- const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
994
- const searchRayPosition = planePosition.add(rayDirection.mul(
995
- searchPlaneDepth.sub(dot(planePosition, frameDirection))
996
- .div(safeRayPlaneDenominator),
997
- ));
998
- const searchUv = vec2(
999
- dot(searchRayPosition, right),
1000
- dot(searchRayPosition, up),
1001
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1002
- const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv)))
1003
- .or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
1004
- If(searchOutOfBounds, () => {
1005
- previousHasSurface.assign(false);
988
+ // Empty billboard pixels must not traverse the whole capture volume.
989
+ // The source AABB is conservative, with one texel for filtered edges.
990
+ const safeRay = vec3(
991
+ nonZeroSign(rayDirection.x),
992
+ nonZeroSign(rayDirection.y),
993
+ nonZeroSign(rayDirection.z),
994
+ ).mul(max(abs(rayDirection), vec3(1e-7)));
995
+ const boundsGuard = radius.mul(2).div(interiorResolution);
996
+ const boxT0 = vec3(...descriptor.boundsMin).sub(boundsGuard)
997
+ .sub(planePosition).div(safeRay);
998
+ const boxT1 = vec3(...descriptor.boundsMax).add(boundsGuard)
999
+ .sub(planePosition).div(safeRay);
1000
+ const boxLow = min(boxT0, boxT1), boxHigh = max(boxT0, boxT1);
1001
+ const boxStart = max(boxLow.x, max(boxLow.y, boxLow.z));
1002
+ const boxEnd = min(boxHigh.x, min(boxHigh.y, boxHigh.z));
1003
+ const intersectsBounds = boxStart.lessThanEqual(boxEnd);
1004
+ const captureDepthAt = (t) => radius.sub(
1005
+ dot(planePosition.add(rayDirection.mul(t)), frameDirection),
1006
+ ).div(radius.mul(2));
1007
+ const depth0 = captureDepthAt(boxStart), depth1 = captureDepthAt(boxEnd);
1008
+ // UV is affine in capture depth. Clipping to the capture crop spends
1009
+ // the existing march budget on possible surfaces instead of empty space.
1010
+ const uvDepthSlope = vec2(dot(rayDirection, right), dot(rayDirection, up))
1011
+ .mul(radius.mul(-2).div(safeRayPlaneDenominator))
1012
+ .div(bounds.zw.sub(bounds.xy));
1013
+ const safeSlope = vec2(
1014
+ nonZeroSign(uvDepthSlope.x), nonZeroSign(uvDepthSlope.y),
1015
+ ).mul(max(abs(uvDepthSlope), vec2(1e-7)));
1016
+ const uvStart = projectedFrameUv.sub(uvDepthSlope.mul(0.5));
1017
+ const t0 = vec2(minimumFrameUv).sub(uvStart).div(safeSlope);
1018
+ const t1 = vec2(maximumFrameUv).sub(uvStart).div(safeSlope);
1019
+ const lower = min(t0, t1), upper = max(t0, t1);
1020
+ const searchStart = max(min(depth0, depth1), max(0, max(lower.x, lower.y)));
1021
+ const searchEnd = min(max(depth0, depth1), min(1, min(upper.x, upper.y)));
1022
+ const searchRange = max(searchEnd.sub(searchStart), 1e-6);
1023
+ const coarseStep = searchRange.div(coarseSearchSteps);
1024
+ const validateRoot = () => {
1025
+ If(rootSolved, () => {
1026
+ const at = atlasUv(frame, frameUv);
1027
+ const surface = reconstructPosition(frameUv, bounds, frameDirection, right, up, sampleDepth16(at));
1028
+ const closest = planePosition.add(rayDirection.mul(dot(surface.sub(planePosition), rayDirection)));
1029
+ const footprint = max(bounds.z.sub(bounds.x), bounds.w.sub(bounds.y)).div(interiorResolution);
1030
+ const nearestPixel = floor(at.mul(atlasResolution));
1031
+ const nearestDepth = loadDepth16(nearestPixel);
1032
+ const depthJump = abs(sampleDepth16(at).sub(nearestDepth)).mul(radius).mul(2);
1033
+ const discontinuity = bool(false).toVar();
1034
+ If(depthJump.greaterThan(footprint.mul(4)), () => {
1035
+ const neighbor = (offset) => loadDepth16(nearestPixel.add(offset).clamp(
1036
+ frame.mul(frameResolution), frame.add(1).mul(frameResolution).sub(1),
1037
+ ));
1038
+ // A steep plane can change depth arbitrarily fast. Unlike a slope
1039
+ // cap, second differences reject interpolated depth steps without
1040
+ // rejecting a continuous plane merely because it is steep.
1041
+ const curvature = max(
1042
+ abs(neighbor(vec2(-1, 0)).add(neighbor(vec2(1, 0))).sub(nearestDepth.mul(2))),
1043
+ abs(neighbor(vec2(0, -1)).add(neighbor(vec2(0, 1))).sub(nearestDepth.mul(2))),
1044
+ ).mul(radius).mul(2);
1045
+ discontinuity.assign(curvature.greaterThan(footprint.mul(4)));
1046
+ });
1047
+ If(surface.sub(closest).length().greaterThan(footprint.mul(4))
1048
+ .or(discontinuity)
1049
+ .or(sampleCapturedAlpha(at).lessThan(reprojectionSurfaceCutoff)), () => {
1050
+ rootFound.assign(false);
1051
+ rootSolved.assign(false);
1052
+ strictRootSolved.assign(false);
1053
+ });
1006
1054
  });
1007
- If(searchOutOfBounds.not(), () => {
1008
- const searchAtlasUv = atlasUv(frame, searchUv);
1009
- const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
1010
- const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
1011
- // Only captured coverage represents geometry. The transition SDF
1012
- // deliberately extends outside the silhouette; accepting it as a
1013
- // ray hit turns that band into repeated depth shells and streaks.
1014
- const hasSurface = searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff);
1015
- const depthDelta = searchDepth.sub(searchSurfaceDepth);
1016
- const crossesDepth = previousHasSurface
1017
- .and(previousSearchDepth.lessThan(previousSurfaceDepth))
1018
- .and(depthDelta.greaterThanEqual(0));
1019
- const continuousDepthStep = abs(searchSurfaceDepth.sub(previousSurfaceDepth))
1020
- .lessThanEqual(coarseStep.mul(2));
1021
- const crossesCoveredSurface = crossesDepth.and(continuousDepthStep);
1022
- // A thin captured feature can occupy less than one coarse ray step,
1023
- // so its first covered sample may already lie just behind the stored
1024
- // surface. Only accept that entry when the root is inside the
1025
- // immediately preceding march interval. A late silhouette entry is
1026
- // not a height-field crossing and must not become a second layer.
1027
- const entersAtSurface = previousHasSurface.not()
1028
- .and(depthDelta.greaterThanEqual(0))
1029
- .and(depthDelta.lessThanEqual(coarseStep.mul(1.15)));
1030
- const boundedRoot = crossesCoveredSurface.or(entersAtSurface);
1031
- If(
1032
- rootFound.not()
1033
- .and(hasSurface)
1034
- .and(features.boundedReprojection
1035
- ? boundedRoot
1036
- : depthDelta.greaterThanEqual(0)),
1037
- () => {
1038
- rootLower.assign(max(searchDepth.sub(coarseStep), 0));
1055
+ };
1056
+ const refineRoot = () => {
1057
+ If(rootFound, () => {
1058
+ Loop(profile.depthSamples, () => {
1059
+ const searchDepth = rootLower.add(rootUpper).mul(0.5);
1060
+ const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
1061
+ const searchRayPosition = planePosition.add(rayDirection.mul(
1062
+ searchPlaneDepth.sub(dot(planePosition, frameDirection))
1063
+ .div(safeRayPlaneDenominator),
1064
+ ));
1065
+ const searchUv = vec2(
1066
+ dot(searchRayPosition, right),
1067
+ dot(searchRayPosition, up),
1068
+ ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1069
+ const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv)))
1070
+ .or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
1071
+ const midpointIsUpper = bool(false).toVar();
1072
+ If(searchOutOfBounds.not(), () => {
1073
+ const searchAtlasUv = atlasUv(frame, searchUv);
1074
+ const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
1075
+ const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
1076
+ midpointIsUpper.assign(
1077
+ searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff)
1078
+ .and(searchDepth.greaterThanEqual(searchSurfaceDepth)),
1079
+ );
1080
+ });
1081
+ If(midpointIsUpper, () => {
1039
1082
  rootUpper.assign(searchDepth);
1040
- rootFound.assign(true);
1041
- rootSolved.assign(true);
1042
- strictRootSolved.assign(true);
1043
- },
1044
- );
1045
- If(rootFound.not().and(hasSurface), () => {
1046
- previousHasSurface.assign(true);
1047
- previousSearchDepth.assign(searchDepth);
1048
- previousSurfaceDepth.assign(searchSurfaceDepth);
1083
+ });
1084
+ If(midpointIsUpper.not(), () => {
1085
+ rootLower.assign(searchDepth);
1086
+ });
1049
1087
  });
1050
- If(hasSurface.not(), () => {
1051
- previousHasSurface.assign(false);
1088
+ const residualAt = (depth) => {
1089
+ const planeDepth = radius.sub(depth.mul(radius).mul(2));
1090
+ const position = planePosition.add(rayDirection.mul(
1091
+ planeDepth.sub(dot(planePosition, frameDirection)).div(safeRayPlaneDenominator),
1092
+ ));
1093
+ const address = vec2(dot(position, right), dot(position, up))
1094
+ .sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1095
+ return depth.sub(sampleDepth16(atlasUv(frame,address)));
1096
+ };
1097
+ // A midpoint leaves large physical errors on a steep height field.
1098
+ // Secant interpolation is exact for a plane and stays in the bracket.
1099
+ const lowResidual = residualAt(rootLower), highResidual = residualAt(rootUpper);
1100
+ const difference = lowResidual.sub(highResidual);
1101
+ const fraction = lowResidual.div(
1102
+ nonZeroSign(difference).mul(max(abs(difference), 1e-8)),
1103
+ ).clamp(0, 1);
1104
+ const rootDepth = mix(rootLower,rootUpper,fraction);
1105
+
1106
+ const rootPlaneDepth = radius.sub(rootDepth.mul(radius).mul(2));
1107
+ const rootRayPosition = planePosition.add(rayDirection.mul(
1108
+ rootPlaneDepth.sub(dot(planePosition, frameDirection))
1109
+ .div(safeRayPlaneDenominator),
1110
+ ));
1111
+ frameUv.assign(vec2(
1112
+ dot(rootRayPosition, right),
1113
+ dot(rootRayPosition, up),
1114
+ ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy)).clamp(
1115
+ minimumFrameUv,
1116
+ maximumFrameUv,
1117
+ ));
1118
+ });
1119
+ };
1120
+ const traceInterval = (steps, start = searchStart, end = searchEnd) => {
1121
+ const coarseStep = max(end.sub(start), 1e-6).div(float(steps));
1122
+ Loop({ start: 0, end: steps, type: 'int' }, ({ i }) => {
1123
+ If(rootFound.or(intersectsBounds.not()).or(end.lessThan(start)), () => { Break(); });
1124
+ const searchDepth = start.add(float(i).add(1).mul(coarseStep));
1125
+ const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
1126
+ const searchRayPosition = planePosition.add(rayDirection.mul(
1127
+ searchPlaneDepth.sub(dot(planePosition, frameDirection))
1128
+ .div(safeRayPlaneDenominator),
1129
+ ));
1130
+ const searchUv = vec2(
1131
+ dot(searchRayPosition, right),
1132
+ dot(searchRayPosition, up),
1133
+ ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1134
+ const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv)))
1135
+ .or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
1136
+ If(searchOutOfBounds.not(), () => {
1137
+ const searchAtlasUv = atlasUv(frame, searchUv);
1138
+ const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
1139
+ const hasSurface = searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff);
1140
+ If(hasSurface, () => {
1141
+ const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
1142
+ const depthDelta = searchDepth.sub(searchSurfaceDepth);
1143
+ If(
1144
+ rootFound.not()
1145
+ .and(hasSurface)
1146
+ .and(depthDelta.greaterThanEqual(0)),
1147
+ () => {
1148
+ rootLower.assign(max(searchDepth.sub(coarseStep), 0));
1149
+ rootUpper.assign(searchDepth);
1150
+ rootFound.assign(true);
1151
+ rootSolved.assign(true);
1152
+ strictRootSolved.assign(true);
1153
+ refineRoot();
1154
+ if (features.boundedReprojection) validateRoot();
1155
+ },
1156
+ );
1157
+ });
1052
1158
  });
1053
1159
  });
1054
- });
1055
- // The bounded march deliberately rejects height-field discontinuities.
1056
- // Near a dense capture direction, a valid surface can still be too steep
1057
- // to form a coarse bracket. Use the center-plane depth only as a seed for
1058
- // a fixed-point solve, and accept it solely after the projected UV/depth
1059
- // pair converges inside captured coverage. Unlike the removed planar
1060
- // fallback, this path produces a point on the runtime camera ray and
1061
- // never contributes an unconverged card layer.
1062
- If(rootFound.not(), () => {
1063
- const iterativeUv = frameUv.toVar();
1160
+ };
1161
+ traceInterval(int(coarseSearchSteps));
1162
+ // A fixed-point solution can recover a grazing ray with no sign bracket.
1163
+ // Retain it only after convergence inside captured coverage; blend-time
1164
+ // seed suppression still prevents an unrelated background layer.
1165
+ If(rootFound.not().and(intersectsBounds), () => {
1166
+ // A rejected march root must not seed itself back into validity.
1167
+ const iterativeUv = projectedFrameUv.clamp(minimumFrameUv, maximumFrameUv).toVar();
1064
1168
  const iterativeDepth = sampleDepth16(atlasUv(frame, iterativeUv)).toVar();
1065
1169
  const iterationResidual = float(1).toVar();
1066
1170
  const iterationValid = bool(true).toVar();
@@ -1098,54 +1202,10 @@ export function createPbrImpostorNodeMaterial(descriptor, maps, inputFeatures =
1098
1202
  () => {
1099
1203
  frameUv.assign(iterativeUv);
1100
1204
  rootSolved.assign(true);
1205
+ if (features.boundedReprojection) validateRoot();
1101
1206
  },
1102
1207
  );
1103
1208
  });
1104
- If(rootFound, () => {
1105
- Loop(profile.depthSamples, () => {
1106
- const searchDepth = rootLower.add(rootUpper).mul(0.5);
1107
- const searchPlaneDepth = radius.sub(searchDepth.mul(radius).mul(2));
1108
- const searchRayPosition = planePosition.add(rayDirection.mul(
1109
- searchPlaneDepth.sub(dot(planePosition, frameDirection))
1110
- .div(safeRayPlaneDenominator),
1111
- ));
1112
- const searchUv = vec2(
1113
- dot(searchRayPosition, right),
1114
- dot(searchRayPosition, up),
1115
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy));
1116
- const searchOutOfBounds = any(searchUv.lessThan(vec2(minimumFrameUv)))
1117
- .or(any(searchUv.greaterThan(vec2(maximumFrameUv))));
1118
- const midpointIsUpper = bool(false).toVar();
1119
- If(searchOutOfBounds.not(), () => {
1120
- const searchAtlasUv = atlasUv(frame, searchUv);
1121
- const searchCoverage = sampleCapturedAlpha(searchAtlasUv);
1122
- const searchSurfaceDepth = sampleDepth16(searchAtlasUv);
1123
- midpointIsUpper.assign(
1124
- searchCoverage.greaterThanEqual(reprojectionSurfaceCutoff)
1125
- .and(searchDepth.greaterThanEqual(searchSurfaceDepth)),
1126
- );
1127
- });
1128
- If(midpointIsUpper, () => {
1129
- rootUpper.assign(searchDepth);
1130
- });
1131
- If(midpointIsUpper.not(), () => {
1132
- rootLower.assign(searchDepth);
1133
- });
1134
- });
1135
- const rootDepth = rootLower.add(rootUpper).mul(0.5);
1136
- const rootPlaneDepth = radius.sub(rootDepth.mul(radius).mul(2));
1137
- const rootRayPosition = planePosition.add(rayDirection.mul(
1138
- rootPlaneDepth.sub(dot(planePosition, frameDirection))
1139
- .div(safeRayPlaneDenominator),
1140
- ));
1141
- frameUv.assign(vec2(
1142
- dot(rootRayPosition, right),
1143
- dot(rootRayPosition, up),
1144
- ).sub(bounds.xy).div(bounds.zw.sub(bounds.xy)).clamp(
1145
- minimumFrameUv,
1146
- maximumFrameUv,
1147
- ));
1148
- });
1149
1209
  }
1150
1210
  const uvNode = atlasUv(frame, frameUv);
1151
1211
  reconstructed.assign(reconstructPosition(
@@ -0,0 +1,14 @@
1
+ // @ts-check
2
+ export {
3
+ NEEDLE_BOX_IMPOSTOR,
4
+ parseBoxImpostorDescriptor,
5
+ } from '../box-impostor/box-impostor-metadata.js';
6
+ export { createBoxImpostorGeometry } from '../box-impostor/box-impostor-geometry.js';
7
+ export { createBoxImpostorMaterial } from '../box-impostor/box-impostor-material.js';
8
+ export {
9
+ NeedleBoxImpostorLoaderPlugin,
10
+ registerNeedleBoxImpostorLoader,
11
+ } from '../box-impostor/box-impostor-runtime.js';
12
+
13
+ export { DEFAULT_BOX_IMPOSTOR_DISPLAY, normalizeBoxImpostorDisplay } from '../box-impostor/box-impostor-display-settings.js';
14
+ export { setBoxImpostorDisplay } from '../box-impostor/box-impostor-display.js';
@@ -1,4 +1,5 @@
1
1
  // @ts-check
2
+ /** @typedef {import('../rendering/wireframe.js').WireframeBlendMode} WireframeBlendMode */
2
3
 
3
4
  export { bindHull, getHullEffect, NeedleHullEffectLoaderPlugin } from '../rendering/hull-effect-runtime.js';
4
5
  export { NEEDLE_HULL_EFFECT, parseHullEffectDescriptor } from '../rendering/hull-effect-metadata.js';
@@ -5,7 +5,7 @@
5
5
  /** @typedef {import('three/addons/loaders/GLTFLoader.js').GLTF} GLTF */
6
6
  /** @typedef {import('../impostor/impostor-node-material.js').ImpostorRenderer} Renderer */
7
7
 
8
- /** @typedef {'NEEDLE_deformation_rig' | 'NEEDLE_mesh_impostor' | 'NEEDLE_pixel_impostor' | 'NEEDLE_hull_effect'} NeedleExtension */
8
+ /** @typedef {'NEEDLE_deformation_rig' | 'NEEDLE_mesh_impostor' | 'NEEDLE_pixel_impostor' | 'NEEDLE_hull_effect' | 'NEEDLE_box_impostor'} NeedleExtension */
9
9
  /** @typedef {{disable?: readonly NeedleExtension[]}} LoaderOptions */
10
10
 
11
11
  /** @type {WeakMap<GLTFLoader, {renderer: Renderer, disabled: Set<NeedleExtension>}>} */
@@ -69,5 +69,9 @@ export function useLoader(loader, renderer, options = {}) {
69
69
  const { NeedleHullEffectLoaderPlugin } = await import('@needle-tools/three-mesh-effects/hull-effects');
70
70
  await new NeedleHullEffectLoaderPlugin(parser).afterRoot(result);
71
71
  });
72
+ register('NEEDLE_box_impostor', async (parser, result, renderer) => {
73
+ const { NeedleBoxImpostorLoaderPlugin } = await import('@needle-tools/three-mesh-effects/box-impostor-runtime');
74
+ await new NeedleBoxImpostorLoaderPlugin(parser, { renderer }).afterRoot(result);
75
+ });
72
76
  return loader;
73
77
  }
@@ -1,8 +1,8 @@
1
1
  // @ts-check
2
2
  import * as THREE from 'three';
3
3
  import { MeshBasicNodeMaterial } from 'three/webgpu';
4
- import { attribute, fwidth, min, vec3, uniform, float } from 'three/tsl';
5
- /** @typedef {'black' | 'white' | 'invert'} WireframeBlendMode */
4
+ import { attribute, fwidth, min, vec3, uniform, float, mix } from 'three/tsl';
5
+ /** @typedef {'high-contrast' | 'black' | 'white' | 'invert' | 'cyan' | 'lime' | 'magenta'} WireframeBlendMode */
6
6
 
7
7
  /**
8
8
  * Produces a triangle overlay with a screen-space barycentric edge mask. Unlike
@@ -25,14 +25,15 @@ export function createPreviewWireframeMaterial(sourceMaterial, isImpostor, thick
25
25
  edgeDistanceVector.z,
26
26
  );
27
27
  const halfWidth = thicknessNode.mul(0.5);
28
- // Exact overlap of the requested line interval with a one-pixel box filter.
29
- // The difference of the two ramps preserves integrated line energy for
30
- // subpixel widths instead of quantizing or over-attenuating thin lines.
31
- const coverage = halfWidth.add(0.5).sub(edgeDistance).clamp(0, 1).sub(
32
- float(0.5).sub(halfWidth).sub(edgeDistance).clamp(0, 1),
33
- );
28
+ const coverage = lineCoverage(edgeDistance, halfWidth);
29
+ // A narrow halo preserves dark-background visibility without making the
30
+ // antialiased gray outline dominate the black core on dense geometry.
31
+ const contrastCore = lineCoverage(edgeDistance, halfWidth.max(1));
32
+ const contrastCoverage = lineCoverage(edgeDistance, halfWidth.max(1).add(0.125));
33
+ const contrast = uniform(0);
34
+ const opacity = mix(coverage, contrastCoverage, contrast);
34
35
  const material = new MeshBasicNodeMaterial({
35
- name: isImpostor ? 'Black impostor quad overlay' : 'Black mesh wireframe overlay',
36
+ name: isImpostor ? 'Impostor quad overlay' : 'Mesh wireframe overlay',
36
37
  color: 0x000000,
37
38
  side: isImpostor ? THREE.DoubleSide : sourceMaterial.side,
38
39
  depthTest: !isImpostor,
@@ -44,20 +45,39 @@ export function createPreviewWireframeMaterial(sourceMaterial, isImpostor, thick
44
45
  });
45
46
  material.transparent = true;
46
47
  material.premultipliedAlpha = true;
47
- material.opacityNode = coverage;
48
- material.maskNode = coverage.greaterThan(1 / 1024);
48
+ material.opacityNode = opacity;
49
+ const lineColor = uniform(new THREE.Vector3());
50
+ material.colorNode = mix(lineColor,
51
+ vec3(float(1).sub(contrastCore.div(contrastCoverage.max(1e-4)))).mul(0.45), contrast,
52
+ );
53
+ material.maskNode = opacity.greaterThan(1 / 1024);
49
54
  if ('positionNode' in sourceMaterial) {
50
55
  const sourceNodeMaterial = /** @type {import('three/webgpu').NodeMaterial} */ (sourceMaterial);
51
56
  material.positionNode = sourceNodeMaterial.positionNode ?? null;
52
57
  }
53
58
  material.userData.previewWireframeThickness = thicknessNode;
59
+ material.userData.previewWireframeContrast = contrast;
60
+ material.userData.previewWireframeColor = lineColor;
54
61
  applyPreviewWireframeBlendMode(material, blendMode);
55
62
  return material;
56
63
  }
57
64
 
65
+ /** Screen-space interval coverage with a one-pixel box filter. The second
66
+ * ramp preserves integrated energy for subpixel line widths.
67
+ * @param {import('three/webgpu').Node<'float'>} distance
68
+ * @param {import('three/webgpu').Node<'float'>} halfWidth
69
+ */
70
+ function lineCoverage(distance, halfWidth) {
71
+ return halfWidth.add(0.5).sub(distance).clamp(0, 1).sub(
72
+ float(0.5).sub(halfWidth).sub(distance).clamp(0, 1),
73
+ );
74
+ }
75
+
58
76
  /** @param {string} value @returns {WireframeBlendMode} */
59
77
  export function parseWireframeBlendMode(value) {
60
- return value === 'white' || value === 'invert' ? value : 'black';
78
+ return value === 'black' || value === 'white' || value === 'invert'
79
+ || value === 'cyan' || value === 'lime' || value === 'magenta'
80
+ ? value : 'high-contrast';
61
81
  }
62
82
 
63
83
  /**
@@ -66,7 +86,10 @@ export function parseWireframeBlendMode(value) {
66
86
  */
67
87
  export function applyPreviewWireframeBlendMode(material, mode) {
68
88
  const basic = /** @type {import('three/webgpu').MeshBasicNodeMaterial} */ (material);
69
- basic.color.set(mode === 'black' ? 0x000000 : 0xffffff);
89
+ const colors = { black: 0x000000, cyan: 0x00ffff, lime: 0x39ff14, magenta: 0xff00ff };
90
+ basic.color.set(mode in colors ? colors[/** @type {keyof typeof colors} */ (mode)] : 0xffffff);
91
+ basic.userData.previewWireframeColor.value.set(basic.color.r, basic.color.g, basic.color.b);
92
+ basic.userData.previewWireframeContrast.value = mode === 'high-contrast' ? 1 : 0;
70
93
  if (mode === 'invert') {
71
94
  basic.blending = THREE.CustomBlending;
72
95
  basic.blendEquation = THREE.AddEquation;
@@ -111,5 +134,20 @@ export function createBarycentricWireframeGeometry(source) {
111
134
  'previewWireframeEdgeMask',
112
135
  new THREE.BufferAttribute(edgeMask, 3),
113
136
  );
137
+ // toNonIndexed separates even interleaved source attributes. Cards need
138
+ // their camera-facing transform attributes as well as edge coordinates;
139
+ // separate buffers exceed WebGPU's portable eight-buffer limit.
140
+ const floats = Object.entries(geometry.attributes)
141
+ .filter(([, value]) => value.array instanceof Float32Array);
142
+ const stride = floats.reduce((sum, [, value]) => sum + value.itemSize, 0);
143
+ const packed = new THREE.InterleavedBuffer(new Float32Array(vertexCount * stride), stride);
144
+ let offset = 0;
145
+ for (const [name, value] of floats) {
146
+ for (let vertex = 0; vertex < vertexCount; vertex++)
147
+ for (let component = 0; component < value.itemSize; component++)
148
+ packed.array[vertex * stride + offset + component] = value.getComponent(vertex, component);
149
+ geometry.setAttribute(name, new THREE.InterleavedBufferAttribute(packed, value.itemSize, offset, value.normalized));
150
+ offset += value.itemSize;
151
+ }
114
152
  return geometry;
115
153
  }