Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Move some model cleanup functionality to separate cleanup utils class to separate from loading

Scott Petrovice4d47c6ea008
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index d1068d9..bb3d114 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -3,6 +3,7 @@ import { ModelPreviewDisplay } from './enums/ModelPreviewDisplay'
import { ProcessStep } from './enums/ProcessStep'
import { TransformSpace } from './enums/TransformSpace'
import { Utility } from './Utilities'
+import { ModelCleanupUtility } from './processes/load-model/ModelCleanupUtility'
export class EventListeners {
constructor (private readonly bootstrap: Mesh2MotionEngine) {}
@@ -112,7 +113,8 @@ export class EventListeners {
})
this.bootstrap.ui.dom_move_model_to_floor_button?.addEventListener('click', () => {
- this.bootstrap.load_model_step.move_model_to_floor()
+ const mesh_data = this.bootstrap.load_model_step.model_meshes()
+ ModelCleanupUtility.move_model_to_floor(mesh_data)
})
this.bootstrap.ui.dom_show_skeleton_checkbox?.addEventListener('click', (event: MouseEvent) => {
diff --git a/src/lib/processes/load-model/ModelCleanupUtility.ts b/src/lib/processes/load-model/ModelCleanupUtility.ts
new file mode 100644
index 0000000..a2cc6ac
--- /dev/null
+++ b/src/lib/processes/load-model/ModelCleanupUtility.ts
@@ -0,0 +1,76 @@
+import { Box3, type Group, type Object3DEventMap, type Object3D, Scene, Mesh } from 'three'
+
+/**
+ * Utility helpers to clean up and normalize loaded model geometry.
+ */
+export class ModelCleanupUtility {
+ public static calculate_bounding_box (scene_object: Scene | Group<Object3DEventMap>): Box3 {
+ let bounding_box: Box3 = new Box3()
+
+ scene_object.traverse((child: Object3D) => {
+ const test_bb = new Box3().setFromObject(child)
+ if (test_bb.max.x - test_bb.min.x > bounding_box.max.x - bounding_box.min.x ||
+ test_bb.max.y - test_bb.min.y > bounding_box.max.y - bounding_box.min.y ||
+ test_bb.max.z - test_bb.min.z > bounding_box.max.z - bounding_box.min.z) {
+ bounding_box = test_bb
+ }
+ })
+
+ return bounding_box
+ }
+
+ public static scale_model_on_import_if_extreme (scene_object: Scene | Group<Object3DEventMap>): void {
+ let scale_factor = 1.0
+
+ const bounding_box = this.calculate_bounding_box(scene_object)
+ const height = bounding_box.max.y - bounding_box.min.y
+ const width = bounding_box.max.x - bounding_box.min.x
+ const depth = bounding_box.max.z - bounding_box.min.z
+
+ const largest_dimension = Math.max(height, width, depth)
+
+ if (largest_dimension > 0.5 && largest_dimension < 8) {
+ console.log('Model a reasonable size, so no scaling applied: ', bounding_box, ' units is bounding box')
+ return
+ } else {
+ console.log('Model is very large or small, so scaling applied: ', bounding_box, ' units is bounding box')
+ }
+
+ scale_factor = 1.5 / height
+
+ scene_object.traverse((child) => {
+ const child_obj = child as Mesh
+ if (child_obj.geometry) {
+ console.log('Scaling mesh:', child_obj, ' by factor of ', scale_factor)
+ child_obj.geometry.scale(scale_factor, scale_factor, scale_factor)
+ child_obj.geometry.computeBoundingBox()
+ child_obj.geometry.computeBoundingSphere()
+ }
+ })
+ }
+
+ public static move_model_to_floor (mesh_data: Scene | Group<Object3DEventMap>): void {
+ let final_lowest_point = 0
+ mesh_data.traverse((obj: Object3D) => {
+ if (obj.type === 'Mesh') {
+ const mesh_obj: Mesh = obj as Mesh
+ const bounding_box = new Box3().setFromObject(mesh_obj)
+
+ if (bounding_box.min.y < final_lowest_point) {
+ final_lowest_point = bounding_box.min.y
+ }
+ }
+ })
+
+ mesh_data.traverse((obj: Object3D) => {
+ if (obj.type === 'Mesh') {
+ const mesh_obj: Mesh = obj as Mesh
+
+ const offset = final_lowest_point * -1
+ mesh_obj.geometry.translate(0, offset, 0)
+ mesh_obj.geometry.computeBoundingBox()
+ mesh_obj.geometry.computeBoundingSphere()
+ }
+ })
+ }
+}
diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.ts
index ac4fbc4..aa47d96 100644
--- a/src/lib/processes/load-model/StepLoadModel.ts
+++ b/src/lib/processes/load-model/StepLoadModel.ts
@@ -1,7 +1,6 @@
import { UI } from '../../UI.ts'
import { ModelZipLoader } from './ModelZipLoader.ts'
import { CustomFBXLoader, type FBXResults } from './CustomFBXLoader.ts'
-import { Box3 } from 'three/src/math/Box3.js'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { Scene } from 'three/src/scenes/Scene.js'
@@ -10,6 +9,7 @@ import { MathUtils } from 'three/src/math/MathUtils.js'
import { FrontSide } from 'three/src/constants.js'
import { BufferGeometry, Group, MeshPhongMaterial, Object3DEventMap, type Material, type Object3D, type SkinnedMesh } from 'three'
import { ModalDialog } from '../../ModalDialog.ts'
+import { ModelCleanupUtility } from './ModelCleanupUtility.ts'
// Note: EventTarget is a built-ininterface and do not need to import it
export class StepLoadModel extends EventTarget {
@@ -353,7 +353,7 @@ export class StepLoadModel extends EventTarget {
// Some objects come in very large, which makes it harder to work with
// scale everything down to a max height. mutate the clean scene object
- this.scale_model_on_import_if_extreme(clean_scene_with_only_models)
+ ModelCleanupUtility.scale_model_on_import_if_extreme(clean_scene_with_only_models)
// 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.
@@ -383,6 +383,7 @@ export class StepLoadModel extends EventTarget {
// Find SkinnedMesh objects and preserve their complete bone hierarchy
const skinned_meshes: SkinnedMesh[] = []
model_data.traverse((child) => {
+ console.log('building out retargetable model data, inspecting child:', child)
if (child.type === 'SkinnedMesh') {
skinned_meshes.push(child as SkinnedMesh)
}
@@ -434,57 +435,6 @@ export class StepLoadModel extends EventTarget {
return new_scene
}
- private scale_model_on_import_if_extreme (scene_object: Scene | Group<Object3DEventMap>): void {
- let scale_factor: number = 1.0
-
- // calculate all the meshes to find out the max height
- // some models are more wide like a bird, so don't just use height for this calculation
- const bounding_box = this.calculate_bounding_box(scene_object)
- const height = bounding_box.max.y - bounding_box.min.y
- const width = bounding_box.max.x - bounding_box.min.x
- const depth = bounding_box.max.z - bounding_box.min.z
-
- const largest_dimension = Math.max(height, width, depth)
-
- // if model is very large, or very small, scale it to 1.5 to help with application
- if (largest_dimension > 0.5 && largest_dimension < 8) {
- console.log('Model a reasonable size, so no scaling applied: ', bounding_box, ' units is bounding box')
- return
- } else {
- console.log('Model is very large or small, so scaling applied: ', bounding_box, ' units is bounding box')
- }
-
- scale_factor = 1.5 / height // goal is to scale the model to 1.5 units height (similar to skeleton proportions)
-
- // scale all the meshes down by the calculated amount
- scene_object.traverse((child) => {
- const child_obj = child as Mesh
- if (child_obj.geometry) { // only scale if child has geometry data
- console.log('Scaling mesh:', child_obj, ' by factor of ', scale_factor)
- child_obj.geometry.scale(scale_factor, scale_factor, scale_factor)
- // Recompute bounds after geometry modification
- child_obj.geometry.computeBoundingBox()
- child_obj.geometry.computeBoundingSphere()
- }
- })
- }
-
- private calculate_bounding_box (scene_object: Scene | Group<Object3DEventMap>): Box3 {
- let bounding_box: Box3 = new Box3()
-
- // see if this new object is a larger bounding box than previous
- scene_object.traverse((child: Object3D) => {
- const test_bb = new Box3().setFromObject(child)
- if (test_bb.max.x - test_bb.min.x > bounding_box.max.x - bounding_box.min.x ||
- test_bb.max.y - test_bb.min.y > bounding_box.max.y - bounding_box.min.y ||
- test_bb.max.z - test_bb.min.z > bounding_box.max.z - bounding_box.min.z) {
- bounding_box = test_bb
- }
- })
-
- return bounding_box
- }
-
public model_meshes (): Scene {
// if the scene has some children in it, that means we already built it
// this function gets called when we enter the application
@@ -541,36 +491,4 @@ export class StepLoadModel extends EventTarget {
})
}
- public move_model_to_floor (): void {
- // go through all the meshes and find out the lowest point
- // to use later. A model could contain multiple meshes
- // and we want to make sure the offset is the same between all of them
- let final_lowest_point: number = 0
- this.final_mesh_data.traverse((obj: Object3D) => {
- // if object is a mesh, rotate the geometry data
- if (obj.type === 'Mesh') {
- const mesh_obj: Mesh = obj as Mesh
- const bounding_box = new Box3().setFromObject(mesh_obj)
-
- if (bounding_box.min.y < final_lowest_point) {
- final_lowest_point = bounding_box.min.y
- }
- }
- })
-
- // move all the meshes to the floor by the amount we calculated above
- this.final_mesh_data.traverse((obj: Object3D) => {
- // if object is a mesh, rotate the geometry data
- if (obj.type === 'Mesh') {
- const mesh_obj: Mesh = obj as Mesh
-
- // this actually updates the geometry, so the origin will still be at 0,0,0
- // maybe need to recompute the bounding box and sphere internally after translate
- const offset = final_lowest_point * -1
- mesh_obj.geometry.translate(0, offset, 0)
- mesh_obj.geometry.computeBoundingBox()
- mesh_obj.geometry.computeBoundingSphere()
- }
- })
- }
}
diff --git a/src/retarget/StepLoadTargetModel.ts b/src/retarget/StepLoadTargetModel.ts
index b6e32cc..12a8152 100644
--- a/src/retarget/StepLoadTargetModel.ts
+++ b/src/retarget/StepLoadTargetModel.ts
@@ -75,6 +75,16 @@ export class StepLoadTargetModel extends EventTarget {
const is_valid_skinned_mesh = RetargetUtils.validate_skinned_mesh_has_bones(retargetable_meshes)
if (is_valid_skinned_mesh) {
console.log('adding retargetable meshes to scene for retargeting')
+
+ // we have valid skinned mesh(s). The could be very large though,
+ // we let's check to see how large everything is
+ const bounding_box = new Box3().setFromObject(retargetable_meshes)
+ const size = new Vector3()
+ bounding_box.getSize(size)
+ console.log('Retargetable meshes bounding box size:', size)
+ console.log('Skinned mesh data to inspect:', retargetable_meshes)
+
+
RetargetUtils.reset_skinned_mesh_to_rest_pose(retargetable_meshes)
this.mesh2motion_engine.get_scene().add(retargetable_meshes)