Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Convert animation listing file to typescript for easier reading

Scott Petrovic8d70082bb505
diff --git a/src/lib/processes/StepAnimationsListing.js b/src/lib/processes/StepAnimationsListing.ts
similarity index 60%
rename from src/lib/processes/StepAnimationsListing.js
rename to src/lib/processes/StepAnimationsListing.ts
index 9d1bd69..b0b891a 100644
--- a/src/lib/processes/StepAnimationsListing.js
+++ b/src/lib/processes/StepAnimationsListing.ts
@@ -4,16 +4,17 @@ import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { AnimationClip, AnimationMixer, Quaternion, Vector3 } from 'three';
import { SkeletonType } from '../enums/SkeletonType.js';
+import { SkinnedMesh } from 'three';
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepAnimationsListing extends EventTarget
{
- ui = null;
- animation_clips_loaded = []
- animation_loader = null
- animation_mixer = null
- skinned_meshes_to_animate = []
- current_playing_index = 0
+ private ui: UI;
+ private animation_clips_loaded: AnimationClip[] = []
+ private animation_loader: GLTFLoader;
+ private animation_mixer: AnimationMixer = null;
+ private skinned_meshes_to_animate: SkinnedMesh[] = []
+ private current_playing_index: number = 0
constructor()
{
@@ -21,17 +22,35 @@ export class StepAnimationsListing extends EventTarget
this.ui = new UI();
}
- begin()
- {
- this.ui.dom_current_step_index.innerHTML = '4'
- this.ui.dom_current_step_element.innerHTML = 'Test animations'
- this.ui.dom_skinned_mesh_tools.style.display = 'flex';
- this.ui.dom_skinned_mesh_animation_tools.style.display = 'flex';
+ public begin() {
+ if(this.ui)
+ {
+ if (this.ui.dom_current_step_index)
+ {
+ this.ui.dom_current_step_index.innerHTML = '4';
+ }
+
+ if (this.ui.dom_current_step_element)
+ {
+ this.ui.dom_current_step_element.innerHTML = 'Test animations';
+ }
+
+ if (this.ui.dom_skinned_mesh_tools)
+ {
+ this.ui.dom_skinned_mesh_tools.style.display = 'flex';
+ }
- this.addEventListeners()
+ if (this.ui.dom_skinned_mesh_animation_tools) {
+ this.ui.dom_skinned_mesh_animation_tools.style.display = 'flex';
+ }
+
+ this.addEventListeners();
+ }
+
+
}
- instructions_text()
+ public instructions_text()
{
return `<div>Instructions</div>
<div>Test and export animations to GLB format</div>
@@ -42,60 +61,52 @@ export class StepAnimationsListing extends EventTarget
</ol>`;
}
- addEventListeners()
+ public mixer()
{
- // event listener for animation clip list with changing the current animation
- this.ui.dom_animation_clip_list.addEventListener('click', (event) => {
- if (event.target.tagName === 'BUTTON')
- {
- let animation_index = event.target.getAttribute('data-index');
- this._play_animation(animation_index);
- }
- });
-
-
-
- this.ui.dom_import_animations_button.addEventListener('change', (event) => {
- const file = event.target.files[0];
- const reader = new FileReader();
- reader.readAsDataURL(file);
+ return this.animation_mixer
+ }
- console.log('loading more files')
-
- reader.onload = () => {
- var animations_import = reader.result
+ public animation_clips()
+ {
+ return this.animation_clips_loaded
+ }
- this.loader = new GLTFLoader();
- this.loader.load(animations_import, (gltf) => {
+ public load_and_apply_default_animation_to_skinned_mesh(final_skinned_meshes, skeleton_type)
+ {
+ this.animation_loader = new GLTFLoader();
+ this.skinned_meshes_to_animate = final_skinned_meshes
- const animations_for_scene = gltf.animations; // we only need the animations
+ let animations_to_load_filepath = null
+ switch(skeleton_type)
+ {
+ case SkeletonType.BipedalSimple:
+ animations_to_load_filepath = 'animations/character-simple-multiple.glb'
+ break;
+ case SkeletonType.Quadraped:
+ animations_to_load_filepath = 'animations/quad-creature-animations.glb'
+ break;
+ case SkeletonType.BipedalFull:
+ animations_to_load_filepath = 'animations/animations-mixamo-default.glb'
+ break;
+ }
- // make sure the animations are compatible with the skeleton
- // TODO: figure out what type of validation I could do with this
+ this.animation_loader.load(animations_to_load_filepath, (gltf) => {
- // add the animations to the animation_clips_loaded
- // update the UI with the new animation clips
- this._append_animation_clips(animations_for_scene)
- this.ui.build_animation_clip_ui(this.animation_clips_loaded);
+ // remove the animation position keyframes. That will mess up the skinning
+ // process since we will be offsetting and moving the bone root positions
+ this.load_animation_clips(gltf.animations)
- // clear out the file input field in case we want to test by loading same file again
- this.ui.dom_import_animations_button.value = '';
+ // create user interface with all available animation clips
+ this.ui.build_animation_clip_ui(this.animation_clips_loaded);
- });
+ // create an animation mixer to do the playback. play the first by by default
+ this.animation_mixer = new AnimationMixer();
- };
-
+ this.play_animation(0);
});
-
- this.ui.dom_extend_arm_button.addEventListener('click', (event) => {
- let extend_arm_value = this.ui.dom_extend_arm_input.value
- this._extend_arm_animations_by_percentage(extend_arm_value)
- this._play_animation(this.current_playing_index)
- })
-
}
- _extend_arm_animations_by_percentage(percentage)
+ private extend_arm_animations_by_percentage(percentage)
{
// loop through each animation clip to update the tracks
this.animation_clips_loaded.forEach((animation_clip) => {
@@ -112,22 +123,30 @@ export class StepAnimationsListing extends EventTarget
{
// get correct value since it is a quaternion
const units_in_quaternions = 4
-
-
const quaternion = new Quaternion();
- quaternion.setFromAxisAngle( new Vector3( 0, percentage, 0 ), Math.PI / 2 );
+ quaternion.setFromAxisAngle( new Vector3( 1, 0, 0 ), percentage/100 );
+
+
+ // get the existing quaternion
+ const existingQuaternion = new Quaternion(
+ new_track_values[i * units_in_quaternions + 0],
+ new_track_values[i * units_in_quaternions + 1],
+ new_track_values[i * units_in_quaternions + 2],
+ new_track_values[i * units_in_quaternions + 3]
+ );
+ // multiply the existing quaternion by the new quaternion
+ existingQuaternion.multiply(quaternion);
// this should change the first quaternion component of the track
- new_track_values[i * units_in_quaternions + 0] = quaternion.x
- new_track_values[i * units_in_quaternions + 1] = quaternion.y
- new_track_values[i * units_in_quaternions + 2] = quaternion.z
- new_track_values[i * units_in_quaternions + 3] = quaternion.w
+ new_track_values[i * units_in_quaternions + 0] = existingQuaternion.x
+ new_track_values[i * units_in_quaternions + 1] = existingQuaternion.y
+ new_track_values[i * units_in_quaternions + 2] = existingQuaternion.z
+ new_track_values[i * units_in_quaternions + 3] = existingQuaternion.w
}
track.values = new_track_values
- console.log(new_track_values)
}
@@ -135,48 +154,116 @@ export class StepAnimationsListing extends EventTarget
})
+ // test out the new values for the animation
+ console.log(this.animation_clips_loaded)
+ this.animation_clips_loaded.forEach((animation_clip) => {
+ animation_clip.tracks.forEach((track) => {
+
+ if(track.name.indexOf('_Arm.quaternion') > -1)
+ {
+ console.log(animation_clip.name, track.name, track.values)
+ }
+ })
+ })
+
+
}
- _play_animation(index = 0)
+ private play_animation(index = 0)
{
this.current_playing_index = index
this.animation_mixer.stopAllAction();
this.skinned_meshes_to_animate.forEach((skinned_mesh) => {
- let anim_clip = this.animation_mixer.clipAction(this.animation_clips_loaded[this.current_playing_index], skinned_mesh);
- anim_clip.play();
+
+
+ const clip_to_play = this.animation_clips_loaded[this.current_playing_index]
+ let anim_action = this.animation_mixer.clipAction(clip_to_play, skinned_mesh);
+
+ // remove the cache for the animation action before playing
+
+
+
+
+
+
+
+ anim_action.reset();
+ anim_action.stop();
+ anim_action.play();
});
}
- removeEventListeners()
+ private load_animation_clips(animation_clips)
{
- this.ui.dom_animation_clip_list.removeEventListener('click', () => {});
+ this.animation_clips_loaded = this.remove_position_and_scale_keyframes_from_animations(animation_clips)
}
-
- mixer()
+ private addEventListeners()
{
- return this.animation_mixer
- }
+ // event listener for animation clip list with changing the current animation
+ if (this.ui.dom_animation_clip_list) {
+ this.ui.dom_animation_clip_list.addEventListener('click', (event) => {
+ if (event.target && event.target.tagName === 'BUTTON')
+ {
+ let animation_index = event.target.getAttribute('data-index');
+ this.play_animation(animation_index);
+ }
+ });
+ }
- animation_clips()
- {
- return this.animation_clips_loaded
+ this.ui.dom_import_animations_button.addEventListener('change', (event) => {
+ const file = event.target.files[0];
+ const reader = new FileReader();
+ reader.readAsDataURL(file);
+
+ console.log('loading more files')
+
+ reader.onload = () => {
+ var animations_import = reader.result
+
+ this.loader = new GLTFLoader();
+ this.loader.load(animations_import, (gltf) => {
+
+ const animations_for_scene = gltf.animations; // we only need the animations
+
+ // make sure the animations are compatible with the skeleton
+ // TODO: figure out what type of validation I could do with this
+
+ // add the animations to the animation_clips_loaded
+ // update the UI with the new animation clips
+ this.append_animation_clips(animations_for_scene)
+ this.ui.build_animation_clip_ui(this.animation_clips_loaded);
+
+ // clear out the file input field in case we want to test by loading same file again
+ this.ui.dom_import_animations_button.value = '';
+
+ });
+
+ };
+
+ });
+
+ this.ui.dom_extend_arm_button.addEventListener('click', (event) => {
+
+ let extend_arm_value = this.ui.dom_extend_arm_input.value
+ this.extend_arm_animations_by_percentage(extend_arm_value)
+
+ this.play_animation(this.current_playing_index)
+
+ })
}
- load_animation_clips(animation_clips)
+ private removeEventListeners()
{
- this.animation_clips_loaded = this._remove_position_keyframes_from_animations(animation_clips)
+ this.ui.dom_animation_clip_list.removeEventListener('click', () => {});
}
- _append_animation_clips(animation_clips)
+ private append_animation_clips(animation_clips)
{
// loop through each animation_clip and change name if the animaton name already exists
// see if the aniation name is already taken.. if so, add a number to the end of the name
-
- console.log(this.animation_clips_loaded)
-
animation_clips.forEach((animation_clip) => {
let is_name_found = this.animation_clips_loaded.find((animation_clip_loaded) => {
@@ -189,17 +276,16 @@ export class StepAnimationsListing extends EventTarget
}
});
- this.animation_clips_loaded.push(...this._remove_position_keyframes_from_animations(animation_clips))
-
+ this.animation_clips_loaded.push(...this.remove_position_and_scale_keyframes_from_animations(animation_clips))
}
- _remove_position_keyframes_from_animations(animation_clip_list)
+ private remove_position_and_scale_keyframes_from_animations(animation_clip_list)
{
- let animation_clips = this._deep_clone_animation_clips(animation_clip_list)
+ let animation_clips = this.deep_clone_animation_clips(animation_clip_list)
let filtered_tracks = animation_clips.map((animation_clip) => {
const filteredTracks = animation_clip.tracks.filter((track) => {
- return track.name.indexOf('position') === -1;
+ return track.name.indexOf('quaternion') > 0;
});
return new AnimationClip(animation_clip.name, animation_clip.duration, filteredTracks);
});
@@ -207,7 +293,7 @@ export class StepAnimationsListing extends EventTarget
return filtered_tracks
}
- _deep_clone_animation_clips(animation_clips)
+ private deep_clone_animation_clips(animation_clips)
{
return animation_clips.map(clip => {
let tracks = clip.tracks.map(track => track.clone());
@@ -215,41 +301,4 @@ export class StepAnimationsListing extends EventTarget
});
}
- load_and_apply_default_animation_to_skinned_mesh(final_skinned_meshes, skeleton_type)
- {
- this.animation_loader = new GLTFLoader();
- this.skinned_meshes_to_animate = final_skinned_meshes
-
- let animations_to_load_filepath = null
- switch(skeleton_type)
- {
- case SkeletonType.BipedalSimple:
- animations_to_load_filepath = 'animations/character-simple-multiple.glb'
- break;
- case SkeletonType.Quadraped:
- animations_to_load_filepath = 'animations/quad-creature-animations.glb'
- break;
- case SkeletonType.BipedalFull:
- animations_to_load_filepath = 'animations/animations-mixamo-default.glb'
- break;
- }
-
- this.animation_loader.load(animations_to_load_filepath, (gltf) => {
-
- // remove the animation position keyframes. That will mess up the skinning
- // process since we will be offsetting and moving the bone root positions
- this.load_animation_clips(gltf.animations)
-
- // create user interface with all available animation clips
- this.ui.build_animation_clip_ui(this.animation_clips_loaded);
-
- // create an animation mixer to do the playback. play the first by by default
- this.animation_mixer = new AnimationMixer();
-
- this._play_animation(0);
- });
- }
-
-
-
}
\ No newline at end of file
diff --git a/src/script.js b/src/script.js
index ab420b3..9596125 100644
--- a/src/script.js
+++ b/src/script.js
@@ -11,7 +11,7 @@ import { UI } from './lib/UI.js';
import { StepLoadModel } from './lib/processes/StepLoadModel.js';
import { StepLoadSkeleton } from './lib/processes/StepLoadSkeleton.js';
import { StepEditSkeleton } from './lib/processes/StepEditSkeleton.js';
-import { StepAnimationsListing } from './lib/processes/StepAnimationsListing.js';
+import { StepAnimationsListing } from './lib/processes/StepAnimationsListing.ts';
import { StepExportToFile } from './lib/processes/StepExportToFile.js';
import { StepWeightSkin } from './lib/processes/StepWeightSkin.js';