Skip to content

Commit 9cff364

Browse files
committed
fix non 0,0,0 root pos bug
1 parent 65a3275 commit 9cff364

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

plugins/bone_view/bone_view.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@
6666
joint.isBoneIndicator = true;
6767
joint.renderOrder = RENDER_ORDER_BONE;
6868

69-
scene.add(arrow, axes, joint);
69+
three_grid.add(arrow, axes, joint);
7070
return { arrow, axes, joint };
7171
}
7272

7373
function disposeIndicators(entry) {
7474
if (!entry) return;
75-
scene.remove(entry.arrow, entry.axes, entry.joint);
75+
three_grid.remove(entry.arrow, entry.axes, entry.joint);
7676
}
7777

7878
function ensureFor(group) {
@@ -116,25 +116,21 @@
116116

117117
const parentPos = new THREE.Vector3();
118118
parentGroup.mesh.getWorldPosition(parentPos);
119-
const length = worldPos.distanceTo(parentPos);
120-
if (length <= 0.001) {
121-
return { worldPos, worldQuat, valid: false };
122-
}
123-
124-
const dir = new THREE.Vector3().subVectors(parentPos, worldPos).normalize();
125-
return { worldPos, worldQuat, dir, length, parentPos, valid: true };
119+
120+
return { worldPos, worldQuat, parentPos, valid: true };
126121
}
127122

128123
function updateTransforms() {
129124
ResourceManager.updateColors();
130125
const active = pluginAction && pluginAction.value;
131-
// offset by root x,y,z
132-
const rootOffset = new THREE.Vector3(0, 0, 0);
133-
if (Group.all.length) {
134-
var group = Group.all[0];
135-
var origin = group.origin;
136-
rootOffset.set(origin[0], origin[1], origin[2]);
137-
}
126+
127+
const inverseParentMatrix = new THREE.Matrix4();
128+
three_grid.updateWorldMatrix(true, false);
129+
inverseParentMatrix.copy(three_grid.matrixWorld).invert();
130+
131+
const parentWorldQuat = new THREE.Quaternion();
132+
three_grid.getWorldQuaternion(parentWorldQuat);
133+
const inverseParentQuat = parentWorldQuat.clone().invert();
138134

139135
for (const group of Group.all) {
140136
const entry = ensureFor(group);
@@ -153,24 +149,35 @@
153149
entry.joint.visible = false;
154150
continue;
155151
}
152+
153+
const localPos = data.worldPos.clone().applyMatrix4(inverseParentMatrix);
154+
const parentLocalPos = data.parentPos.clone().applyMatrix4(inverseParentMatrix);
155+
const localQuat = inverseParentQuat.clone().multiply(data.worldQuat);
156156

157-
const offsetWorldPos = new THREE.Vector3().copy(data.worldPos).add(rootOffset);
157+
const length = localPos.distanceTo(parentLocalPos);
158+
if (length <= 0.001) {
159+
entry.arrow.visible = false;
160+
entry.axes.visible = false;
161+
entry.joint.visible = false;
162+
continue;
163+
}
164+
const dir = new THREE.Vector3().subVectors(parentLocalPos, localPos).normalize();
158165

159166
entry.axes.visible = true;
160-
entry.axes.position.copy(offsetWorldPos);
161-
entry.axes.quaternion.copy(data.worldQuat);
167+
entry.axes.position.copy(localPos);
168+
entry.axes.quaternion.copy(localQuat);
162169

163170
entry.joint.visible = true;
164-
entry.joint.position.copy(offsetWorldPos);
171+
entry.joint.position.copy(localPos);
165172

166173
entry.arrow.visible = true;
167-
const qToDir = new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 1, 0), data.dir);
174+
const qToDir = new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir);
168175
entry.arrow.quaternion.copy(qToDir);
169176

170-
const mid = new THREE.Vector3().copy(offsetWorldPos).addScaledVector(data.dir, data.length / 2);
177+
const mid = new THREE.Vector3().copy(localPos).addScaledVector(dir, length / 2);
171178
entry.arrow.position.copy(mid);
172179

173-
entry.arrow.scale.set(BONE_RADIUS, data.length, BONE_RADIUS);
180+
entry.arrow.scale.set(BONE_RADIUS, length, BONE_RADIUS);
174181
}
175182
}
176183

0 commit comments

Comments
 (0)