Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

fix a bug with skinning where there are branching children

Scott Petrovice05ca28b1f1e
diff --git a/src/lib/Utilities.ts b/src/lib/Utilities.ts
index a352f8f..71052ac 100644
--- a/src/lib/Utilities.ts
+++ b/src/lib/Utilities.ts
@@ -118,7 +118,18 @@ export class Utility {
}
/**
- * The point halfway between a bone's start joint and its first child's joint.
+ * The child joint that marks where this bone ends. A leaf/tip child exists
+ * only to orient its parent, so it wins over branching children — e.g.
+ * mouth_upper -> [mouth_lower, mouth_upper_tip] or fox nose -> [ear_1_l, ..., head_tip].
+ * Otherwise falls back to the first child.
+ */
+ static primary_child_bone (bone: Bone): Bone | undefined {
+ const child_bones = bone.children.filter((c): c is Bone => c instanceof Bone)
+ return child_bones.find(c => Utility.is_leaf_bone(c)) ?? child_bones[0]
+ }
+
+ /**
+ * The point halfway between a bone's start joint and its primary child's joint.
* This is the reference point the skinning solvers measure vertex distances
* against — a bone's "center of mass" rather than its start joint, which
* keeps long bones from losing vertices to their neighbors.
@@ -126,11 +137,10 @@ export class Utility {
*/
static bone_midpoint_to_child (bone: Bone): Vector3 {
const bone_position = Utility.world_position_from_object(bone)
- if (bone.children.length === 0) {
+ const child = Utility.primary_child_bone(bone)
+ if (child === undefined) {
return bone_position.clone()
}
- // Assume first child is the relevant one
- const child = bone.children[0] as Bone
const child_position = Utility.world_position_from_object(child)
return new Vector3().lerpVectors(bone_position, child_position, 0.5)
}
@@ -308,7 +318,7 @@ export class Utility {
// if bone has a child, we are going to calculate the distance by getting the half way
// point between bone and child bone...to hopefully yield better results
if (bone.has_child_bone) {
- const child_bone: Bone = bone.bone_object.children[0] as Bone
+ const child_bone: Bone = Utility.primary_child_bone(bone.bone_object) ?? bone.bone_object.children[0] as Bone
const child_bone_position: Vector3 = Utility.world_position_from_object(child_bone)
const bone_position: Vector3 = Utility.world_position_from_object(bone.bone_object)
const half_way_point: Vector3 = bone_position.add(child_bone_position).divideScalar(2)
diff --git a/src/lib/solvers/ExtremityWeightCorrector.ts b/src/lib/solvers/ExtremityWeightCorrector.ts
index 2d0f380..638ee85 100644
--- a/src/lib/solvers/ExtremityWeightCorrector.ts
+++ b/src/lib/solvers/ExtremityWeightCorrector.ts
@@ -86,8 +86,9 @@ export class ExtremityWeightCorrector {
// Axis points from the start joint toward the child (down the finger).
// For leaf bones with no child, use the direction away from the parent.
let direction: Vector3
- if (bone.children.length > 0) {
- const child_head = Utility.world_position_from_object(bone.children[0] as Bone)
+ const primary_child = Utility.primary_child_bone(bone)
+ if (primary_child !== undefined) {
+ const child_head = Utility.world_position_from_object(primary_child)
direction = Utility.direction_between_points(head, child_head)
} else {
const parent_head = Utility.world_position_from_object(parent as Bone)