-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfix-bones-scale.ts
58 lines (52 loc) · 1.9 KB
/
fix-bones-scale.ts
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as THREE from "three";
import { Group } from "three";
import { getBonesBoundingBox } from "./getBonesBoundingBox";
import { LogMessage, Step, StepResult } from "./types";
const scaleCorrection = new THREE.Matrix4().makeScale(0.01, 0.01, 0.01);
const scaleKCorrection = new THREE.Matrix4().makeScale(0.001, 0.001, 0.001);
export const fixBonesScaleCorrectionStep = {
name: "fix-bones-scale",
action: (group: Group): StepResult => {
const bonesBoundingBox = getBonesBoundingBox(group);
const xBonesSize = bonesBoundingBox.max.x - bonesBoundingBox.min.x;
const yBonesSize = bonesBoundingBox.max.y - bonesBoundingBox.min.y;
const zBonesSize = bonesBoundingBox.max.z - bonesBoundingBox.min.z;
const bonesSizeLog: LogMessage = {
level: "info",
message: `Bones size: x: ${xBonesSize}, y: ${yBonesSize}, z: ${zBonesSize}`,
};
const bonesAre100TimesTooLarge = zBonesSize > 50 || yBonesSize > 50;
const bonesAre1000TimesTooLarge = zBonesSize > 500 || yBonesSize > 500;
if (!bonesAre100TimesTooLarge) {
return {
didApply: false,
topLevelMessage: {
level: "info",
message: "Detected bones were < 10 in y/z. No correction needed.",
},
logs: [bonesSizeLog],
};
}
group.traverse((child) => {
const asBone = child as THREE.Bone;
if (asBone.isBone) {
if (bonesAre1000TimesTooLarge) {
asBone.position.applyMatrix4(scaleKCorrection);
} else {
asBone.position.applyMatrix4(scaleCorrection);
}
asBone.updateMatrixWorld(true);
}
});
return {
didApply: true,
topLevelMessage: {
level: "info",
message: `Detected bones were > ${
bonesAre1000TimesTooLarge ? "100" : "10"
} in y/z. Scaled down to ${bonesAre1000TimesTooLarge ? "1%" : "10%"} initial.`,
},
logs: [bonesSizeLog],
};
},
} as const;