-
Notifications
You must be signed in to change notification settings - Fork 0
/
recalculateVertices.js
32 lines (29 loc) · 1.12 KB
/
recalculateVertices.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// this function recalculates and updates the vertices of a given skinned mesh
function recalculateVertices (skinnedMesh) {
var cleanVertices = [];
for (var i = 0; i < skinnedMesh.geometry.vertices.length; i++) {
cleanVertices.push(skinnedMesh.geometry.vertices[i].clone());
}
var newVerts = [];
var sIndices = skinnedMesh.geometry.skinIndices;
var sWeights = skinnedMesh.geometry.skinWeights;
for (var i = 0; i < cleanVertices.length; i++) {
var cVert = cleanVertices[i].clone().applyMatrix4(skinnedMesh.bindMatrix),
sI = sIndices[i],
sW = sWeights[i],
bones = [],
pos = new THREE.Vector3(0, 0, 0),
tempVector = new THREE.Vector3(0, 0, 0),
tempMatrix = new THREE.Matrix4();
bones[0] = sI.x;
bones[1] = sI.y;
bones[2] = sI.z;
bones[3] = sI.w;
for (var j = 0; j < 4; j++) {
tempMatrix.multiplyMatrices(skinnedMesh.skeleton.bones[bones[j]].matrixWorld, skinnedMesh.skeleton.boneInverses[bones[j]]);
pos.add(tempVector.copy(cVert).applyMatrix4(tempMatrix).multiplyScalar(sW.getComponent(j)));
}
newVerts.push(pos);
}
skinnedMesh.geometry.vertices = newVerts;
}