Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Start adding origin marker manager and points when configuring model and skeleton

Scott Petrovic689dfdabf499
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index c51f0f4..462fee6 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -3,7 +3,6 @@ import { ModelPreviewDisplay } from './enums/ModelPreviewDisplay'
import { ProcessStep } from './enums/ProcessStep'
import { Utility } from './Utilities'
-
export class EventListeners {
constructor (private readonly bootstrap: Bootstrap) {}
@@ -13,6 +12,20 @@ export class EventListeners {
this.bootstrap.regenerate_floor_grid()
})
+ // Listen for skeleton transformation events to update UI and visuals
+ this.bootstrap.edit_skeleton_step.addEventListener('skeletonTransformed', () => {
+ // Update skeleton helper if it exists
+ if (this.bootstrap.skeleton_helper !== undefined) {
+ this.bootstrap.regenerate_skeleton_helper(this.bootstrap.edit_skeleton_step.skeleton(), 'Skeleton Helper')
+ }
+
+ // Refresh weight painting if in weight painted mode
+ if (this.bootstrap.mesh_preview_display_type === ModelPreviewDisplay.WeightPainted) {
+ this.bootstrap.regenerate_weight_painted_preview_mesh()
+ }
+ })
+
+
// listen for view helper changes
document.getElementById('view-control-hitbox')?.addEventListener('pointerdown', (event: PointerEvent) => {
if (this.bootstrap.view_helper.handleClick(event)) {
@@ -66,11 +79,6 @@ export class EventListeners {
this.bootstrap.process_step = this.bootstrap.process_step_changed(ProcessStep.LoadSkeleton)
})
- this.bootstrap.load_skeleton_step.addEventListener('skeletonLoaded', () => {
- this.bootstrap.edit_skeleton_step.load_original_armature_from_model(this.bootstrap.load_skeleton_step.armature())
- this.bootstrap.process_step = this.bootstrap.process_step_changed(ProcessStep.EditSkeleton)
- })
-
this.bootstrap.ui.dom_bind_pose_button?.addEventListener('click', () => {
const passed_bone_skinning_test = this.bootstrap.test_bone_weighting_success()
if (passed_bone_skinning_test) {
@@ -84,11 +92,11 @@ export class EventListeners {
})
this.bootstrap.ui.dom_rotate_model_y_button?.addEventListener('click', () => {
- this.bootstrap.load_model_step.rotate_model_geometry('y', 90)
+ this.bootstrap.load_model_step.rotate_model_geometry('y', 90)
})
this.bootstrap.ui.dom_rotate_model_z_button?.addEventListener('click', () => {
- this.bootstrap.load_model_step.rotate_model_geometry('z', 90)
+ this.bootstrap.load_model_step.rotate_model_geometry('z', 90)
})
this.bootstrap.ui.dom_move_model_to_floor_button?.addEventListener('click', () => {
diff --git a/src/lib/processes/load-skeleton/OriginMarkerManager.ts b/src/lib/processes/load-skeleton/OriginMarkerManager.ts
new file mode 100644
index 0000000..323a8b9
--- /dev/null
+++ b/src/lib/processes/load-skeleton/OriginMarkerManager.ts
@@ -0,0 +1,39 @@
+import { type Object3D, Mesh, SphereGeometry, MeshBasicMaterial, type Scene } from 'three'
+
+export function add_origin_markers (root: Scene): void {
+ remove_origin_markers(root)
+
+ const meshes: Mesh[] = []
+ root.traverse((child: Object3D) => {
+ if ((child as Mesh).isMesh) {
+ meshes.push(child as Mesh)
+ }
+ })
+
+ for (const mesh of meshes) {
+ const marker = new Mesh(
+ new SphereGeometry(0.02, 8, 8),
+ new MeshBasicMaterial({ color: 0xff4c00 }) // orange
+ )
+ marker.name = 'origin_marker'
+ marker.position.set(0, 0, 0)
+ mesh.add(marker)
+ }
+}
+
+export function remove_origin_markers (root: Object3D): void {
+ const markers: Object3D[] = []
+ root.traverse((child: Object3D) => {
+ for (const c of child.children) {
+ if (c.name === 'origin_marker') {
+ markers.push(c)
+ }
+ }
+ })
+
+ for (const marker of markers) {
+ if (marker.parent) {
+ marker.parent.remove(marker)
+ }
+ }
+}
diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
index 1f745b0..6543854 100644
--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
@@ -1,16 +1,9 @@
import { UI } from '../../UI.ts'
-import { Object3D, type Object3DEventMap } from 'three'
+import { Object3D, type Scene, type Object3DEventMap } from 'three'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { SkeletonType, HandSkeletonType } from '../../enums/SkeletonType.js'
-
-// Interface for GLTF loader result
-interface GLTFResult {
- scene: Object3D
- scenes: Object3D[]
- animations: any[]
- cameras: Object3D[]
- asset: object
-}
+import type GLTFResult from './interfaces/GLTFResult.ts'
+import { add_origin_markers, remove_origin_markers } from './OriginMarkerManager'
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepLoadSkeleton extends EventTarget {
@@ -21,12 +14,18 @@ export class StepLoadSkeleton extends EventTarget {
private hand_skeleton_t: HandSkeletonType = HandSkeletonType.AllFingers
private _added_event_listeners: boolean = false
+ private readonly _main_scene: Scene
public skeleton_type (): SkeletonType {
return this.skeleton_t
}
- begin (): void {
+ constructor (main_scene: Scene) {
+ super()
+ this._main_scene = main_scene
+ }
+
+ public begin (): void {
if (this.ui.dom_current_step_index !== null) {
this.ui.dom_current_step_index.innerHTML = '2'
}
@@ -47,6 +46,17 @@ export class StepLoadSkeleton extends EventTarget {
// Initialize hand skeleton options visibility
this.toggle_hand_skeleton_options()
+
+ // add origin markers for debugging model loading issues
+ add_origin_markers(this._main_scene)
+ }
+
+ public regenerate_origin_markers (): void {
+ add_origin_markers(this._main_scene)
+ }
+
+ public dispose (): void {
+ remove_origin_markers(this._main_scene)
}
private add_event_listeners (): void {
diff --git a/src/lib/processes/load-skeleton/interfaces/GLTFResult.ts b/src/lib/processes/load-skeleton/interfaces/GLTFResult.ts
new file mode 100644
index 0000000..359a527
--- /dev/null
+++ b/src/lib/processes/load-skeleton/interfaces/GLTFResult.ts
@@ -0,0 +1,9 @@
+import { type Object3D } from "three"
+
+export default interface GLTFResult {
+ scene: Object3D
+ scenes: Object3D[]
+ animations: any[]
+ cameras: Object3D[]
+ asset: object
+}
diff --git a/src/script.ts b/src/script.ts
index 4c3f074..28f6e67 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -37,18 +37,18 @@ export class Bootstrap {
public is_transform_controls_dragging: boolean = false
public readonly transform_controls_hover_distance: number = 0.03 // distance to hover over bones to select them
- public view_helper: CustomViewHelper // mini 3d view to help orient orthographic views
+ public view_helper: CustomViewHelper | undefined // mini 3d view to help orient orthographic views
// has UI elements on the HTML page that we will reference/use
- public readonly theme_manager = new ThemeManager()
- public readonly ui = UI.getInstance()
- 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(this.theme_manager)
- public readonly file_export_step = new StepExportToFile()
- public readonly scene: Scene = new Scene()
+ public scene: Scene
+ public theme_manager: ThemeManager
+ public ui: UI
+ public load_model_step: StepLoadModel
+ public load_skeleton_step: StepLoadSkeleton
+ public edit_skeleton_step: StepEditSkeleton
+ public weight_skin_step: StepWeightSkin
+ public animations_listing_step: StepAnimationsListing
+ public file_export_step: StepExportToFile
// for looking at specific bones
public process_step: ProcessStep = ProcessStep.LoadModel
@@ -65,31 +65,28 @@ export class Bootstrap {
private readonly eventListeners: EventListeners
- public initialize (): void {
- this.setup_environment()
- this.eventListeners.addEventListeners()
- this.process_step = this.process_step_changed(ProcessStep.LoadModel)
- this.animate() // start the render loop which will continue rendering the scene
- this.inject_build_version()
- } // end initialize()
-
constructor () {
this.eventListeners = new EventListeners(this)
// helps resolve requestAnimationFrame calling animate() with wrong context
this.animate = this.animate.bind(this)
- // Listen for skeleton transformation events to update UI and visuals
- this.edit_skeleton_step.addEventListener('skeletonTransformed', () => {
- // Update skeleton helper if it exists
- if (this.skeleton_helper !== undefined) {
- this.regenerate_skeleton_helper(this.edit_skeleton_step.skeleton(), 'Skeleton Helper')
- }
+ this.scene = new Scene()
+ this.theme_manager = new ThemeManager()
+ this.ui = UI.getInstance()
- // Refresh weight painting if in weight painted mode
- if (this.mesh_preview_display_type === ModelPreviewDisplay.WeightPainted) {
- this.regenerate_weight_painted_preview_mesh()
- }
- })
+ // setting up steps
+ this.load_model_step = new StepLoadModel()
+ this.load_skeleton_step = new StepLoadSkeleton(this.scene)
+ this.edit_skeleton_step = new StepEditSkeleton()
+ this.weight_skin_step = new StepWeightSkin()
+ this.animations_listing_step = new StepAnimationsListing(this.theme_manager)
+ this.file_export_step = new StepExportToFile()
+
+ this.setup_environment()
+ this.eventListeners.addEventListeners()
+ this.process_step = this.process_step_changed(ProcessStep.LoadModel)
+ this.animate() // start the render loop which will continue rendering the scene
+ this.inject_build_version()
}
private inject_build_version (): void {
@@ -205,8 +202,9 @@ export class Bootstrap {
// we will have the current step turn on the UI elements it needs
this.ui.hide_all_elements()
- // clean up things related to edit step in case we are leaving it
+ // clean up things related to steps in since we can navigate back and forth
this.edit_skeleton_step.cleanup_on_exit_step()
+ this.load_skeleton_step.dispose()
// only show animation player on the animation listing page
if (this.ui.dom_animation_player !== null) {
@@ -219,7 +217,6 @@ export class Bootstrap {
switch (process_step) {
case ProcessStep.LoadModel:
-
// reset the state in the case of coming back to this step
if (this.load_model_step.model_meshes() !== undefined) {
const imported_model = this.scene.getObjectByName('Imported Model')
@@ -234,6 +231,13 @@ export class Bootstrap {
case ProcessStep.LoadSkeleton:
// Resetting state for the load skeleton step
+ this.load_skeleton_step.begin()
+
+ // add event listener. TODO: put this in the load skeleton process step
+ this.load_skeleton_step.addEventListener('skeletonLoaded', () => {
+ this.edit_skeleton_step.load_original_armature_from_model(this.load_skeleton_step.armature())
+ this.process_step = this.process_step_changed(ProcessStep.EditSkeleton)
+ })
// if skeleton helper existed because we are going back to this
if (this.skeleton_helper !== undefined) {
@@ -250,6 +254,8 @@ export class Bootstrap {
this.load_skeleton_step.begin()
break
case ProcessStep.EditSkeleton:
+ this.load_skeleton_step?.dispose()
+
this.regenerate_skeleton_helper(this.edit_skeleton_step.skeleton())
process_step = ProcessStep.EditSkeleton
this.edit_skeleton_step.begin()
@@ -406,8 +412,8 @@ export class Bootstrap {
}
public remove_skinned_meshes_from_scene (): void {
- const existing_skinned_meshes = this.scene.children.filter((child: Object3D) => child.name === 'Skinned Mesh')
- existing_skinned_meshes.forEach((existing_skinned_mesh: Object3D) => {
+ const existing_skinned_meshes = this.scene.children.filter((child: THREE.Object3D) => child.name === 'Skinned Mesh')
+ existing_skinned_meshes.forEach((existing_skinned_mesh: THREE.Object3D) => {
Utility.remove_object_with_children(existing_skinned_mesh)
})
}
@@ -508,4 +514,3 @@ export class Bootstrap {
// Create an instance of the Bootstrap class when the script is loaded
const app = new Bootstrap()
-app.initialize()