Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add small lerp when changing views. remove sheen on ground plane

Scott Petrovic39ac25010440
diff --git a/src/index.html b/src/index.html
index 8256e64..b169046 100644
--- a/src/index.html
+++ b/src/index.html
@@ -40,12 +40,12 @@
<div id="load-model-tools">
- <p class="step-instructions">Supports GLTF,GLB and FBX files.</p>
+ <p class="step-instructions">Supports GLB, GLTF, FBX</p>
<label for="model-upload" class="button" >
<span class="button-icon-group">
<span class="material-symbols-outlined" >upload</span>
- <span>Upload Custom Model</span>
+ <span>Upload Model</span>
</span>
</label>
<input id="model-upload" type="file" name="file-upload" accept=".glb, .gltf, .fbx" />
diff --git a/src/lib/Generators.ts b/src/lib/Generators.ts
index 39fc8a5..1abd0cd 100644
--- a/src/lib/Generators.ts
+++ b/src/lib/Generators.ts
@@ -16,13 +16,15 @@ export class Generators {
static create_material (wireframe_value = true, color_hex = 0x00ff00): MeshPhongMaterial {
const material = new MeshPhongMaterial({ color: color_hex, wireframe: wireframe_value })
material.side = DoubleSide
+ material.specular = 0.0
+ material.shininess = 0.0
return material
}
static create_grid_helper (color = 0x111155): any[] {
// create floor mesh and add to scene to help with shadows
const floor_geometry = new PlaneGeometry(30, 30, 30, 30)
- const floor_material = new MeshPhongMaterial({ color: 0x4e4e7a, wireframe: false, transparent: false })
+ const floor_material = new MeshPhongMaterial({ color: 0x4e4e7a, wireframe: false, transparent: false, shininess: 0.0, specular: 0.0 })
floor_material.side = DoubleSide // helps us see that we are below the character
const floor_mesh = new Mesh(floor_geometry, floor_material)
diff --git a/src/lib/processes/StepLoadModel.ts b/src/lib/processes/StepLoadModel.ts
index 0a6c92c..4a14098 100644
--- a/src/lib/processes/StepLoadModel.ts
+++ b/src/lib/processes/StepLoadModel.ts
@@ -165,7 +165,10 @@ export class StepLoadModel extends EventTarget {
// 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
+
+ // Some objects come in very large, which makes it harder to work with
+ // scale everything down to a max height
+ this.scale_model_on_import(clean_scene_with_only_models, max_height)
// loop through each child in scene and reset rotation
// if we don't the skinning process doesn't take rotation into account
diff --git a/src/script.ts b/src/script.ts
index e5a4b33..f30e167 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -80,7 +80,6 @@ export class Bootstrap {
// Set default camera position for front view
// this will help because we first want the user to rotate the model to face the front
this.camera.position.set(0, 1.7, 15) // X:0 (centered), Y:1.7 (eye-level), Z:5 (front view)
- this.camera.lookAt(0, 1.7, 0) // Look at center at eye-level
Generators.create_window_resize_listener(this.renderer, this.camera)
document.body.appendChild(this.renderer.domElement)
@@ -449,25 +448,41 @@ export class Bootstrap {
}
const distance = 10
+ const target_position = new THREE.Vector3()
switch (view) {
case 'front':
- this.camera.position.set(0, 0, distance)
+ target_position.set(0, 0, distance)
break
case 'side':
- this.camera.position.set(distance, 0, 0)
+ target_position.set(distance, 0, 0)
break
case 'top':
- this.camera.position.set(0, distance, 0)
+ target_position.set(0, distance, 0)
break
}
- this.controls.target.set(0, 0, 0) // look at origin
- // how to pan up 1/2 the height of the model?
const bounding_box = this.load_model_step.model_meshes().children[0].geometry.boundingBox
const model_height = bounding_box.max.y - bounding_box.min.y
- this.controls.target.y += model_height / 2
+ const target = new THREE.Vector3(0, model_height / 2, 0) // look at origin, adjusted for model height
- this.controls.update() // update the camera position
+ const start_position = this.camera.position.clone()
+ const start_time = performance.now()
+ const duration = 0.25 // seconds
+
+ const animate = (current_time: number) => {
+ const elapsed_time = (current_time - start_time) / 1000
+ const t = Math.min(elapsed_time / duration, 1) // normalize time to [0, 1]
+
+ this.camera.position.lerpVectors(start_position, target_position, t)
+ this.controls.target.lerp(target, t)
+ this.controls.update()
+
+ if (t < 1) {
+ requestAnimationFrame(animate)
+ }
+ }
+
+ requestAnimationFrame(animate)
}
} // end Bootstrap class