Import a 3D Model and automatically assign and export animations
TypeScript
Scale position keyframes based on skeleton scale amount instead of hip bone logic
Scott Petrovicd88a77aa62c2
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.tsindex b85d177..55d12d7 100644--- a/src/lib/EventListeners.ts+++ b/src/lib/EventListeners.ts@@ -13,6 +13,7 @@ export class EventListeners {})// Listen for skeleton transformation events to update UI and visuals+ // this can happen with undo/redo systemthis.bootstrap.edit_skeleton_step.addEventListener('skeletonTransformed', () => {// Update skeleton helper if it existsif (this.bootstrap.skeleton_helper !== undefined) {@@ -94,7 +95,7 @@ export class EventListeners {// rotate model after loading it in to orient it correctlythis.bootstrap.ui.dom_rotate_model_x_button?.addEventListener('click', () => {- this.bootstrap.load_model_step.rotate_model_geometry('x', 90)+ this.bootstrap.load_model_step.rotate_model_geometry('x', 90)})this.bootstrap.ui.dom_rotate_model_y_button?.addEventListener('click', () => {diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex 85da7c4..985f258 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -30,7 +30,7 @@ export class StepAnimationsListing extends EventTarget {// retrieved from load skeleton step// we will use this to scale all position animation keyframes (uniform scale)- private skeleton_scale: number = 1.0+ private skeleton_scale: number = 1.0private _added_event_listeners: boolean = false@@ -38,8 +38,8 @@ export class StepAnimationsListing extends EventTarget {public animation_search: AnimationSearch | null = null// -z will bring hip bone down- private hip_bone_offset: Vector3 = new Vector3(0, 0, 0) // -z will bring hip bone down. Helps set new base hip position- private hip_bone_scale_factor_z: number = 1.0 // this is used to scale the hip bone position for animations+ // private hip_bone_offset: Vector3 = new Vector3(0, 0, 0) // -z will bring hip bone down. Helps set new base hip position+ // private hip_bone_scale_factor_z: number = 1.0 // this is used to scale the hip bone position for animations/*** The amount to raise the arms.@@ -49,33 +49,33 @@ export class StepAnimationsListing extends EventTarget {// the human model has a hip bone that needs position changes applied// This will be for animations like falling. We need to capture the offset between// the original position and the new position from our edited armature- public calculate_hip_bone_offset (original_armature: Object3D, edited_armature: Object3D): void {- const original_hip_bone: Bone = this.find_bone_from_armature(original_armature, 'DEF-hips')- const edited_hip_bone: Bone = this.find_bone_from_armature(edited_armature, 'DEF-hips')- this.hip_bone_offset = this.calculate_position_offset(original_hip_bone.position, edited_hip_bone.position)-- // calculate the scale factor for the animation clips- // this should take the distance between the original and edited armature- // and divide it by the original armature hip bone position- this.hip_bone_scale_factor_z = edited_hip_bone.position.z / original_hip_bone.position.z+ // public calculate_hip_bone_offset (original_armature: Object3D, edited_armature: Object3D): void {+ // const original_hip_bone: Bone = this.find_bone_from_armature(original_armature, 'DEF-hips')+ // const edited_hip_bone: Bone = this.find_bone_from_armature(edited_armature, 'DEF-hips')+ // this.hip_bone_offset = this.calculate_position_offset(original_hip_bone.position, edited_hip_bone.position)++ // // calculate the scale factor for the animation clips+ // // this should take the distance between the original and edited armature+ // // and divide it by the original armature hip bone position+ // this.hip_bone_scale_factor_z = edited_hip_bone.position.z / original_hip_bone.position.z- // small T-pose offset that somehow is getting lost- this.hip_bone_offset = this.hip_bone_offset.sub(new Vector3(0, 0, -0.04))- }-- private find_bone_from_armature (armature: Object3D, bone_name: string): Bone | null {- let found_bone: Bone | null = null- armature.traverse((object: Object3D) => {- if (found_bone === null && object.name === bone_name && object instanceof Bone) {- found_bone = object- }- })- return found_bone- }-- private calculate_position_offset (position_1: Vector3, position_2: Vector3): Vector3 {- return new Vector3(position_1.x - position_2.x, position_1.y - position_2.y, position_1.z - position_2.z)- }+ // // small T-pose offset that somehow is getting lost+ // this.hip_bone_offset = this.hip_bone_offset.sub(new Vector3(0, 0, -0.04))+ // }++ // private find_bone_from_armature (armature: Object3D, bone_name: string): Bone | null {+ // let found_bone: Bone | null = null+ // armature.traverse((object: Object3D) => {+ // if (found_bone === null && object.name === bone_name && object instanceof Bone) {+ // found_bone = object+ // }+ // })+ // return found_bone+ // }++ // private calculate_position_offset (position_1: Vector3, position_2: Vector3): Vector3 {+ // return new Vector3(position_1.x - position_2.x, position_1.y - position_2.y, position_1.z - position_2.z)+ // }private has_added_event_listeners: boolean = false@@ -125,8 +125,8 @@ export class StepAnimationsListing extends EventTarget {this.skinned_meshes_to_animate = []this.animation_mixer = new AnimationMixer()this.current_playing_index = 0- this.hip_bone_offset = new Vector3(0, 0, 0)- this.hip_bone_scale_factor_z = 1.0+ // this.hip_bone_offset = new Vector3(0, 0, 0)+ // this.hip_bone_scale_factor_z = 1.0this.animation_player.clear_animation()}@@ -185,8 +185,8 @@ export class StepAnimationsListing extends EventTarget {// this mutates the cloned_anims, so no need for returning anythingUtility.clean_track_data(cloned_anims, true)- // apply hip bone offset- this.apply_hip_bone_offset(cloned_anims)+ // apply scaling to position keyframes if we scaled skeleton up or down+ this.apply_skeleton_scale_to_position_keyframes(cloned_anims)// we did all the processing needed, so add them// to the full list of animation clips@@ -238,18 +238,17 @@ export class StepAnimationsListing extends EventTarget {this.ui.dom_animations_listing_count.innerHTML = animation_length_string + ' animations'}- private apply_hip_bone_offset (animation_clips: AnimationClip[]): void {- // update the position keyframes for the hips bone- // this will be used for animations like falling+ private apply_skeleton_scale_to_position_keyframes (animation_clips: AnimationClip[]): void {animation_clips.forEach((animation_clip: AnimationClip) => {animation_clip.tracks.forEach((track: KeyframeTrack) => {- if (track.name.includes('hips.position')) {+ if (track.name.includes('.position')) {const values = track.valuesfor (let i = 0; i < values.length; i += 3) {- values[i] = values[i] - this.hip_bone_offset.x- values[i + 1] = values[i + 1] - this.hip_bone_offset.y- values[i + 2] = values[i + 2] * this.hip_bone_scale_factor_z+ values[i] *= this.skeleton_scale+ values[i + 1] *= this.skeleton_scale+ values[i + 2] *= this.skeleton_scale}+ console.log(track.name, this.skeleton_scale, track.values)}})})diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.tsindex b8f4809..172366a 100644--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts@@ -171,19 +171,19 @@ export class StepEditSkeleton extends EventTarget {})}- if (this.ui.dom_scale_skeleton_button !== null && this.ui.dom_scale_skeleton_input_box !== null) {- this.ui.dom_scale_skeleton_button.addEventListener('click', () => {- // Store undo state before scaling- this.store_bone_state_for_undo()+ // if (this.ui.dom_scale_skeleton_button !== null && this.ui.dom_scale_skeleton_input_box !== null) {+ // this.ui.dom_scale_skeleton_button.addEventListener('click', () => {+ // // Store undo state before scaling+ // this.store_bone_state_for_undo()- const modify_scale = 1.0 + (this.ui.dom_scale_skeleton_input_box.value / 100.0)- Utility.scale_armature_by_scalar(this.edited_armature, modify_scale)- this.edited_armature.updateWorldMatrix(true, true)+ // const modify_scale = 1.0 + (this.ui.dom_scale_skeleton_input_box.value / 100.0)+ // Utility.scale_armature_by_scalar(this.edited_armature, modify_scale)+ // this.edited_armature.updateWorldMatrix(true, true)- // Dispatch skeleton transformed event to update UI- this.dispatchEvent(new CustomEvent('skeletonTransformed'))- })- }+ // // Dispatch skeleton transformed event to update UI+ // this.dispatchEvent(new CustomEvent('skeletonTransformed'))+ // })+ // }if (this.ui.dom_mirror_skeleton_checkbox !== null) {this.ui.dom_mirror_skeleton_checkbox.addEventListener('change', (event) => {@@ -285,6 +285,7 @@ export class StepEditSkeleton extends EventTarget {*/public load_original_armature_from_model (armature: Object3D): void {this.edited_armature = armature.clone()+this.create_threejs_skeleton_object()// Initialize the undo/redo system with the skeletondiff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.tsindex 837b9b1..31549fd 100644--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts@@ -225,8 +225,27 @@ export class StepLoadSkeleton extends EventTarget {btn.disabled = !allow // disable when not allowed}+ // returns a skeleton object that has been baked (applied) for scalepublic armature (): Object3D<Object3DEventMap> {- return this.loaded_armature+ return this.bake_scale_for_armature(this.loaded_armature)+ }++ // this does not mutate armature that goes in+ // update all positions for bones and resets scale to 1+ private bake_scale_for_armature (armature: Object3D): Object3D {+ const scale = armature.scale.x // assumes uniform scale+ if (scale === 1) return;++ const cloned_armature: Object3D = armature.clone()+ cloned_armature.traverse((obj) => {+ if (obj instanceof Object3D && obj !== cloned_armature) {+ obj.position.multiplyScalar(scale)+ }+ })+ cloned_armature.scale.set(1, 1, 1)+ cloned_armature.updateMatrixWorld(true)++ return cloned_armature}private toggle_ui_hand_skeleton_options (): void {diff --git a/src/script.ts b/src/script.tsindex 49b6698..e4e58e0 100644--- a/src/script.ts+++ b/src/script.ts@@ -285,7 +285,7 @@ export class Bootstrap {this.process_step = ProcessStep.BindPosethis.transform_controls.enabled = false // shouldn't be editing bonesthis.calculate_skin_weighting_for_models()- this.regenerate_skeleton_helper(this.weight_skin_step.skeleton())+ // this.regenerate_skeleton_helper(this.weight_skin_step.skeleton())this.scene.add(...this.weight_skin_step.final_skinned_meshes()) // add final skinned mesh to scenethis.weight_skin_step.weight_painted_mesh_group().visible = false // hide weight painted meshthis.process_step_changed(ProcessStep.AnimationsListing)@@ -304,12 +304,6 @@ export class Bootstrap {// Show/hide A-Pose correction options based on skeleton typethis.update_a_pose_options_visibility()- // calculate hip bone offset for human skeleton type- if (this.load_skeleton_step.skeleton_type() === SkeletonType.Human) {- this.animations_listing_step.calculate_hip_bone_offset(this.load_skeleton_step.armature(),- this.edit_skeleton_step.armature())- }-this.animations_listing_step.load_and_apply_default_animation_to_skinned_mesh(this.weight_skin_step.final_skinned_meshes())if (this.skeleton_helper !== undefined) {