@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
@@ -0,0 +1,27 @@
1
+ // @ts-check
2
+
3
+ /** Keep material-slot texture replacement connected to the shader nodes.
4
+ * @param {import('three').Material} material
5
+ * @param {string} slot
6
+ * @param {import('three').Texture} initial
7
+ * @param {{value: import('three').Texture}[]} nodes
8
+ * @param {((value: import('three').Texture) => void)=} onChange
9
+ */
10
+ export function bindTextureSlot(material, slot, initial, nodes, onChange) {
11
+ let current = initial;
12
+ Object.defineProperty(material, slot, {
13
+ configurable: true, enumerable: true,
14
+ get: () => current,
15
+ set: (/** @type {import('three').Texture} */ value) => {
16
+ value.channel = current.channel;
17
+ value.offset = current.offset;
18
+ value.repeat = current.repeat;
19
+ value.center = current.center;
20
+ value.rotation = current.rotation;
21
+ current = value;
22
+ for (const node of nodes) node.value = value;
23
+ onChange?.(value);
24
+ material.needsUpdate = true;
25
+ },
26
+ });
27
+ }
@@ -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
  }