Import a 3D Model and automatically assign and export animations
TypeScript
Small simplification
Scott Petrovic56c66ab32af6
diff --git a/src/lib/solvers/AbstractAutoSkinSolver.ts b/src/lib/solvers/AbstractAutoSkinSolver.tsindex 4ea6cb7..c556d44 100644--- a/src/lib/solvers/AbstractAutoSkinSolver.ts+++ b/src/lib/solvers/AbstractAutoSkinSolver.ts@@ -1,11 +1,10 @@import { BufferGeometry, Object3D, type Bone } from 'three'import { type SkeletonType } from '../enums/SkeletonType.js'import BoneTesterData from '../models/BoneTesterData.js'-import BoneCalculationData from '../models/BoneCalculationData.js'import { Utility } from '../Utilities.js'export abstract class AbstractAutoSkinSolver {- protected readonly bones_master_data: BoneCalculationData[] = []+ protected bones_master_data: Bone[] = []protected geometry: BufferGeometry = new BufferGeometry()protected show_debug: boolean = falseprotected bone_idx_test: number = -1@@ -14,7 +13,7 @@ export abstract class AbstractAutoSkinSolver {constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {this.set_skeleton_type(skeleton_type)- this.init_bone_weights_data_structure(bone_hier)+ this.bones_master_data = Utility.bone_list_from_hierarchy(bone_hier)}// calculation that will bring back the skin indices and weights@@ -44,7 +43,7 @@ export abstract class AbstractAutoSkinSolver {this.skeleton_type = skinning_type}- public get_bone_master_data (): BoneCalculationData[] {+ public get_bone_master_data (): Bone[] {return this.bones_master_data}@@ -60,12 +59,4 @@ export abstract class AbstractAutoSkinSolver {return this.geometry.attributes.position.array.length / 3}-- protected init_bone_weights_data_structure (bone_hier: Object3D): void {- const bones_list: Bone[] = Utility.bone_list_from_hierarchy(bone_hier)- bones_list.forEach((bone: Bone) => {- const new_bone_object: BoneCalculationData = new BoneCalculationData(bone)- this.bones_master_data.push(new_bone_object)- })- }}diff --git a/src/lib/solvers/SolverDistanceChildTargeting.ts b/src/lib/solvers/SolverDistanceChildTargeting.tsindex f51cbbf..68fb5a2 100644--- a/src/lib/solvers/SolverDistanceChildTargeting.ts+++ b/src/lib/solvers/SolverDistanceChildTargeting.ts@@ -7,7 +7,7 @@ import { Utility } from '../Utilities.js'import { SkeletonType } from '../enums/SkeletonType.js'import { AbstractAutoSkinSolver } from './AbstractAutoSkinSolver.js'import { Generators } from '../Generators.js'-import type BoneCalculationData from '../models/BoneCalculationData.js'+/*** SolverDistanceChildTargeting@@ -31,10 +31,10 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolverconst skin_weights: number[] = []// create cached items for all the vertex calculations later- this.cached_bone_positions = this.get_bone_master_data().map(b => Utility.world_position_from_object(b.bone_object))- this.cached_median_child_bone_positions = this.get_bone_master_data().map(b => this.midpoint_to_child(b.bone_object))+ this.cached_bone_positions = this.get_bone_master_data().map(b => Utility.world_position_from_object(b))+ this.cached_median_child_bone_positions = this.get_bone_master_data().map(b => this.midpoint_to_child(b))- this.get_bone_master_data().forEach((b, idx) => this.bone_object_to_index.set(b.bone_object, idx))+ this.get_bone_master_data().forEach((b, idx) => this.bone_object_to_index.set(b, idx))this.distance_to_bottom_of_hip = this.calculate_distance_to_bottom_of_hip()// mutates (assigns) skin_indices and skin_weights@@ -64,20 +64,20 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolver// every vertex checks to see if it is below the hips area,// so do this calculation once and cache it for the lookup laterprivate calculate_distance_to_bottom_of_hip (): number {- const hip_bone_object: BoneCalculationData | undefined = this.get_bone_master_data().find(b => b.bone_object.name.toLowerCase().includes('hips'))+ const hip_bone_object: Bone | undefined = this.get_bone_master_data().find(b => b.name.toLowerCase().includes('hips'))if (hip_bone_object === undefined) {throw new Error('Hip bone not found')}- const intesection_point: Vector3 | null = this.cast_intersection_ray_down_from_bone(hip_bone_object.bone_object)+ const intesection_point: Vector3 | null = this.cast_intersection_ray_down_from_bone(hip_bone_object)// get the distance from the bone point to the intersection point- const bone_index = this.get_bone_master_data().findIndex(b => b.bone_object === hip_bone_object.bone_object)+ const bone_index = this.get_bone_master_data().findIndex(b => b === hip_bone_object)const bone_position: Vector3 = this.cached_median_child_bone_positions[bone_index]- let distance_to_bottom_of_hip: number = intesection_point?.distanceTo(bone_position) ?? 0- distance_to_bottom_of_hip *= 1.1 // buffer zone to make sure to include vertices at intersection+ let distance_to_bottom: number = intesection_point?.distanceTo(bone_position) ?? 0+ distance_to_bottom *= 1.1 // buffer zone to make sure to include vertices at intersection- return distance_to_bottom_of_hip+ return distance_to_bottom}/**@@ -95,13 +95,13 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolverthis.get_bone_master_data().forEach((bone, idx) => {// The root bone is only for global transform changes, so we won't assign it to any vertices- if (bone.bone_object.name === 'root') {+ if (bone.name === 'root') {return // skip the root bone and continue to the next bone}// hip bones should have custom logic for distance. If the distance is too far away we should ignore it// This will help with hips when left/right legs could be closer than knee bones- if (this.skeleton_type === SkeletonType.Human && bone.bone_object.name.includes('hips') === true) {+ if (this.skeleton_type === SkeletonType.Human && bone.name.includes('hips') === true) {// if the intersection point is lower than the vertex position, that means the vertex is below// the hips area, and is part of the left or right leg...ignore that resultif (this.distance_to_bottom_of_hip !== null && this.distance_to_bottom_of_hip < vertex_position.y) {@@ -139,7 +139,7 @@ export default class SolverDistanceChildTargeting extends AbstractAutoSkinSolverconst raycaster = new Raycaster()// Set the ray's origin to the bone's world position- const bone_index = this.get_bone_master_data().findIndex(b => b.bone_object === bone)+ const bone_index = this.get_bone_master_data().findIndex(b => b === bone)const bone_position = this.cached_median_child_bone_positions[bone_index]// Direction is straight down to find the pevlis "gap"