Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Remove skeleton gizmo and simplify model positioning

DarthMarinocfe30e3cd289
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index d376b15..360de81 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -43,7 +43,6 @@ export class Mesh2MotionEngine {
public is_transform_controls_dragging: boolean = false
public readonly transform_controls_hover_distance: number = 0.02 // distance to hover over bones to select them
public is_model_gizmo_active: boolean = false
- public is_skeleton_gizmo_active: boolean = false
public view_helper: CustomViewHelper | undefined // mini 3d view to help orient orthographic views
@@ -250,23 +249,7 @@ export class Mesh2MotionEngine {
}
public handle_transform_controls_moving (): void {
- if (this.is_model_gizmo_active) {
- const pos = this.load_model_step.model_meshes().position
- if (this.ui.dom_model_gizmo_x !== null) this.ui.dom_model_gizmo_x.value = pos.x.toFixed(3)
- if (this.ui.dom_model_gizmo_y !== null) this.ui.dom_model_gizmo_y.value = pos.y.toFixed(3)
- if (this.ui.dom_model_gizmo_z !== null) this.ui.dom_model_gizmo_z.value = pos.z.toFixed(3)
- return
- }
- if (this.is_skeleton_gizmo_active) {
- const attached = this.transform_controls.object
- if (attached !== undefined && attached !== null) {
- const pos = attached.position
- if (this.ui.dom_skeleton_gizmo_x !== null) this.ui.dom_skeleton_gizmo_x.value = pos.x.toFixed(3)
- if (this.ui.dom_skeleton_gizmo_y !== null) this.ui.dom_skeleton_gizmo_y.value = pos.y.toFixed(3)
- if (this.ui.dom_skeleton_gizmo_z !== null) this.ui.dom_skeleton_gizmo_z.value = pos.z.toFixed(3)
- }
- return
- }
+ if (this.is_model_gizmo_active) { return }
const selected_bone: Bone = this.transform_controls.object as Bone
@@ -287,64 +270,21 @@ export class Mesh2MotionEngine {
public enable_model_gizmo (): void {
this.is_model_gizmo_active = true
- const pos = this.load_model_step.model_meshes().position
- if (this.ui.dom_model_gizmo_x !== null) this.ui.dom_model_gizmo_x.value = pos.x.toFixed(3)
- if (this.ui.dom_model_gizmo_y !== null) this.ui.dom_model_gizmo_y.value = pos.y.toFixed(3)
- if (this.ui.dom_model_gizmo_z !== null) this.ui.dom_model_gizmo_z.value = pos.z.toFixed(3)
- if (this.ui.dom_model_gizmo_inputs !== null) this.ui.dom_model_gizmo_inputs.style.display = 'flex'
this.transform_controls.attach(this.load_model_step.model_meshes())
this.transform_controls.setMode('translate')
this.transform_controls.enabled = true
}
- public disable_model_gizmo (): void {
- this.is_model_gizmo_active = false
- this.load_model_step.model_meshes().position.set(0, 0, 0)
- if (this.ui.dom_model_gizmo_inputs !== null) this.ui.dom_model_gizmo_inputs.style.display = 'none'
- this.transform_controls.detach()
- this.transform_controls.enabled = false
- }
-
- public confirm_model_gizmo_position (): void {
+ private bake_and_disable_model_gizmo (): void {
const mesh_data = this.load_model_step.model_meshes()
const pos = mesh_data.position
if (pos.x !== 0 || pos.y !== 0 || pos.z !== 0) {
ModelCleanupUtility.translate_model_vertices(mesh_data, pos.x, pos.y, pos.z)
mesh_data.position.set(0, 0, 0)
}
- this.disable_model_gizmo()
- }
-
- // --- Skeleton position gizmo (edit skeleton sidebar, step 3) ---
-
- public enable_skeleton_gizmo (): void {
- const armature = this.edit_skeleton_step.armature()
- // Add to scene so TransformControls can compute world matrices
- this.scene.add(armature)
- this.is_skeleton_gizmo_active = true
- const pos = armature.position
- if (this.ui.dom_skeleton_gizmo_x !== null) this.ui.dom_skeleton_gizmo_x.value = pos.x.toFixed(3)
- if (this.ui.dom_skeleton_gizmo_y !== null) this.ui.dom_skeleton_gizmo_y.value = pos.y.toFixed(3)
- if (this.ui.dom_skeleton_gizmo_z !== null) this.ui.dom_skeleton_gizmo_z.value = pos.z.toFixed(3)
- if (this.ui.dom_skeleton_gizmo_inputs !== null) this.ui.dom_skeleton_gizmo_inputs.style.display = 'flex'
- this.transform_controls.attach(armature)
- this.transform_controls.setMode('translate')
- }
-
- public disable_skeleton_gizmo (): void {
- const armature = this.edit_skeleton_step.armature()
- const pos = armature.position
- this.load_skeleton_step.store_skeleton_gizmo_position(pos.x, pos.y, pos.z)
- this.scene.remove(armature)
- this.is_skeleton_gizmo_active = false
- if (this.ui.dom_skeleton_gizmo_button !== null) this.ui.dom_skeleton_gizmo_button.checked = false
- if (this.ui.dom_skeleton_gizmo_inputs !== null) this.ui.dom_skeleton_gizmo_inputs.style.display = 'none'
+ this.is_model_gizmo_active = false
this.transform_controls.detach()
- // re-attach to previously selected bone if any
- const selected_bone = this.edit_skeleton_step.get_currently_selected_bone()
- if (selected_bone !== null) {
- this.transform_controls.attach(selected_bone)
- }
+ this.transform_controls.enabled = false
}
private show_skin_failure_message (bone_names_with_errors: string[], error_point_positions: Vector3[]): void {
@@ -406,6 +346,15 @@ export class Mesh2MotionEngine {
this.show_animation_player(false)
}
+ // bake model gizmo position into vertices before transitioning away from step 2
+ if (this.is_model_gizmo_active) {
+ this.bake_and_disable_model_gizmo()
+ }
+
+ // when we change steps, we are re-creating the skeleton and helper
+ // so the current transform control reference will be lost/give an error
+ this.transform_controls.detach()
+
/**********
* MAIN PROCESS FLOW LOGIC
* I am doing else if here since the bindpose step changes the step at the end
@@ -430,9 +379,8 @@ export class Mesh2MotionEngine {
// initializing all the load skeleton step stuff
this.scene.add(this.load_model_step.model_meshes())
- // we generate a preview skeleton on this step, and we don't want
- // the user to start trying to edit the skeleton at this point
- this.transform_controls.enabled = false
+ // enable model position gizmo so user can freely position the model
+ this.enable_model_gizmo()
// finish initialization and add origin markers
// this needs to happen at the end since it is expecting the mesh data
@@ -485,18 +433,6 @@ export class Mesh2MotionEngine {
}
}
- // clean up any active step-2 gizmos before transitioning
- if (this.is_model_gizmo_active) {
- this.disable_model_gizmo()
- }
- if (this.is_skeleton_gizmo_active) {
- this.disable_skeleton_gizmo()
- }
-
- // when we change steps, we are re-creating the skeleeton and helper
- // so the current transform control reference will be lost/give an error
- this.transform_controls.detach()
-
return this.process_step
} // end process_step_changed()
@@ -566,9 +502,6 @@ export class Mesh2MotionEngine {
if (is_primary_button_click === false) { return }
- // while skeleton gizmo is active, clicks move the armature not bones
- if (this.is_skeleton_gizmo_active) { return }
-
if (this.edit_skeleton_step.skeleton()?.bones === undefined) { return }
// when we are done with skinned mesh, we shouldn't be editing transforms
diff --git a/src/create.html b/src/create.html
index f10cdba..c9e2b3c 100644
--- a/src/create.html
+++ b/src/create.html
@@ -180,35 +180,14 @@
<hr />
<span style="max-width: 220px">
- <div style="display: flex; align-items: center; justify-content: space-between">
- <span>Position model</span>
- <button class="secondary-button" id="reset-model-position-button" style="padding: 0.1rem 0.4rem; font-size: 0.75rem">Reset</button>
- </div>
+ <span>Position model</span>
<div style="display: flex; flex-direction: column; gap: 0.5rem">
- <select id="model-position-mode-select">
- <option value="auto-align">Auto-align to floor</option>
- <option value="gizmo-move">3D gizmo move</option>
- </select>
- <button class="secondary-button" id="apply-model-position-button">
- Apply
+ <button class="secondary-button" id="reset-model-position-button">
+ Reset position
</button>
- <button class="secondary-button" id="confirm-gizmo-position-button" style="display: none">
- Confirm position
+ <button class="secondary-button" id="auto-align-model-button">
+ Auto-align to floor
</button>
- <div id="model-gizmo-inputs" style="display: none; flex-direction: column; gap: 0.25rem">
- <div style="display: flex; gap: 0.5rem; align-items: center">
- <label style="width: 1rem">X</label>
- <input id="model-gizmo-x" type="number" step="0.01" value="0" style="flex-grow: 1" />
- </div>
- <div style="display: flex; gap: 0.5rem; align-items: center">
- <label style="width: 1rem">Y</label>
- <input id="model-gizmo-y" type="number" step="0.01" value="0" style="flex-grow: 1" />
- </div>
- <div style="display: flex; gap: 0.5rem; align-items: center">
- <label style="width: 1rem">Z</label>
- <input id="model-gizmo-z" type="number" step="0.01" value="0" style="flex-grow: 1" />
- </div>
- </div>
</div>
</span>
@@ -530,28 +509,6 @@
<hr />
</div>
- <!-- Move skeleton (3D gizmo) -->
- <div id="skeleton-position-section" style="display: flex; flex-direction: column; gap: 0.5rem">
- <div class="styled-checkbox">
- <input type="checkbox" id="skeleton-gizmo-button" />
- <label for="skeleton-gizmo-button">Enable skeleton position gizmo</label>
- </div>
- <div id="skeleton-gizmo-inputs" style="display: none; flex-direction: column; gap: 0.25rem">
- <div style="display: flex; gap: 0.5rem; align-items: center">
- <label style="width: 1rem">X</label>
- <input id="skeleton-gizmo-x" type="number" step="0.01" value="0" style="flex-grow: 1" />
- </div>
- <div style="display: flex; gap: 0.5rem; align-items: center">
- <label style="width: 1rem">Y</label>
- <input id="skeleton-gizmo-y" type="number" step="0.01" value="0" style="flex-grow: 1" />
- </div>
- <div style="display: flex; gap: 0.5rem; align-items: center">
- <label style="width: 1rem">Z</label>
- <input id="skeleton-gizmo-z" type="number" step="0.01" value="0" style="flex-grow: 1" />
- </div>
- </div>
- </div>
-
<hr />
<!-- back to load skeleton -->
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index 0099ee0..4e4a1fb 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -122,92 +122,17 @@ export class EventListeners {
this.bootstrap.load_model_step.rotate_model_geometry('z', 90)
})
- // Model position mode dropdown
- this.bootstrap.ui.dom_model_position_mode_select?.addEventListener('change', () => {
- const mode = this.bootstrap.ui.dom_model_position_mode_select?.value
- const applyBtn = this.bootstrap.ui.dom_apply_model_position_button
- const confirmBtn = this.bootstrap.ui.dom_confirm_gizmo_position_button
-
- if (mode === 'gizmo-move') {
- if (applyBtn !== null) applyBtn.style.display = 'none'
- if (confirmBtn !== null) confirmBtn.style.display = 'block'
- this.bootstrap.enable_model_gizmo()
- } else {
- if (applyBtn !== null) applyBtn.style.display = 'block'
- if (confirmBtn !== null) confirmBtn.style.display = 'none'
- if (this.bootstrap.is_model_gizmo_active) {
- this.bootstrap.disable_model_gizmo()
- }
- }
- })
-
- // Model position apply button (auto-align to floor)
- this.bootstrap.ui.dom_apply_model_position_button?.addEventListener('click', () => {
- const mesh_data = this.bootstrap.load_model_step.model_meshes()
- ModelCleanupUtility.move_model_to_floor(mesh_data)
- })
-
// Reset model position to import state
this.bootstrap.ui.dom_reset_model_position_button?.addEventListener('click', () => {
- if (this.bootstrap.is_model_gizmo_active) {
- this.bootstrap.disable_model_gizmo()
- const select = this.bootstrap.ui.dom_model_position_mode_select
- if (select !== null) select.value = 'auto-align'
- const applyBtn = this.bootstrap.ui.dom_apply_model_position_button
- const confirmBtn = this.bootstrap.ui.dom_confirm_gizmo_position_button
- if (applyBtn !== null) applyBtn.style.display = 'block'
- if (confirmBtn !== null) confirmBtn.style.display = 'none'
- }
this.bootstrap.load_model_step.reset_model_position()
})
- // Model position confirm button (bake gizmo position into vertices)
- this.bootstrap.ui.dom_confirm_gizmo_position_button?.addEventListener('click', () => {
- this.bootstrap.confirm_model_gizmo_position()
- const select = this.bootstrap.ui.dom_model_position_mode_select
- if (select !== null) select.value = 'auto-align'
- const applyBtn = this.bootstrap.ui.dom_apply_model_position_button
- const confirmBtn = this.bootstrap.ui.dom_confirm_gizmo_position_button
- if (applyBtn !== null) applyBtn.style.display = 'block'
- if (confirmBtn !== null) confirmBtn.style.display = 'none'
- })
-
- // Skeleton gizmo checkbox
- this.bootstrap.ui.dom_skeleton_gizmo_button?.addEventListener('change', () => {
- const checkbox = this.bootstrap.ui.dom_skeleton_gizmo_button
- if (checkbox?.checked === true) {
- this.bootstrap.enable_skeleton_gizmo()
- } else {
- this.bootstrap.disable_skeleton_gizmo()
- }
+ // Auto-align model to floor
+ this.bootstrap.ui.dom_auto_align_model_button?.addEventListener('click', () => {
+ const mesh_data = this.bootstrap.load_model_step.model_meshes()
+ ModelCleanupUtility.move_model_to_floor(mesh_data)
})
- // Model gizmo X/Y/Z fine-tune inputs
- const sync_model_gizmo_inputs = (): void => {
- const mesh = this.bootstrap.load_model_step.model_meshes()
- mesh.position.set(
- Number(this.bootstrap.ui.dom_model_gizmo_x?.value ?? 0),
- Number(this.bootstrap.ui.dom_model_gizmo_y?.value ?? 0),
- Number(this.bootstrap.ui.dom_model_gizmo_z?.value ?? 0)
- )
- }
- this.bootstrap.ui.dom_model_gizmo_x?.addEventListener('input', sync_model_gizmo_inputs)
- this.bootstrap.ui.dom_model_gizmo_y?.addEventListener('input', sync_model_gizmo_inputs)
- this.bootstrap.ui.dom_model_gizmo_z?.addEventListener('input', sync_model_gizmo_inputs)
-
- // Skeleton gizmo X/Y/Z fine-tune inputs
- const sync_skeleton_gizmo_inputs = (): void => {
- if (!this.bootstrap.is_skeleton_gizmo_active) return
- this.bootstrap.edit_skeleton_step.armature().position.set(
- Number(this.bootstrap.ui.dom_skeleton_gizmo_x?.value ?? 0),
- Number(this.bootstrap.ui.dom_skeleton_gizmo_y?.value ?? 0),
- Number(this.bootstrap.ui.dom_skeleton_gizmo_z?.value ?? 0)
- )
- }
- this.bootstrap.ui.dom_skeleton_gizmo_x?.addEventListener('input', sync_skeleton_gizmo_inputs)
- this.bootstrap.ui.dom_skeleton_gizmo_y?.addEventListener('input', sync_skeleton_gizmo_inputs)
- this.bootstrap.ui.dom_skeleton_gizmo_z?.addEventListener('input', sync_skeleton_gizmo_inputs)
-
this.bootstrap.ui.dom_show_skeleton_checkbox?.addEventListener('click', (event: MouseEvent) => {
if (this.bootstrap.skeleton_helper !== undefined) {
this.bootstrap.skeleton_helper.visible = event.target.checked
diff --git a/src/lib/UI.ts b/src/lib/UI.ts
index 1d2611c..3d563a4 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -16,14 +16,8 @@ export class UI {
dom_rotate_model_x_button: HTMLButtonElement | null = null
dom_rotate_model_y_button: HTMLButtonElement | null = null
dom_rotate_model_z_button: HTMLButtonElement | null = null
- dom_model_position_mode_select: HTMLSelectElement | null = null
- dom_apply_model_position_button: HTMLButtonElement | null = null
- dom_confirm_gizmo_position_button: HTMLButtonElement | null = null
dom_reset_model_position_button: HTMLButtonElement | null = null
- dom_model_gizmo_inputs: HTMLElement | null = null
- dom_model_gizmo_x: HTMLInputElement | null = null
- dom_model_gizmo_y: HTMLInputElement | null = null
- dom_model_gizmo_z: HTMLInputElement | null = null
+ dom_auto_align_model_button: HTMLButtonElement | null = null
dom_load_skeleton_tools: HTMLElement | null = null
dom_load_skeleton_button: HTMLButtonElement | null = null
@@ -46,13 +40,6 @@ export class UI {
dom_scale_skeleton_controls: HTMLElement | null = null
dom_reset_skeleton_scale_button: HTMLButtonElement | null = null
- // skeleton gizmo controls (edit skeleton sidebar)
- dom_skeleton_gizmo_button: HTMLInputElement | null = null
- dom_skeleton_gizmo_inputs: HTMLElement | null = null
- dom_skeleton_gizmo_x: HTMLInputElement | null = null
- dom_skeleton_gizmo_y: HTMLInputElement | null = null
- dom_skeleton_gizmo_z: HTMLInputElement | null = null
-
// edit skeleton UI step controls
dom_selected_bone_label: HTMLElement | null = null
dom_transform_type_radio_group: HTMLElement | null = null
@@ -137,14 +124,8 @@ export class UI {
this.dom_rotate_model_x_button = document.querySelector('#rotate-model-x-button')
this.dom_rotate_model_y_button = document.querySelector('#rotate-model-y-button')
this.dom_rotate_model_z_button = document.querySelector('#rotate-model-z-button')
- this.dom_model_position_mode_select = document.querySelector('#model-position-mode-select')
- this.dom_apply_model_position_button = document.querySelector('#apply-model-position-button')
- this.dom_confirm_gizmo_position_button = document.querySelector('#confirm-gizmo-position-button')
this.dom_reset_model_position_button = document.querySelector('#reset-model-position-button')
- this.dom_model_gizmo_inputs = document.querySelector('#model-gizmo-inputs')
- this.dom_model_gizmo_x = document.querySelector('#model-gizmo-x')
- this.dom_model_gizmo_y = document.querySelector('#model-gizmo-y')
- this.dom_model_gizmo_z = document.querySelector('#model-gizmo-z')
+ this.dom_auto_align_model_button = document.querySelector('#auto-align-model-button')
// UI controls for loading/working with skeleton
this.dom_load_skeleton_tools = document.querySelector('#load-skeleton-tools')
@@ -157,11 +138,6 @@ export class UI {
this.dom_independent_bone_movement_checkbox = document.querySelector('#independent-bone-movement')
this.dom_scale_skeleton_button = document.querySelector('#scale-skeleton-button')
this.dom_reset_skeleton_scale_button = document.querySelector('#reset-skeleton-scale-button')
- this.dom_skeleton_gizmo_button = document.querySelector('#skeleton-gizmo-button')
- this.dom_skeleton_gizmo_inputs = document.querySelector('#skeleton-gizmo-inputs')
- this.dom_skeleton_gizmo_x = document.querySelector('#skeleton-gizmo-x')
- this.dom_skeleton_gizmo_y = document.querySelector('#skeleton-gizmo-y')
- this.dom_skeleton_gizmo_z = document.querySelector('#skeleton-gizmo-z')
this.dom_undo_button = document.querySelector('#undo-button')
this.dom_redo_button = document.querySelector('#redo-button')
diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
index 9b57bf1..eec6695 100644
--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
@@ -217,12 +217,6 @@ export class StepLoadSkeleton extends EventTarget {
})
}
- public store_skeleton_gizmo_position (x: number, y: number, z: number): void {
- this.skeleton_offset_x = x
- this.skeleton_offset_y = y
- this.skeleton_offset_z = z
- }
-
public load_skeleton_file (file_path: string): void {
// load skeleton from GLB file
this.loader.load(file_path, (gltf: GLTFResult) => {