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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
import * as THREE from 'three';
import { TransformControls } from 'three/addons/controls/TransformControls.js';
/**
* Posing. Each bone is driven as a rotation *relative to its rest pose*, so
* every bone reads 0/0/0 in the original stance and a saved pose stays valid
* even if the model is re-exported with different bone orientations.
*
* `bone.quaternion = rest * delta` and `delta = rest⁻¹ * quaternion`, with the
* delta expressed as XYZ Euler angles in degrees for the UI.
*/
const RAD = Math.PI / 180;
const DEG = 180 / Math.PI;
/** three sanitises node names, so `armsupport.l` arrives as `armsupportl`. */
const normalizeName = (name) => (name ?? '').replace(/[.\s]/g, '').toLowerCase();
export class Rig {
constructor({ bones, scene, camera, domElement, orbit, pickTargets, onChange = null }) {
this.bones = bones;
this.byName = new Map();
for (const entry of bones) {
this.byName.set(entry.name, entry);
const alias = normalizeName(entry.name);
if (!this.byName.has(alias)) this.byName.set(alias, entry);
}
this.scene = scene;
this.domElement = domElement;
this.orbit = orbit;
this.pickTargets = pickTargets;
this.onChange = onChange;
this.selected = null;
this.controls = new TransformControls(camera, domElement);
this.controls.setMode('rotate');
this.controls.setSpace('local');
this.controls.setSize(0.8);
// A *hidden* TransformControls still grabs the pointer in three: its picker
// stays raycastable even when the helper is not drawn, so grabbing the body
// (the rings sit on the `master` bone, right where you grab) silently turned
// the character. Keep the controls disabled until the gizmo is actually
// shown, so the mouse can only move the character (see setGizmoVisible).
this.controls.enabled = false;
this.helper = this.controls.getHelper();
this.helper.visible = false;
// Whether the user wants the gizmo at all. It starts OFF: the model is the
// point, and the rings sat on top of it until you went looking for them.
// Selecting a bone must not switch it back on by itself, so `select()`
// consults this instead of always showing the helper.
this.gizmoWanted = false;
scene.add(this.helper);
this.controls.addEventListener('dragging-changed', (event) => {
this.orbit.enabled = !event.value;
});
this.controls.addEventListener('objectChange', () => {
if (this.selected) this.onChange?.(this.selected);
});
}
/* ------------------------------------------------------------ selection */
select(name, { silent = false } = {}) {
if (name && !this.byName.has(name)) return;
this.selected = name ?? null;
if (name) {
this.controls.attach(this.byName.get(name).bone);
this.helper.visible = this.gizmoWanted;
} else {
this.controls.detach();
this.helper.visible = false;
}
// Only an actually-shown gizmo may take the pointer (see the constructor).
this.controls.enabled = this.helper.visible;
if (!silent) this.onChange?.(this.selected);
}
// Clicking the model no longer reaches for a bone: the mouse is for composing
// (see the drag handling in src/main.js), and bones are picked from the panel's
// bone list, which calls `select()` directly. So there is no pointer handler on
// the canvas here at all.
/* ----------------------------------------------------------------- pose */
getDelta(name) {
const entry = this.byName.get(name);
if (!entry) return { x: 0, y: 0, z: 0 };
const delta = entry.rest.clone().invert().multiply(entry.bone.quaternion);
const euler = new THREE.Euler().setFromQuaternion(delta, 'XYZ');
return { x: euler.x * DEG, y: euler.y * DEG, z: euler.z * DEG };
}
setDelta(name, { x = 0, y = 0, z = 0 }) {
const entry = this.byName.get(name);
if (!entry) return;
const delta = new THREE.Quaternion().setFromEuler(
new THREE.Euler(x * RAD, y * RAD, z * RAD, 'XYZ'),
);
entry.bone.quaternion.copy(entry.rest).multiply(delta);
}
/** `pose.bones` maps a bone name to `[x, y, z]` degrees; `pose.root` is a translation. */
applyPose(pose = {}) {
// Presets and saved files use the glTF bone names (`armsupport.l`) while
// three sanitises them to `armsupportl`, so compare on the normalised form -
// otherwise every preset that touches an arm or leg is silently ignored.
const rotations = new Map();
for (const [key, value] of Object.entries(pose.bones ?? {})) {
rotations.set(normalizeName(key), value);
}
for (const entry of this.bones) {
const value = rotations.get(normalizeName(entry.name));
if (Array.isArray(value)) this.setDelta(entry.name, { x: value[0], y: value[1], z: value[2] });
else this.setDelta(entry.name, { x: 0, y: 0, z: 0 });
}
}
getPose({ onlyMoved = true } = {}) {
const rotations = {};
for (const entry of this.bones) {
const { x, y, z } = this.getDelta(entry.name);
const rounded = [round1(x), round1(y), round1(z)];
if (onlyMoved && rounded.every((value) => value === 0)) continue;
rotations[entry.name] = rounded;
}
return { bones: rotations };
}
reset(name) {
if (name) {
this.setDelta(name, { x: 0, y: 0, z: 0 });
return;
}
for (const entry of this.bones) this.setDelta(entry.name, { x: 0, y: 0, z: 0 });
}
/** True when every bone sits at its rest rotation. */
isRestPose() {
return this.bones.every((entry) => {
const { x, y, z } = this.getDelta(entry.name);
return Math.abs(x) < 0.01 && Math.abs(y) < 0.01 && Math.abs(z) < 0.01;
});
}
setGizmoVisible(visible) {
this.gizmoWanted = visible !== false;
this.helper.visible = this.gizmoWanted && this.selected != null;
// A hidden gizmo must not grab the pointer (see the constructor).
this.controls.enabled = this.helper.visible;
}
dispose() {
this.controls.detach();
this.controls.dispose();
this.helper.removeFromParent();
}
}
const round1 = (value) => Math.round(value * 10) / 10;
|