Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Create a new bone mapping class and start showing the bone list on the UI

Scott Petrovic1dcefbf4bfe2
diff --git a/src/retarget/StepBoneMapping.ts b/src/retarget/StepBoneMapping.ts
new file mode 100644
index 0000000..f1e26f0
--- /dev/null
+++ b/src/retarget/StepBoneMapping.ts
@@ -0,0 +1,161 @@
+import { type Group, type Object3D, type Object3DEventMap, type Scene, type SkinnedMesh } from 'three'
+import { SkeletonType } from '../lib/enums/SkeletonType.ts'
+
+export class StepBoneMapping extends EventTarget {
+ private readonly _main_scene: Scene
+ private source_skeleton_data: Group<Object3DEventMap> | null = null
+ private target_armature: Object3D | null = null
+ private target_skeleton_type: SkeletonType = SkeletonType.None
+
+ // DOM references
+ private bone_mapping_container: HTMLDivElement | null = null
+ private source_bones_list: HTMLDivElement | null = null
+ private target_bones_list: HTMLDivElement | null = null
+
+ constructor (main_scene: Scene) {
+ super()
+ this._main_scene = main_scene // might not need this, but keeping for now
+ }
+
+ public begin (): void {
+ // Get DOM references
+ this.bone_mapping_container = document.getElementById('bone-mapping-container') as HTMLDivElement
+ this.source_bones_list = document.getElementById('source-bones-list') as HTMLDivElement
+ this.target_bones_list = document.getElementById('target-bones-list') as HTMLDivElement
+
+ // Show the bone mapping UI
+ this.show_bone_mapping_ui()
+
+ // Populate the lists
+ this.update_bone_lists()
+ }
+
+ public set_source_skeleton_data (skeleton_data: Group<Object3DEventMap>): void {
+ this.source_skeleton_data = skeleton_data
+ console.log('Source skeleton data set in bone mapping:', this.source_skeleton_data)
+ }
+
+ public set_target_skeleton_data (armature: Object3D, skeleton_type: SkeletonType): void {
+ this.target_armature = armature
+ this.target_skeleton_type = skeleton_type
+ console.log('Target skeleton data set in bone mapping:', this.target_armature, 'Type:', this.target_skeleton_type)
+ }
+
+ public has_source_skeleton (): boolean {
+ return this.source_skeleton_data !== null
+ }
+
+ public has_target_skeleton (): boolean {
+ return this.target_armature !== null
+ }
+
+ public has_both_skeletons (): boolean {
+ return this.has_source_skeleton() && this.has_target_skeleton()
+ }
+
+ // Getters
+ public get_source_skeleton_data (): Group<Object3DEventMap> | null {
+ return this.source_skeleton_data
+ }
+
+ public get_target_armature (): Object3D | null {
+ return this.target_armature
+ }
+
+ public get_target_skeleton_type (): SkeletonType {
+ return this.target_skeleton_type
+ }
+
+ // Extract bone names from source skeleton
+ public get_source_bone_names (): string[] {
+ if (this.source_skeleton_data === null) {
+ return []
+ }
+
+ const bone_names: string[] = []
+ // Source skeleton data contains SkinnedMesh objects with skeleton property
+ this.source_skeleton_data.traverse((child) => {
+ if (child.type === 'SkinnedMesh') {
+ const skinned_mesh = child as SkinnedMesh
+ const skeleton = skinned_mesh.skeleton
+ skeleton.bones.forEach((bone) => {
+ bone_names.push(bone.name)
+ })
+ }
+ })
+
+ return bone_names
+ }
+
+ // Extract bone names from target skeleton
+ public get_target_bone_names (): string[] {
+ if (this.target_armature === null) {
+ return []
+ }
+
+ const bone_names: string[] = []
+ this.target_armature.traverse((child) => {
+ if (child.type === 'Bone') {
+ bone_names.push(child.name)
+ }
+ })
+
+ return bone_names
+ }
+
+ // Update UI with current bone lists
+ public update_bone_lists (): void {
+ this.update_source_bones_list()
+ this.update_target_bones_list()
+ }
+
+ private update_source_bones_list (): void {
+ if (this.source_bones_list === null) return
+
+ const bone_names = this.get_source_bone_names()
+ if (bone_names.length === 0) {
+ this.source_bones_list.innerHTML = '<p style="color: #888;">No source skeleton loaded</p>'
+ return
+ }
+
+ this.source_bones_list.innerHTML = ''
+ bone_names.forEach((name) => {
+ const bone_item = document.createElement('div')
+ bone_item.textContent = name
+ bone_item.style.padding = '0.5rem'
+ bone_item.style.borderBottom = '1px solid #eee'
+ this.source_bones_list?.appendChild(bone_item)
+ })
+ }
+
+ private update_target_bones_list (): void {
+ if (this.target_bones_list === null) return
+
+ const bone_names = this.get_target_bone_names()
+ if (bone_names.length === 0) {
+ this.target_bones_list.innerHTML = '<p style="color: #888;">No target skeleton loaded</p>'
+ return
+ }
+
+ this.target_bones_list.innerHTML = ''
+ bone_names.forEach((name) => {
+ const bone_item = document.createElement('div')
+ bone_item.textContent = name
+ bone_item.style.padding = '0.5rem'
+ bone_item.style.borderBottom = '1px solid #eee'
+ this.target_bones_list?.appendChild(bone_item)
+ })
+ }
+
+ private show_bone_mapping_ui (): void {
+ if (this.bone_mapping_container !== null) {
+ this.bone_mapping_container.style.display = 'block'
+ }
+ }
+
+ private hide_bone_mapping_ui (): void {
+ if (this.bone_mapping_container !== null) {
+ this.bone_mapping_container.style.display = 'none'
+ }
+ }
+}
diff --git a/src/retarget/index.html b/src/retarget/index.html
index 91ee54e..88b75b0 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -42,7 +42,7 @@
<!-- there are some issues going on with scaling the skeleton for FBX and other formats
so we will start simple for now with just GLB files -->
<input type="file" id="upload-file" accept=".glb" style="display: none;" />
- <label for="upload-file" class="button" id="load-model-button">Choose 3D Model File</label>
+ <label for="upload-file" class="button" id="load-model-button">Choose Skinned 3d Model File</label>
<div id="skeleton-selection-container" >
<label for="skeleton-type-select">Choose Target Skeleton:</label>
@@ -56,6 +56,25 @@
</div>
<button class="button" id="bone-map-button" >Bone Map</button>
+
+ <!-- Bone Mapping UI -->
+ <div id="bone-mapping-container" style="display: none; margin-top: 2rem;">
+ <h3>Bone Mapping</h3>
+ <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem;">
+ <div>
+ <h4>Source Skeleton Bones</h4>
+ <div id="source-bones-list" style="max-height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 1rem;">
+ <!-- Source bones will be populated here -->
+ </div>
+ </div>
+ <div>
+ <h4>Target Skeleton Bones</h4>
+ <div id="target-bones-list" style="max-height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 1rem;">
+ <!-- Target bones will be populated here -->
+ </div>
+ </div>
+ </div>
+ </div>
</div>
<!-- 3d view controls shown on bottom right. This helps with click events -->
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index a1789f8..e380efb 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -2,6 +2,7 @@ import { Mesh2MotionEngine } from '../Mesh2MotionEngine.ts'
import { type Group, type Object3DEventMap, type SkinnedMesh, Vector3 } from 'three'
import { ModalDialog } from '../lib/ModalDialog.ts'
import { StepLoadTargetSkeleton } from './StepLoadTargetSkeleton.ts'
+import { StepBoneMapping } from './StepBoneMapping.ts'
import { RetargetUtils } from './RetargetUtils.ts'
class RetargetModule {
@@ -10,6 +11,7 @@ class RetargetModule {
private load_model_button: HTMLLabelElement | null = null
private bone_map_button: HTMLButtonElement | null = null
private readonly step_load_target_skeleton: StepLoadTargetSkeleton
+ private readonly step_bone_mapping: StepBoneMapping
constructor () {
// Set up camera position similar to marketing bootstrap
@@ -19,6 +21,9 @@ class RetargetModule {
// Initialize skeleton loading step
this.step_load_target_skeleton = new StepLoadTargetSkeleton(this.mesh2motion_engine.get_scene())
+
+ // Initialize bone mapping step
+ this.step_bone_mapping = new StepBoneMapping(this.mesh2motion_engine.get_scene())
}
public init (): void {
@@ -38,6 +43,13 @@ class RetargetModule {
this.handleFileSelect(event)
})
+ // Listen for target skeleton loaded
+ this.step_load_target_skeleton.addEventListener('skeleton-loaded', () => {
+ const target_armature = this.step_load_target_skeleton.get_loaded_target_armature()
+ const skeleton_type = this.step_load_target_skeleton.get_skeleton_type()
+ this.step_bone_mapping.set_target_skeleton_data(target_armature, skeleton_type)
+ })
+
// Bone map button click listener
this.bone_map_button?.addEventListener('click', () => {
this.handle_bone_map_requested()
@@ -88,8 +100,8 @@ class RetargetModule {
// Add skeleton helper
this.add_skeleton_helper(retargetable_meshes)
- // Transition to skeleton loading step
- this.transition_to_skeleton_loading()
+ // Set source skeleton data in bone mapping
+ this.step_bone_mapping.set_source_skeleton_data(retargetable_meshes)
}
}, { once: true })
} catch (error) {
@@ -108,22 +120,14 @@ class RetargetModule {
}
})
}
-
- private transition_to_skeleton_loading (): void {
- // Hide the model loading button
- // if (this.load_model_button !== null) {
- // this.load_model_button.style.display = 'none'
- // }
- // TODO: don't need this since we are doing both model and skeleton loading in same step
-
- }
-
private handle_bone_map_requested (): void {
console.log('Bone map requested - proceeding to bone mapping step')
console.log('Target skeleton type:', this.step_load_target_skeleton.get_skeleton_type())
console.log('Target armature:', this.step_load_target_skeleton.get_loaded_target_armature())
- // TODO: Implement bone mapping step
+
+ // Begin the bone mapping step
+ this.step_bone_mapping.begin()
}
}