Import a 3D Model and automatically assign and export animations
TypeScript
First pass at getting animation player working
Scott Petrovic3b4b1cabe9c3
diff --git a/src/animation-player.css b/src/animation-player.cssnew file mode 100644index 0000000..6dc7651--- /dev/null+++ b/src/animation-player.css@@ -0,0 +1,94 @@+#animation-player {+ background-color: var(--bg-alternate);+ border-radius: 8px;+ padding: 1rem;+ margin: 1rem 0;+ border: 1px solid var(--bg-tertiary);+}++#animation-player-header {+ display: flex;+ justify-content: space-between;+ align-items: center;+ margin-bottom: 0.75rem;+}++#current-animation-name {+ font-weight: bold;+ color: var(--text-primary);+ flex: 1;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+}++#animation-player-controls {+ display: flex;+ align-items: center;+ gap: 0.5rem;+}++#play-pause-button {+ padding: 0.5rem;+ width: 40px;+ height: 40px;+ display: flex;+ align-items: center;+ justify-content: center;+ border-radius: 50%;+ margin: 0;+}++#play-pause-button:disabled {+ opacity: 0.5;+ cursor: not-allowed;+}++#animation-timeline-container {+ width: 100%;+}++#animation-timeline {+ width: 100%;+}++#animation-scrubber {+ width: 100%;+ height: 6px;+ border-radius: 3px;+ background: var(--bg-primary);+ outline: none;+ margin-bottom: 0.5rem;+ cursor: pointer;+}++#animation-scrubber::-webkit-slider-thumb {+ appearance: none;+ width: 16px;+ height: 16px;+ border-radius: 50%;+ background: var(--button-primary-bg);+ cursor: pointer;+ border: 2px solid var(--text-primary);+}++#animation-scrubber::-moz-range-thumb {+ width: 16px;+ height: 16px;+ border-radius: 50%;+ background: var(--button-primary-bg);+ cursor: pointer;+ border: 2px solid var(--text-primary);+}++#timeline-labels {+ display: flex;+ justify-content: space-between;+ font-size: 12px;+ color: var(--text-alternate);+}++#animation-scrubber:disabled {+ opacity: 0.5;+ cursor: not-allowed;+}\ No newline at end of filediff --git a/src/index.html b/src/index.htmlindex 2cdef35..dbf3fa5 100644--- a/src/index.html+++ b/src/index.html@@ -11,6 +11,7 @@<script type="module" src="./script.ts"></script><link href="./styles.css" rel="stylesheet" crossorigin="anonymous" /><link rel="stylesheet" href="./radio-button-group.css" crossorigin="anonymous" />+ <link rel="stylesheet" href="./animation-player.css" crossorigin="anonymous" /><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />@@ -276,6 +277,26 @@<!-- Filter textbox for animations --><input type="text" id="animation-filter" placeholder="Filter animations...">+ <!-- Mini Animation Player -->+ <div id="animation-player">+ <div id="animation-player-header">+ <span id="current-animation-name">No animation selected</span>+ <div id="animation-player-controls">+ <button id="play-pause-button" class="secondary-button" disabled>+ <span class="material-symbols-outlined">play_arrow</span>+ </button>+ </div>+ </div>+ <div id="animation-timeline-container">+ <div id="animation-timeline">+ <input type="range" id="animation-scrubber" min="0" max="100" value="0" disabled>+ <div id="timeline-labels">+ <span id="current-time">0</span>+ <span id="total-time">0</span>+ </div>+ </div>+ </div>+ </div><div id="animations-listing-header"><span>Animation name</span>diff --git a/src/lib/AnimationPlayer.ts b/src/lib/AnimationPlayer.tsnew file mode 100644index 0000000..d37c4d3--- /dev/null+++ b/src/lib/AnimationPlayer.ts@@ -0,0 +1,221 @@+import { type AnimationClip, type AnimationAction } from 'three'+import { UI } from './UI'++export class AnimationPlayer {+ private readonly ui: UI+ private current_animation_clip: AnimationClip | null = null+ private current_animation_actions: AnimationAction[] = []+ private is_playing: boolean = false+ private is_user_scrubbing: boolean = false++ private readonly has_added_event_listeners: boolean = false++ constructor () {+ this.ui = new UI()++ if (!this.has_added_event_listeners) {+ this.setup_event_listeners()+ this.has_added_event_listeners = true+ }+ }++ private setup_event_listeners (): void {+ // Play/Pause button+ if (this.ui.dom_play_pause_button !== null) {+ this.ui.dom_play_pause_button.addEventListener('click', () => {+ this.toggle_play_pause()+ })+ }++ // Animation scrubber+ if (this.ui.dom_animation_scrubber !== null) {+ this.ui.dom_animation_scrubber.addEventListener('input', (event) => {+ this.handle_scrubber_input(event)+ })++ this.ui.dom_animation_scrubber.addEventListener('mousedown', () => {+ this.is_user_scrubbing = true+ })++ this.ui.dom_animation_scrubber.addEventListener('mouseup', () => {+ this.is_user_scrubbing = false+ })+ }+ }++ public set_animation (animation_clip: AnimationClip, animation_actions: AnimationAction[]): void {+ this.current_animation_clip = animation_clip+ this.current_animation_actions = animation_actions++ // Calculate total frames (assuming 30 FPS)+ const fps = 30+ const total_frames = Math.floor(animation_clip.duration * fps)++ // Update UI+ if (this.ui.dom_current_animation_name !== null) {+ this.ui.dom_current_animation_name.textContent = animation_clip.name+ }++ if (this.ui.dom_total_time !== null) {+ this.ui.dom_total_time.textContent = `${total_frames}f`+ }++ // Enable controls+ if (this.ui.dom_play_pause_button !== null) {+ this.ui.dom_play_pause_button.disabled = false+ }++ if (this.ui.dom_animation_scrubber !== null) {+ this.ui.dom_animation_scrubber.disabled = false+ this.ui.dom_animation_scrubber.max = total_frames.toString()+ this.ui.dom_animation_scrubber.value = '0'+ }++ // Set initial playing state based on first animation action+ this.is_playing = animation_actions.length > 0 ? animation_actions[0].isRunning() : false+ this.update_play_pause_button()+ }++ public clear_animation (): void {+ this.current_animation_clip = null+ this.current_animation_actions = []+ this.is_playing = false++ // Update UI+ if (this.ui.dom_current_animation_name !== null) {+ this.ui.dom_current_animation_name.textContent = 'No animation selected'+ }++ if (this.ui.dom_current_time !== null) {+ this.ui.dom_current_time.textContent = '0'+ }++ if (this.ui.dom_total_time !== null) {+ this.ui.dom_total_time.textContent = '0'+ }++ // Disable controls+ if (this.ui.dom_play_pause_button !== null) {+ this.ui.dom_play_pause_button.disabled = true+ }++ if (this.ui.dom_animation_scrubber !== null) {+ this.ui.dom_animation_scrubber.disabled = true+ this.ui.dom_animation_scrubber.value = '0'+ }++ this.update_play_pause_button()+ }++ public toggle_play_pause (): void {+ if (this.current_animation_actions.length === 0) {+ return+ }++ if (this.is_playing) {+ this.pause()+ } else {+ this.play()+ }+ }++ public play (): void {+ if (this.current_animation_actions.length === 0) {+ return+ }++ // Apply play/unpause to all animation actions+ this.current_animation_actions.forEach((action) => {+ if (action.paused === true) {+ action.paused = false+ } else {+ action.play()+ }+ })++ this.is_playing = true+ this.update_play_pause_button()+ }++ public pause (): void {+ if (this.current_animation_actions.length === 0) {+ return+ }++ // Apply pause to all animation actions+ this.current_animation_actions.forEach((action) => {+ action.paused = true+ })++ this.is_playing = false+ this.update_play_pause_button()+ }++ private update_play_pause_button (): void {+ if (this.ui.dom_play_pause_button === null) {+ return+ }++ const icon = this.ui.dom_play_pause_button.querySelector('.material-symbols-outlined')+ if (icon !== null) {+ icon.textContent = this.is_playing ? 'pause' : 'play_arrow'+ }+ }++ private handle_scrubber_input (event: Event): void {+ if (this.current_animation_actions.length === 0 || this.current_animation_clip === null) {+ return+ }++ const target = event.target as HTMLInputElement+ const frame_number = parseInt(target.value)++ // Convert frame to time (assuming 30 FPS)+ const fps = 30+ const scrub_time = frame_number / fps++ // Set the animation time for all actions+ this.current_animation_actions.forEach((action) => {+ action.time = scrub_time+ })++ // Update current frame display+ if (this.ui.dom_current_time !== null) {+ this.ui.dom_current_time.textContent = `${frame_number}f`+ }+ }++ public update (_delta_time: number): void {+ if (this.current_animation_actions.length === 0 || this.current_animation_clip === null || this.is_user_scrubbing) {+ return+ }++ // Use the first action for time tracking (they should all be synchronized)+ const first_action = this.current_animation_actions[0]++ // Convert current time to frame number (assuming 30 FPS)+ const fps = 30+ const current_frame = Math.floor(first_action.time * fps)++ // Update scrubber position based on frame number+ if (this.ui.dom_animation_scrubber !== null) {+ this.ui.dom_animation_scrubber.value = current_frame.toString()+ }++ // Update current frame display+ if (this.ui.dom_current_time !== null) {+ this.ui.dom_current_time.textContent = `${current_frame}f`+ }++ // Check if animation has finished and loop it+ if (first_action.time >= this.current_animation_clip.duration) {+ this.current_animation_actions.forEach((action) => {+ action.time = 0+ })+ }+ }++ public get_is_playing (): boolean {+ return this.is_playing+ }+}diff --git a/src/lib/AnimationSearch.ts b/src/lib/AnimationSearch.tsindex dc3f41b..b43e08a 100644--- a/src/lib/AnimationSearch.ts+++ b/src/lib/AnimationSearch.ts@@ -102,8 +102,7 @@ export class AnimationSearch {this.animation_list_container.innerHTML +=`<div class="anim-item">- <button class="secondary-button play" data-index="${original_index}">- ►+ <button class="secondary-button play" data-index="${original_index}">${animation_clip.name}</button>diff --git a/src/lib/UI.ts b/src/lib/UI.tsindex dbdd07a..b4cad22 100644--- a/src/lib/UI.ts+++ b/src/lib/UI.ts@@ -51,6 +51,14 @@ export class UI {dom_animation_clip_list: HTMLElement | null = nulldom_export_button: HTMLButtonElement | null = null+ // Animation player controls+ dom_animation_player: HTMLElement | null = null+ dom_current_animation_name: HTMLElement | null = null+ dom_play_pause_button: HTMLButtonElement | null = null+ dom_animation_scrubber: HTMLInputElement | null = null+ dom_current_time: HTMLElement | null = null+ dom_total_time: HTMLElement | null = null+dom_info_container: HTMLElement | null = nulldom_info_panel: HTMLElement | null = null@@ -130,6 +138,14 @@ export class UI {this.dom_animation_clip_list = document.querySelector('#animations-items')this.dom_export_button = document.querySelector('#export-button')+ // Animation player controls+ this.dom_animation_player = document.querySelector('#animation-player')+ this.dom_current_animation_name = document.querySelector('#current-animation-name')+ this.dom_play_pause_button = document.querySelector('#play-pause-button')+ this.dom_animation_scrubber = document.querySelector('#animation-scrubber')+ this.dom_current_time = document.querySelector('#current-time')+ this.dom_total_time = document.querySelector('#total-time')+this.dom_info_container = document.querySelector('#info-panel')this.dom_info_panel = document.querySelector('#info-messaging')diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.tsindex c2f2b6b..fa92f2d 100644--- a/src/lib/processes/StepAnimationsListing.ts+++ b/src/lib/processes/StepAnimationsListing.ts@@ -1,8 +1,8 @@import { UI } from '../UI.ts'+import { AnimationPlayer } from '../AnimationPlayer.ts'import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js'-import { BVHLoader } from 'three/examples/jsm/loaders/BVHLoader.js'import { AnimationClip, AnimationMixer, Quaternion, Vector3, type SkinnedMesh, type QuaternionKeyframeTrack,type KeyframeTrack, type AnimationAction, type Object3D, Bone } from 'three'@@ -12,9 +12,10 @@ import { SkeletonType } from '../enums/SkeletonType.ts'// Note: EventTarget is a built-ininterface and do not need to import itexport class StepAnimationsListing extends EventTarget {private readonly ui: UI+ private readonly animation_player: AnimationPlayerprivate animation_clips_loaded: AnimationClip[] = []private gltf_animation_loader: GLTFLoader = new GLTFLoader()- private fbx_animation_loader: FBXLoader = new FBXLoader()+ private readonly fbx_animation_loader: FBXLoader = new FBXLoader()private animation_mixer: AnimationMixer = new AnimationMixer()private skinned_meshes_to_animate: SkinnedMesh[] = []@@ -63,6 +64,7 @@ export class StepAnimationsListing extends EventTarget {constructor () {super()this.ui = new UI()+ this.animation_player = new AnimationPlayer()}public begin (): void {@@ -102,6 +104,7 @@ export class StepAnimationsListing extends EventTarget {this.current_playing_index = 0this.hip_bone_offset = new Vector3(0, 0, 0)this.hip_bone_scale_factor_z = 1.0+ this.animation_player.clear_animation()}public mixer (): AnimationMixer {@@ -112,35 +115,7 @@ export class StepAnimationsListing extends EventTarget {// on this steppublic frame_change (delta_time: number): void {this.mixer().update(delta_time)- this.update_active_animation_css_progress(delta_time)- }-- // show a progress bar for teh current animation clip in the form of a gradient background- // on the button that is playing the animation- private update_active_animation_css_progress (delta_time: number): void {- if (this.animation_mixer === null || this.animation_clips_loaded.length === 0) { return }-- this.skinned_meshes_to_animate.forEach((skinned_mesh) => {- const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index]-- const anim_action: AnimationAction = this.animation_mixer.clipAction(clip_to_play, skinned_mesh)-- if (anim_action === null) {- console.info('No anim_action found for clipAction. Aborting. Clip name trying to play: :', clip_to_play.name, this.animation_mixer)- return- }-- const progress = Math.min(anim_action.time / clip_to_play.duration, 1)- const progress_percent: number = progress * 100 // returns 0-100-- // background gradient to simulate a progress bar for the animation- if (this.ui.dom_animation_clip_list === null) { return }-- const animation_button: HTMLButtonElement | null = this.ui.dom_animation_clip_list.querySelector(`button[data-index="${this.current_playing_index}"]`)- if (animation_button !== null) {- animation_button.style.background = `linear-gradient(to right,var(--bg-primary) ${progress_percent}%, var(--bg-alternate) ${progress_percent}%)`- }- })+ this.animation_player.update(delta_time)}public animation_clips (): AnimationClip[] {@@ -315,21 +290,24 @@ export class StepAnimationsListing extends EventTarget {// otherwise modifications like arm extension will not updatethis.animation_mixer = new AnimationMixer()+ const all_animation_actions: AnimationAction[] = []+this.skinned_meshes_to_animate.forEach((skinned_mesh) => {const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index]const anim_action: AnimationAction = this.animation_mixer.clipAction(clip_to_play, skinned_mesh)anim_action.stop()anim_action.play()- })- // reset all the animation button background styles. This will remove the- // progress bar effect from whatever was animating before- if (this.ui.dom_animation_clip_list === null) { return }- const all_buttons = this.ui.dom_animation_clip_list.querySelectorAll('button[data-index]');- all_buttons.forEach((btn: HTMLButtonElement) => {- btn.style.background = '' // or set to your default, e.g. ''+ // Collect all animation actions for the animation player+ all_animation_actions.push(anim_action)})++ // Update the animation player with the current animation and all actions+ if (all_animation_actions.length > 0) {+ const clip_to_play: AnimationClip = this.animation_clips_loaded[this.current_playing_index]+ this.animation_player.set_animation(clip_to_play, all_animation_actions)+ }}private update_download_button_enabled (): void {diff --git a/src/styles.css b/src/styles.cssindex 80179bb..cc5e1d6 100644--- a/src/styles.css+++ b/src/styles.css@@ -72,7 +72,6 @@ h1, h2, h3, h4, h5, h6, input, button, select {border-radius: 4px;}-hr {border:1px solid var(--bg-alternate);width: 100%;