Import a 3D Model and automatically assign and export animations
TypeScript
Better job with importing files with multiple meshes and skinned meshes
Scott Petrovic519128135003
diff --git a/src/lib/processes/StepLoadModel.js b/src/lib/processes/StepLoadModel.jsindex 6996882..1128432 100644--- a/src/lib/processes/StepLoadModel.js+++ b/src/lib/processes/StepLoadModel.js@@ -1,8 +1,8 @@-import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';import { UI } from '../UI.js';+import * as THREE from 'three';import { Box3 } from 'three/src/math/Box3.js';-+import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';// Note: EventTarget is a built-ininterface and do not need to import itexport class StepLoadModel extends EventTarget@@ -68,44 +68,73 @@ export class StepLoadModel extends EventTarget_load_model_file(model_file){this.loader = new GLTFLoader();+ const max_height = 1.5; // have 3d model scaled to be 1.5 units tall. helps normalize the models to work withthis.loader.load(model_file, (gltf) => {- const model = gltf.scene;+ const loaded_scene = gltf.scene;- this.original_model_data = model.clone()- this.original_model_data.name = 'Unskinned Mesh';+ this.original_model_data = loaded_scene.clone()+ this.original_model_data.name = 'Cloned Scene';this.original_model_data.traverse((child) => {child.castShadow = true;})-- const max_height = 1.5; // have 3d model scaled to be 1.5 units tall. helps normalize the models to work with- this._scale_model_on_import(this.original_model_data, max_height); // if we have multiple objects, we want to scale them all the same++ // strip out stuff that we are not bringing into the model step+ const clean_scene_with_only_models = this._strip_out_all_unecessary_model_data(this.original_model_data)+ this._scale_model_on_import(clean_scene_with_only_models, max_height); // if we have multiple objects, we want to scale them all the same+ // assign the final cleaned up model to the original model data+ this.original_model_data = clean_scene_with_only_models;++this.dispatchEvent(new CustomEvent('modelLoaded'));});}++ _strip_out_all_unecessary_model_data(model_data)+ {+ // create a new scene object, and only include meshes+ const new_scene = new THREE.Scene();+ new_scene.name = 'New Scene';+ model_data.traverse((child) => {++ // if the schild is a skinned mesh, create a new mesh object and apply the geometry and material+ if(child.type === 'SkinnedMesh')+ {+ const new_mesh = new THREE.Mesh(child.geometry, child.material);+ new_mesh.name = child.name;+ new_scene.add(new_mesh);+ }++ if(child.type === 'Mesh')+ {+ new_scene.add(child.clone());+ }++ })++ return new_scene+ }+_scale_model_on_import(mesh, max_height = 1.0){let scale_factor = null;- // need to get the Box3 from everthing.. not just an individual child object- console.log(mesh)-// calculate all the meshes to find out the max heightlet bounding_box = this._calculate_bounding_box(mesh)+const height = bounding_box.max.y - bounding_box.min.y;scale_factor = max_height / height;// scale all the meshes down by the calculated amountmesh.traverse((child) =>- {+ {if(child.type === 'Mesh'){child.geometry.scale(scale_factor, scale_factor, scale_factor);}-})}@@ -148,8 +177,6 @@ export class StepLoadModel extends EventTarget}})- console.log(this.geometry_list)-return this.geometry_list;}