diff --git a/runtime/look/src/rendercam.rs b/runtime/look/src/rendercam.rs index 32e2177f..9ad5669c 100644 --- a/runtime/look/src/rendercam.rs +++ b/runtime/look/src/rendercam.rs @@ -25,6 +25,10 @@ pub fn render_cameras(all: &[Vec], tracks: &[(String, Vec>)], p.keys.insert((id.clone(), "on".into()), step_keys(&on)); } row.insert("tonemap".into(), V::S(first.get("tonemap").and_then(Value::as_str).unwrap_or("none").to_string())); + // its own aspect where its image's shape isn't it (three draws a projection into any target; matchmove's OffAxis) + if let Some(a) = first.get("aspect").and_then(Value::as_f64) { + row.insert("aspect".into(), V::N(a)); + } ambient_columns(all, p, &mut row, &id, &layers_of(first)); rows.push(row); } diff --git a/sdk/r3f/src/input/passes.ts b/sdk/r3f/src/input/passes.ts index 429fc358..a09e44eb 100644 --- a/sdk/r3f/src/input/passes.ts +++ b/sdk/r3f/src/input/passes.ts @@ -50,6 +50,16 @@ function intoTarget(p: Part): Part { * shader places the quad itself, so any camera that sees the quad at z 0 will do */ const QUAD: Part = { type: "OrthographicCamera", props: { position: [0, 0, 1], lookAt: [0, 0, 0], up: [0, 1, 0], height: 2, near: 0.01, far: 2 } }; +/** a perspective pass camera's lens as three draws with it, from its projection matrix (drei's reflector copies the + * screen camera's projection into a camera of its own, whose fov and aspect stay as it was made): its fov, and its + * aspect where that isn't its target's shape (three draws the projection into any target; the engine keeps it) */ +function lensOf(c: THREE.Camera, t: THREE.RenderTarget): Partial> { + if (!(c as THREE.PerspectiveCamera).isPerspectiveCamera) return {}; + const e = c.projectionMatrix.elements; + const fov = (2 * Math.atan(1 / e[5]) * 180) / Math.PI, aspect = e[5] / e[0]; + return Math.abs(aspect - t.width / t.height) > 1e-3 ? { fov, aspect } : { fov }; +} + /** a pass as parts: its scene's on its layer and its camera into the target's image */ function passParts(p: Omit, gl: THREE.WebGLRenderer): Part[] { const key = `${p.target.texture.uuid}#${p.what}#${p.k}`; @@ -60,7 +70,7 @@ function passParts(p: Omit, gl: THREE.WebGLRenderer): Part[] { const cam = cameraOf(p.camera) ?? QUAD; const bg = (p.scene as THREE.Scene).background; const clear = bg instanceof THREE.Color ? { bg: clearOf(bg, 1) } : p.autoClear ? { bg: clearOf(new THREE.Color(p.clear[0], p.clear[1], p.clear[2]), p.clear[3]) } : { clear: "none" }; - const camera: Part = { type: cam.type, props: { ...cam.props, name: root, target: imageOf(p.target), layers: [layer], order: FIRST_ORDER + i, ...clear } }; + const camera: Part = { type: cam.type, props: { ...cam.props, ...lensOf(p.camera, p.target), name: root, target: imageOf(p.target), layers: [layer], order: FIRST_ORDER + i, ...clear } }; return [camera, ...onLayer([...world, ...drawn], layer)]; } diff --git a/systems/matchmove/src/lib.rs b/systems/matchmove/src/lib.rs index 5f7ba335..a0e0ee2d 100644 --- a/systems/matchmove/src/lib.rs +++ b/systems/matchmove/src/lib.rs @@ -8,6 +8,8 @@ //! camera view_w view_h view_x view_y: the frame is the window at (view_x, view_y), the frame's size, of a view //! view_w x view_h frame pixels whose vertical field is `fov` (an off-axis projection, `OffAxis`). A 9:16 //! crop of 16:9 footage keeps the lens's centre where the footage had it, so the 3D stays locked to it. +//! camera aspect: the view's own width over height, drawn into its image whatever that image's shape (a camera into a +//! render image drawing three's projection: drei's reflector draws the screen's 16:9 into a square target). //! //! The plate is drawn as the scene's own picture, so it goes through tone mapping like everything else: //! puts `` in its scene, which leaves an unlit texture's colours as they are. @@ -103,11 +105,16 @@ fn off_axis_matrix(l: f32, r: f32, b: f32, t: f32, n: f32) -> Mat4 { /// The camera's projection for a frame's row: an OffAxis window when it has view_* columns. fn off_axis(v: &BTreeMap, frame: Vec2, near: f32, far: f32) -> Option { + let fov = num(v, "fov", 45.0).to_radians(); + if let Some(a) = v.get("aspect").map(V::num).filter(|a| *a > 0.0) { + let full = Vec2::new(a as f32, 1.0); + return Some(OffAxis { persp: PerspectiveProjection { fov, near, far, ..default() }, full, offset: Vec2::ZERO, size: full }); + } let (w, h) = (num(v, "view_w", 0.0), num(v, "view_h", 0.0)); if !(w > 0.0 && h > 0.0) { return None; } - let persp = PerspectiveProjection { fov: num(v, "fov", 45.0).to_radians(), near, far, aspect_ratio: frame.x / frame.y, ..default() }; + let persp = PerspectiveProjection { fov, near, far, aspect_ratio: frame.x / frame.y, ..default() }; Some(OffAxis { persp, full: Vec2::new(w, h), offset: Vec2::new(num(v, "view_x", 0.0), num(v, "view_y", 0.0)), size: frame }) } @@ -154,7 +161,10 @@ pub fn sync_view(frame: Res, edit: Res, mut q: Query<(Entity, & if new.persp.fov != old.persp.fov || new.full != old.full || new.offset != old.offset { *p = Projection::custom(new.clone()); } - window = Some(new); + // a look's camera copies the screen's window, never an image's own aspect + if !v.contains_key("aspect") { + window = Some(new); + } } let Some(w) = window else { return }; for (e, mut p, _) in &mut q {