Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Do a better job at setting rotations when independent bone movement is being done.

Scott Petrovic855fb9bf2eb4
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 4c43a2a..c5a865e 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -123,6 +123,19 @@ export class EventListeners {
}
}
+ if (!event.value &&
+ this.bootstrap.process_step === ProcessStep.EditSkeleton &&
+ this.bootstrap.transform_controls.getMode() === 'translate' &&
+ this.bootstrap.edit_skeleton_step.independent_bone_movement.is_enabled()) {
+ const selected_bone = this.bootstrap.transform_controls.object
+ if (selected_bone !== undefined && selected_bone !== null) {
+ const mirror_bone = this.bootstrap.edit_skeleton_step.is_mirror_mode_enabled()
+ ? this.bootstrap.edit_skeleton_step.find_mirror_bone(selected_bone as Bone)
+ : undefined
+ this.bootstrap.edit_skeleton_step.independent_bone_movement.finalize_drop(selected_bone as Bone, mirror_bone)
+ }
+ }
+
// if we stopped dragging, that means a mouse up.
// if we are editing skeleton and viewing weight painted mesh, refresh the weight painting
if (this.bootstrap.process_step === ProcessStep.EditSkeleton &&
diff --git a/src/lib/processes/edit-skeleton/IndependentBoneMovement.ts b/src/lib/processes/edit-skeleton/IndependentBoneMovement.ts
index a6e5812..6eb5a61 100644
--- a/src/lib/processes/edit-skeleton/IndependentBoneMovement.ts
+++ b/src/lib/processes/edit-skeleton/IndependentBoneMovement.ts
@@ -1,4 +1,4 @@
-import { Vector3, type Bone } from 'three'
+import { Quaternion, Vector3, type Bone, type Skeleton } from 'three'
/*
* IndependentBoneMovement
@@ -14,7 +14,10 @@ import { Vector3, type Bone } from 'three'
*/
export class IndependentBoneMovement {
private _enabled: boolean = false
- private readonly _children_initial_world_positions: Map<string, Vector3> = new Map()
+ private readonly _children_initial_world_positions: Map<string, Vector3> = new Map<string, Vector3>()
+ private readonly _children_initial_world_rotations: Map<string, Quaternion> = new Map<string, Quaternion>()
+ private readonly _rest_bone_world_positions: Map<string, Vector3> = new Map<string, Vector3>()
+ private readonly _rest_bone_world_rotations: Map<string, Quaternion> = new Map<string, Quaternion>()
public is_enabled (): boolean {
return this._enabled
@@ -24,6 +27,24 @@ export class IndependentBoneMovement {
this._enabled = value
}
+ /**
+ * Capture the initial (rest) world-space transforms for all bones.
+ * This should be called once when a fresh editable skeleton is created.
+ */
+ public set_rest_pose (skeleton: Skeleton): void {
+ this._rest_bone_world_positions.clear()
+ this._rest_bone_world_rotations.clear()
+
+ skeleton.bones.forEach((bone) => {
+ const world_pos = new Vector3()
+ const world_rot = new Quaternion()
+ bone.getWorldPosition(world_pos)
+ bone.getWorldQuaternion(world_rot)
+ this._rest_bone_world_positions.set(bone.uuid, world_pos.clone())
+ this._rest_bone_world_rotations.set(bone.uuid, world_rot.clone())
+ })
+ }
+
/**
* Snapshot the world positions of the direct bone children at drag start.
* Clears any previously stored positions first.
@@ -32,6 +53,7 @@ export class IndependentBoneMovement {
*/
public record_drag_start (bone: Bone, mirror_bone?: Bone): void {
this._children_initial_world_positions.clear()
+ this._children_initial_world_rotations.clear()
this._append_children_world_positions(bone)
if (mirror_bone !== undefined) {
this._append_children_world_positions(mirror_bone)
@@ -51,26 +73,224 @@ export class IndependentBoneMovement {
}
}
+ /**
+ * At drag end, update rotation data for the moved bone AND its parent bone.
+ *
+ * When you translate a bone (e.g. elbow), two rotations change:
+ * 1. The PARENT bone (e.g. upper arm) — because the direction from parent
+ * to the moved child has changed.
+ * 2. The moved bone itself — because the direction from it to its own
+ * children has changed (children were pinned in place).
+ *
+ * After each rotation update the affected bone's children are re-pinned so
+ * their world-space transforms are preserved.
+ */
+ public finalize_drop (bone: Bone, mirror_bone?: Bone): void {
+ this._finalize_bone_with_parent(bone)
+
+ if (mirror_bone !== undefined) {
+ this._finalize_bone_with_parent(mirror_bone)
+ }
+ }
+
+ private _finalize_bone_with_parent (bone: Bone): void {
+ // Snapshot world transforms of the moved bone and its children
+ // These are the "ground truth" we want to preserve through rotation updates
+ const snapshot = new Map<string, { pos: Vector3, rot: Quaternion }>()
+ this._snapshot_bone_and_children(bone, snapshot)
+
+ // ── Step 1: Update the PARENT bone's rotation ────────────────────────────
+ // The parent-to-child direction changed because the child was translated.
+ const parent_bone = (bone.parent !== null && this._is_bone(bone.parent))
+ ? bone.parent
+ : null
+
+ if (parent_bone !== null) {
+ // Also snapshot siblings so they can be re-pinned after parent rotates
+ parent_bone.children.forEach((sibling) => {
+ if (sibling === bone || !this._is_bone(sibling)) { return }
+ this._snapshot_bone_and_children(sibling, snapshot)
+ })
+
+ this._finalize_bone_rotation_from_rest_pose(parent_bone)
+
+ // Re-pin ALL parent children (moved bone + siblings) to their snapshots
+ this._repin_children_from_snapshot(parent_bone, snapshot)
+ }
+
+ // ── Step 2: Update the MOVED bone's rotation ─────────────────────────────
+ // The bone-to-child direction changed because children were pinned in place.
+ this._finalize_bone_rotation_from_rest_pose(bone)
+
+ // Re-pin the moved bone's children to their snapshots
+ this._repin_children_from_snapshot(bone, snapshot)
+ }
+
+ private _snapshot_bone_and_children (bone: Bone, out: Map<string, { pos: Vector3, rot: Quaternion }>): void {
+ const pos = new Vector3()
+ const rot = new Quaternion()
+ bone.getWorldPosition(pos)
+ bone.getWorldQuaternion(rot)
+ out.set(bone.uuid, { pos: pos.clone(), rot: rot.clone() })
+
+ bone.children.forEach((child) => {
+ if (!this._is_bone(child)) { return }
+ const child_pos = new Vector3()
+ const child_rot = new Quaternion()
+ child.getWorldPosition(child_pos)
+ child.getWorldQuaternion(child_rot)
+ out.set(child.uuid, { pos: child_pos.clone(), rot: child_rot.clone() })
+ })
+ }
+
+ private _repin_children_from_snapshot (bone: Bone, snapshot: Map<string, { pos: Vector3, rot: Quaternion }>): void {
+ const bone_world_rot = new Quaternion()
+ bone.getWorldQuaternion(bone_world_rot)
+ const inv_bone_world_rot = bone_world_rot.clone().invert()
+
+ bone.children.forEach((child) => {
+ if (!this._is_bone(child)) { return }
+ const snap = snapshot.get(child.uuid)
+ if (snap === undefined) { return }
+
+ const local_pos = snap.pos.clone()
+ bone.worldToLocal(local_pos)
+ child.position.copy(local_pos)
+
+ const local_rot = inv_bone_world_rot.clone().multiply(snap.rot)
+ child.quaternion.copy(local_rot)
+
+ child.updateWorldMatrix(true, true)
+ })
+ }
+
private _append_children_world_positions (bone: Bone): void {
bone.children.forEach((child) => {
- if ('isBone' in child && child.isBone) {
- const world_pos = new Vector3()
- child.getWorldPosition(world_pos)
- this._children_initial_world_positions.set(child.uuid, world_pos.clone())
- }
+ if (!this._is_bone(child)) { return }
+
+ const world_pos = new Vector3()
+ const world_rot = new Quaternion()
+ child.getWorldPosition(world_pos)
+ child.getWorldQuaternion(world_rot)
+ this._children_initial_world_positions.set(child.uuid, world_pos.clone())
+ this._children_initial_world_rotations.set(child.uuid, world_rot.clone())
})
}
private _apply_to_bone (bone: Bone): void {
+ const parent_world_rotation = new Quaternion()
+ bone.getWorldQuaternion(parent_world_rotation)
+ const inverse_parent_world_rotation = parent_world_rotation.clone().invert()
+
bone.children.forEach((child) => {
- if (!('isBone' in child) || !child.isBone) { return }
+ if (!this._is_bone(child)) { return }
const initial_world_pos = this._children_initial_world_positions.get(child.uuid)
+ const initial_world_rot = this._children_initial_world_rotations.get(child.uuid)
if (initial_world_pos === undefined) { return }
const local_pos = initial_world_pos.clone()
bone.worldToLocal(local_pos)
child.position.copy(local_pos)
+
+ if (initial_world_rot !== undefined) {
+ const local_rot = inverse_parent_world_rotation.clone().multiply(initial_world_rot)
+ child.quaternion.copy(local_rot)
+ }
+
// updateWorldMatrix(updateParents, updateChildren) - propagate changes up and down the hierarchy
child.updateWorldMatrix(true, true)
})
}
+
+ private _finalize_bone_rotation_from_rest_pose (bone: Bone): void {
+ const rest_world_rotation = this._rest_bone_world_rotations.get(bone.uuid)
+ const rest_direction = this._average_child_direction_from_rest_pose(bone)
+ const current_direction = this._average_child_direction_from_current_pose(bone)
+
+ if (rest_world_rotation === undefined || rest_direction === null || current_direction === null) {
+ return
+ }
+
+ const world_rotation_delta = new Quaternion().setFromUnitVectors(rest_direction, current_direction)
+ const target_world_rotation = world_rotation_delta.multiply(rest_world_rotation.clone())
+
+ const parent_world_rotation = new Quaternion()
+ if (bone.parent !== null && 'getWorldQuaternion' in bone.parent) {
+ bone.parent.getWorldQuaternion(parent_world_rotation)
+ } else {
+ parent_world_rotation.identity()
+ }
+
+ const target_local_rotation = parent_world_rotation.clone().invert().multiply(target_world_rotation)
+ bone.quaternion.copy(target_local_rotation)
+ bone.updateWorldMatrix(true, true)
+ }
+
+ private _average_child_direction_from_rest_pose (bone: Bone): Vector3 | null {
+ const bone_rest_world_position = this._rest_bone_world_positions.get(bone.uuid)
+ if (bone_rest_world_position === undefined) {
+ return null
+ }
+
+ const averaged_direction = new Vector3(0, 0, 0)
+ let direction_count = 0
+
+ bone.children.forEach((child) => {
+ if (!this._is_bone(child)) { return }
+ const child_rest_world_position = this._rest_bone_world_positions.get(child.uuid)
+ if (child_rest_world_position === undefined) { return }
+
+ const child_direction = child_rest_world_position.clone().sub(bone_rest_world_position)
+ if (child_direction.lengthSq() < 1e-8) { return }
+
+ child_direction.normalize()
+ averaged_direction.add(child_direction)
+ direction_count += 1
+ })
+
+ if (direction_count === 0 || averaged_direction.lengthSq() < 1e-8) {
+ return null
+ }
+
+ return averaged_direction.normalize()
+ }
+
+ private _average_child_direction_from_current_pose (bone: Bone): Vector3 | null {
+ const bone_world_position = new Vector3()
+ bone.getWorldPosition(bone_world_position)
+
+ const averaged_direction = new Vector3(0, 0, 0)
+ let direction_count = 0
+
+ bone.children.forEach((child) => {
+ if (!this._is_bone(child)) { return }
+
+ const child_world_position = new Vector3()
+ child.getWorldPosition(child_world_position)
+
+ const child_direction = child_world_position.sub(bone_world_position)
+ if (child_direction.lengthSq() < 1e-8) { return }
+
+ child_direction.normalize()
+ averaged_direction.add(child_direction)
+ direction_count += 1
+ })
+
+ if (direction_count === 0 || averaged_direction.lengthSq() < 1e-8) {
+ return null
+ }
+
+ return averaged_direction.normalize()
+ }
+
+ private _is_bone (value: unknown): value is Bone {
+ if (typeof value !== 'object' || value === null) {
+ return false
+ }
+
+ if (!('isBone' in value)) {
+ return false
+ }
+
+ return (value as { isBone?: boolean }).isBone === true
+ }
}
diff --git a/src/lib/processes/edit-skeleton/MeshDragBonePlacement.ts b/src/lib/processes/edit-skeleton/MeshDragBonePlacement.ts
index 7ae1f13..e08336e 100644
--- a/src/lib/processes/edit-skeleton/MeshDragBonePlacement.ts
+++ b/src/lib/processes/edit-skeleton/MeshDragBonePlacement.ts
@@ -111,6 +111,14 @@ export class MeshDragBonePlacement {
return false
}
+ const selected_bone = this.edit_skeleton_step.get_currently_selected_bone()
+ if (selected_bone !== null && this.edit_skeleton_step.independent_bone_movement.is_enabled()) {
+ const mirror_bone = this.edit_skeleton_step.is_mirror_mode_enabled()
+ ? this.edit_skeleton_step.find_mirror_bone(selected_bone)
+ : undefined
+ this.edit_skeleton_step.independent_bone_movement.finalize_drop(selected_bone, mirror_bone)
+ }
+
this.is_dragging_mode_active = false
if (this.orbit_controls !== undefined) {
this.orbit_controls.enabled = true
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index f0b1b6e..caada3b 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -490,6 +490,7 @@ export class StepEditSkeleton extends EventTarget {
this.edited_armature = armature.clone()
this.create_threejs_skeleton_object()
+ this.independent_bone_movement.set_rest_pose(this.threejs_skeleton)
// Initialize the undo/redo system with the skeleton
this.undo_redo_system.set_skeleton(this.threejs_skeleton)