Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Lerp mesh when rotating mesh

Scott Petrovicc6dcdc3fe62e
diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.ts
index aa7067f..14ae959 100644
--- a/src/lib/EventListeners.ts
+++ b/src/lib/EventListeners.ts
@@ -72,15 +72,15 @@ export class EventListeners {
// rotate model after loading it in to orient it correctly
this.bootstrap.ui.dom_rotate_model_x_button?.addEventListener('click', () => {
- this.bootstrap.load_model_step.rotate_model_by_axis('x', 90)
+ this.bootstrap.load_model_step.set_model_rotation_target('x', 90)
})
this.bootstrap.ui.dom_rotate_model_y_button?.addEventListener('click', () => {
- this.bootstrap.load_model_step.rotate_model_by_axis('y', 90)
+ this.bootstrap.load_model_step.set_model_rotation_target('y', 90)
})
this.bootstrap.ui.dom_rotate_model_z_button?.addEventListener('click', () => {
- this.bootstrap.load_model_step.rotate_model_by_axis('z', 90)
+ this.bootstrap.load_model_step.set_model_rotation_target('z', 90)
})
this.bootstrap.ui.dom_move_model_to_floor_button?.addEventListener('click', () => {
diff --git a/src/lib/processes/StepLoadModel.ts b/src/lib/processes/StepLoadModel.ts
index 142d1a8..cec32ae 100644
--- a/src/lib/processes/StepLoadModel.ts
+++ b/src/lib/processes/StepLoadModel.ts
@@ -30,6 +30,13 @@ export class StepLoadModel extends EventTarget {
triangle_count = 0
objects_count = 0
+ // Smooth rotation (lerp) support for rotating mesh
+ private readonly rotationStart = { x: 0, y: 0, z: 0 }
+ private readonly rotationTarget = { x: 0, y: 0, z: 0 }
+ private rotationStartTime: number = 0
+ private rotationDuration: number = 0.25 // seconds (twice as fast)
+ private isRotating: boolean = false
+
// function that goes through all our geometry data and calculates how many triangles we have
private calculate_mesh_metrics (): void {
let triangle_count = 0
@@ -304,23 +311,54 @@ export class StepLoadModel extends EventTarget {
return this.material_list
}
- public rotate_model_by_axis (axis: string, angle: number): void {
- this.final_mesh_data.traverse((obj) => {
- // if object is a mesh, rotate the geometry data
+ /**
+ * Set the target rotation for the model (in degrees, per axis) and start smooth animation.
+ * @param axis 'x' | 'y' | 'z'
+ * @param angle Angle in degrees
+ * @param duration Optional duration in seconds (default: 0.5)
+ */
+ public set_model_rotation_target (axis: 'x' | 'y' | 'z', angle: number, duration: number = 0.25): void {
+ // Get current rotation from the first mesh (assumes all meshes rotate together)
+ let mesh: Mesh | undefined
+ this.final_mesh_data.traverse((obj: Object3D) => {
+ if (!mesh && obj.type === 'Mesh') mesh = obj as Mesh
+ })
+ if (!(mesh)) return
+
+ this.rotationStart.x = mesh.rotation.x
+ this.rotationStart.y = mesh.rotation.y
+ this.rotationStart.z = mesh.rotation.z
+ // Make rotation cumulative: add angle to current target
+ this.rotationTarget[axis] = this.rotationTarget[axis] + MathUtils.degToRad(angle)
+ this.rotationStartTime = performance.now() / 1000
+ this.rotationDuration = duration
+ this.isRotating = true
+ }
+
+ /**
+ * Called in the main animation loop to smoothly animate the model's rotation toward the target.
+ * Uses a timed lerp for smooth, predictable results.
+ */
+ public lerp_model_rotation (): void {
+ if (!this.isRotating) return // only call if the model is actively rotating
+ const now = performance.now() / 1000
+ const t = Math.min((now - this.rotationStartTime) / this.rotationDuration, 1)
+ // Interpolate each axis
+ const lerped = {
+ x: MathUtils.lerp(this.rotationStart.x, this.rotationTarget.x, t),
+ y: MathUtils.lerp(this.rotationStart.y, this.rotationTarget.y, t),
+ z: MathUtils.lerp(this.rotationStart.z, this.rotationTarget.z, t)
+ }
+ this.final_mesh_data.traverse((obj: Object3D) => {
if (obj.type === 'Mesh') {
- switch (axis) {
- case 'x':
- (obj as Mesh).geometry.rotateX(MathUtils.degToRad(angle))
- break
- case 'y':
- (obj as Mesh).geometry.rotateY(MathUtils.degToRad(angle))
- break
- case 'z':
- (obj as Mesh).geometry.rotateZ(MathUtils.degToRad(angle))
- break
- }
+ obj.rotation.x = lerped.x
+ obj.rotation.y = lerped.y
+ obj.rotation.z = lerped.z
}
})
+
+ // stop animating if we are done
+ if (t >= 1) this.isRotating = false
}
public move_model_to_floor (): void {
diff --git a/src/script.ts b/src/script.ts
index bfaf867..25c5f44 100644
--- a/src/script.ts
+++ b/src/script.ts
@@ -294,6 +294,10 @@ export class Bootstrap {
this.animations_listing_step.frame_change(delta_time)
}
+ // if we rotate the model manually, show lerped
+ // only does logic when actively rotating mesh
+ this.load_model_step.lerp_model_rotation()
+
this.renderer.render(this.scene, this.camera)
}