Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Camera cuts: a camera that jumps drops its history, and motion blur skips the jump

shinyobjectz2929ce6a04b4
diff --git a/engine/render/src/engine.rs b/engine/render/src/engine.rs
index f36337d8..b796c66c 100644
--- a/engine/render/src/engine.rs
+++ b/engine/render/src/engine.rs
@@ -87,6 +87,7 @@ use raea_sys_shader as shader_ext;
use raea_sys_post::lens;
// the camera's motion blur (<MotionBlur>), a function of the frame
use raea_sys_post::motion_blur;
+use raea_sys_post::cut;
// Bevy's depth of field, patched to keep the frame's alpha (the paint look's sky)
use raea_sys_post::dof;
use raea_sys_post::effect_stack;
diff --git a/engine/render/src/engine/open.rs b/engine/render/src/engine/open.rs
index 4259b8fd..53346751 100644
--- a/engine/render/src/engine/open.rs
+++ b/engine/render/src/engine/open.rs
@@ -65,6 +65,7 @@ impl Engine {
#[cfg(feature = "raytracing")]
if raea_render_raytrace::wanted(prog) {
raea_render_raytrace::add(&mut app);
+ app.add_systems(PostUpdate, solari_cut.after(raea_sys_post::cut::DetectCuts));
}
app
.init_resource::<Clock>()
@@ -279,3 +280,13 @@ impl Engine {
}
}
+/// A camera that jumped (raea-sys-post cut.rs, RT31): Solari's history starts again, and with it MetalFX's denoiser's,
+/// its scaler's and emit_taa's, which follow Solari's reset.
+#[cfg(feature = "raytracing")]
+fn solari_cut(mut cams: Query<(&raea_sys_post::cut::CameraCut, &mut bevy::solari::prelude::SolariLighting)>) {
+ for (cut, mut s) in &mut cams {
+ if cut.0 {
+ s.reset = true;
+ }
+ }
+}
diff --git a/engine/render/src/engine/raea_plugins.rs b/engine/render/src/engine/raea_plugins.rs
index 21c524bb..8633770d 100644
--- a/engine/render/src/engine/raea_plugins.rs
+++ b/engine/render/src/engine/raea_plugins.rs
@@ -75,6 +75,7 @@ raea_plugin! {
PhaseItemPlugin: raea_sys_phase::install, "Your own draws in a camera's opaque phase, as Bevy's custom_phase_item queues one (raea-sys-phase).";
LensPlugin: super::lens::install, "The camera's lens (Fisheye, LensDistortion), after tone mapping (lens.rs).";
ParityPlugin: raea_sys_parity::install, "The parity probe: a frame's cost and World, written when RAEA_PARITY is set (raea-sys-parity, docs/control.feature FC0a).";
+ CameraCutPlugin: super::cut::install, "A camera that jumps is a cut: its history starts again and its motion blur waits (cut.rs, RT31).";
MotionBlurPlugin: super::motion_blur::install, "Motion blur's frame counter pinned to the frame (motion_blur.rs).";
}
diff --git a/systems/post/src/cut.rs b/systems/post/src/cut.rs
new file mode 100644
index 00000000..635cd431
--- /dev/null
+++ b/systems/post/src/cut.rs
@@ -0,0 +1,157 @@
+//! A camera that jumps is a cut (docs/raytracing.feature RT31). A cut to another camera is marked (raea-sys-frame's
+//! Shots, RR2), but one camera moved far in one frame, as a game's respawn or teleport moves it, wasn't: Solari's and
+//! MetalFX's history carried the last view into the new one for half a second (lightbench window-cut: the worst tile
+//! 0.50 stops off on the first frame, 0.38 on the third, against 0.14 and 0.12 with the history dropped), steady's
+//! filter blended it in, and motion blur streaked every pixel along the jump.
+//!
+//! A jump is a turn of more than half the field of view in one frame, a lens changed by half or more, or a step ten
+//! times the camera's recent pace and longer than ten near planes. So a fast, steady move (a flight, a whip pan eased
+//! in, a player's flick of 90 degrees in a quarter second) is no cut, and a teleport from rest is. A camera's first move
+//! since it appeared (or cut) is a shot opening on a move, no step cut, unless it was seen resting first, unmoved from
+//! one film frame to the next twice: so a shot opening on a fast move keeps its blur, as a frame drawn alone (`--at`,
+//! where the engine loads the frame's pose at frame 0 and then steps from the frame before) does.
+
+use bevy::{
+ prelude::*,
+ render::extract_component::{ExtractComponent, ExtractComponentPlugin},
+};
+
+/// Whether the camera jumped this frame, set on each 3D camera every frame: its temporal history starts again (Solari's,
+/// MetalFX's, steady's) and its motion blur draws nothing.
+#[derive(Component, Clone, Copy, Default, PartialEq, Debug, ExtractComponent)]
+#[extract_app(bevy::render::RenderApp)]
+pub struct CameraCut(pub bool);
+
+/// a camera's pose and lens at its last update, and that update's film frame; how far it has been stepping a frame (an
+/// average over its last few moves), once it has moved since it appeared or last cut (`paced`); and for how many film
+/// frames in a row it has been seen unmoved (`still`)
+#[derive(Component, Clone, Copy, Debug)]
+struct Pace {
+ at: Vec3,
+ rot: Quat,
+ fov: f32,
+ frame: i64,
+ step: f32,
+ paced: bool,
+ still: u32,
+}
+
+/// the lens's field of view (radians; an orthographic camera's counted as 45 degrees), and the shortest step that can be
+/// a cut however still the camera was (ten near planes, or half an orthographic view's height)
+fn lens(p: &Projection) -> (f32, f32) {
+ match p {
+ Projection::Perspective(p) => (p.fov, 10.0 * p.near),
+ Projection::Orthographic(o) => (std::f32::consts::FRAC_PI_4, 0.5 * o.area.height()),
+ _ => (std::f32::consts::FRAC_PI_4, 1.0),
+ }
+}
+
+/// Whether a move from `was` to (`at`, `rot`, `fov`) is a cut, `least` the shortest step that can be one.
+fn jumped(was: &Pace, at: Vec3, rot: Quat, fov: f32, least: f32) -> bool {
+ let step = at.distance(was.at);
+ let turn = was.rot.angle_between(rot);
+ let lens = (fov / was.fov).max(was.fov / fov);
+ let opening = !was.paced && was.still < 2;
+ turn > 0.5 * fov || lens > 1.5 || (!opening && step > 10.0 * was.step && step > least)
+}
+
+/// Where each camera's CameraCut is set, in PostUpdate after the transforms: a system that acts on a cut runs after it.
+#[derive(SystemSet, Clone, PartialEq, Eq, Hash, Debug)]
+pub struct DetectCuts;
+
+pub fn install(app: &mut App) {
+ app.add_plugins(ExtractComponentPlugin::<CameraCut>::default());
+ app.add_systems(PostUpdate, detect.in_set(DetectCuts).after(bevy::transform::TransformSystems::Propagate));
+}
+
+/// Each 3D camera's CameraCut this frame (the film frame, raea-sys-frame's Clock; an engine without one counts its
+/// updates). An update that leaves a camera where it was (motion blur's step to the frame before draws a frame's pose
+/// twice) is no cut and doesn't count toward its pace; a cut's step doesn't either.
+fn detect(
+ mut commands: Commands,
+ clock: Option<Res<raea_sys_frame::Clock>>,
+ mut updates: Local<i64>,
+ mut cams: Query<(Entity, &GlobalTransform, &Projection, Option<&mut Pace>, Option<&mut CameraCut>), With<Camera3d>>,
+) {
+ let frame = clock.map_or(*updates, |c| c.0);
+ *updates += 1;
+ for (e, t, p, pace, cut) in &mut cams {
+ let (_, rot, at) = t.to_scale_rotation_translation();
+ let (fov, least) = lens(p);
+ let now = match pace {
+ None => {
+ commands.entity(e).insert(Pace { at, rot, fov, frame, step: 0.0, paced: false, still: 0 });
+ false
+ }
+ Some(mut was) => {
+ let moved = was.at != at || was.rot != rot || was.fov != fov;
+ let now = moved && jumped(&was, at, rot, fov, least);
+ if now {
+ *was = Pace { at, rot, fov, frame, step: 0.0, paced: false, still: 0 };
+ } else if moved {
+ let d = at.distance(was.at);
+ let step = if was.paced { 0.75 * was.step + 0.25 * d } else { d };
+ *was = Pace { at, rot, fov, frame, step, paced: true, still: 0 };
+ } else if frame != was.frame {
+ // unmoved from one film frame to the next, or seen again after a gap (a frame drawn alone)
+ was.still = if frame == was.frame + 1 { was.still + 1 } else { 0 };
+ was.frame = frame;
+ }
+ now
+ }
+ };
+ match cut {
+ Some(mut c) if c.0 != now => c.0 = now,
+ Some(_) => {}
+ None => {
+ commands.entity(e).insert(CameraCut(now));
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const FOV: f32 = 44.0 * std::f32::consts::PI / 180.0;
+
+ /// a camera seen unmoved for ten frames
+ fn at_rest() -> Pace {
+ Pace { at: Vec3::ZERO, rot: Quat::IDENTITY, fov: FOV, frame: 10, step: 0.0, paced: false, still: 10 }
+ }
+
+ #[test]
+ fn a_teleport_is_a_cut_and_a_first_step_is_not() {
+ assert!(jumped(&at_rest(), Vec3::new(5.0, 0.0, 0.0), Quat::IDENTITY, FOV, 1.0));
+ assert!(!jumped(&at_rest(), Vec3::new(0.02, 0.0, 0.0), Quat::IDENTITY, FOV, 1.0));
+ }
+
+ #[test]
+ fn a_shot_that_opens_on_a_fast_move_is_no_cut() {
+ let new = Pace { still: 0, ..at_rest() };
+ assert!(!jumped(&new, Vec3::new(2.0, 0.0, 0.0), Quat::IDENTITY, FOV, 1.0));
+ // a turn past half the view still is
+ assert!(jumped(&new, Vec3::ZERO, Quat::from_rotation_y(1.0), FOV, 1.0));
+ }
+
+ #[test]
+ fn a_steady_fast_flight_is_no_cut() {
+ let flying = Pace { step: 4.0, paced: true, ..at_rest() };
+ assert!(!jumped(&flying, Vec3::new(4.5, 0.0, 0.0), Quat::IDENTITY, FOV, 1.0));
+ }
+
+ #[test]
+ fn a_turn_past_half_the_view_is_a_cut_and_a_flick_is_not() {
+ let deg = |d: f32| Quat::from_rotation_y(d.to_radians());
+ assert!(jumped(&at_rest(), Vec3::ZERO, deg(30.0), FOV, 1.0));
+ // 90 degrees in 15 frames, eased: 9 degrees at its fastest
+ assert!(!jumped(&at_rest(), Vec3::ZERO, deg(9.0), FOV, 1.0));
+ }
+
+ #[test]
+ fn another_lens_is_a_cut() {
+ assert!(jumped(&at_rest(), Vec3::ZERO, Quat::IDENTITY, FOV / 2.0, 1.0));
+ assert!(!jumped(&at_rest(), Vec3::ZERO, Quat::IDENTITY, FOV * 1.1, 1.0));
+ }
+}
diff --git a/systems/post/src/lib.rs b/systems/post/src/lib.rs
index 45869aea..3ab20f18 100644
--- a/systems/post/src/lib.rs
+++ b/systems/post/src/lib.rs
@@ -15,6 +15,8 @@ pub mod views;
pub mod motion_blur;
// raea's motion blur: the shutter's own (docs/raytracing.feature RT29)
pub mod shutter;
+// a camera that jumps is a cut: history starts again, motion blur waits (docs/raytracing.feature RT31)
+pub mod cut;
pub mod dof;
pub mod effect_stack;
pub mod outline;
diff --git a/systems/post/src/shutter.rs b/systems/post/src/shutter.rs
index e887cf3d..d64fdd5b 100644
--- a/systems/post/src/shutter.rs
+++ b/systems/post/src/shutter.rs
@@ -28,6 +28,8 @@ use bevy::{
},
};
+use crate::cut::CameraCut;
+
/// A camera drawn with its shutter open for `open` of each frame, gathered by `taps` taps a pixel. Brings the depth
/// and motion prepasses it reads.
#[derive(Component, Clone, Copy, PartialEq, Debug, ExtractComponent)]
@@ -163,9 +165,13 @@ fn pass(ctx: &mut RenderContext, label: &'static str, out: &TextureView, rp: &Re
pass.draw(0..3, 0..1);
}
-fn draw(view: ViewQuery<(&ExtractedCamera, &ViewTarget, &ViewPrepassTextures, &ShutterFrame, &Msaa)>, pipeline: Option<Res<ShutterPipeline>>, cache: Res<PipelineCache>, mut ctx: RenderContext) {
+fn draw(view: ViewQuery<(&ExtractedCamera, &ViewTarget, &ViewPrepassTextures, &ShutterFrame, &Msaa, Option<&CameraCut>)>, pipeline: Option<Res<ShutterPipeline>>, cache: Res<PipelineCache>, mut ctx: RenderContext) {
let Some(p) = pipeline else { return };
- let (camera, target, prepass, f, msaa) = view.into_inner();
+ let (camera, target, prepass, f, msaa, cut) = view.into_inner();
+ // a cut's motion is the jump, not anything the shutter saw (cut.rs)
+ if cut.is_some_and(|c| c.0) {
+ return;
+ }
let r = if msaa.samples() > 1 { &p.multi } else { &p.single };
let id = if target.main_texture_format() == super::bevy_api::HDR_FORMAT { r.hdr } else { r.ldr };
let (Some(rows), Some(longest), Some(near), Some(gather), Some(motion), Some(depth)) = (
diff --git a/systems/post/src/steady.rs b/systems/post/src/steady.rs
index c17fc8ba..de947baa 100644
--- a/systems/post/src/steady.rs
+++ b/systems/post/src/steady.rs
@@ -87,18 +87,18 @@ fn env(name: &str, default: f32) -> f32 {
}
/// Each steadied view's histories, swapped each frame, and its parameters: the slowest and fastest rates a frame, the
-/// motion in pixels where the fastest is reached, and whether the history starts again (a view's first frame, or a
-/// new size).
+/// motion in pixels where the fastest is reached, and whether the history starts again (a view's first frame, a new
+/// size, or a cut: cut.rs).
fn textures(
mut commands: Commands,
mut cache: ResMut<TextureCache>,
device: Res<RenderDevice>,
frame: Res<FrameCount>,
- views: Query<(Entity, &ExtractedCamera), With<Steady>>,
+ views: Query<(Entity, &ExtractedCamera, Option<&crate::cut::CameraCut>), With<Steady>>,
mut seen: Local<HashMap<Entity, UVec2>>,
) {
let (still, moving, px) = (1.0 / env("RAEA_STEADY_STILL", 32.0), 1.0 / env("RAEA_STEADY_MOVING", 8.0), env("RAEA_STEADY_PX", 4.0));
- for (e, camera) in &views {
+ for (e, camera, cut) in &views {
let Some(size) = camera.physical_target_size else { continue };
let desc = |label: &'static str| TextureDescriptor {
label: Some(label),
@@ -112,7 +112,7 @@ fn textures(
};
let (a, b) = (cache.get(&device, desc("steady_a")), cache.get(&device, desc("steady_b")));
let (read, write) = if frame.0 % 2 == 0 { (b, a) } else { (a, b) };
- let reset = seen.insert(e, size) != Some(size);
+ let reset = seen.insert(e, size) != Some(size) || cut.is_some_and(|c| c.0);
let words: Vec<u8> = [still.to_bits(), moving.to_bits(), px.to_bits(), reset as u32].iter().flat_map(|w| w.to_le_bytes()).collect();
let params = device.create_buffer_with_data(&BufferInitDescriptor { label: Some("steady_params"), contents: &words, usage: BufferUsages::UNIFORM });
commands.entity(e).insert(SteadyFrame { read, write, params });