-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrotate-root.ts
81 lines (73 loc) · 2.11 KB
/
rotate-root.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as THREE from "three";
import { Group } from "three";
import { StepResult } from "./types";
function isNear(a: number, b: number, epsilon = 0.1) {
return Math.abs(a - b) < epsilon;
}
const HalfPi = Math.PI / 2;
export const rotateRootCorrectionStep = {
name: "rotate-root",
action: (group: Group): StepResult => {
const rootBone = group.getObjectByName("root") as THREE.Bone;
if (!rootBone) {
return {
didApply: false,
topLevelMessage: {
level: "error",
message: "Could not find root bone. Cannot rotate root.",
},
};
}
if (
isNear(rootBone.rotation.x, 0) &&
isNear(rootBone.rotation.y, 0) &&
isNear(rootBone.rotation.z, 0)
) {
return {
didApply: false,
topLevelMessage: {
level: "info",
message: "Detected root was at zero rotation. No correction needed.",
},
};
}
if (
isNear(rootBone.rotation.x, -HalfPi) &&
isNear(rootBone.rotation.y, 0) &&
isNear(rootBone.rotation.z, 0)
) {
// Root bone has a rotation - rotate it to 0,0,0
rootBone.rotation.x = 0;
rootBone.rotation.y = 0;
rootBone.rotation.z = 0;
rootBone.updateMatrixWorld(true);
for (const child of rootBone.children) {
const asBone = child as THREE.Bone;
if (asBone.isBone) {
const tempPosY = asBone.position.y;
asBone.position.y = asBone.position.z;
asBone.position.z = -tempPosY;
const [x, y, z, w] = asBone.quaternion.toArray();
asBone.quaternion.set(x, -y, -z, w);
asBone.updateMatrixWorld(true);
}
}
return {
didApply: true,
topLevelMessage: {
level: "info",
message:
"Detected root was not at expected rotation. Rotated root and translated immediate children to correct.",
},
};
} else {
return {
didApply: false,
topLevelMessage: {
level: "error",
message: "Detected root was at unrecognized rotation.",
},
};
}
},
} as const;