Import a 3D Model and automatically assign and export animations
TypeScript
Move a couple more cleanup tasks to the utility class and make static
Scott Petrovic10e45dd14617
diff --git a/src/lib/processes/load-model/ModelCleanupUtility.ts b/src/lib/processes/load-model/ModelCleanupUtility.tsindex a2cc6ac..5da82d5 100644--- a/src/lib/processes/load-model/ModelCleanupUtility.ts+++ b/src/lib/processes/load-model/ModelCleanupUtility.ts@@ -1,4 +1,5 @@-import { Box3, type Group, type Object3DEventMap, type Object3D, Scene, Mesh } from 'three'+import { Box3, Group, type Object3DEventMap, type Object3D, Scene, Mesh, MeshPhongMaterial, type SkinnedMesh } from 'three'+import { FrontSide } from 'three/src/constants.js'/*** Utility helpers to clean up and normalize loaded model geometry.@@ -73,4 +74,66 @@ export class ModelCleanupUtility {}})}++ 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++ model_data.traverse((child) => {+ let new_mesh: Mesh++ if (child.type === 'SkinnedMesh') {+ new_mesh = new Mesh((child as SkinnedMesh).geometry, (child as SkinnedMesh).material)+ new_mesh.name = child.name+ new_scene.add(new_mesh)+ } else if (child.type === 'Mesh') {+ new_mesh = (child as Mesh).clone()+ new_mesh.name = child.name+ new_scene.add(new_mesh)+ }++ let material_to_use: MeshPhongMaterial+ if (debug_model_loading && new_mesh !== undefined) {+ material_to_use = new MeshPhongMaterial()+ material_to_use.side = FrontSide+ material_to_use.color.set(0x00aaee)+ new_mesh.material = material_to_use+ }+ })++ return new_scene+ }++ /**+ * We need to keep parents of skinned meshes and bones for retargeting+ * This is because the hierarchy is important for retargeting to work properly+ * @param model_data Scene or Group containing the model+ * @returns Group with everything needed for retargeting (SkinnedMesh, Bones, and their parents)+ */+ public static strip_out_retargeting_model_data (model_data: Scene | Group): Group<Object3DEventMap> {+ // Create a new Group to hold only the objects we want to keep+ const filtered_group = new Group()+ filtered_group.name = 'Filtered Retargeting Data'++ // 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)+ }+ })++ // For each SkinnedMesh, clone it along with its skeleton to preserve bone hierarchy+ skinned_meshes.forEach((skinned_mesh) => {+ // Clone the SkinnedMesh to avoid modifying the original+ const cloned_skinned_mesh = skinned_mesh.clone()++ // The clone should preserve the skeleton with proper bone hierarchy+ // Since we're cloning, the skeleton hierarchy should remain intact+ filtered_group.add(cloned_skinned_mesh)+ })++ return filtered_group+ }}diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.tsindex aa47d96..2b2dac9 100644--- a/src/lib/processes/load-model/StepLoadModel.ts+++ b/src/lib/processes/load-model/StepLoadModel.ts@@ -6,8 +6,7 @@ import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import { Scene } from 'three/src/scenes/Scene.js'import { Mesh } from 'three/src/objects/Mesh.js'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 { BufferGeometry, Group, MeshPhongMaterial, Object3DEventMap, type Material, type Object3D } from 'three'import { ModalDialog } from '../../ModalDialog.ts'import { ModelCleanupUtility } from './ModelCleanupUtility.ts'@@ -317,9 +316,9 @@ export class StepLoadModel extends EventTarget {// retargeting mesh could be a Group object, so accept both and share this variablelet clean_scene_with_only_models: Scene | Group<Object3DEventMap>if (this.preserve_skinned_mesh) {- clean_scene_with_only_models = this.strip_out_retargeting_model_data(this.original_model_data)+ clean_scene_with_only_models = ModelCleanupUtility.strip_out_retargeting_model_data(this.original_model_data)} else {- clean_scene_with_only_models = this.strip_out_all_unecessary_model_data(this.original_model_data)+ clean_scene_with_only_models = ModelCleanupUtility.strip_out_all_unecessary_model_data(this.original_model_data, this.model_display_name, this.debug_model_loading)}// if there are no valid mesh, or skinned mesh, show error dialog@@ -369,72 +368,6 @@ export class StepLoadModel extends EventTarget {this.dispatchEvent(new CustomEvent('modelLoaded'))}- /**- * We need to keep parents of skinned meshes and bones for retargeting- * This is because the hierarchy is important for retargeting to work properly- * @param model_data- * @returns Group with everything needed for retargeting (SkinnedMesh, Bones, and their parents)- */- private strip_out_retargeting_model_data (model_data: Scene | Group): Group<Object3DEventMap> {- // Create a new Group to hold only the objects we want to keep- const filtered_group = new Group()- filtered_group.name = 'Filtered Retargeting Data'-- // 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)- }- })-- // For each SkinnedMesh, clone it along with its skeleton to preserve bone hierarchy- skinned_meshes.forEach((skinned_mesh) => {- // Clone the SkinnedMesh to avoid modifying the original- const cloned_skinned_mesh = skinned_mesh.clone()-- // The clone should preserve the skeleton with proper bone hierarchy- // Since we're cloning, the skeleton hierarchy should remain intact- filtered_group.add(cloned_skinned_mesh)- })-- return filtered_group- }-- private strip_out_all_unecessary_model_data (model_data: Scene): Scene {- // create a new scene object, and only include meshes- const new_scene = new Scene()- new_scene.name = this.model_display_name-- model_data.traverse((child) => {- let new_mesh: Mesh-- // if the child is a skinned mesh, create a new mesh object and apply the geometry and material- if (child.type === 'SkinnedMesh') {- new_mesh = new Mesh((child as SkinnedMesh).geometry, (child as SkinnedMesh).material)- new_mesh.name = child.name- new_scene.add(new_mesh)- } else if (child.type === 'Mesh') {- new_mesh = (child as Mesh).clone()- new_mesh.name = child.name- new_scene.add(new_mesh)- }-- // potentially use normal material to help debugging models that look odd- // some materials have some odd transparency or back-face things that make it look odd- let material_to_use: MeshPhongMaterial- if (this.debug_model_loading && new_mesh !== undefined) {- material_to_use = new MeshPhongMaterial()- material_to_use.side = FrontSide- material_to_use.color.set(0x00aaee)- new_mesh.material = material_to_use- }- })-- return new_scene- }-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@@ -490,5 +423,4 @@ export class StepLoadModel extends EventTarget {}})}-}