Import a 3D Model and automatically assign and export animations
TypeScript
don't use leaf/tip bones when skinning.
Scott Petrovica59685db6eb9
diff --git a/src/lib/Utilities.test.ts b/src/lib/Utilities.test.tsnew file mode 100644index 0000000..8adbb62--- /dev/null+++ b/src/lib/Utilities.test.ts@@ -0,0 +1,40 @@+import { describe, it, expect } from 'vitest'+import { Bone } from 'three'+import { Utility } from './Utilities'++/**+ * Creates a Bone with the given name, optionally with a child bone attached so+ * it is no longer childless.+ */+function make_bone (name: string, with_child = false): Bone {+ const bone = new Bone()+ bone.name = name+ if (with_child) {+ bone.add(new Bone())+ }+ return bone+}++describe('Utility.is_leaf_bone', () => {+ it('treats childless, name-marked tips as leaf bones', () => {+ expect(Utility.is_leaf_bone(make_bone('head_leaf'))).toBe(true)+ expect(Utility.is_leaf_bone(make_bone('thumb_04_leaf_l'))).toBe(true)+ expect(Utility.is_leaf_bone(make_bone('Ear_Tip_L'))).toBe(true)+ expect(Utility.is_leaf_bone(make_bone('tip'))).toBe(true)+ })++ it('keeps a name-marked bone that still has an animated child', () => {+ // e.g. a snake `tail_tip` whose child is the true chain-end `tip`+ expect(Utility.is_leaf_bone(make_bone('tail_tip', true))).toBe(false)+ })++ it('keeps a childless bone without a leaf/tip marker', () => {+ // e.g. simplified-hand mode where `finger_03` becomes the childless end+ expect(Utility.is_leaf_bone(make_bone('finger_03'))).toBe(false)+ })++ it('keeps a normal animatable bone', () => {+ expect(Utility.is_leaf_bone(make_bone('upperarm_l', true))).toBe(false)+ expect(Utility.is_leaf_bone(make_bone('upperarm_l'))).toBe(false)+ })+})diff --git a/src/lib/Utilities.ts b/src/lib/Utilities.tsindex b2ef150..f5e6c3c 100644--- a/src/lib/Utilities.ts+++ b/src/lib/Utilities.ts@@ -105,6 +105,18 @@ export class Utility {obj.removeFromParent()}+ /**+ * A "leaf" bone is an orientation-only tip at the end of a chain (finger/toe+ * tips, head top, tail/ear/wing tips). These are not meant to be animated, so+ * the skinning algorithm ignores them. Identified as childless AND name-marked.+ * Human rigs mark them with `_leaf`; the animal rigs mark them with `tip`.+ */+ static is_leaf_bone (bone: Bone): boolean {+ if (bone.children.length > 0) return false+ const name = bone.name.toLowerCase()+ return name.includes('leaf') || name.includes('tip')+ }+static bone_list_from_hierarchy (bone_hierarchy: Object3D): Bone[] {if (bone_hierarchy === undefined || bone_hierarchy === null) {console.warn('bone_hierarchy is undefined or null')diff --git a/src/lib/solvers/WeightCalculator.ts b/src/lib/solvers/WeightCalculator.tsindex 9492c77..80bf19c 100644--- a/src/lib/solvers/WeightCalculator.ts+++ b/src/lib/solvers/WeightCalculator.ts@@ -25,6 +25,10 @@ export class WeightCalculator {// each index will be a bone index. the value will be a list of vertex indices that belong to that boneprivate readonly bones_vertex_segmentation: number[][] = []+ // bone indices that should never receive vertex weights: the root (global+ // transform only) and leaf/orientation bones (finger/toe/tail tips, etc.)+ private readonly skipped_bone_indices = new Set<number>()+constructor (bones: Bone[], geometry: BufferGeometry, skeleton_type: SkeletonType | null) {this.bones = bonesthis.geometry = geometry@@ -38,6 +42,16 @@ export class WeightCalculator {public initialize_caches (): void {this.cached_median_child_bone_positions = this.bones.map(b => this.midpoint_to_child(b))this.bones.forEach((b, idx) => this.bone_object_to_index.set(b, idx))++ // The root bone is only for global transform changes, and leaf/orientation+ // bones exist only to orient their parent — neither should be assigned any+ // vertices. Compute the skip set once instead of checking names per vertex.+ this.bones.forEach((b, idx) => {+ if (b.name === 'root' || Utility.is_leaf_bone(b)) {+ this.skipped_bone_indices.add(idx)+ }+ })+this.distance_to_bottom_of_position_tracking_bone = this.calculate_distance_to_bottom_of_position_tracking_bone()}@@ -58,9 +72,10 @@ export class WeightCalculator {let closest_bone_index: number = 0this.bones.forEach((bone, idx) => {- // The root bone is only for global transform changes, so we won't assign it to any vertices- if (bone.name === 'root') {- return // skip the root bone and continue to the next bone+ // Skip the root bone (global transform only) and leaf/orientation bones.+ // See skipped_bone_indices, computed once in initialize_caches.+ if (this.skipped_bone_indices.has(idx)) {+ return}// hip bones should have custom logic for distance. If the distance is too far away we should ignore it