@needle-tools/three-mesh-effects 0.2.3 → 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 (113) hide show
  1. package/CHANGELOG.md +34 -8
  2. package/README.md +86 -3
  3. package/SKILL.md +14 -4
  4. package/build/box-impostor-runtime.js +1423 -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 +264 -133
  11. package/build/impostor-runtime.js.map +4 -4
  12. package/build/index.js +8 -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 +1102 -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 +1056 -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 +276 -0
  29. package/build/internal/box-impostor/box-impostor-projection.js.map +7 -0
  30. package/build/internal/box-impostor/box-impostor-runtime.js +1369 -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 +264 -133
  41. package/build/internal/impostor/impostor-node-material.js.map +4 -4
  42. package/build/internal/pixel-impostor/pixel-impostor-display.js +84 -38
  43. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +4 -4
  44. package/build/internal/rendering/captured-surface-depth.js +35 -0
  45. package/build/internal/rendering/captured-surface-depth.js.map +7 -0
  46. package/build/internal/rendering/captured-surface-material.js +28 -0
  47. package/build/internal/rendering/captured-surface-material.js.map +7 -0
  48. package/build/internal/rendering/hull-effect-runtime.js +59 -27
  49. package/build/internal/rendering/hull-effect-runtime.js.map +3 -3
  50. package/build/internal/rendering/renderer-support.js +31 -0
  51. package/build/internal/rendering/renderer-support.js.map +7 -0
  52. package/build/internal/rendering/wireframe.js +41 -9
  53. package/build/internal/rendering/wireframe.js.map +2 -2
  54. package/build/internal/rendering/wrap-preview.js +59 -27
  55. package/build/internal/rendering/wrap-preview.js.map +3 -3
  56. package/build/pixel-impostor-runtime.js +84 -38
  57. package/build/pixel-impostor-runtime.js.map +4 -4
  58. package/build/types/box-impostor/box-impostor-display-settings.d.ts +30 -0
  59. package/build/types/box-impostor/box-impostor-display-settings.js +1 -0
  60. package/build/types/box-impostor/box-impostor-display.d.ts +13 -0
  61. package/build/types/box-impostor/box-impostor-display.js +1 -0
  62. package/build/types/box-impostor/box-impostor-face-axes.d.ts +5 -0
  63. package/build/types/box-impostor/box-impostor-face-axes.js +1 -0
  64. package/build/types/box-impostor/box-impostor-faces.d.ts +11 -0
  65. package/build/types/box-impostor/box-impostor-faces.js +1 -0
  66. package/build/types/box-impostor/box-impostor-geometry.d.ts +5 -0
  67. package/build/types/box-impostor/box-impostor-geometry.js +1 -0
  68. package/build/types/box-impostor/box-impostor-material.d.ts +14 -0
  69. package/build/types/box-impostor/box-impostor-material.js +1 -0
  70. package/build/types/box-impostor/box-impostor-metadata.d.ts +31 -0
  71. package/build/types/box-impostor/box-impostor-metadata.js +1 -0
  72. package/build/types/box-impostor/box-impostor-projection.d.ts +19 -0
  73. package/build/types/box-impostor/box-impostor-projection.js +1 -0
  74. package/build/types/box-impostor/box-impostor-runtime.d.ts +19 -0
  75. package/build/types/box-impostor/box-impostor-runtime.js +1 -0
  76. package/build/types/box-impostor/box-impostor-surface-search.d.ts +13 -0
  77. package/build/types/box-impostor/box-impostor-surface-search.js +1 -0
  78. package/build/types/deformation/cloth-surface.d.ts +1 -0
  79. package/build/types/deformation/pin-cloth.d.ts +1 -0
  80. package/build/types/impostor/impostor-node-material.d.ts +2 -15
  81. package/build/types/public/box-impostor-runtime.d.ts +6 -0
  82. package/build/types/public/box-impostor-runtime.js +1 -0
  83. package/build/types/public/hull-effects.d.ts +1 -0
  84. package/build/types/public/index.d.ts +1 -1
  85. package/build/types/rendering/captured-surface-depth.d.ts +5 -0
  86. package/build/types/rendering/captured-surface-depth.js +1 -0
  87. package/build/types/rendering/captured-surface-material.d.ts +8 -0
  88. package/build/types/rendering/captured-surface-material.js +1 -0
  89. package/build/types/rendering/renderer-support.d.ts +27 -0
  90. package/build/types/rendering/renderer-support.js +1 -0
  91. package/build/types/rendering/wireframe.d.ts +2 -2
  92. package/package.json +29 -8
  93. package/src/box-impostor/box-impostor-display-settings.js +54 -0
  94. package/src/box-impostor/box-impostor-display.js +50 -0
  95. package/src/box-impostor/box-impostor-face-axes.js +11 -0
  96. package/src/box-impostor/box-impostor-faces.js +118 -0
  97. package/src/box-impostor/box-impostor-geometry.js +140 -0
  98. package/src/box-impostor/box-impostor-material.js +771 -0
  99. package/src/box-impostor/box-impostor-metadata.js +122 -0
  100. package/src/box-impostor/box-impostor-projection.js +276 -0
  101. package/src/box-impostor/box-impostor-runtime.js +124 -0
  102. package/src/box-impostor/box-impostor-surface-search.js +11 -0
  103. package/src/deformation/cloth-surface.ts +28 -17
  104. package/src/deformation/pin-cloth.ts +9 -3
  105. package/src/impostor/impostor-node-material.js +188 -157
  106. package/src/pixel-impostor/pixel-impostor-display.js +5 -13
  107. package/src/public/box-impostor-runtime.js +14 -0
  108. package/src/public/hull-effects.js +1 -0
  109. package/src/public/index.js +10 -1
  110. package/src/rendering/captured-surface-depth.js +55 -0
  111. package/src/rendering/captured-surface-material.js +27 -0
  112. package/src/rendering/renderer-support.js +50 -0
  113. package/src/rendering/wireframe.js +51 -13
