@needle-tools/three-mesh-effects 0.0.0-stage → 0.1.0-alpha.0

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 (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -2
  3. package/build/deformation-runtime.js +2143 -0
  4. package/build/deformation-runtime.js.map +7 -0
  5. package/build/impostor-runtime-needle-engine.js +42 -0
  6. package/build/impostor-runtime-needle-engine.js.map +7 -0
  7. package/build/impostor-runtime.js +1333 -0
  8. package/build/impostor-runtime.js.map +7 -0
  9. package/build/internal/deformation/cloth-surface.js +288 -0
  10. package/build/internal/deformation/cloth-surface.js.map +7 -0
  11. package/build/internal/deformation/fur-surface.js +130 -0
  12. package/build/internal/deformation/fur-surface.js.map +7 -0
  13. package/build/internal/deformation/ik-constraints.js +44 -0
  14. package/build/internal/deformation/ik-constraints.js.map +7 -0
  15. package/build/internal/deformation/joints-json.js +197 -0
  16. package/build/internal/deformation/joints-json.js.map +7 -0
  17. package/build/internal/deformation/limits.js +16 -0
  18. package/build/internal/deformation/limits.js.map +7 -0
  19. package/build/internal/deformation/motion-settings.js +12 -0
  20. package/build/internal/deformation/motion-settings.js.map +7 -0
  21. package/build/internal/deformation/pin-cloth.js +196 -0
  22. package/build/internal/deformation/pin-cloth.js.map +7 -0
  23. package/build/internal/deformation/pin-ik.js +650 -0
  24. package/build/internal/deformation/pin-ik.js.map +7 -0
  25. package/build/internal/deformation/pin-types.js +12 -0
  26. package/build/internal/deformation/pin-types.js.map +7 -0
  27. package/build/internal/deformation/pin-wiggle.js +249 -0
  28. package/build/internal/deformation/pin-wiggle.js.map +7 -0
  29. package/build/internal/deformation/pose-slots.js +84 -0
  30. package/build/internal/deformation/pose-slots.js.map +7 -0
  31. package/build/internal/deformation/pose-timeline.js +210 -0
  32. package/build/internal/deformation/pose-timeline.js.map +7 -0
  33. package/build/internal/deformation/rig-drivers.js +48 -0
  34. package/build/internal/deformation/rig-drivers.js.map +7 -0
  35. package/build/internal/deformation/rig-extension.js +334 -0
  36. package/build/internal/deformation/rig-extension.js.map +7 -0
  37. package/build/internal/deformation/rig-runtime.js +2141 -0
  38. package/build/internal/deformation/rig-runtime.js.map +7 -0
  39. package/build/internal/deformation/wind.js +34 -0
  40. package/build/internal/deformation/wind.js.map +7 -0
  41. package/build/internal/impostor/impostor-metadata.js +77 -0
  42. package/build/internal/impostor/impostor-metadata.js.map +7 -0
  43. package/build/internal/impostor/impostor-node-material.js +1331 -0
  44. package/build/internal/impostor/impostor-node-material.js.map +7 -0
  45. package/build/internal/impostor/impostor-runtime-support.js +162 -0
  46. package/build/internal/impostor/impostor-runtime-support.js.map +7 -0
  47. package/build/internal/impostor/octahedral.js +232 -0
  48. package/build/internal/impostor/octahedral.js.map +7 -0
  49. package/build/internal/pixel-impostor/pixel-impostor-display.js +728 -0
  50. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +7 -0
  51. package/build/internal/pixel-impostor/pixel-impostor-metadata.js +71 -0
  52. package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +7 -0
  53. package/build/internal/pixel-impostor/pixel-impostor-runtime.js +150 -0
  54. package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +7 -0
  55. package/build/internal/rendering/skin-projection.js +4630 -0
  56. package/build/internal/rendering/skin-projection.js.map +7 -0
  57. package/build/internal/rendering/surface-query.js +4519 -0
  58. package/build/internal/rendering/surface-query.js.map +7 -0
  59. package/build/internal/rendering/wireframe.js +112 -0
  60. package/build/internal/rendering/wireframe.js.map +7 -0
  61. package/build/internal/rendering/wrap-preview-settings.js +126 -0
  62. package/build/internal/rendering/wrap-preview-settings.js.map +7 -0
  63. package/build/internal/rendering/wrap-preview.js +5610 -0
  64. package/build/internal/rendering/wrap-preview.js.map +7 -0
  65. package/build/internal/rendering/wrap-skin.js +4746 -0
  66. package/build/internal/rendering/wrap-skin.js.map +7 -0
  67. package/build/pixel-impostor-runtime.js +876 -0
  68. package/build/pixel-impostor-runtime.js.map +7 -0
  69. package/build/types/deformation/cloth-surface.d.ts +39 -0
  70. package/build/types/deformation/cloth-surface.js +1 -0
  71. package/build/types/deformation/fur-surface.d.ts +27 -0
  72. package/build/types/deformation/fur-surface.js +1 -0
  73. package/build/types/deformation/ik-constraints.d.ts +18 -0
  74. package/build/types/deformation/ik-constraints.js +1 -0
  75. package/build/types/deformation/joints-json.d.ts +17 -0
  76. package/build/types/deformation/joints-json.js +1 -0
  77. package/build/types/deformation/limits.d.ts +23 -0
  78. package/build/types/deformation/limits.js +1 -0
  79. package/build/types/deformation/motion-settings.d.ts +9 -0
  80. package/build/types/deformation/motion-settings.js +1 -0
  81. package/build/types/deformation/pin-cloth.d.ts +28 -0
  82. package/build/types/deformation/pin-cloth.js +1 -0
  83. package/build/types/deformation/pin-ik.d.ts +37 -0
  84. package/build/types/deformation/pin-ik.js +1 -0
  85. package/build/types/deformation/pin-types.d.ts +53 -0
  86. package/build/types/deformation/pin-types.js +1 -0
  87. package/build/types/deformation/pin-wiggle.d.ts +16 -0
  88. package/build/types/deformation/pin-wiggle.js +1 -0
  89. package/build/types/deformation/pose-slots.d.ts +41 -0
  90. package/build/types/deformation/pose-slots.js +1 -0
  91. package/build/types/deformation/pose-timeline.d.ts +65 -0
  92. package/build/types/deformation/pose-timeline.js +1 -0
  93. package/build/types/deformation/rig-drivers.d.ts +19 -0
  94. package/build/types/deformation/rig-drivers.js +1 -0
  95. package/build/types/deformation/rig-extension.d.ts +33 -0
  96. package/build/types/deformation/rig-extension.js +1 -0
  97. package/build/types/deformation/rig-runtime.d.ts +95 -0
  98. package/build/types/deformation/rig-runtime.js +1 -0
  99. package/build/types/deformation/wind.d.ts +16 -0
  100. package/build/types/deformation/wind.js +1 -0
  101. package/build/types/impostor/impostor-metadata.d.ts +61 -0
  102. package/build/types/impostor/impostor-metadata.js +1 -0
  103. package/build/types/impostor/impostor-node-material.d.ts +140 -0
  104. package/build/types/impostor/impostor-node-material.js +1 -0
  105. package/build/types/impostor/impostor-runtime-support.d.ts +52 -0
  106. package/build/types/impostor/impostor-runtime-support.js +1 -0
  107. package/build/types/impostor/octahedral.d.ts +84 -0
  108. package/build/types/impostor/octahedral.js +1 -0
  109. package/build/types/pixel-impostor/pixel-impostor-display.d.ts +48 -0
  110. package/build/types/pixel-impostor/pixel-impostor-display.js +1 -0
  111. package/build/types/pixel-impostor/pixel-impostor-metadata.d.ts +34 -0
  112. package/build/types/pixel-impostor/pixel-impostor-metadata.js +1 -0
  113. package/build/types/pixel-impostor/pixel-impostor-runtime.d.ts +18 -0
  114. package/build/types/pixel-impostor/pixel-impostor-runtime.js +1 -0
  115. package/build/types/public/deformation-runtime.d.ts +3 -0
  116. package/build/types/public/deformation-runtime.js +1 -0
  117. package/build/types/public/impostor-runtime-needle-engine.d.ts +31 -0
  118. package/build/types/public/impostor-runtime-needle-engine.js +1 -0
  119. package/build/types/public/impostor-runtime.d.ts +6 -0
  120. package/build/types/public/impostor-runtime.js +1 -0
  121. package/build/types/public/pixel-impostor-runtime.d.ts +3 -0
  122. package/build/types/public/pixel-impostor-runtime.js +1 -0
  123. package/build/types/public/wrap-effects.d.ts +4 -0
  124. package/build/types/public/wrap-effects.js +1 -0
  125. package/build/types/rendering/skin-projection.d.ts +44 -0
  126. package/build/types/rendering/skin-projection.js +1 -0
  127. package/build/types/rendering/surface-query.d.ts +66 -0
  128. package/build/types/rendering/surface-query.js +1 -0
  129. package/build/types/rendering/wireframe.d.ts +25 -0
  130. package/build/types/rendering/wireframe.js +1 -0
  131. package/build/types/rendering/wrap-preview-settings.d.ts +22 -0
  132. package/build/types/rendering/wrap-preview-settings.js +1 -0
  133. package/build/types/rendering/wrap-preview.d.ts +39 -0
  134. package/build/types/rendering/wrap-preview.js +1 -0
  135. package/build/types/rendering/wrap-skin.d.ts +14 -0
  136. package/build/types/rendering/wrap-skin.js +1 -0
  137. package/build/wrap-effects.js +5640 -0
  138. package/build/wrap-effects.js.map +7 -0
  139. package/package.json +105 -4
  140. package/src/deformation/cloth-surface.ts +223 -0
  141. package/src/deformation/fur-surface.ts +129 -0
  142. package/src/deformation/ik-constraints.ts +37 -0
  143. package/src/deformation/joints-json.ts +94 -0
  144. package/src/deformation/limits.ts +25 -0
  145. package/src/deformation/motion-settings.ts +9 -0
  146. package/src/deformation/pin-cloth.ts +149 -0
  147. package/src/deformation/pin-ik.ts +582 -0
  148. package/src/deformation/pin-types.ts +22 -0
  149. package/src/deformation/pin-wiggle.ts +119 -0
  150. package/src/deformation/pose-slots.ts +106 -0
  151. package/src/deformation/pose-timeline.ts +242 -0
  152. package/src/deformation/rig-drivers.ts +32 -0
  153. package/src/deformation/rig-extension.ts +87 -0
  154. package/src/deformation/rig-runtime.ts +416 -0
  155. package/src/deformation/wind.ts +36 -0
  156. package/src/impostor/impostor-metadata.js +137 -0
  157. package/src/impostor/impostor-node-material.js +1581 -0
  158. package/src/impostor/impostor-runtime-support.js +139 -0
  159. package/src/impostor/octahedral.js +280 -0
  160. package/src/pixel-impostor/pixel-impostor-display.js +794 -0
  161. package/src/pixel-impostor/pixel-impostor-metadata.js +74 -0
  162. package/src/pixel-impostor/pixel-impostor-runtime.js +78 -0
  163. package/src/public/deformation-runtime.ts +3 -0
  164. package/src/public/impostor-runtime-needle-engine.js +56 -0
  165. package/src/public/impostor-runtime.js +21 -0
  166. package/src/public/pixel-impostor-runtime.js +5 -0
  167. package/src/public/wrap-effects.js +21 -0
  168. package/src/rendering/skin-projection.js +113 -0
  169. package/src/rendering/surface-query.js +88 -0
  170. package/src/rendering/wireframe.js +115 -0
  171. package/src/rendering/wrap-preview-settings.js +123 -0
  172. package/src/rendering/wrap-preview.js +645 -0
  173. package/src/rendering/wrap-skin.js +109 -0
@@ -0,0 +1,109 @@
1
+ // @ts-check
2
+ import * as THREE from 'three';
3
+ import { mergeSkinGeometry, projectSkinGeometry } from './skin-projection.js';
4
+
5
+ /** @type {WeakMap<THREE.Object3D, THREE.Matrix4>} */
6
+ const restMatrices = new WeakMap();
7
+ /** @param {THREE.Object3D} root */
8
+ export function captureWrapSkinPose(root) {
9
+ root.traverse(node => { node.updateMatrix(); restMatrices.set(node, node.matrix.clone()); });
10
+ }
11
+
12
+ /** Project preview surfaces to a source rig in the wrap's local frame.
13
+ * The rig is borrowed from the cloned original, never from the source viewer.
14
+ * @param {THREE.Object3D} original @param {THREE.Object3D} copy @param {THREE.Object3D} asset
15
+ * @param {boolean} [animate]
16
+ */
17
+ export function createWrapSkin(original, copy, asset, animate) {
18
+ /** @type {THREE.Object3D[]} */
19
+ const sourceNodes = [];
20
+ /** @type {THREE.Object3D[]} */
21
+ const copyNodes = [];
22
+ original.traverse(n => sourceNodes.push(n)); copy.traverse(n => copyNodes.push(n));
23
+ if (!(animate ?? sourceNodes.some(n => n instanceof THREE.SkinnedMesh))) return null;
24
+ const clones = new Map(sourceNodes.map((n, i) => [n, copyNodes[i]]));
25
+ for (const source of sourceNodes) {
26
+ const target = clones.get(source), rest = restMatrices.get(source);
27
+ if (target && rest && source !== original) {
28
+ target.matrix.copy(rest); target.matrix.decompose(target.position, target.quaternion, target.scale);
29
+ }
30
+ }
31
+ // Before attachment, explicitly supply the eventual parent frame.
32
+ copy.matrixWorld.multiplyMatrices(asset.matrixWorld, copy.matrix);
33
+ for (const child of copy.children) child.updateMatrixWorld(true);
34
+ const bones = [], inverses = [], surfaces = [];
35
+ const boneIds = new Map();
36
+ const worldToAsset = asset.matrixWorld.clone().invert();
37
+ const p = new THREE.Vector3();
38
+ for (const source of sourceNodes) {
39
+ if (!(source instanceof THREE.Mesh) || source.userData.previewWireframeOverlay) continue;
40
+ const geometry = source.geometry, pos = geometry.getAttribute('position');
41
+ if (!pos) continue;
42
+ const posedSource = clones.get(source);
43
+ if (!(posedSource instanceof THREE.Mesh)) continue;
44
+ const sourceWorld = posedSource.matrixWorld.clone();
45
+ const matrix = worldToAsset.clone().multiply(sourceWorld);
46
+ const positions = new Float32Array(pos.count * 3), ids = new Float32Array(pos.count * 4), weights = new Float32Array(ids.length);
47
+ const mapping = [];
48
+ if (source instanceof THREE.SkinnedMesh) {
49
+ for (let i = 0; i < source.skeleton.bones.length; i++) {
50
+ const bone = clones.get(source.skeleton.bones[i]);
51
+ if (!(bone instanceof THREE.Bone)) throw new Error('Source skin references a bone outside its scene.');
52
+ let id = boneIds.get(bone);
53
+ if (id === undefined) {
54
+ id = bones.length; boneIds.set(bone, id);
55
+ bones.push(bone); inverses.push(bone.matrixWorld.clone().invert());
56
+ }
57
+ mapping.push(id);
58
+ }
59
+ } else {
60
+ const bone = new THREE.Bone();
61
+ clones.get(source)?.add(bone);
62
+ mapping.push(bones.length); bones.push(bone); inverses.push(sourceWorld.clone().invert());
63
+ }
64
+ const j = geometry.getAttribute('skinIndex'), w = geometry.getAttribute('skinWeight');
65
+ for (let i = 0; i < pos.count; i++) {
66
+ p.fromBufferAttribute(pos, i).applyMatrix4(matrix).toArray(positions, i * 3);
67
+ if (posedSource instanceof THREE.SkinnedMesh) posedSource.getVertexPosition(i, p).applyMatrix4(matrix).toArray(positions, i * 3);
68
+ if (source instanceof THREE.SkinnedMesh && j && w) {
69
+ for (let k = 0; k < 4; k++) { ids[i * 4 + k] = mapping[j.getComponent(i, k)]; weights[i * 4 + k] = w.getComponent(i, k); }
70
+ } else { ids[i * 4] = mapping[0]; weights[i * 4] = 1; }
71
+ }
72
+ surfaces.push({ positions, indices: geometry.index ? Uint32Array.from(geometry.index.array) : Uint32Array.from({ length: pos.count }, (_, i) => i), attributes: new Map([
73
+ ['POSITION', { size: 3, data: positions }], ['JOINTS_0', { size: 4, data: ids }], ['WEIGHTS_0', { size: 4, data: weights }],
74
+ ]) });
75
+ }
76
+ const surface = mergeSkinGeometry(surfaces);
77
+ const skeleton = new THREE.Skeleton(bones, inverses);
78
+ return {
79
+ /** @param {THREE.BufferGeometry} geometry @param {THREE.Material | THREE.Material[]} material */
80
+ mesh(geometry, material) {
81
+ const position = geometry.getAttribute('position');
82
+ const positions = Float32Array.from(position.array);
83
+ const projected = projectSkinGeometry(surface, { positions, indices: new Uint32Array() });
84
+ const joints = projected.attributes.get('JOINTS_0'), weights = projected.attributes.get('WEIGHTS_0');
85
+ if (!joints || !weights) throw new Error('Missing projected skin influences.');
86
+ geometry.setAttribute('skinIndex', new THREE.Uint16BufferAttribute(joints.data, 4));
87
+ geometry.setAttribute('skinWeight', new THREE.Float32BufferAttribute(weights.data, 4));
88
+ const mesh = new THREE.SkinnedMesh(geometry, material);
89
+ mesh.bind(skeleton, asset.matrixWorld);
90
+ return mesh;
91
+ },
92
+ update() {
93
+ // Copy transforms only; preserve cloned resources and the wrapper frame.
94
+ for (const source of sourceNodes) {
95
+ const target = clones.get(source);
96
+ if (!target || source === original) continue;
97
+ target.position.copy(source.position); target.quaternion.copy(source.quaternion); target.scale.copy(source.scale);
98
+ target.updateMatrix();
99
+ }
100
+ copy.updateWorldMatrix(true, true);
101
+ skeleton.update();
102
+ },
103
+ dispose() {
104
+ skeleton.dispose();
105
+ const borrowedCopies = new Set(copyNodes.filter(n => n instanceof THREE.SkinnedMesh).map(n => n.skeleton));
106
+ borrowedCopies.forEach(s => s.dispose());
107
+ },
108
+ };
109
+ }