Skip to main content

cranpose_liquid/
dynamics.rs

1//! Water-droplet motion physics shared by every travelling liquid lens.
2//!
3//! The reference lens (example/iphone17_records, example/target/tab-swipe)
4//! deforms with its motion, not with its position: cruising speed stretches
5//! the bubble along the travel axis, acceleration compresses it against the
6//! push, deceleration releases that compression past neutral and swells the
7//! leading edge — and whatever one axis does, the orthogonal axis does in
8//! reverse so the droplet keeps its area (an incompressible bubble).
9//!
10//! [`LiquidDynamics`] is the one frame integrator implementing that law.
11//! Widgets feed it the lens ride position once per drawn frame (from inside
12//! their `glass_effect_with` closure) and map the returned [`LiquidPose`]
13//! onto the morph uniforms. Time comes from the runtime's animation clock —
14//! never wall time — so poses stay exact under robot keyframe captures and
15//! on wasm.
16//!
17//! Every channel relaxes exponentially, so none of them ever reaches
18//! neutral on its own. Once travel, strain and swell are all within a
19//! fraction of a device pixel of rest, the integrator snaps them to exactly
20//! [`LiquidPose::default`] and stops producing new poses, so a settled lens
21//! costs nothing per frame.
22
23use std::{cell::Cell, rc::Rc};
24
25use cranpose_core::{RuntimeHandle, with_current_composer};
26use cranpose_macros::composable;
27
28use crate::material::GlassDeformation;
29
30const STRETCH_PER_SPEED: f32 = 3.2e-4;
31const STRETCH_PER_ACCEL: f32 = 3.5e-5;
32/// The droplet never deforms past these bounds, however violent the fling.
33/// Public so hosting nodes can budget layout headroom for the extremes.
34pub const STRETCH_MIN: f32 = 0.78;
35pub const STRETCH_MAX: f32 = 1.50;
36const BULGE_PER_ACCELERATION: f32 = 4.5e-4;
37pub const BULGE_MAX: f32 = 8.0;
38const ATTACK_TAU: f32 = 0.03;
39const RELEASE_TAU: f32 = 0.11;
40const POINTER_VELOCITY_TAU: f32 = 0.045;
41const POINTER_STOP_HORIZON_NANOS: u64 = 40_000_000;
42const POINTER_COAST_TAU: f32 = 0.10;
43const AXIS_MIN_SPEED: f32 = 60.0;
44const REST_SPEED: f32 = 1.0;
45const REST_STRETCH: f32 = 5.0e-4;
46const REST_BULGE: f32 = 1.0e-2;
47const DT_MIN: f32 = 1.0 / 1000.0;
48const DT_MAX: f32 = 1.0 / 15.0;
49const TELEPORT_SPEED: f32 = 30_000.0;
50
51/// Motion-derived deformation of a liquid lens for one frame.
52#[derive(Clone, Copy, Debug, PartialEq)]
53pub struct LiquidPose {
54    /// Scale along the motion axis (>1 = elongated, <1 = compressed).
55    pub stretch: f32,
56    /// Scale across the motion axis; always `1/stretch` (area conserved).
57    pub ortho: f32,
58    /// Unit motion direction (math convention, +x right / +y up in caller
59    /// space — callers feed whatever space they draw in).
60    pub axis: (f32, f32),
61    /// Leading-edge swell amplitude (dp) toward `bulge_direction` (radians).
62    pub bulge_amplitude: f32,
63    pub bulge_direction: f32,
64    /// Smoothed travel speed (dp/s) for auxiliary channels (surface depth,
65    /// wobble, chromatic fringe).
66    pub speed: f32,
67}
68
69impl Default for LiquidPose {
70    fn default() -> Self {
71        Self {
72            stretch: 1.0,
73            ortho: 1.0,
74            axis: (1.0, 0.0),
75            bulge_amplitude: 0.0,
76            bulge_direction: 0.0,
77            speed: 0.0,
78        }
79    }
80}
81
82impl LiquidPose {
83    /// The full rotating strain tensor for the shader. Applying this to the
84    /// signed-distance field preserves area for horizontal, vertical and
85    /// diagonal travel; projecting it into an axis-aligned width/height pair
86    /// cannot preserve that invariant away from the cardinal axes.
87    pub fn deformation(&self) -> GlassDeformation {
88        GlassDeformation::incompressible(self.axis, self.stretch)
89    }
90
91    /// Normalized motion energy in 0..1 (`speed` against a reference fling
92    /// of ~1100 dp/s) — the shared ramp for speed-driven side channels.
93    pub fn energy(&self) -> f32 {
94        (self.speed / 1100.0).clamp(0.0, 1.0)
95    }
96}
97
98/// Per-lens frame integrator. Interior-mutable so per-frame draw closures
99/// (plain `Fn`) can advance it without a `RefCell` dance.
100pub struct LiquidDynamics {
101    runtime: RuntimeHandle,
102    last_nanos: Cell<Option<u64>>,
103    last_pos: Cell<Option<(f32, f32)>>,
104    velocity: Cell<(f32, f32)>,
105    stretch: Cell<f32>,
106    bulge_vector: Cell<(f32, f32)>,
107    speed: Cell<f32>,
108    axis: Cell<(f32, f32)>,
109    pose: Cell<LiquidPose>,
110    pointer_pose_pending: Cell<bool>,
111    pointer_active: Cell<bool>,
112    last_pointer_nanos: Cell<Option<u64>>,
113}
114
115impl LiquidDynamics {
116    pub fn new(runtime: RuntimeHandle) -> Self {
117        Self {
118            runtime,
119            last_nanos: Cell::new(None),
120            last_pos: Cell::new(None),
121            velocity: Cell::new((0.0, 0.0)),
122            stretch: Cell::new(1.0),
123            bulge_vector: Cell::new((0.0, 0.0)),
124            speed: Cell::new(0.0),
125            axis: Cell::new((1.0, 0.0)),
126            pose: Cell::new(LiquidPose::default()),
127            pointer_pose_pending: Cell::new(false),
128            pointer_active: Cell::new(false),
129            last_pointer_nanos: Cell::new(None),
130        }
131    }
132
133    /// Forget all motion state (fresh grab, teleporting relayout): the next
134    /// update re-anchors at rest.
135    pub fn reset(&self) {
136        self.last_nanos.set(None);
137        self.last_pos.set(None);
138        self.velocity.set((0.0, 0.0));
139        self.stretch.set(1.0);
140        self.bulge_vector.set((0.0, 0.0));
141        self.speed.set(0.0);
142        self.pose.set(LiquidPose {
143            axis: self.axis.get(),
144            ..LiquidPose::default()
145        });
146        self.pointer_pose_pending.set(false);
147        self.pointer_active.set(false);
148        self.last_pointer_nanos.set(None);
149    }
150
151    pub(crate) fn anchor_pointer(&self, pos: (f32, f32)) {
152        self.reset();
153        self.last_pos.set(Some(pos));
154        let now = self.runtime.last_frame_time_nanos();
155        self.last_nanos.set(now);
156        self.last_pointer_nanos.set(now);
157        self.pointer_active.set(true);
158    }
159
160    pub(crate) fn advance_pointer(&self, pos: (f32, f32), dt: f32) -> LiquidPose {
161        let Some(last_pos) = self.last_pos.get() else {
162            self.last_pos.set(Some(pos));
163            return self.pose.get();
164        };
165        let dt = dt.clamp(DT_MIN, DT_MAX);
166        let raw_velocity = ((pos.0 - last_pos.0) / dt, (pos.1 - last_pos.1) / dt);
167        self.last_pos.set(Some(pos));
168        let previous = self.velocity.get();
169        let follow = 1.0 - (-dt / POINTER_VELOCITY_TAU).exp();
170        let filtered_velocity = (
171            previous.0 + (raw_velocity.0 - previous.0) * follow,
172            previous.1 + (raw_velocity.1 - previous.1) * follow,
173        );
174        let pose = self.advance_velocity(filtered_velocity, dt);
175        let now = self.runtime.last_frame_time_nanos();
176        self.last_nanos.set(now);
177        self.last_pointer_nanos.set(now);
178        self.pointer_active.set(true);
179        self.pointer_pose_pending.set(true);
180        pose
181    }
182
183    pub(crate) fn release_pointer(&self) {
184        self.pointer_active.set(false);
185    }
186
187    pub(crate) fn update_pointer(&self, pos: (f32, f32)) -> LiquidPose {
188        if self.pointer_pose_pending.replace(false) {
189            self.last_pos.set(Some(pos));
190            self.last_nanos.set(self.runtime.last_frame_time_nanos());
191            return self.pose.get();
192        }
193        let Some(now) = self.runtime.last_frame_time_nanos() else {
194            self.last_pos.set(Some(pos));
195            return self.pose.get();
196        };
197        let Some(last) = self.last_nanos.get() else {
198            self.last_nanos.set(Some(now));
199            self.last_pos.set(Some(pos));
200            return self.pose.get();
201        };
202        if last == now {
203            return self.pose.get();
204        }
205        let dt = (now.saturating_sub(last)) as f32 / 1_000_000_000.0;
206        self.last_nanos.set(Some(now));
207        let stationary = self.last_pos.get().is_some_and(|last_pos| {
208            (last_pos.0 - pos.0).abs() < 0.001 && (last_pos.1 - pos.1).abs() < 0.001
209        });
210        if !stationary {
211            return self.advance(pos, dt);
212        }
213        let within_pointer_horizon = self.pointer_active.get()
214            && self
215                .last_pointer_nanos
216                .get()
217                .is_some_and(|sample| now.saturating_sub(sample) <= POINTER_STOP_HORIZON_NANOS);
218        if within_pointer_horizon {
219            return self.pose.get();
220        }
221        let dt = dt.clamp(DT_MIN, DT_MAX);
222        let decay = (-dt / POINTER_COAST_TAU).exp();
223        let velocity = self.velocity.get();
224        self.advance_velocity((velocity.0 * decay, velocity.1 * decay), dt)
225    }
226
227    /// Advance with the lens ride position using the runtime's animation
228    /// clock. Multiple reads within one frame return the same pose.
229    pub fn update(&self, pos: (f32, f32)) -> LiquidPose {
230        let Some(now) = self.runtime.last_frame_time_nanos() else {
231            self.last_pos.set(Some(pos));
232            return self.pose.get();
233        };
234        match self.last_nanos.get() {
235            Some(last) if last == now => {
236                self.last_pos.set(Some(pos));
237                self.pose.get()
238            }
239            Some(last) => {
240                let dt = (now.saturating_sub(last)) as f32 / 1_000_000_000.0;
241                self.last_nanos.set(Some(now));
242                self.advance(pos, dt)
243            }
244            None => {
245                self.last_nanos.set(Some(now));
246                self.last_pos.set(Some(pos));
247                self.pose.get()
248            }
249        }
250    }
251
252    /// Pure integration step (exposed for tests and custom clocks): advance
253    /// by `dt` seconds toward `pos`.
254    pub fn advance(&self, pos: (f32, f32), dt: f32) -> LiquidPose {
255        let Some(last_pos) = self.last_pos.get() else {
256            self.last_pos.set(Some(pos));
257            return self.pose.get();
258        };
259        let dt = dt.clamp(DT_MIN, DT_MAX);
260        let delta = (pos.0 - last_pos.0, pos.1 - last_pos.1);
261        self.last_pos.set(Some(pos));
262
263        let velocity = (delta.0 / dt, delta.1 / dt);
264        let raw_speed = (velocity.0 * velocity.0 + velocity.1 * velocity.1).sqrt();
265        if raw_speed > TELEPORT_SPEED {
266            self.velocity.set((0.0, 0.0));
267            return self.pose.get();
268        }
269
270        self.advance_velocity(velocity, dt)
271    }
272
273    fn advance_velocity(&self, velocity: (f32, f32), dt: f32) -> LiquidPose {
274        let dt = dt.clamp(DT_MIN, DT_MAX);
275        let raw_speed = (velocity.0 * velocity.0 + velocity.1 * velocity.1).sqrt();
276        let previous_velocity = self.velocity.get();
277        self.velocity.set(velocity);
278        let accel = (
279            (velocity.0 - previous_velocity.0) / dt,
280            (velocity.1 - previous_velocity.1) / dt,
281        );
282
283        let mut axis = self.axis.get();
284        if raw_speed > AXIS_MIN_SPEED {
285            axis = (velocity.0 / raw_speed, velocity.1 / raw_speed);
286            self.axis.set(axis);
287        }
288        let accel_along = accel.0 * axis.0 + accel.1 * axis.1;
289
290        let target_stretch = (1.0 + STRETCH_PER_SPEED * raw_speed
291            - STRETCH_PER_ACCEL * accel_along)
292            .clamp(STRETCH_MIN, STRETCH_MAX);
293        let signed_bulge = (-BULGE_PER_ACCELERATION * accel_along).clamp(-BULGE_MAX, BULGE_MAX);
294        let target_bulge = (axis.0 * signed_bulge, axis.1 * signed_bulge);
295
296        let follow = |current: f32, target: f32, neutral: f32| {
297            let tau = if (target - neutral).abs() > (current - neutral).abs() {
298                ATTACK_TAU
299            } else {
300                RELEASE_TAU
301            };
302            current + (target - current) * (1.0 - (-dt / tau).exp())
303        };
304        let stretch = follow(self.stretch.get(), target_stretch, 1.0);
305        let current_bulge = self.bulge_vector.get();
306        let current_bulge_length = current_bulge.0.hypot(current_bulge.1);
307        let target_bulge_length = target_bulge.0.hypot(target_bulge.1);
308        let bulge_tau = if target_bulge_length > current_bulge_length {
309            ATTACK_TAU
310        } else {
311            RELEASE_TAU
312        };
313        let bulge_follow = 1.0 - (-dt / bulge_tau).exp();
314        let bulge_vector = (
315            current_bulge.0 + (target_bulge.0 - current_bulge.0) * bulge_follow,
316            current_bulge.1 + (target_bulge.1 - current_bulge.1) * bulge_follow,
317        );
318        let speed = follow(self.speed.get(), raw_speed, 0.0);
319        let at_rest = raw_speed <= REST_SPEED
320            && speed <= REST_SPEED
321            && (stretch - 1.0).abs() <= REST_STRETCH
322            && bulge_vector.0.hypot(bulge_vector.1) <= REST_BULGE;
323        let (stretch, bulge_vector, speed) = if at_rest {
324            (1.0, (0.0, 0.0), 0.0)
325        } else {
326            (stretch, bulge_vector, speed)
327        };
328        let bulge = bulge_vector.0.hypot(bulge_vector.1);
329        self.stretch.set(stretch);
330        self.bulge_vector.set(bulge_vector);
331        self.speed.set(speed);
332
333        let bulge_direction = if bulge > 1.0e-4 {
334            bulge_vector.1.atan2(bulge_vector.0)
335        } else {
336            axis.1.atan2(axis.0)
337        };
338
339        let pose = LiquidPose {
340            stretch,
341            ortho: 1.0 / stretch,
342            axis,
343            bulge_amplitude: bulge,
344            bulge_direction,
345            speed,
346        };
347        self.pose.set(pose);
348        pose
349    }
350
351    /// Latest pose without advancing.
352    pub fn pose(&self) -> LiquidPose {
353        self.pose.get()
354    }
355}
356
357/// Remember one [`LiquidDynamics`] for the calling composition site.
358#[composable]
359#[track_caller]
360pub fn rememberLiquidDynamics() -> Rc<LiquidDynamics> {
361    let caller = cranpose_core::caller_location_key();
362    with_current_composer(|composer| {
363        let runtime = composer.runtime_handle();
364        composer
365            .remember_at(caller, move || Rc::new(LiquidDynamics::new(runtime)))
366            .with(Rc::clone)
367    })
368}
369
370#[cfg(test)]
371#[path = "tests/dynamics_tests.rs"]
372mod tests;