@needle-tools/three-mesh-effects 0.1.0-alpha → 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 (86) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +94 -88
  3. package/build/deformation-runtime.js.map +1 -1
  4. package/build/impostor-runtime-needle-engine.js.map +1 -1
  5. package/build/impostor-runtime.js.map +1 -1
  6. package/build/internal/deformation/cloth-surface.js.map +1 -1
  7. package/build/internal/deformation/fur-surface.js.map +1 -1
  8. package/build/internal/deformation/ik-constraints.js.map +1 -1
  9. package/build/internal/deformation/joints-json.js.map +1 -1
  10. package/build/internal/deformation/limits.js.map +1 -1
  11. package/build/internal/deformation/motion-settings.js.map +1 -1
  12. package/build/internal/deformation/pin-cloth.js.map +1 -1
  13. package/build/internal/deformation/pin-ik.js.map +1 -1
  14. package/build/internal/deformation/pin-types.js.map +1 -1
  15. package/build/internal/deformation/pin-wiggle.js.map +1 -1
  16. package/build/internal/deformation/pose-slots.js.map +1 -1
  17. package/build/internal/deformation/pose-timeline.js.map +1 -1
  18. package/build/internal/deformation/rig-drivers.js.map +1 -1
  19. package/build/internal/deformation/rig-extension.js.map +1 -1
  20. package/build/internal/deformation/rig-runtime.js.map +1 -1
  21. package/build/internal/deformation/wind.js.map +1 -1
  22. package/build/internal/impostor/impostor-metadata.js.map +1 -1
  23. package/build/internal/impostor/impostor-node-material.js.map +1 -1
  24. package/build/internal/impostor/impostor-runtime-support.js.map +1 -1
  25. package/build/internal/impostor/octahedral.js.map +1 -1
  26. package/build/internal/pixel-impostor/pixel-impostor-display.js.map +1 -1
  27. package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +1 -1
  28. package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +1 -1
  29. package/build/internal/rendering/skin-projection.js +52 -52
  30. package/build/internal/rendering/skin-projection.js.map +2 -2
  31. package/build/internal/rendering/surface-query.js +52 -52
  32. package/build/internal/rendering/surface-query.js.map +2 -2
  33. package/build/internal/rendering/wireframe.js.map +1 -1
  34. package/build/internal/rendering/wrap-preview-settings.js.map +1 -1
  35. package/build/internal/rendering/wrap-preview.js +52 -52
  36. package/build/internal/rendering/wrap-preview.js.map +2 -2
  37. package/build/internal/rendering/wrap-skin.js +52 -52
  38. package/build/internal/rendering/wrap-skin.js.map +2 -2
  39. package/build/pixel-impostor-runtime.js.map +1 -1
  40. package/build/wrap-effects.js +52 -52
  41. package/build/wrap-effects.js.map +2 -2
  42. package/package.json +107 -94
  43. package/src/deformation/cloth-surface.ts +223 -223
  44. package/src/deformation/fur-surface.ts +129 -129
  45. package/src/deformation/ik-constraints.ts +37 -37
  46. package/src/deformation/joints-json.ts +94 -94
  47. package/src/deformation/limits.ts +25 -25
  48. package/src/deformation/motion-settings.ts +9 -9
  49. package/src/deformation/pin-cloth.ts +149 -149
  50. package/src/deformation/pin-ik.ts +582 -582
  51. package/src/deformation/pin-types.ts +22 -22
  52. package/src/deformation/pin-wiggle.ts +119 -119
  53. package/src/deformation/pose-slots.ts +106 -106
  54. package/src/deformation/pose-timeline.ts +242 -242
  55. package/src/deformation/rig-drivers.ts +32 -32
  56. package/src/deformation/rig-extension.ts +87 -87
  57. package/src/deformation/rig-runtime.ts +416 -416
  58. package/src/deformation/wind.ts +36 -36
  59. package/src/impostor/impostor-metadata.js +137 -137
  60. package/src/impostor/impostor-node-material.js +1581 -1581
  61. package/src/impostor/impostor-runtime-support.js +139 -139
  62. package/src/impostor/octahedral.js +280 -280
  63. package/src/pixel-impostor/pixel-impostor-display.js +794 -794
  64. package/src/pixel-impostor/pixel-impostor-metadata.js +74 -74
  65. package/src/pixel-impostor/pixel-impostor-runtime.js +78 -78
  66. package/src/public/deformation-runtime.ts +3 -3
  67. package/src/public/impostor-runtime-needle-engine.js +56 -56
  68. package/src/public/impostor-runtime.js +21 -21
  69. package/src/public/pixel-impostor-runtime.js +5 -5
  70. package/src/public/wrap-effects.js +21 -21
  71. package/src/rendering/skin-projection.js +113 -113
  72. package/src/rendering/surface-query.js +88 -88
  73. package/src/rendering/wireframe.js +115 -115
  74. package/src/rendering/wrap-preview-settings.js +123 -123
  75. package/src/rendering/wrap-preview.js +645 -645
  76. package/src/rendering/wrap-skin.js +109 -109
  77. package/build/internal/public/deformation-runtime.js +0 -2143
  78. package/build/internal/public/deformation-runtime.js.map +0 -7
  79. package/build/internal/public/impostor-runtime-needle-engine.js +0 -42
  80. package/build/internal/public/impostor-runtime-needle-engine.js.map +0 -7
  81. package/build/internal/public/impostor-runtime.js +0 -1333
  82. package/build/internal/public/impostor-runtime.js.map +0 -7
  83. package/build/internal/public/pixel-impostor-runtime.js +0 -876
  84. package/build/internal/public/pixel-impostor-runtime.js.map +0 -7
  85. package/build/internal/public/wrap-effects.js +0 -5640
  86. package/build/internal/public/wrap-effects.js.map +0 -7
@@ -1,109 +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
- }
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
+ }