Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add ability to rotate model after importing

Scott Petrovicf42fdd4338d8
diff --git a/src/index.html b/src/index.html
index 1acd9f6..6b10567 100644
--- a/src/index.html
+++ b/src/index.html
@@ -53,6 +53,20 @@
<option value="bipedal-full">Mixamo Human</option>
</select>
<button id="load-skeleton-button">Load</button>
+
+
+ <hr />
+
+ <span>
+ <span>Rotate Model</span>
+ <div style="display: flex; flex-direction: row">
+ <button id="rotate-model-x-button">X</button>
+ <button id="rotate-model-y-button">Y</button>
+ <button id="rotate-model-z-button">Z</button>
+ </div>
+ </span>
+
+
</div>
<!-- radio buttons to toggle between rotation and translate -->
diff --git a/src/lib/UI.js b/src/lib/UI.js
index 931d836..66d40d0 100644
--- a/src/lib/UI.js
+++ b/src/lib/UI.js
@@ -17,6 +17,11 @@ export class UI {
this.dom_load_model_button = document.querySelector('#load-model-button')
this.dom_load_model_debug_checkbox = document.querySelector('#load-model-debug-checkbox')
+ 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')
+
+
// UI controls for loading/working with skeleton
this.dom_load_skeleton_tools = document.querySelector('#load-skeleton-tools')
this.dom_load_skeleton_button = document.querySelector('#load-skeleton-button')
diff --git a/src/lib/processes/StepLoadModel.js b/src/lib/processes/StepLoadModel.js
index 8527c82..ff0cbf7 100644
--- a/src/lib/processes/StepLoadModel.js
+++ b/src/lib/processes/StepLoadModel.js
@@ -10,6 +10,9 @@ export class StepLoadModel extends EventTarget
loader = null
ui = null
original_model_data = null
+
+ final_mesh_data = null
+
debug_model_loading = false
// there can be multiple objects in a model, so store them in a list
@@ -42,17 +45,19 @@ export class StepLoadModel extends EventTarget
_calculate_geometry_list()
{
- if(this.original_model_data === undefined)
+
+ if(this.final_mesh_data === undefined)
{
console.error('original model not loaded yet. Cannot do calculations')
}
- this.original_model_data.traverse((child) => {
+ this.final_mesh_data.traverse((child) => {
if(child.type === 'Mesh')
{
let geometry_to_add = child.geometry.clone();
geometry_to_add.name = child.name;
this.geometry_list.push(geometry_to_add)
+ this.material_list.push(child.material.clone())
}
})
@@ -128,6 +133,7 @@ export class StepLoadModel extends EventTarget
this.original_model_data = loaded_scene.clone()
this.original_model_data.name = 'Cloned Scene';
+
this.original_model_data.traverse((child) => {
child.castShadow = true;
})
@@ -137,7 +143,9 @@ export class StepLoadModel extends EventTarget
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.final_mesh_data = clean_scene_with_only_models;
+
+
this._calculate_geometry_list()
@@ -224,32 +232,75 @@ export class StepLoadModel extends EventTarget
return bounding_box
}
- model_mesh()
+ model_meshes()
{
- return this.original_model_data
+ if(this.final_mesh_data !== undefined)
+ {
+ return this.final_mesh_data;
+ }
+
+
+ // create a new scene object, and only include meshes
+ const new_scene = new THREE.Scene();
+ new_scene.name = 'Model data';
+
+ // do a for loop to add all the meshes to the scene from the geometry and material list
+ for(let i = 0; i < this.geometry_list.length; i++)
+ {
+ const mesh = new THREE.Mesh(this.geometry_list[i], this.material_list[i])
+ new_scene.add(mesh)
+ }
+
+ this.final_mesh_data = new_scene;
+
+ return this.final_mesh_data
}
models_geometry_list()
{
- return this.geometry_list;
+ // loop through final mesh data and return the geometeries
+ const geometries_to_return = []
+ this.final_mesh_data.traverse((child) => {
+ if(child.type === 'Mesh')
+ {
+ geometries_to_return.push(child.geometry.clone())
+ }
+ })
+
+ return geometries_to_return;
}
models_material_list()
{
+ return this.material_list;
+ }
- if(this.material_list.length > 0)
- {
- return this.material_list;
- }
- this.original_model_data.traverse((child) => {
- if(child.type === 'Mesh')
+ rotate_model_by_axis(axis, angle)
+ {
+ this.final_mesh_data.traverse((obj) => {
+
+ // if object is a mesh, rotate the geometry data
+ if(obj.type === 'Mesh')
{
- this.material_list.push(child.material.clone())
+ switch( axis )
+ {
+ case 'x':
+ obj.geometry.rotateX(THREE.MathUtils.degToRad(angle))
+ break;
+ case 'y':
+ obj.geometry.rotateY(THREE.MathUtils.degToRad(angle))
+ break;
+ case 'z':
+ obj.geometry.rotateZ(THREE.MathUtils.degToRad(angle))
+ break;
+ }
+
+ //obj.geometry.rotateX(THREE.MathUtils.degToRad(angle))
}
- })
- return this.material_list;
+ })
}
+
}
\ No newline at end of file
diff --git a/src/script.js b/src/script.js
index 598db0b..ab420b3 100644
--- a/src/script.js
+++ b/src/script.js
@@ -178,12 +178,9 @@ function start_skin_weighting_step()
scene.add(...weight_skin_step.final_skinned_meshes())
});
-
-
-
// remember our skeleton position before we do the skinning process
// that way if we revert to try again...we will have the original positions/rotations
- load_model_step.model_mesh().visible = false; // hide our unskinned mesh after we have done the skinning process
+ load_model_step.model_meshes().visible = false; // hide our unskinned mesh after we have done the skinning process
// re-define skeleton helper to use the skinned mesh)
regenerate_skeleton_helper(weight_skin_step.skeleton())
@@ -333,7 +330,7 @@ transformControls.addEventListener('dragging-changed', function (event) {
});
load_model_step.addEventListener('modelLoaded', (event) => {
- scene.add(load_model_step.model_mesh())
+ scene.add(load_model_step.model_meshes())
process_step = process_step_changed(ProcessStep.LoadSkeleton)
})
@@ -380,7 +377,7 @@ ui.dom_back_to_edit_skeleton_button.addEventListener('click', () => {
regenerate_skeleton_helper(edit_skeleton_step.skeleton())
- load_model_step.model_mesh().visible = true; // show the unskinned mesh again
+ load_model_step.model_meshes().visible = true; // show the unskinned mesh again
});
@@ -416,3 +413,16 @@ ui.dom_export_button.addEventListener('click', () => {
file_export_step.set_animation_clips_to_export(all_clips)
file_export_step.export(weight_skin_step.final_skinned_meshes(), 'exported-model');
});
+
+ui.dom_rotate_model_x_button.addEventListener('click', () => {
+ load_model_step.rotate_model_by_axis('x', 90)
+})
+
+ui.dom_rotate_model_y_button.addEventListener('click', () => {
+ load_model_step.rotate_model_by_axis('y', 90)
+})
+
+
+ui.dom_rotate_model_z_button.addEventListener('click', () => {
+ load_model_step.rotate_model_by_axis('z', 90)
+})
\ No newline at end of file