Import a 3D Model and automatically assign and export animations
TypeScript
Start moving animation listing cleanup to its own utility class
Scott Petrovic2edef784ccd5
diff --git a/src/lib/Utilities.ts b/src/lib/Utilities.tsindex 1e1a3cb..960896c 100644--- a/src/lib/Utilities.ts+++ b/src/lib/Utilities.ts@@ -1,5 +1,5 @@import {- Vector3, Vector2, type Object3D, Mesh, Group, Bone, type Skeleton, Euler, Raycaster, AnimationClip, type KeyframeTrack,+ Vector3, Vector2, type Object3D, Mesh, Group, Bone, type Skeleton, Euler, Raycaster,type PerspectiveCamera, type Scene, type Object3DEventMap, type BufferAttribute, type BufferGeometry, type InterleavedBufferAttribute} from 'three'import BoneTransformState from './interfaces/BoneTransformState'@@ -293,36 +293,6 @@ export class Utility {return closest_bone_index}- static deep_clone_animation_clip (clip: AnimationClip): AnimationClip {- const tracks = clip.tracks.map((track: KeyframeTrack) => track.clone())- return new AnimationClip(clip.name, clip.duration, tracks)- }-- static deep_clone_animation_clips (animation_clips: AnimationClip[]): AnimationClip[] {- return animation_clips.map((clip: AnimationClip) => {- return Utility.deep_clone_animation_clip(clip)- })- }-- /// Removes position tracks from animation clips, keeping only rotation tracks.- /// @param animation_clips - The animation clips to modify.- /// @param preserve_root_position - Whether to keep the root position track.- static clean_track_data (animation_clips: AnimationClip[], preserve_root_position: boolean = false): void {- animation_clips.forEach((animation_clip: AnimationClip) => {- // remove all position nodes except root- let rotation_tracks: KeyframeTrack[] = []-- if (preserve_root_position) {- rotation_tracks = animation_clip.tracks.filter((x: KeyframeTrack) => x.name.includes('quaternion') || x.name.toLowerCase().includes('hips.position'))- } else {- rotation_tracks = animation_clip.tracks.filter((x: KeyframeTrack) => x.name.includes('quaternion') || x.name.includes('hips.position'))- }-- animation_clip.tracks = rotation_tracks // update track data- // console.log(animation_clip.tracks) // UNUSED DEBUG CODE- })- }-static parse_input_number (value: string | undefined): number {if (value === undefined || value === null) {return 0diff --git a/src/lib/processes/animations-listing/AnimationUtility.ts b/src/lib/processes/animations-listing/AnimationUtility.tsnew file mode 100644index 0000000..2863051--- /dev/null+++ b/src/lib/processes/animations-listing/AnimationUtility.ts@@ -0,0 +1,53 @@+import { AnimationClip, type KeyframeTrack } from "three"++export class AnimationUtility {+ // when we scaled the skeleton itself near the beginning, we kept track of that+ // this scaling will affect position keyframes since they expect the original skeleton scale+ // this will fix any issues with position keyframes not matching the current skeleton scale+ static apply_skeleton_scale_to_position_keyframes (animation_clips: AnimationClip[], scaleAmount: number): void {+ animation_clips.forEach((animation_clip: AnimationClip) => {+ animation_clip.tracks.forEach((track: KeyframeTrack) => {+ if (track.name.includes('.position')) {+ const values = track.values+ for (let i = 0; i < values.length; i += 3) {+ values[i] *= scaleAmount+ values[i + 1] *= scaleAmount+ values[i + 2] *= scaleAmount+ }+ }+ })+ })+ }++ static deep_clone_animation_clip (clip: AnimationClip): AnimationClip {+ const tracks = clip.tracks.map((track: KeyframeTrack) => track.clone())+ return new AnimationClip(clip.name, clip.duration, tracks)+ }++ static deep_clone_animation_clips (animation_clips: AnimationClip[]): AnimationClip[] {+ return animation_clips.map((clip: AnimationClip) => {+ return this.deep_clone_animation_clip(clip)+ })+ }++ /// Removes position tracks from animation clips, keeping only rotation tracks.+ /// @param animation_clips - The animation clips to modify.+ /// @param preserve_root_position - Whether to keep the root position track.+ static clean_track_data (animation_clips: AnimationClip[], preserve_root_position: boolean = false): void {+ animation_clips.forEach((animation_clip: AnimationClip) => {+ // remove all position nodes except root+ let rotation_tracks: KeyframeTrack[] = []++ if (preserve_root_position) {+ rotation_tracks = animation_clip.tracks+ .filter((x: KeyframeTrack) => x.name.includes('quaternion') || x.name.toLowerCase().includes('hips.position'))+ } else {+ rotation_tracks = animation_clip.tracks+ .filter((x: KeyframeTrack) => x.name.includes('quaternion') || x.name.includes('hips.position'))+ }++ animation_clip.tracks = rotation_tracks // update track data+ // console.log(animation_clip.tracks) // UNUSED DEBUG CODE+ })+ }+}diff --git a/src/lib/processes/animations-listing/StepAnimationsListing.ts b/src/lib/processes/animations-listing/StepAnimationsListing.tsindex 5bdded3..dd77bb5 100644--- a/src/lib/processes/animations-listing/StepAnimationsListing.ts+++ b/src/lib/processes/animations-listing/StepAnimationsListing.ts@@ -5,16 +5,17 @@ import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import {type AnimationClip, AnimationMixer, Quaternion, Vector3, type SkinnedMesh, type QuaternionKeyframeTrack,- type KeyframeTrack, type AnimationAction, type Object3D, Bone,- Skeleton+ type KeyframeTrack, type AnimationAction, type Object3D, Bone, Skeleton} from 'three'+import { AnimationUtility } from './AnimationUtility.ts'+import { SkeletonType } from '../../enums/SkeletonType.ts'import { Utility } from '../../Utilities.ts'import { type ThemeManager } from '../../ThemeManager.ts'import { AnimationSearch } from './AnimationSearch.ts'import { type TransformedAnimationClipPair } from './interfaces/TransformedAnimationClipPair.ts'-import { AnimationWithState } from './interfaces/AnimationWithState.ts'+import { type AnimationWithState } from './interfaces/AnimationWithState.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepAnimationsListing extends EventTarget {@@ -159,20 +160,20 @@ export class StepAnimationsListing extends EventTarget {// load the animation clips into a new array// then, remove the animation position keyframes. That will mess up the skinning// process since we will be offsetting and moving the bone root positions- const cloned_anims: AnimationClip[] = Utility.deep_clone_animation_clips(gltf.animations)+ const cloned_anims: AnimationClip[] = AnimationUtility.deep_clone_animation_clips(gltf.animations)// only keep position tracks// this mutates the cloned_anims, so no need for returning anything- Utility.clean_track_data(cloned_anims, true)+ AnimationUtility.clean_track_data(cloned_anims, true)// apply scaling to position keyframes if we scaled skeleton up or down- this.apply_skeleton_scale_to_position_keyframes(cloned_anims)+ AnimationUtility.apply_skeleton_scale_to_position_keyframes(cloned_anims, this.skeleton_scale)// we did all the processing needed, so add them// to the full list of animation clipsthis.animation_clips_loaded.push(...cloned_anims.map(clip => ({original_animation_clip: clip,- display_animation_clip: Utility.deep_clone_animation_clip(clip),+ display_animation_clip: AnimationUtility.deep_clone_animation_clip(clip)})))remaining_loads--@@ -218,31 +219,14 @@ export class StepAnimationsListing extends EventTarget {this.ui.dom_animations_listing_count.innerHTML = animation_length_string + ' animations'}- // when we scaled the skeleton itself near the beginning, we kept track of that- // this scaling will affect position keyframes since they expect the original skeleton scale- // this will fix any issues with position keyframes not matching the current skeleton scale- 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('.position')) {- const values = track.values- for (let i = 0; i < values.length; i += 3) {- values[i] *= this.skeleton_scale- values[i + 1] *= this.skeleton_scale- values[i + 2] *= this.skeleton_scale- }- }- })- })- }-+/*** Rebuilds all of the warped animations by applying the specified warps.*/private rebuild_warped_animations (): void {// Reset all of the warped clips to the corresponding original clip.this.animation_clips_loaded.forEach((warped_clip: TransformedAnimationClipPair) => {- warped_clip.display_animation_clip = Utility.deep_clone_animation_clip(warped_clip.original_animation_clip)+ warped_clip.display_animation_clip = AnimationUtility.deep_clone_animation_clip(warped_clip.original_animation_clip)})/// Apply the arm extension warp:this.apply_arm_extension_warp(this.warp_arm_amount)