Import a 3D Model and automatically assign and export animations
TypeScript
Start new solver for trying to do skinning looking at direction of children and parents
Scott Petrovic643531ae410d
diff --git a/src/index.html b/src/index.htmlindex af84d43..2bb05ab 100644--- a/src/index.html+++ b/src/index.html@@ -144,6 +144,7 @@<label for="skinning-algorithm-options">Skinning algorithm:</label><select id="skinning-algorithm-options"><option value="closest-bone">Closest Bone</option>+ <option value="closest-bone-child">Closest Distance Child</option><!-- <option value="bone-envelope">Bone Envelope</option><option value="closest-bone-middle">Closest Middle Bone</option> --></select>diff --git a/src/lib/enums/SkinningFormula.ts b/src/lib/enums/SkinningFormula.tsindex 8090f41..bf94c39 100644--- a/src/lib/enums/SkinningFormula.ts+++ b/src/lib/enums/SkinningFormula.ts@@ -1,5 +1,6 @@export enum SkinningFormula {Distance = 'distance',+ DistanceChild = 'distance-child',MedianDistance = 'median-distance',Envelope = 'envelope'}diff --git a/src/lib/processes/StepEditSkeleton.ts b/src/lib/processes/StepEditSkeleton.tsindex 387d900..9340fdd 100644--- a/src/lib/processes/StepEditSkeleton.ts+++ b/src/lib/processes/StepEditSkeleton.ts@@ -133,6 +133,8 @@ export class StepEditSkeleton extends EventTarget {return SkinningFormula.Envelopecase 'closest-bone-middle':return SkinningFormula.MedianDistance+ case 'closest-bone-child':+ return SkinningFormula.DistanceChild}return SkinningFormula.Distance // default if other two don't matchdiff --git a/src/lib/processes/StepWeightSkin.ts b/src/lib/processes/StepWeightSkin.tsindex ba6b12c..a18379d 100644--- a/src/lib/processes/StepWeightSkin.ts+++ b/src/lib/processes/StepWeightSkin.ts@@ -12,6 +12,7 @@ import { type BufferGeometry, type Material, type Object3D, type Skeleton, Skinnimport { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.ts'import BoneTesterData from '../models/BoneTesterData.ts'import { type SkeletonType } from '../enums/SkeletonType.ts'+import BoneWeightsByDistanceChild from '../solvers/BoneWeightsByDistanceChild.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepWeightSkin extends EventTarget {@@ -52,6 +53,8 @@ export class StepWeightSkin extends EventTarget {this.bone_skinning_formula = new BoneWeightsByDistance(this.skinning_armature.children[0], skeleton_type)} else if (skinning_formula === SkinningFormula.MedianDistance) {this.bone_skinning_formula = new BoneWeightsByMedianDistance(this.skinning_armature.children[0], skeleton_type)+ } else if (skinning_formula === SkinningFormula.DistanceChild) {+ this.bone_skinning_formula = new BoneWeightsByDistanceChild(this.skinning_armature.children[0], skeleton_type)}return this.bone_skinning_formuladiff --git a/src/lib/solvers/BoneWeightsByDistanceChild.ts b/src/lib/solvers/BoneWeightsByDistanceChild.tsnew file mode 100644index 0000000..5fc2b0d--- /dev/null+++ b/src/lib/solvers/BoneWeightsByDistanceChild.ts@@ -0,0 +1,113 @@+import { type Bone, BufferGeometry, Object3D, Vector3 } from 'three'+import { Utility } from '../Utilities.js'+import BoneCalculationData from '../models/BoneCalculationData.js'+import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.js'+import BoneTesterData from '../models/BoneTesterData.js'+import { SkeletonType } from '../enums/SkeletonType.js'++/**+ * BoneWeightsByDistanceChild+ * This works very similar to the normal distance solver.+ * 1. First pass is to assign closest bone to each vertex like before+ * 2. Second pass is to look through each vertex and look at bone assigned and direction+ * - if bone to vertex direction points to a similar direction to parent bone, assign to the parent bone+ */+export default class BoneWeightsByDistanceChild implements IAutoSkinSolver {+ private readonly bones_master_data: BoneCalculationData[] = []+ private geometry: BufferGeometry = new BufferGeometry()+ private show_debug: boolean = false+ private skeleton_type: SkeletonType | null = null++ constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {+ this.set_skeleton_type(skeleton_type)+ this.init_bone_weights_data_structure(bone_hier)+ console.log('Using the Bone weights by distance child solver')+ }++ public set_debugging_scene_object (scene_object: Object3D): void {+ this.debugging_scene_object = scene_object+ }++ public set_geometry (geom: BufferGeometry): void {+ this.geometry = geom+ }++ public set_skeleton_type (skinning_type: SkeletonType): void {+ this.skeleton_type = skinning_type+ }++ public set_show_debug (debug_value: boolean): void {+ this.show_debug = debug_value+ }++ public set_bone_index_to_test (bone_idx: number): void {+ this.bone_idx_test = bone_idx+ }++ public calculate_indexes_and_weights (): number[][] {+ const skin_indices: number[] = []+ const skin_weights: number[] = []++ // loop through each vertex and find the closest bone+ // then assign the closest vertices to that bone in the assigned_vertices property+ for (let i = 0; i < this.get_vertex_count(); i++) {+ const vertex_position: Vector3 = new Vector3().fromBufferAttribute(this.geometry.attributes.position, i)+ // let closest_bone: Bone = this.bones_master_data[0].bone_object+ let closest_bone_distance: number = 10000+ let closest_bone_index: number = 0++ this.bones_master_data.forEach((bone, idx) => {+ // our human skeleton has a root controller bone that isn't in the mesh. Don't assign weights to this?+ if (this.skeleton_type === SkeletonType.Human) {+ if (bone.bone_object.name === 'root') {+ return+ }+ }++ const distance: number = Utility.world_position_from_object(bone.bone_object).distanceTo(vertex_position)++ if (distance < closest_bone_distance) {+ // closest_bone = bone.bone_object+ closest_bone_distance = distance+ closest_bone_index = idx+ }+ })++ this.bones_master_data[closest_bone_index].assigned_vertices.push(i)++ // assign to final weights. closest bone is always 100% weight+ skin_indices.push(closest_bone_index, 0, 0, 0)+ skin_weights.push(1.0, 0, 0, 0)+ }++ if (this.show_debug) {+ console.log('Assigned all the bones to vertices: ', this.bones_master_data)+ }++ return [skin_indices, skin_weights]+ }++ public test_bones_outside_in_mesh (): BoneTesterData {+ // don't do test for now and just return success+ return new BoneTesterData([], [])+ }++ private init_bone_weights_data_structure (bone_hier: Object3D): void {+ const bones_list: Bone[] = Utility.bone_list_from_hierarchy(bone_hier)+ bones_list.forEach((bone) => {+ const has_child_bone: boolean = bone.children.length > 0+ const supports_envelope: boolean = false+ const new_bone_object: BoneCalculationData =+ new BoneCalculationData(bone.name, bone, supports_envelope, has_child_bone)+ this.bones_master_data.push(new_bone_object)+ })+ }++ private get_vertex_count (): number {+ if (this.geometry === null) {+ return -1+ }++ return this.geometry.attributes.position.array.length / 3+ }+}