#162: a load with nothing to step has a pure plugin clock: Time (Time<Virtual> too, which the fixed loop copies back) and the keyed rows are the frame drawn's, f / fps, however it was reached. Since #77 skipped the replay, the clock stepped only on the frame after the last stepped: every still sampled globals.time 0, and a batch's second frame drew the first's <Material> uniforms (keyed rows) and component rows. Measured on a face split into globals.time and a uniform from the frame, as seconds: before, lone 60 = 0 / 1.96, batch 30,90 = 0 / 0.97 and 0 / 0.97; after, lone 60 = 2.01 / 2.01, lone 90 = 3.01 / 3.01, batch = 1.00 / 1.00 and 3.01 / 3.01. A stepping clock is unchanged.

This commit is contained in:
shinyobjectz committed 2026-11-15 20:33:14 -08:00
1 parent 523fd30929
commit cdebceb9d9
4 files changed
+109 -3

No files matched your search

+1 -1
View File
@@ -184,7 +184,7 @@ fn open(names: &[String], fps: f64, serve: Option<u16>) -> R<Peer> {
// no plugin named: the clock still steps by frames
let dt = Duration::from_secs_f64(1.0 / fps.max(1.0));
app.world_mut().resource_mut::<Time<Fixed>>().set_timestep(dt);
app.insert_resource(PluginClock { stepped: 0, dt, advancing: false, steps: 0 });
app.insert_resource(PluginClock { stepped: 0, dt, advancing: false, steps: 0, pure: false });
}
if let Some(port) = serve {
#[cfg(feature = "remote")]
+6
View File
@@ -15,6 +15,12 @@ impl Engine {
}
// kept (and the frames before a frame stepped through: catch_up) only when the rows need steps (#77)
self.replay = (plugins::active(self.app.world()) && plugins::steps(inserted)).then(|| (prog.clone(), inserted.to_vec()));
// without them, the plugins' clock steps nothing: Time and the keyed rows are each frame's however it's reached
// (a still at frame 200, a batch's second frame, a seek), not frozen at the first frame drawn (#162)
let pure = self.replay.is_none();
if let Some(mut c) = self.app.world_mut().get_resource_mut::<plugins::PluginClock>() {
c.pure = pure;
}
self.drawn = None;
let world = self.app.world_mut();
let g = world.resource::<Gen>().0;
+43 -2
View File
@@ -15,6 +15,9 @@
//! by zero. A frame reached by a jump is stepped to through the frames between, and a frame before the last stepped one
//! reloads the edit and steps from 0, so a plugin's state at frame f is always f steps from the load, however f was
//! reached (a render of one frame, a play, a seek).
//!
//! A load whose rows give the plugins nothing to carry from frame to frame (`steps` false) isn't stepped (#77): its
//! clock is `pure`, and Time and the keyed rows are the frame drawn's, f / fps from 0, however it was reached (#162).
use std::any::TypeId;
use std::collections::BTreeMap;
@@ -92,6 +95,11 @@ pub struct PluginClock {
pub dt: Duration,
pub advancing: bool,
pub steps: u64,
/// the load gives the plugins nothing to step (`steps` false): nothing is stepped, and Time and the keyed rows are
/// the frame drawn's however it was reached (pure_time, keyed_rows). Stepping only on the frame after the last
/// stepped one froze them: a still at frame 200 sampled globals.time 0, and a batch's second frame drew the
/// first's <Material> uniforms (#162)
pub pure: bool,
}
/// A mesh whose Transform a plugin owns (sync_meshes leaves it alone).
@@ -200,7 +208,28 @@ fn install_list(app: &mut App, fps: f64, list: Vec<fn(&mut App)>) {
}
app.insert_resource(TimeUpdateStrategy::ManualDuration(Duration::ZERO));
app.world_mut().resource_mut::<Time<Fixed>>().set_timestep(dt);
app.insert_resource(PluginClock { stepped: 0, dt, advancing: false, steps: 0 });
app.insert_resource(PluginClock { stepped: 0, dt, advancing: false, steps: 0, pure: false });
app.add_systems(First, pure_time.after(bevy::time::TimeSystems));
}
/// A pure clock's Time: the frame drawn's, f / fps from 0, whatever was drawn before it (a still alone, a batch's
/// next frame, a seek back in live play); Time<Virtual> too, which the fixed loop copies back into Time. It moves by
/// one step on an update that draws another frame than the last, and by none on the same frame drawn again.
fn pure_time(clock: Option<Res<Clock>>, pc: Option<Res<PluginClock>>, mut virt: ResMut<Time<Virtual>>, mut time: ResMut<Time>, mut last: Local<Option<i64>>) {
let (Some(f), Some(c)) = (clock, pc) else { return };
if !c.pure {
*last = None;
return;
}
let f = f.0.max(0);
let at = c.dt * u32::try_from(f).unwrap_or(u32::MAX);
let step = if *last == Some(f) { Duration::ZERO } else { c.dt.min(at) };
*last = Some(f);
let mut v = Time::<Virtual>::default();
v.advance_by(at - step);
v.advance_by(step);
*time = v.as_generic();
*virt = v;
}
pub fn active(world: &World) -> bool {
@@ -228,6 +257,12 @@ pub fn steps(inserted: &[Tup]) -> bool {
pub fn arm(world: &mut World) {
let f = world.resource::<Clock>().0;
let Some(mut c) = world.get_resource_mut::<PluginClock>() else { return };
if c.pure {
// nothing stepped: Time is the frame's (pure_time), and its real clock doesn't move
c.advancing = false;
world.insert_resource(TimeUpdateStrategy::ManualDuration(Duration::ZERO));
return;
}
let step = f == c.stepped + 1;
c.advancing = step;
if std::env::var_os("RAEA_PLUGIN_TRACE").is_some() {
@@ -244,12 +279,18 @@ pub fn arm(world: &mut World) {
}
/// Keyed rows whose frame has come, set on the update that draws it (so a plugin's steps after it see them): with a
/// plugin, on the frame its clock steps to; without one, on the frame drawn, whichever way it was reached.
/// plugin that steps, on the frame its clock steps to; without one, or with a pure clock, on the frame drawn, whichever
/// way it was reached.
/// A live engine (a fed render's, live play's) takes them too: its frames' rows set the values that change
/// (apply_live_rows), but a row held for its `from` never changes after the load, so nothing else sets it (a render to
/// MP4 left a rigid body from frame 36 asleep for good). Each lands once, on its frame.
fn keyed_rows(world: &mut World) {
let f = match world.get_resource::<PluginClock>() {
// a pure clock steps nothing: the frame drawn's rows, as without a plugin (#162)
Some(c) if c.pure => match world.get_resource::<Clock>() {
Some(c) => c.0,
None => return,
},
Some(c) if !c.advancing => return,
Some(c) => c.stepped,
None => match world.get_resource::<Clock>() {
+59
View File
@@ -145,3 +145,62 @@ fn only_rows_a_plugin_carries_from_frame_to_frame_need_the_frames_before_a_still
assert!(steps(&rows), "{more:?} needs its frames stepped");
}
}
/// A load with nothing to step (#77) has a pure clock: its keyed rows land on the frame drawn, not frozen until a step
/// that never comes. A still at frame 200 alone drew none, and a batch's second frame drew the first's (#162)
#[test]
fn a_pure_clock_takes_each_frames_keyed_rows_however_it_was_reached() {
let mut app = app();
let w = app.world_mut();
let a = w.spawn_empty().id();
let ty = "bevy_transform::components::transform::Transform".to_string();
let rows = [0, 60, 200].map(|f| (Some(a), ty.clone(), json!({"translation": [f as f64, 0.0, 0.0]}), f)).to_vec();
w.insert_resource(Keyed { rows, ..default() });
w.insert_resource(Error(None));
w.insert_resource(PluginClock { stepped: 0, dt: Duration::from_secs_f64(1.0 / 30.0), advancing: false, steps: 0, pure: true });
let x = |w: &World| w.get::<Transform>(a).map(|t| t.translation.x);
for (f, want) in [(200, 200.0), (60, 60.0), (61, 60.0), (200, 200.0), (0, 0.0)] {
w.insert_resource(Clock(f));
arm(w);
keyed_rows(w);
assert_eq!(x(w), Some(want), "frame {f}");
}
// a clock that steps still holds them for its step (a plugin's state needs them on the update it steps)
w.resource_mut::<PluginClock>().pure = false;
w.insert_resource(Clock(200));
arm(w);
keyed_rows(w);
assert_eq!(x(w), Some(0.0), "held: 200 isn't the frame after the last stepped");
}
/// A pure clock's Time is the frame's, f / fps, in any order: what globals.time and a shader's animation read (#162)
#[test]
fn a_pure_clocks_time_is_the_frame_drawns() {
let mut app = App::new();
app.add_plugins(bevy::time::TimePlugin);
app.insert_resource(TimeUpdateStrategy::ManualDuration(Duration::ZERO));
app.add_systems(First, pure_time.after(bevy::time::TimeSystems));
// what reads Time in Update, after the fixed loop has copied Time<Virtual> back into it
#[derive(Resource, Default)]
struct Seen(Vec<(f64, f64)>);
app.init_resource::<Seen>();
app.add_systems(Update, |t: Res<Time>, mut s: ResMut<Seen>| s.0.push((t.elapsed_secs_f64(), t.delta_secs_f64())));
let dt = Duration::from_secs_f64(1.0 / 30.0);
app.insert_resource(PluginClock { stepped: 0, dt, advancing: false, steps: 0, pure: true });
fn at(app: &mut App, f: i64) -> (f64, f64) {
app.insert_resource(Clock(f));
arm(app.world_mut());
app.update();
*app.world().resource::<Seen>().0.last().unwrap()
}
let near = |(t, d): (f64, f64), want: (f64, f64)| (t - want.0).abs() < 1e-6 && (d - want.1).abs() < 1e-6;
let step = 1.0 / 30.0;
for (f, want) in [(60, (2.0, step)), (60, (2.0, 0.0)), (200, (200.0 / 30.0, step)), (61, (61.0 / 30.0, step)), (0, (0.0, 0.0)), (1, (step, step))] {
let got = at(&mut app, f);
assert!(near(got, want), "frame {f}: Time {got:?}, want {want:?}");
}
// a clock that steps is left to its steps
app.world_mut().resource_mut::<PluginClock>().pure = false;
let held = at(&mut app, 500);
assert!(held.0 < 1.0, "a stepping clock's Time isn't set to the frame's: {held:?}");
}