@@ -0,0 +1,50 @@
1
+ // @ts-check
2
+ import * as TSL from 'three/tsl';
3
+
4
+ /**
5
+ * @typedef {object} ImpostorRenderer
6
+ * @property {boolean=} isWebGPURenderer
7
+ * @property {boolean=} isWebGLRenderer
8
+ * @property {(object & {supportsNodeMaterials?: boolean})=} capabilities
9
+ * @property {((handler: import('three/addons/tsl/WebGLNodesHandler.js').WebGLNodesHandler) => void)=} setNodesHandler
10
+ */
11
+
12
+ /**
13
+ * Reports whether a renderer can compile Baker's TSL impostor material.
14
+ * WebGPURenderer supports node materials directly. Classic WebGLRenderer
15
+ * integrations must advertise that they installed a compatible nodes handler.
16
+ *
17
+ * @param {ImpostorRenderer | undefined} renderer
18
+ */
19
+ export function rendererSupportsImpostorNodeMaterials(renderer) {
20
+ if (typeof Reflect.get(TSL, 'struct') !== 'function') return false;
21
+ return renderer?.isWebGPURenderer === true
22
+ || renderer?.capabilities?.supportsNodeMaterials === true;
23
+ }
24
+
25
+ /** @type {WeakMap<ImpostorRenderer, Promise<void>>} */
26
+ const pending = new WeakMap();
27
+
28
+ /** Enable Three's existing TSL handler only when an enabled asset needs it.
29
+ * Hosts with their own compiler advertise supportsNodeMaterials to retain it.
30
+ * @param {ImpostorRenderer | undefined} renderer */
31
+ export async function enableRendererNodeMaterials(renderer) {
32
+ if (rendererSupportsImpostorNodeMaterials(renderer) ||
33
+ !renderer?.isWebGLRenderer || !renderer.setNodesHandler ||
34
+ typeof Reflect.get(TSL, 'struct') !== 'function') return;
35
+ let setup = pending.get(renderer);
36
+ if (!setup) {
37
+ setup = import('three/addons/tsl/WebGLNodesHandler.js').then(({WebGLNodesHandler}) => {
38
+ if (rendererSupportsImpostorNodeMaterials(renderer)) return;
39
+ renderer.setNodesHandler?.(new WebGLNodesHandler());
40
+ renderer.capabilities ??= {};
41
+ renderer.capabilities.supportsNodeMaterials = true;
42
+ }).catch(error => {
43
+ pending.delete(renderer);
44
+ throw error;
45
+ });
46
+ pending.set(renderer, setup);
47
+ }
48
+ await setup;
49
+ }
50
+
@@ -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
  }