Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

implement better naming for theme and animation with each preview. Generate light and dark previews

Scott Petrovicfa49355b55cc
diff --git a/src/lib/AnimationSearch.ts b/src/lib/AnimationSearch.ts
index b6223bd..a07a9d8 100644
--- a/src/lib/AnimationSearch.ts
+++ b/src/lib/AnimationSearch.ts
@@ -1,4 +1,6 @@
import { type AnimationClip } from 'three'
+import { ThemeManager } from './ThemeManager'
+import { SkeletonType } from './enums/SkeletonType'
export interface AnimationWithState extends AnimationClip {
isChecked?: boolean
@@ -10,9 +12,14 @@ export class AnimationSearch {
private readonly filter_input: HTMLInputElement | null = null
private readonly animation_list_container: HTMLElement | null = null
- constructor (filter_input_id: string, animation_list_container_id: string) {
+ private readonly theme_manager: ThemeManager
+ private readonly skeleton_type: SkeletonType
+
+ constructor (filter_input_id: string, animation_list_container_id: string, theme_manager: ThemeManager, skeleton_type: SkeletonType) {
this.filter_input = document.querySelector(`#${filter_input_id}`)
this.animation_list_container = document.querySelector(`#${animation_list_container_id}`)
+ this.theme_manager = theme_manager
+ this.skeleton_type = skeleton_type
this.setup_event_listeners()
}
@@ -24,13 +31,21 @@ export class AnimationSearch {
animation_with_state.isChecked = false
return animation_with_state
})
-
+
this.render_filtered_animations('')
}
private setup_event_listeners (): void {
this.setup_filter_listener()
this.setup_checkbox_listeners()
+ this.setup_theme_change_listener()
+ }
+
+ private setup_theme_change_listener (): void {
+ // rebuild animation previews so we have the correct theme
+ this.theme_manager.addEventListener('theme-changed', (new_theme) => {
+ this.render_filtered_animations(this.filter_input?.value || '')
+ })
}
private setup_filter_listener (): void {
@@ -100,11 +115,29 @@ export class AnimationSearch {
const was_checked: boolean = animation_clip.isChecked ?? false
const checked_attribute = was_checked ? 'checked' : ''
+ // build out where the video previews will be stored
+ // each skeleton type has its own folder
+ let preview_folder: string = ''
+ switch (this.skeleton_type) {
+ case SkeletonType.Human:
+ preview_folder = 'human'
+ break
+ case SkeletonType.Quadraped:
+ preview_folder = 'four-legged'
+ break
+ case SkeletonType.Bird:
+ preview_folder = 'bird'
+ break
+ }
+
+ const anim_name: string = animation_clip.name
+ const theme_name: string = this.theme_manager.get_current_theme()
+
// Use a placeholder for the video preview, to be replaced by IntersectionObserver
this.animation_list_container.innerHTML +=
`<div class="anim-item">
<button class="secondary-button play" data-index="${original_index}" style="display: flex; flex-direction:column">
- <div class="anim-preview-placeholder" data-src="../animpreviews/sample.webm" style="pointer-events: none;"></div>
+ <div class="anim-preview-placeholder" data-src="../animpreviews/${preview_folder}/${theme_name}_${anim_name}.webm" style="pointer-events: none;"></div>
<span class="anim-preview-label">${this.animation_name_clean(animation_clip.name)}</span>
</button>
<div class="styled-checkbox">
@@ -148,7 +181,7 @@ export class AnimationSearch {
video.className = 'anim-preview'
const src = placeholder.getAttribute('data-src') ?? ''
video.src = src
- video.width = 80
+ video.width = 140
video.height = 80
video.loop = true
video.muted = true
diff --git a/src/lib/UI.ts b/src/lib/UI.ts
index 81e2c9f..5f84c06 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -1,5 +1,7 @@
import { type AnimationClip } from 'three'
import { AnimationSearch, type AnimationWithState } from './AnimationSearch'
+import { ThemeManager } from './ThemeManager'
+import { SkeletonType } from './enums/SkeletonType'
export class UI {
dom_current_step_index: HTMLElement | null = null
@@ -171,10 +173,10 @@ export class UI {
}
}
- public build_animation_clip_ui (animation_clips_to_load: AnimationClip[]): void {
+ public build_animation_clip_ui (animation_clips_to_load: AnimationClip[], theme_manager: ThemeManager, skeleton_type: SkeletonType): void {
// Initialize AnimationSearch if not already done
if (this.animation_search === null) {
- this.animation_search = new AnimationSearch('animation-filter', 'animations-items')
+ this.animation_search = new AnimationSearch('animation-filter', 'animations-items', theme_manager, skeleton_type)
}
// Use the animation search class to handle the UI
diff --git a/src/lib/processes/StepAnimationsListing.ts b/src/lib/processes/StepAnimationsListing.ts
index 09aa7dc..c1a4cd6 100644
--- a/src/lib/processes/StepAnimationsListing.ts
+++ b/src/lib/processes/StepAnimationsListing.ts
@@ -9,9 +9,11 @@ import { AnimationClip, AnimationMixer, Quaternion, Vector3, type SkinnedMesh, t
import { SkeletonType } from '../enums/SkeletonType.ts'
import { Utility } from '../Utilities.ts'
+import { ThemeManager } from '../ThemeManager.ts'
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepAnimationsListing extends EventTarget {
+ private readonly theme_manager: ThemeManager
private readonly ui: UI
private readonly animation_player: AnimationPlayer
private animation_clips_loaded: AnimationClip[] = []
@@ -21,7 +23,7 @@ export class StepAnimationsListing extends EventTarget {
private animation_mixer: AnimationMixer = new AnimationMixer()
private skinned_meshes_to_animate: SkinnedMesh[] = []
private current_playing_index: number = 0
- private skeleton_type: string = SkeletonType.Human
+ private skeleton_type: SkeletonType = SkeletonType.Human
private _added_event_listeners: boolean = false
@@ -62,10 +64,11 @@ export class StepAnimationsListing extends EventTarget {
private has_added_event_listeners: boolean = false
- constructor () {
+ constructor (theme_manager: ThemeManager) {
super()
this.ui = new UI()
this.animation_player = new AnimationPlayer()
+ this.theme_manager = theme_manager
}
public begin (): void {
@@ -186,7 +189,7 @@ export class StepAnimationsListing extends EventTarget {
console.log('all animations loaded:', this.animation_clips_loaded)
// create user interface with all available animation clips
- this.ui.build_animation_clip_ui(this.animation_clips_loaded)
+ this.ui.build_animation_clip_ui(this.animation_clips_loaded, this.theme_manager, this.skeleton_type)
this.play_animation(0) // play the first animation by default
}
diff --git a/src/preview-generator/preview-generator.ts b/src/preview-generator/preview-generator.ts
index 69da6c9..650b33e 100644
--- a/src/preview-generator/preview-generator.ts
+++ b/src/preview-generator/preview-generator.ts
@@ -121,8 +121,22 @@ class PreviewGenerator {
console.warn('Animation or animations not loaded yet.')
return
}
+ // First pass: current theme
this.current_animation_index_processing = 0 // reset counter if we do it again
await this.process_animation_clip()
+
+ // Switch theme and do a second pass
+ this.theme_manager.toggle_theme()
+ this.regenerate_floor_grid() // update grid/floor colors for new theme
+ // Optionally, you may want to force a re-render or update other theme-dependent visuals here
+
+ this.current_animation_index_processing = 0
+ await this.process_animation_clip()
+
+ // zip up all the video results and download everything
+ await this.generate_zip()
+
+ console.log('finishe successfully')
}
private async process_animation_clip (): Promise<void> {
@@ -142,8 +156,6 @@ class PreviewGenerator {
// Stop recording after animation ends
const file = await this.recorder.stop()
- console.log('file name saved ', file.name)
-
// Start recording before playing the animation
this.zip.file(file.name, file)
resolve()
@@ -162,13 +174,10 @@ class PreviewGenerator {
this.current_animation_index_processing += 1
// for testing a few, change to something small like 5
- const temp_limit = 5 // this.animation_clips.length
+ const temp_limit = this.animation_clips.length
if (this.current_animation_index_processing < temp_limit) {
await this.process_animation_clip()
- } else {
- console.log('finished processing')
- await this.generate_zip()
}
}
diff --git a/src/preview-generator/webm-recorder.ts b/src/preview-generator/webm-recorder.ts
index 4c7a7fa..107f0c0 100644
--- a/src/preview-generator/webm-recorder.ts
+++ b/src/preview-generator/webm-recorder.ts
@@ -1,10 +1,10 @@
import * as THREE from 'three'
export class WebMRecorder {
- public width: number = 120
+ public width: number = 140
public height: number = 80
public fps: number = 24
- public kbps: number = 1000
+ public kbps: number = 1200
public readonly recorded_files: File[] = []
private readonly renderer_ref: THREE.WebGLRenderer
diff --git a/src/script.ts b/src/script.ts
index 7255524..cf69595 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -37,12 +37,13 @@ export class Bootstrap {
public readonly transform_controls_hover_distance: number = 0.03 // distance to hover over bones to select them
// has UI elements on the HTML page that we will reference/use
+ public readonly theme_manager = new ThemeManager()
public readonly ui = new UI()
public readonly load_model_step = new StepLoadModel()
public readonly load_skeleton_step = new StepLoadSkeleton()
public readonly edit_skeleton_step = new StepEditSkeleton()
public readonly weight_skin_step = new StepWeightSkin()
- public readonly animations_listing_step = new StepAnimationsListing()
+ public readonly animations_listing_step = new StepAnimationsListing(this.theme_manager)
public readonly file_export_step = new StepExportToFile()
public readonly scene: Scene = new Scene()
@@ -59,7 +60,7 @@ export class Bootstrap {
private environment_container: Group = new Group()
private readonly eventListeners: EventListeners
- public readonly theme_manager = new ThemeManager()
+
public initialize (): void {
this.setup_environment()