Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add model and skeleton gizmo positioning UI

DarthMarino9d1b3e76ba78
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 91c7c42..d376b15 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -32,6 +32,7 @@ import { TransformControlType } from './lib/enums/TransformControlType.ts'
import { TransformSpace } from './lib/enums/TransformSpace.ts'
import { ThemeManager } from './lib/ThemeManager.ts'
import { ModalDialog } from './lib/ModalDialog.ts'
+import { ModelCleanupUtility } from './lib/processes/load-model/ModelCleanupUtility.ts'
export class Mesh2MotionEngine {
public readonly camera = Generators.create_camera()
@@ -41,6 +42,8 @@ export class Mesh2MotionEngine {
public readonly transform_controls: TransformControls = new TransformControls(this.camera, this.renderer.domElement)
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
@@ -247,6 +250,24 @@ 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
+ }
+
const selected_bone: Bone = this.transform_controls.object as Bone
if (this.edit_skeleton_step.is_mirror_mode_enabled()) {
@@ -262,6 +283,70 @@ export class Mesh2MotionEngine {
}
}
+ // --- Model position gizmo (step 2) ---
+
+ 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 {
+ 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.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)
+ }
+ }
+
private show_skin_failure_message (bone_names_with_errors: string[], error_point_positions: Vector3[]): void {
// add the bone vertices as X markers to debugging object
const error_markers: Group = Generators.create_x_markers(error_point_positions, 0.02, 0xff0000)
@@ -400,6 +485,14 @@ 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()
@@ -473,6 +566,9 @@ 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 fa85aa9..f10cdba 100644
--- a/src/create.html
+++ b/src/create.html
@@ -180,11 +180,35 @@
<hr />
<span style="max-width: 220px">
- <span>If model is below the ground floor</span>
- <div style="display: flex; flex-direction: row">
- <button class="secondary-button" id="move-model-to-floor-button">
- Move
+ <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>
+ <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>
+ <button class="secondary-button" id="confirm-gizmo-position-button" style="display: none">
+ Confirm position
+ </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>
@@ -272,58 +296,6 @@
/>
</div>
</div>
- <!-- displace skeleton to get it close to the model position in the Z axis (forward / backward depth) -->
- <div
- id="skeleton-offset-controls"
- style="display: none; flex-direction: column"
- >
- <div
- style="
- display: flex;
- flex-direction: row;
- align-items: center;
- justify-content: center;
- gap: 1rem;
- "
- >
- <label style="display: inline-flex">Offset skeleton</label>
- <span
- class="material-symbols-outlined"
- data-tippy-content="Adjust the depth of the skeleton (Z axis)"
- >help</span
- >
-
- <button class="secondary-button" id="reset-skeleton-offset-button">
- <img
- src="images/icons/reset.svg"
- alt="Reset Skeleton Offset"
- width="20"
- height="20"
- />
- </button>
- </div>
- <div
- style="
- display: flex;
- flex-direction: row;
- gap: 1rem;
- justify-content: flex-start;
- align-items: center;
- "
- >
- <span id="skeleton-offset-display">0.00</span>
- <input
- id="skeleton-offset-input"
- style="flex-grow: 1"
- type="range"
- min="-1.00"
- max="1.00"
- value="0.0"
- step="0.01"
- />
- </div>
- </div>
-
<div style="display: flex; gap: 0.5rem">
<button class="no-style-button" id="action_back_to_load_model">
&#x2039; Back
@@ -523,7 +495,6 @@
display: flex;
flex-direction: column;
gap: 0.5rem;
- margin-bottom: 1rem;
"
>
<div class="styled-checkbox">
@@ -559,6 +530,30 @@
<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 -->
<div class="display: flex; gap: 0.5rem;">
<button class="no-style-button" id="action_back_to_load_skeleton">
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index ea14246..0099ee0 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -122,11 +122,92 @@ export class EventListeners {
this.bootstrap.load_model_step.rotate_model_geometry('z', 90)
})
- this.bootstrap.ui.dom_move_model_to_floor_button?.addEventListener('click', () => {
+ // 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()
+ }
+ })
+
+ // 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 af6f0fe..1d2611c 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -16,7 +16,14 @@ 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_move_model_to_floor_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_load_skeleton_tools: HTMLElement | null = null
dom_load_skeleton_button: HTMLButtonElement | null = null
@@ -39,11 +46,12 @@ export class UI {
dom_scale_skeleton_controls: HTMLElement | null = null
dom_reset_skeleton_scale_button: HTMLButtonElement | null = null
- // skeleton offset controls
- dom_skeleton_offset_input: HTMLInputElement | null = null
- dom_skeleton_offset_display: HTMLElement | null = null
- dom_skeleton_offset_controls: HTMLElement | null = null
- dom_reset_skeleton_offset_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
@@ -129,7 +137,14 @@ 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_move_model_to_floor_button = document.querySelector('#move-model-to-floor-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')
// UI controls for loading/working with skeleton
this.dom_load_skeleton_tools = document.querySelector('#load-skeleton-tools')
@@ -142,7 +157,11 @@ 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_reset_skeleton_offset_button = document.querySelector('#reset-skeleton-offset-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')
@@ -170,11 +189,6 @@ export class UI {
this.dom_scale_skeleton_percentage_display = document.querySelector('#scale-skeleton-percentage-display')
this.dom_scale_skeleton_controls = document.querySelector('#scale-skeleton-controls')
- // offsetting the skeleton option
- this.dom_skeleton_offset_input = document.querySelector('#skeleton-offset-input')
- this.dom_skeleton_offset_display = document.querySelector('#skeleton-offset-display')
- this.dom_skeleton_offset_controls = document.querySelector('#skeleton-offset-controls')
-
// UI controls for working with skinned mesh
this.dom_skinned_mesh_tools = document.querySelector('#skinned-step-tools')
this.dom_skinned_mesh_animation_tools = document.querySelector('#skinned-step-animation-export-options')
diff --git a/src/lib/processes/load-model/ModelCleanupUtility.ts b/src/lib/processes/load-model/ModelCleanupUtility.ts
index b512e84..476748a 100644
--- a/src/lib/processes/load-model/ModelCleanupUtility.ts
+++ b/src/lib/processes/load-model/ModelCleanupUtility.ts
@@ -51,7 +51,7 @@ export class ModelCleanupUtility {
}
public static move_model_to_floor (mesh_data: Scene | Group<Object3DEventMap>): void {
- let final_lowest_point = 0
+ let final_lowest_point = Infinity
mesh_data.traverse((obj: Object3D) => {
if (obj.type === 'Mesh') {
const mesh_obj: Mesh = obj as Mesh
@@ -63,6 +63,8 @@ export class ModelCleanupUtility {
}
})
+ if (!isFinite(final_lowest_point) || final_lowest_point === 0) return
+
mesh_data.traverse((obj: Object3D) => {
if (obj.type === 'Mesh') {
const mesh_obj: Mesh = obj as Mesh
@@ -75,6 +77,22 @@ export class ModelCleanupUtility {
})
}
+ public static translate_model_vertices (
+ mesh_data: Scene | Group<Object3DEventMap>,
+ dx: number,
+ dy: number,
+ dz: number
+ ): void {
+ mesh_data.traverse((obj: Object3D) => {
+ if (obj.type === 'Mesh') {
+ const mesh_obj: Mesh = obj as Mesh
+ mesh_obj.geometry.translate(dx, dy, dz)
+ mesh_obj.geometry.computeBoundingBox()
+ mesh_obj.geometry.computeBoundingSphere()
+ }
+ })
+ }
+
public static strip_out_all_unecessary_model_data (model_data: Scene, model_display_name: string, debug_model_loading: boolean): Scene {
const new_scene = new Scene()
new_scene.name = model_display_name
diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.ts
index 827e8bc..1b06d13 100644
--- a/src/lib/processes/load-model/StepLoadModel.ts
+++ b/src/lib/processes/load-model/StepLoadModel.ts
@@ -43,6 +43,8 @@ export class StepLoadModel extends EventTarget {
triangle_count = 0
objects_count = 0
+ private original_geometry_positions: Float32Array[] = []
+
/**
* Skinned mesh data that will be used for retargeting
* @returns Loaded Skinned mesh data that will be used for retargeting
@@ -223,6 +225,7 @@ export class StepLoadModel extends EventTarget {
this.final_mesh_data = new Scene()
this.geometry_list.length = 0
this.material_list.length = 0
+ this.original_geometry_positions = []
this.vertex_count = 0
this.triangle_count = 0
this.objects_count = 0
@@ -230,6 +233,24 @@ export class StepLoadModel extends EventTarget {
this.preserve_skinned_mesh = false
}
+ public reset_model_position (): void {
+ let i = 0
+ this.final_mesh_data.traverse((obj: Object3D) => {
+ if (obj.type === 'Mesh') {
+ const mesh = obj as Mesh
+ const attr = mesh.geometry.attributes.position
+ const original = this.original_geometry_positions[i++]
+ if (original !== undefined) {
+ ;(attr.array as Float32Array).set(original)
+ attr.needsUpdate = true
+ mesh.geometry.computeBoundingBox()
+ mesh.geometry.computeBoundingSphere()
+ }
+ }
+ })
+ this.final_mesh_data.position.set(0, 0, 0)
+ }
+
/**
*
* @param preserve
@@ -381,6 +402,15 @@ export class StepLoadModel extends EventTarget {
// assign the final cleaned up model to the original model data
this.final_mesh_data = this.model_meshes()
+ // snapshot vertex positions so we can reset later
+ this.original_geometry_positions = []
+ this.final_mesh_data.traverse((obj: Object3D) => {
+ if (obj.type === 'Mesh') {
+ const positions = (obj as Mesh).geometry.attributes.position.array as Float32Array
+ this.original_geometry_positions.push(new Float32Array(positions))
+ }
+ })
+
console.log('final mesh data should be prepared at this point', this.final_mesh_data)
this.dispatchEvent(new CustomEvent('modelLoaded'))
diff --git a/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts b/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
index 244bf9f..2b31d7f 100644
--- a/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
+++ b/src/lib/processes/load-skeleton/PreviewSkeletonManager.ts
@@ -14,6 +14,8 @@ export async function add_preview_skeleton (
skeleton_file_path: SkeletonType,
hand_skeleton_type: HandSkeletonType,
skeleton_scale: number = 1.0,
+ skeleton_offset_x: number = 0.0,
+ skeleton_offset_y: number = 0.0,
skeleton_offset_z: number = 0.0
): Promise<Object3D<Object3DEventMap>> {
let preview_skeleton_group = root.getObjectByName(skeleton_group_name) as Group | undefined
@@ -26,7 +28,7 @@ export async function add_preview_skeleton (
if (previous_file_path === skeleton_file_path && previous_hand_type === hand_skeleton_type) {
// Only update scale and offset
preview_skeleton_group.scale.set(skeleton_scale, skeleton_scale, skeleton_scale)
- preview_skeleton_group.position.set(0, 0, skeleton_offset_z)
+ preview_skeleton_group.position.set(skeleton_offset_x, skeleton_offset_y, skeleton_offset_z)
// Return the first child (should be loaded_scene)
return preview_skeleton_group.children[0]
} else {
@@ -53,7 +55,7 @@ export async function add_preview_skeleton (
skeleton_helper.name = 'preview_skeleton'
preview_skeleton_group.add(skeleton_helper)
preview_skeleton_group.scale.set(skeleton_scale, skeleton_scale, skeleton_scale)
- preview_skeleton_group.position.set(0, 0, skeleton_offset_z)
+ preview_skeleton_group.position.set(skeleton_offset_x, skeleton_offset_y, skeleton_offset_z)
root.add(preview_skeleton_group)
return loaded_scene
}
diff --git a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
index 0c9d3a6..9b57bf1 100644
--- a/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
+++ b/src/lib/processes/load-skeleton/StepLoadSkeleton.ts
@@ -20,7 +20,9 @@ export class StepLoadSkeleton extends EventTarget {
// this is useful since position keyframes will need to be scaled
// to prevent large offsets
private skeleton_scale_percentage: number = 1.0
- private skeleton_offset_z: number = 0.0
+ public skeleton_offset_x: number = 0.0
+ public skeleton_offset_y: number = 0.0
+ public skeleton_offset_z: number = 0.0
// this was invented since this value is stored on a DOM element
// this helps the marketing page set the type and doesn't rely on a DOM value
@@ -73,17 +75,11 @@ export class StepLoadSkeleton extends EventTarget {
// so just use that and load the preview right when we enter this step
if (!this.has_select_skeleton_ui_option()) {
add_preview_skeleton(this._main_scene, this.skeleton_file_path(),
- this.hand_skeleton_type(), this.skeleton_scale_percentage, this.skeleton_offset_z).catch((err) => {
+ this.hand_skeleton_type(), this.skeleton_scale_percentage,
+ this.skeleton_offset_x, this.skeleton_offset_y, this.skeleton_offset_z).catch((err) => {
console.error('error loading preview skeleton: ', err)
})
- // restore the offset slider and display to the stored value
- if (this.ui.dom_skeleton_offset_input !== null) {
- this.ui.dom_skeleton_offset_input.value = this.skeleton_offset_z.toFixed(2)
- }
- if (this.ui.dom_skeleton_offset_display !== null) {
- this.ui.dom_skeleton_offset_display.textContent = this.skeleton_offset_z.toFixed(2)
- }
}
// Initialize hand skeleton hand options visibility
@@ -157,23 +153,14 @@ export class StepLoadSkeleton extends EventTarget {
this.ui.dom_skeleton_drop_type?.options.remove(0)
}
- // show the scale and offset skeleton options in case they are hidden
+ // show the scale skeleton options and advanced settings in case they are hidden
if (this.ui.dom_scale_skeleton_controls !== null) {
this.ui.dom_scale_skeleton_controls.style.display = 'flex'
}
- if (this.ui.dom_skeleton_offset_controls !== null) {
- this.ui.dom_skeleton_offset_controls.style.display = 'flex'
- }
-
- // force the slider to render at the correct position after becoming visible
- if (this.ui.dom_skeleton_offset_input !== null) {
- this.ui.dom_skeleton_offset_input.value = this.skeleton_offset_z.toFixed(2)
- }
-
// load the preview skeleton
// need to get the file name for the correct skeleton
// we pass the skeleton scale in the case where we set a skeleton, change scale, then change the skeleton
- add_preview_skeleton(this._main_scene, this.skeleton_file_path(), this.hand_skeleton_type(), this.skeleton_scale(), this.skeleton_offset_z).then(() => {
+ add_preview_skeleton(this._main_scene, this.skeleton_file_path(), this.hand_skeleton_type(), this.skeleton_scale(), this.skeleton_offset_x, this.skeleton_offset_y, this.skeleton_offset_z).then(() => {
// enable the ability to progress to next step
this.allow_proceeding_to_next_step(true)
}).catch((err) => {
@@ -198,7 +185,7 @@ export class StepLoadSkeleton extends EventTarget {
this.ui.dom_hand_skeleton_selection?.addEventListener('change', () => {
// rebuild the preview skeleton with the new hand skeleton type
// make sure we keep existing scale if we made a change to that
- add_preview_skeleton(this._main_scene, this.skeleton_file_path(), this.hand_skeleton_type(), this.skeleton_scale(), this.skeleton_offset_z).catch((err) => {
+ add_preview_skeleton(this._main_scene, this.skeleton_file_path(), this.hand_skeleton_type(), this.skeleton_scale(), this.skeleton_offset_x, this.skeleton_offset_y, this.skeleton_offset_z).catch((err) => {
console.error('error loading preview skeleton: ', err)
})
})
@@ -215,16 +202,6 @@ export class StepLoadSkeleton extends EventTarget {
this.update_skeleton_scale_to_value(1.0)
})
- // skeleton offset controls
- this.ui.dom_skeleton_offset_input?.addEventListener('input', (event) => {
- const new_value: number = Number((event.target as HTMLInputElement).value)
- this.update_skeleton_offset_to_value(new_value)
- })
-
- // reset the skeleton offset button
- this.ui.dom_reset_skeleton_offset_button?.addEventListener('click', () => {
- this.update_skeleton_offset_to_value(0.0)
- })
}
private update_skeleton_scale_to_value (new_value: number): void {
@@ -234,28 +211,16 @@ export class StepLoadSkeleton extends EventTarget {
if (this.ui.dom_scale_skeleton_percentage_display !== null) {
this.ui.dom_scale_skeleton_percentage_display.textContent = display_value
}
- // re-add the preview skeleton with the new scale, preserving the existing Z offset
- add_preview_skeleton(this._main_scene, this.skeleton_file_path(), this.hand_skeleton_type(), this.skeleton_scale_percentage, this.skeleton_offset_z)
+ add_preview_skeleton(this._main_scene, this.skeleton_file_path(), this.hand_skeleton_type(), this.skeleton_scale_percentage, this.skeleton_offset_x, this.skeleton_offset_y, this.skeleton_offset_z)
.catch((err) => {
console.error('error loading preview skeleton: ', err)
})
}
- private update_skeleton_offset_to_value (new_value: number): void {
- this.skeleton_offset_z = new_value
-
- if (this.ui.dom_skeleton_offset_input !== null) {
- this.ui.dom_skeleton_offset_input.value = new_value.toFixed(2)
- }
-
- if (this.ui.dom_skeleton_offset_display !== null) {
- this.ui.dom_skeleton_offset_display.textContent = new_value.toFixed(2)
- }
-
- add_preview_skeleton(this._main_scene, this.skeleton_file_path(), this.hand_skeleton_type(), this.skeleton_scale_percentage, this.skeleton_offset_z)
- .catch((err) => {
- console.error('error loading preview skeleton: ', err)
- })
+ 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 {
@@ -290,8 +255,8 @@ export class StepLoadSkeleton extends EventTarget {
helper.modify_hand_skeleton(this.loaded_armature, this.hand_skeleton_type())
}
- // reset the armature position and apply Z offset from the slider
- this.loaded_armature.position.set(0, 0, this.skeleton_offset_z)
+ // reset the armature position and apply offset from the skeleton gizmo
+ this.loaded_armature.position.set(this.skeleton_offset_x, this.skeleton_offset_y, this.skeleton_offset_z)
this.loaded_armature.updateWorldMatrix(true, true)
// scale the armature to what we picked using the scale slider/preview