@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.
- package/LICENSE +21 -21
- package/README.md +94 -88
- package/build/deformation-runtime.js.map +1 -1
- package/build/impostor-runtime-needle-engine.js.map +1 -1
- package/build/impostor-runtime.js.map +1 -1
- package/build/internal/deformation/cloth-surface.js.map +1 -1
- package/build/internal/deformation/fur-surface.js.map +1 -1
- package/build/internal/deformation/ik-constraints.js.map +1 -1
- package/build/internal/deformation/joints-json.js.map +1 -1
- package/build/internal/deformation/limits.js.map +1 -1
- package/build/internal/deformation/motion-settings.js.map +1 -1
- package/build/internal/deformation/pin-cloth.js.map +1 -1
- package/build/internal/deformation/pin-ik.js.map +1 -1
- package/build/internal/deformation/pin-types.js.map +1 -1
- package/build/internal/deformation/pin-wiggle.js.map +1 -1
- package/build/internal/deformation/pose-slots.js.map +1 -1
- package/build/internal/deformation/pose-timeline.js.map +1 -1
- package/build/internal/deformation/rig-drivers.js.map +1 -1
- package/build/internal/deformation/rig-extension.js.map +1 -1
- package/build/internal/deformation/rig-runtime.js.map +1 -1
- package/build/internal/deformation/wind.js.map +1 -1
- package/build/internal/impostor/impostor-metadata.js.map +1 -1
- package/build/internal/impostor/impostor-node-material.js.map +1 -1
- package/build/internal/impostor/impostor-runtime-support.js.map +1 -1
- package/build/internal/impostor/octahedral.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +1 -1
- package/build/internal/rendering/skin-projection.js +52 -52
- package/build/internal/rendering/skin-projection.js.map +2 -2
- package/build/internal/rendering/surface-query.js +52 -52
- package/build/internal/rendering/surface-query.js.map +2 -2
- package/build/internal/rendering/wireframe.js.map +1 -1
- package/build/internal/rendering/wrap-preview-settings.js.map +1 -1
- package/build/internal/rendering/wrap-preview.js +52 -52
- package/build/internal/rendering/wrap-preview.js.map +2 -2
- package/build/internal/rendering/wrap-skin.js +52 -52
- package/build/internal/rendering/wrap-skin.js.map +2 -2
- package/build/pixel-impostor-runtime.js.map +1 -1
- package/build/wrap-effects.js +52 -52
- package/build/wrap-effects.js.map +2 -2
- package/package.json +107 -94
- package/src/deformation/cloth-surface.ts +223 -223
- package/src/deformation/fur-surface.ts +129 -129
- package/src/deformation/ik-constraints.ts +37 -37
- package/src/deformation/joints-json.ts +94 -94
- package/src/deformation/limits.ts +25 -25
- package/src/deformation/motion-settings.ts +9 -9
- package/src/deformation/pin-cloth.ts +149 -149
- package/src/deformation/pin-ik.ts +582 -582
- package/src/deformation/pin-types.ts +22 -22
- package/src/deformation/pin-wiggle.ts +119 -119
- package/src/deformation/pose-slots.ts +106 -106
- package/src/deformation/pose-timeline.ts +242 -242
- package/src/deformation/rig-drivers.ts +32 -32
- package/src/deformation/rig-extension.ts +87 -87
- package/src/deformation/rig-runtime.ts +416 -416
- package/src/deformation/wind.ts +36 -36
- package/src/impostor/impostor-metadata.js +137 -137
- package/src/impostor/impostor-node-material.js +1581 -1581
- package/src/impostor/impostor-runtime-support.js +139 -139
- package/src/impostor/octahedral.js +280 -280
- package/src/pixel-impostor/pixel-impostor-display.js +794 -794
- package/src/pixel-impostor/pixel-impostor-metadata.js +74 -74
- package/src/pixel-impostor/pixel-impostor-runtime.js +78 -78
- package/src/public/deformation-runtime.ts +3 -3
- package/src/public/impostor-runtime-needle-engine.js +56 -56
- package/src/public/impostor-runtime.js +21 -21
- package/src/public/pixel-impostor-runtime.js +5 -5
- package/src/public/wrap-effects.js +21 -21
- package/src/rendering/skin-projection.js +113 -113
- package/src/rendering/surface-query.js +88 -88
- package/src/rendering/wireframe.js +115 -115
- package/src/rendering/wrap-preview-settings.js +123 -123
- package/src/rendering/wrap-preview.js +645 -645
- package/src/rendering/wrap-skin.js +109 -109
- package/build/internal/public/deformation-runtime.js +0 -2143
- package/build/internal/public/deformation-runtime.js.map +0 -7
- package/build/internal/public/impostor-runtime-needle-engine.js +0 -42
- package/build/internal/public/impostor-runtime-needle-engine.js.map +0 -7
- package/build/internal/public/impostor-runtime.js +0 -1333
- package/build/internal/public/impostor-runtime.js.map +0 -7
- package/build/internal/public/pixel-impostor-runtime.js +0 -876
- package/build/internal/public/pixel-impostor-runtime.js.map +0 -7
- package/build/internal/public/wrap-effects.js +0 -5640
- 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
|
+
}
|