Import a 3D Model and automatically assign and export animations
TypeScript
Bring skeleton type into formula so different skeletons can have different logic
Scott Petrovic31741a1695c6
diff --git a/src/lib/interfaces/IAutoSkinSolver.ts b/src/lib/interfaces/IAutoSkinSolver.tsindex 64cd641..872abf1 100644--- a/src/lib/interfaces/IAutoSkinSolver.ts+++ b/src/lib/interfaces/IAutoSkinSolver.ts@@ -1,5 +1,6 @@import { type Object3D, type BufferGeometry } from 'three'import type BoneTesterData from '../models/BoneTesterData'+import { type SkeletonType } from '../enums/SkeletonType'export interface IAutoSkinSolver {set_geometry: (geom: BufferGeometry) => void@@ -8,4 +9,5 @@ export interface IAutoSkinSolver {set_bone_index_to_test: (bone_idx: number) => voidcalculate_indexes_and_weights: () => number[][]test_bones_outside_in_mesh: () => BoneTesterData+ set_skeleton_type: (skinning_type: SkeletonType) => void}diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.tsindex cccf75c..fcda35f 100644--- a/src/lib/processes/StepAnimationsListing.ts+++ b/src/lib/processes/StepAnimationsListing.ts@@ -65,7 +65,7 @@ export class StepAnimationsListing extends EventTarget {return this.animation_clips_loaded}- public load_and_apply_default_animation_to_skinned_mesh(final_skinned_meshes: SkinnedMesh[], skeleton_type: string): void {+ public load_and_apply_default_animation_to_skinned_mesh(final_skinned_meshes: SkinnedMesh[], skeleton_type: SkeletonType): void {this.skinned_meshes_to_animate = final_skinned_meshesthis.skeleton_type = skeleton_typediff --git a/src/lib/processes/StepLoadSkeleton.ts b/src/lib/processes/StepLoadSkeleton.tsindex 533c773..b47b06c 100644--- a/src/lib/processes/StepLoadSkeleton.ts+++ b/src/lib/processes/StepLoadSkeleton.ts@@ -14,10 +14,10 @@ export class StepLoadSkeleton extends EventTarget {private readonly ui: UI = new UI()private loaded_armature: Object3D = new Object3D()private loaded_skeleton: Skeleton | undefined- private skeleton_ty: string = ''+ private skeleton_t: SkeletonType = SkeletonType.BipedalSimple- public skeleton_type (): string {- return this.skeleton_ty+ public skeleton_type (): SkeletonType {+ return this.skeleton_t}begin (): void {@@ -63,18 +63,18 @@ export class StepLoadSkeleton extends EventTarget {// so it knows what animations to loadswitch (skeleton_file) {case 'quadraped':- this.skeleton_ty = SkeletonType.Quadraped+ this.skeleton_t = SkeletonType.Quadrapedbreakcase 'bipedal-simple':- this.skeleton_ty = SkeletonType.BipedalSimple+ this.skeleton_t = SkeletonType.BipedalSimplebreakcase 'bipedal-full':- this.skeleton_ty = SkeletonType.BipedalFull+ this.skeleton_t = SkeletonType.BipedalFullbreak}// load skeleton from GLB file- this.loader.load(this.skeleton_ty, (gltf) => {+ this.loader.load(this.skeleton_t, (gltf) => {// traverse scene and find first bone object// we will go to the parent and mark that as the original armaturelet armature_found = falsediff --git a/src/lib/processes/StepWeightSkin.ts b/src/lib/processes/StepWeightSkin.tsindex de22b57..0518e01 100644--- a/src/lib/processes/StepWeightSkin.ts+++ b/src/lib/processes/StepWeightSkin.ts@@ -9,8 +9,9 @@ import { SkinningFormula } from '../enums/SkinningFormula.ts'import { Generators } from '../Generators.ts'import { type BufferGeometry, type Material, type Object3D, type Skeleton, SkinnedMesh, type Scene } from 'three'-import { IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.ts'+import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.ts'import BoneTesterData from '../models/BoneTesterData.ts'+import { type SkeletonType } from '../enums/SkeletonType.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepWeightSkin extends EventTarget {@@ -49,17 +50,17 @@ export class StepWeightSkin extends EventTarget {</ol>`}- public create_bone_formula_object (editable_armature: Object3D, skinning_formula: string): any {+ public create_bone_formula_object (editable_armature: Object3D, skinning_formula: string, skeleton_type: SkeletonType): any {this.skinning_armature = editable_armature.clone()this.skinning_armature.name = 'Armature for skinning'// Swap out formulas to see different resultsif (skinning_formula === SkinningFormula.Envelope) {- this.bone_skinning_formula = new BoneWeightsByEnvelope(this.skinning_armature.children[0])+ this.bone_skinning_formula = new BoneWeightsByEnvelope(this.skinning_armature.children[0], skeleton_type)} else if (skinning_formula === SkinningFormula.Distance) {- this.bone_skinning_formula = new BoneWeightsByDistance(this.skinning_armature.children[0])+ 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])+ this.bone_skinning_formula = new BoneWeightsByMedianDistance(this.skinning_armature.children[0], skeleton_type)}return this.bone_skinning_formuladiff --git a/src/lib/solvers/BoneWeightsByDistance.ts b/src/lib/solvers/BoneWeightsByDistance.tsindex 23e08c1..164b079 100644--- a/src/lib/solvers/BoneWeightsByDistance.ts+++ b/src/lib/solvers/BoneWeightsByDistance.ts@@ -3,6 +3,7 @@ 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'export default class BoneWeightsByDistance implements IAutoSkinSolver {private readonly bones_master_data: BoneCalculationData[] = []@@ -10,8 +11,10 @@ export default class BoneWeightsByDistance implements IAutoSkinSolver {private show_debug: boolean = falseprivate bone_idx_test: number = -1private debugging_scene_object: Object3D = new Object3D()+ private skeleton_type: SkeletonType = SkeletonType.BipedalSimple- constructor (bone_hier: Object3D) {+ constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {+ this.set_skeleton_type(skeleton_type)this.init_bone_weights_data_structure(bone_hier)}@@ -23,6 +26,10 @@ export default class BoneWeightsByDistance implements IAutoSkinSolver {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}diff --git a/src/lib/solvers/BoneWeightsByEnvelope.ts b/src/lib/solvers/BoneWeightsByEnvelope.tsindex 216d399..2b03aa0 100644--- a/src/lib/solvers/BoneWeightsByEnvelope.ts+++ b/src/lib/solvers/BoneWeightsByEnvelope.ts@@ -8,12 +8,14 @@ import { type IAutoSkinSolver } from '../interfaces/IAutoSkinSolver.ts'import type IntersectionPointData from '../models/IntersectionPointData.ts'import IntersectionGeometryData from '../models/IntersectionGeometryData.ts'import BoneTesterData from '../models/BoneTesterData.ts'+import { SkeletonType } from '../enums/SkeletonType.ts'export class BoneWeightsByEnvelope implements IAutoSkinSolver {private bones_master_data: BoneCalculationData[] = []private geometry: BufferGeometry = new BufferGeometry()private show_debug: boolean = falseprivate bone_idx_test: number = -1+ private skeleton_type: SkeletonType = SkeletonType.BipedalSimpleprivate debugging_scene_object: Object3D = new Object3D()private readonly debug_sphere_size: number = 0.06@@ -23,7 +25,8 @@ export class BoneWeightsByEnvelope implements IAutoSkinSolver {// two variables used for testing to see if we have an ok bone envelope to bindprivate readonly bone_testing_data: BoneTesterData = new BoneTesterData([], [])- constructor (bone_hier: Object3D) {+ constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {+ this.set_skeleton_type(skeleton_type)this.init_bone_weights_data_structure(bone_hier)}@@ -161,6 +164,11 @@ export class BoneWeightsByEnvelope implements IAutoSkinSolver {this.debugging_scene_object = scene_object}+ public set_skeleton_type (skinning_type: SkeletonType): void {+ this.skeleton_type = skinning_type+ console.log('setting skeleton type to: ', this.skeleton_type)+ }+private geometry_vertex_count (): number {return this.geometry?.attributes?.position.array.length / 3}@@ -171,21 +179,30 @@ export class BoneWeightsByEnvelope implements IAutoSkinSolver {bones.forEach((bone) => {const has_child_bone: boolean = bone.children.length > 0const supports_envelope: boolean = this.is_bone_valid_for_envelope_calculation(bone)+ const uses_minimum_expand_distance: boolean = this.is_bone_using_minimum_expand_distance(bone)+ const new_bone_object: BoneCalculationData = new BoneCalculationData(bone.name, bone, supports_envelope, has_child_bone, [], uses_minimum_expand_distance)+ this.bones_master_data.push(new_bone_object)+ })+ }- // 4 leg creature has entire word "right", while human has appreviation "R"- let use_minimum_expand_distance: boolean = false+ private is_bone_using_minimum_expand_distance(bone: Bone): boolean {+ if (this.skeleton_type === SkeletonType.Quadraped) {if (bone.name.includes('Front_Leg_Upper') ||bone.name.includes('Back_Leg_Upper') ||bone.name.includes('_Toe') ||- bone.name.includes('Shoulder') ||bone.name.includes('UpLeg')) {- use_minimum_expand_distance = true+ return true}+ }- const new_bone_object: BoneCalculationData = new BoneCalculationData(bone.name, bone, supports_envelope, has_child_bone, [], use_minimum_expand_distance)+ // humanoid character has separate rules with bone envelopes+ if (this.skeleton_type === SkeletonType.BipedalFull || this.skeleton_type === SkeletonType.BipedalSimple) {+ if (bone.name.includes('Shoulder')) {+ return true+ }+ }- this.bones_master_data.push(new_bone_object)- })+ return false // each direction will expand to the max distance}private create_envelope_mesh_for_bone (bone: BoneCalculationData): Mesh {@@ -363,11 +380,18 @@ export class BoneWeightsByEnvelope implements IAutoSkinSolver {// we are going to ignore certain bones for the bone as they// have odd angles and create bad envelopes- if (- bone.name.includes('Fingers') ||+ if (this.skeleton_type === SkeletonType.BipedalSimple) {+ if (bone.name.includes('Fingers') ||bone.name.includes('HandBase') ||bone.name === 'SpineUp') {- return false+ return false+ }+ }++ if (this.skeleton_type === SkeletonType.BipedalFull) {+ if (bone.name === 'mixamorig_Spine2') {+ return false+ }}return true // bone is ok to do envelope calculationsdiff --git a/src/lib/solvers/BoneWeightsByMedianDistance.ts b/src/lib/solvers/BoneWeightsByMedianDistance.tsindex 23b5921..7ebebaf 100644--- a/src/lib/solvers/BoneWeightsByMedianDistance.ts+++ b/src/lib/solvers/BoneWeightsByMedianDistance.ts@@ -3,6 +3,7 @@ import { type Bone, BufferGeometry, Object3D, type Scene, Vector3 } from 'three'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'/*** This calculation is similar to the normal bone weights by distance, but uses the@@ -17,8 +18,10 @@ export default class BoneWeightsByMedianDistance implements IAutoSkinSolver {private bone_idx_test: number = -1private readonly bones_master_data: BoneCalculationData[] = []private debugging_scene_object: Object3D = new Object3D()+ private skeleton_type: SkeletonType = SkeletonType.BipedalSimple- constructor (bone_hier: Object3D) {+ constructor (bone_hier: Object3D, skeleton_type: SkeletonType) {+ this.set_skeleton_type(skeleton_type)this.init_bone_weights_data_structure(bone_hier)}@@ -38,6 +41,10 @@ export default class BoneWeightsByMedianDistance implements IAutoSkinSolver {this.bone_idx_test = bone_idx}+ public set_skeleton_type (skinning_type: SkeletonType): void {+ this.skeleton_type = skinning_type+ }+public set_debugging_scene_object (scene_object: Object3D): void {this.debugging_scene_object = scene_object}diff --git a/src/script.ts b/src/script.tsindex 95d199e..cd2b005 100644--- a/src/script.ts+++ b/src/script.ts@@ -20,6 +20,7 @@ import { type Bone, Group, Scene, type Skeleton, type Vector3, type SkeletonHelpimport type BoneTesterData from './lib/models/BoneTesterData.ts'import { build_version } from './environment.js'+import { SkeletonType } from './lib/enums/SkeletonType.ts'export class Bootstrap {private readonly camera = Generators.create_camera()@@ -383,7 +384,8 @@ export class Bootstrap {private test_bone_weighting_success (): boolean {this.debugging_visual_object = Utility.regenerate_debugging_scene(this.scene) // clear out the debugging scene- this.weight_skin_step.create_bone_formula_object(this.edit_skeleton_step.armature(), this.edit_skeleton_step.algorithm())+ this.weight_skin_step.create_bone_formula_object(this.edit_skeleton_step.armature(), this.edit_skeleton_step.algorithm(),+ this.load_skeleton_step.skeleton_type())if (this.edit_skeleton_step.show_debugging()) {this.weight_skin_step.set_show_debug(this.edit_skeleton_step.show_debugging())