Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add small screenshake when switching out models

Scott Petrovic8158065baec2
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index 1593e21..a2bcdca 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -36,6 +36,7 @@ import { SettingsDropdownManager } from './lib/SettingsDropdownManager.ts'
import { ModalDialog } from './lib/ModalDialog.ts'
import { ModelCleanupUtility } from './lib/processes/load-model/ModelCleanupUtility.ts'
import { SceneEnvironmentManager } from './lib/SceneEnvironmentManager.ts'
+import { CameraShake } from './lib/CameraShake.ts'
export class Mesh2MotionEngine {
public readonly camera = Generators.create_camera()
@@ -75,11 +76,13 @@ export class Mesh2MotionEngine {
private readonly clock = new THREE.Clock()
private readonly scene_environment: SceneEnvironmentManager
private readonly eventListeners: EventListeners
+ private readonly camera_shake: CameraShake
constructor () {
this.eventListeners = new EventListeners(this)
// helps resolve requestAnimationFrame calling animate() with wrong context
this.animate = this.animate.bind(this)
+ this.camera_shake = new CameraShake(this.camera)
this.scene = new Scene()
this.theme_manager = new ThemeManager()
@@ -151,6 +154,11 @@ export class Mesh2MotionEngine {
this.scene_environment.set_camera_position(position)
}
+ /** Trigger a gentle camera shake transition effect. */
+ public shake_camera (intensity?: number, duration?: number): void {
+ this.camera_shake.start(intensity, duration)
+ }
+
public set_zoom_limits (min_distance: number, max_distance: number): void {
this.scene_environment.set_zoom_limits(min_distance, max_distance)
}
@@ -439,6 +447,9 @@ export class Mesh2MotionEngine {
requestAnimationFrame(this.animate)
const delta_time: number = this.clock.getDelta()
+ // camera shake effect
+ this.camera_shake.update(delta_time)
+
// if we are in the animation listing step, we can call
// render/update functions in that
if (this.process_step === ProcessStep.AnimationsListing) {
diff --git a/src/dialog.css b/src/dialog.css
index 0d140f5..6f18d10 100644
--- a/src/dialog.css
+++ b/src/dialog.css
@@ -20,6 +20,7 @@
border-radius: 5px;
text-align: center;
max-width: 80%;
+ box-shadow: 0 0 5rem #0b1d36;
animation: modal-dialog-bounce-in 0.3s ease-out;
}
@@ -135,6 +136,7 @@
border-radius: 4px;
background: var(--bg-primary);
margin: 0;
+ filter: none !important; /* avoid inverting image like we do for icons in light mode */
}
.variation-name {
diff --git a/src/lib/CameraShake.ts b/src/lib/CameraShake.ts
new file mode 100644
index 0000000..31d7ffd
--- /dev/null
+++ b/src/lib/CameraShake.ts
@@ -0,0 +1,64 @@
+import { PerspectiveCamera, Vector3 } from 'three'
+
+/**
+ * Applies a gentle screen-shake to a PerspectiveCamera.
+ * Call `start()` to begin, then `update(delta)` every frame.
+ * The shake decays over the configured duration and restores the
+ * original camera position when finished.
+ */
+export class CameraShake {
+ private readonly camera: PerspectiveCamera
+ private is_shaking: boolean = false
+ private elapsed: number = 0
+ private duration: number = 0
+ private intensity: number = 0
+ private original_position: Vector3 = new Vector3()
+
+ constructor (camera: PerspectiveCamera) {
+ this.camera = camera
+ }
+
+ /**
+ * Begin a shake effect.
+ * @param intensity Maximum offset in world units (default 0.01)
+ * @param duration How long the shake lasts in seconds (default 0.5)
+ */
+ public start (intensity: number = 0.02, duration: number = 0.7): void {
+ this.original_position.copy(this.camera.position)
+ this.intensity = intensity
+ this.duration = duration
+ this.elapsed = 0
+ this.is_shaking = true
+ }
+
+ /** Call once per frame with the frame delta in seconds. */
+ public update (delta: number): void {
+ if (!this.is_shaking) return
+
+ this.elapsed += delta
+ const progress = Math.min(this.elapsed / this.duration, 1)
+
+ if (progress >= 1) {
+ // restore original position and stop
+ this.camera.position.copy(this.original_position)
+ this.is_shaking = false
+ return
+ }
+
+ // decay multiplier so shake eases out
+ const decay = 1 - progress
+ const strength = this.intensity * decay
+
+ // use sine waves at different frequencies per axis for smooth, organic motion
+ const t = this.elapsed * 2 * Math.PI
+ this.camera.position.set(
+ this.original_position.x + Math.sin(t * 3.1) * strength,
+ this.original_position.y + Math.sin(t * 4.7) * strength,
+ this.original_position.z + Math.sin(t * 2.3) * strength
+ )
+ }
+
+ public get active (): boolean {
+ return this.is_shaking
+ }
+}
diff --git a/src/lib/RigConfig.ts b/src/lib/RigConfig.ts
index 6d007e1..69a3fe6 100644
--- a/src/lib/RigConfig.ts
+++ b/src/lib/RigConfig.ts
@@ -79,13 +79,13 @@ export class RigConfig {
model_file: 'models-variation/fox-base.glb',
display_name: 'Fox',
attribution: 'None',
- preview_image: 'images/variations/fox-base.png'
+ preview_image: 'models-variation/fox-base.png'
} satisfies ModelVariation,
{
model_file: 'models-variation/fox-horse.glb',
display_name: 'Horse',
attribution: 'None',
- preview_image: 'images/variations/fox-base.png'
+ preview_image: 'models-variation/fox-base.png'
} satisfies ModelVariation
]
} satisfies RigConfigEntry,
diff --git a/src/marketing/MarketingBootstrap.ts b/src/marketing/MarketingBootstrap.ts
index b178cb6..86f4f30 100644
--- a/src/marketing/MarketingBootstrap.ts
+++ b/src/marketing/MarketingBootstrap.ts
@@ -84,6 +84,8 @@ export class MarketingBootstrap {
// we are re-creating the engine, so need to manually add the event listeners again
this.mesh2motion_engine.load_model_step.addEventListener('modelLoaded', () => {
+ this.mesh2motion_engine.shake_camera()
+
// resolve the rig file path from the central config
const rig_file = RigConfig.rig_file_for(this.skeleton_type)
this.mesh2motion_engine.process_step_changed(ProcessStep.LoadSkeleton)
diff --git a/src/styles.css b/src/styles.css
index 7f88427..96cc35e 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -52,7 +52,7 @@
}
/* SVG inversion for light theme using body class */
-body.light-theme img[src$=".svg"] ,
+body.light-theme img[src$=".svg"],
body.light-theme img[src$=".png"] {
filter: invert(0.7);
}