Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Allow all mesh types for now to work on handling better. Adjust camera when imported model is huge

Scott Petrovicbb61365fa2b2
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 36fb73d..e28b633 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -123,6 +123,27 @@ export class Mesh2MotionEngine {
this.controls?.update()
}
+ public set_zoom_limits (min_distance: number, max_distance: number): void {
+ if (this.controls !== undefined) {
+ this.controls.minDistance = min_distance
+ this.controls.maxDistance = max_distance
+ this.controls.update()
+ }
+ }
+
+ public set_fog_enabled (enabled: boolean): void {
+ if (enabled) {
+ // Determine fog color based on theme
+ let floor_color = 0x2d4353
+ if (this.theme_manager.get_current_theme() === 'light') {
+ floor_color = 0xecf0f1
+ }
+ this.scene.fog = new THREE.Fog(floor_color, 20, 80)
+ } else {
+ this.scene.fog = null
+ }
+ }
+
private setup_environment (): void {
this.renderer.setSize(window.innerWidth, window.innerHeight)
this.renderer.shadowMap.enabled = true
diff --git a/src/lib/Generators.ts b/src/lib/Generators.ts
index 2f90ff0..a0c9593 100644
--- a/src/lib/Generators.ts
+++ b/src/lib/Generators.ts
@@ -179,7 +179,7 @@ export class Generators {
static create_camera (): PerspectiveCamera {
const field_of_view = 15 // in millimeters. Lower makes the camera more isometric
- const camera = new PerspectiveCamera(field_of_view, window.innerWidth / window.innerHeight, 0.1, 1000)
+ const camera = new PerspectiveCamera(field_of_view, window.innerWidth / window.innerHeight, 0.1, 10000)
camera.position.z = 10
camera.position.y = 5
camera.position.x = 5
diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.ts
index 8f50562..ac4fbc4 100644
--- a/src/lib/processes/load-model/StepLoadModel.ts
+++ b/src/lib/processes/load-model/StepLoadModel.ts
@@ -332,6 +332,15 @@ export class StepLoadModel extends EventTarget {
return
}
+ // if we are doing retargeting, our work ends here for loading the model
+ // assign the final retargetable model data to the cleaned scene with skinned meshes
+ // any scaling or further processing can be donw as part of the retargeting process
+ if (this.preserve_skinned_mesh) {
+ this.final_retargetable_model_data = clean_scene_with_only_models as Group<Object3DEventMap>
+ this.dispatchEvent(new CustomEvent('modelLoadedForRetargeting'))
+ return
+ }
+
// loop through each child in scene and reset rotation
// if we don't the skinning process doesn't take rotation into account
// and creates odd results
@@ -346,14 +355,6 @@ export class StepLoadModel extends EventTarget {
// scale everything down to a max height. mutate the clean scene object
this.scale_model_on_import_if_extreme(clean_scene_with_only_models)
- // if we are doing retargeting, our work ends here for loading the model
- // assign the final retargetable model data to the cleaned scene with skinned meshes
- if (this.preserve_skinned_mesh) {
- this.final_retargetable_model_data = clean_scene_with_only_models as Group<Object3DEventMap>
- this.dispatchEvent(new CustomEvent('modelLoadedForRetargeting'))
- return
- }
-
// preserved skinned meshes shouldn't be breaking apart mesh data
// breaking apart skinned meshes converts it to a regular mesh which we don't want.
this.calculate_geometry_and_materials(clean_scene_with_only_models)
diff --git a/src/retarget/index.html b/src/retarget/index.html
index af9a73a..ce0b4e4 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -74,7 +74,7 @@
<p class="bone-map-heading">Your 3D Rig Model (Target)</p>
<div class="bone-map-content">
<!-- 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;" />
+ <input type="file" id="upload-file" accept=".glb, .fbx, .zip" style="display: none;" />
<label for="upload-file" class="button" id="load-model-button">Upload GLB</label>
<p>Bones List (Drop here) <span id="target-bone-count">(0)</span></p>
diff --git a/src/retarget/retarget.ts b/src/retarget/retarget.ts
index d3529a1..58fdc20 100644
--- a/src/retarget/retarget.ts
+++ b/src/retarget/retarget.ts
@@ -1,5 +1,5 @@
import { Mesh2MotionEngine } from '../Mesh2MotionEngine.ts'
-import { type Group, type Object3DEventMap, type SkinnedMesh, Vector3 } from 'three'
+import { Box3, type Group, type Object3DEventMap, type SkinnedMesh, Vector3 } from 'three'
import { ModalDialog } from '../lib/ModalDialog.ts'
import { StepLoadSourceSkeleton } from './StepLoadSourceSkeleton.ts'
import { StepBoneMapping } from './StepBoneMapping.ts'
@@ -20,6 +20,12 @@ class RetargetModule {
const camera_position = new Vector3().set(0, 1.7, 5)
this.mesh2motion_engine.set_camera_position(camera_position)
+ // Override zoom limits for retargeting to accommodate models of various sizes
+ // Allow closer zoom for small details and farther zoom for large models
+ // FBX are known to have units with 1 = 1 cm, so things like mixamo will import at 200 units
+ // GLB seems to have gone with 1 = 1 meter
+ this.mesh2motion_engine.set_zoom_limits(0.1, 1000)
+
// Initialize Mesh2Motion skeleton loading step (source)
this.step_load_source_skeleton = new StepLoadSourceSkeleton(this.mesh2motion_engine.get_scene())
@@ -114,6 +120,9 @@ class RetargetModule {
RetargetUtils.reset_skinned_mesh_to_rest_pose(retargetable_meshes)
this.mesh2motion_engine.get_scene().add(retargetable_meshes)
+ // Adjust camera based on model size
+ this.adjust_camera_for_model(retargetable_meshes)
+
// Add skeleton helper
this.add_skeleton_helper(retargetable_meshes)
@@ -138,6 +147,52 @@ class RetargetModule {
}
})
}
+
+ private adjust_camera_for_model (model_group: Group<Object3DEventMap>): void {
+ // Calculate bounding box of the model
+ const bounding_box = new Box3().setFromObject(model_group)
+
+ // Calculate model dimensions
+ const size = new Vector3()
+ bounding_box.getSize(size)
+
+ // Calculate center of the model
+ const center = new Vector3()
+ bounding_box.getCenter(center)
+
+ // Get the maximum dimension (height, width, or depth)
+ const max_dimension = Math.max(size.x, size.y, size.z)
+
+ // Disable fog for retargeting to prevent models from appearing foggy when zoomed far out
+ if (max_dimension > 50) {
+ console.log('Model is very large. Removing fog to increase visibility: ', max_dimension)
+ this.mesh2motion_engine.set_fog_enabled(false)
+ }
+
+ // Calculate appropriate camera distance
+ // Use a multiplier to ensure the entire model is visible
+ // The 2.5 multiplier provides good framing with some padding
+ const camera_distance = max_dimension * 2.5
+
+ // Position camera to look at the center of the model
+ // Keep camera slightly elevated (looking down at the model)
+ const camera_position = new Vector3(
+ center.x,
+ center.y + max_dimension * 0.3, // Slight elevation based on model size
+ center.z + camera_distance
+ )
+
+ this.mesh2motion_engine.set_camera_position(camera_position)
+
+ console.log('Adjusted camera for model:', {
+ bounding_box_size: size,
+ center: center,
+ max_dimension: max_dimension,
+ camera_distance: camera_distance,
+ camera_position: camera_position
+ })
+ }
+
private setup_animation_loop (): void {
let last_time = performance.now()