Files
shinyobjectz 9202d88e69 #165: an <R3F> scene lit by PMREMGenerator.fromScene(new RoomEnvironment(), sigma) is lit by that room, as three lights it. Since #131, fromScene handed back a stand-in that raea left out with a warning, because raea's <Environment> and its engine took panorama files only: the scene was drawn with no environment, so a box three draws 255,195,160 on its front face came out 136,76,58, and anything the room alone lights was near black. <Environment> now takes room (three.js's RoomEnvironment, made with no file) and blur (fromScene's sigma) in place of src. The engine makes the room itself (raea-sys-environment room.rs): RoomEnvironment's room, boxes, light boxes and point light at three r185's sizes and strengths, each direction from fromScene's cube camera traced to what it hits and shaded with three's Lambert, GGX and light falloff, blurred by sigma as fromScene's two half blurs do, then filtered like any panorama; runtime/look names it in env_srcs as room:SIGMA. On the R3F side, fromScene of a RoomEnvironment returns a texture marked with its sigma, which lights.ts turns into <Environment room blur> with the scene's environmentIntensity and rotation, and as the background when the room is that too; fromScene of any other scene, and fromCubemap, are still left out with a warning. The other way round, src/three (room.ts) draws <Environment room> as RoomEnvironment through the renderer's PMREMGenerator, and the source export writes it so. Tests: room.rs checks the room's light at 14 directions against three's own RoomEnvironment object (within 1e-3), the blur and the source name; runtime/look's camera test reads room sources into env_srcs; the envmap conformance test and test/errors.test.tsx hold the props and the conversion, and export.test.tsx the export. test/room/check.sh renders test/room/room.tsx and holds three pixels within 8 levels of three's, which test/room/three.ts works out on the CPU with three 0.185's PMREM and MeshStandardMaterial shading: #131's box face is 255,189,155 (three 255,195,160; 136,76,58 before), and two grey boxes lit by the room alone are 137 and 125 (three 140 and 127; 23 and 19 before). Left open: fromScene's options.position is drawn from the origin, with a warning, and the light meter does not count a room. Filed as R3F15b in docs/r3f.feature.
2026-11-16 17:29:56 -08:00

71 lines
4.1 KiB
Rust

//! The camera rows a scene's cameras make (3d/two_passes, 3d/split_screen): the first is "cam", each after it a row of
//! its own with its order, clear and viewport.
use std::collections::BTreeMap;
use std::sync::Arc;
use bevy::math::Mat4;
use raea_core_program::ir::{Program, V};
use raea_core_scene::Item;
use raea_runtime_look::camera;
use serde_json::json;
fn cam(props: serde_json::Value) -> Item {
Item { kind: "camera", props: Arc::new(props.as_object().unwrap().clone()), shape: None, material: None, at: None, world: Mat4::IDENTITY }
}
#[test]
fn each_camera_after_the_first_is_a_row_with_its_order_clear_and_viewport() {
let vp = |x: u32, y: u32| json!({ "physical_position": [x, y], "physical_size": [640, 360] });
let items = vec![
cam(json!({ "position": [0, 200, -150], "lookAt": [0, 0, 0], "viewport": vp(0, 0) })),
cam(json!({ "position": [150, 150, 50], "lookAt": [0, 0, 0], "order": 1, "clear": "none", "viewport": vp(640, 360) })),
];
let mut first: BTreeMap<String, V> = [("id".to_string(), V::S("cam".into())), ("bg".to_string(), V::S("#2B2C2F".into()))].into();
let more = camera(&[items], &mut Program::default(), &mut first);
assert_eq!(more.len(), 1);
let n = |r: &BTreeMap<String, V>, k: &str| r.get(k).map(V::num);
assert_eq!([n(&first, "vp_x"), n(&first, "vp_y"), n(&first, "vp_w"), n(&first, "vp_h")], [Some(0.0), Some(0.0), Some(640.0), Some(360.0)]);
assert_eq!(first.get("order"), None);
let second = &more[0];
assert!(matches!(second.get("id"), Some(V::S(s)) if s == "cam1"));
assert!(matches!(second.get("bg"), Some(V::S(s)) if s == "#2B2C2F"));
assert!(matches!(second.get("clear"), Some(V::S(s)) if s == "none"));
assert_eq!([n(second, "order"), n(second, "vp_x"), n(second, "vp_y")], [Some(1.0), Some(640.0), Some(360.0)]);
assert_eq!(n(second, "x"), Some(150.0));
}
#[test]
fn a_camera_with_a_rotation_keeps_it_exactly_and_one_without_has_none() {
// a Camera3d's quaternion as glam made it (Transform::looking_at from (4, 3, 6) to (0, 0.2, 0)), unrounded: the
// engine places the camera by it, not by its look-at point turned back into a rotation in f32
let q = [-0.17622831463813782, 0.28482943773269653, 0.05335763469338417, 0.9407277703285217];
let mut row: BTreeMap<String, V> = [("id".to_string(), V::S("cam".into()))].into();
camera(&[vec![cam(json!({ "position": [4, 3, 6], "lookAt": [0, 0.2, 0], "rotation": q }))]], &mut Program::default(), &mut row);
let got: Vec<Option<f64>> = ["rot_qx", "rot_qy", "rot_qz", "rot_qw"].iter().map(|k| row.get(*k).map(V::num)).collect();
assert_eq!(got, q.map(Some));
let mut plain: BTreeMap<String, V> = [("id".to_string(), V::S("cam".into()))].into();
camera(&[vec![cam(json!({ "position": [4, 3, 6], "lookAt": [0, 0.2, 0], "rotation": [0.1, 0.2, 0.3] }))]], &mut Program::default(), &mut plain);
assert!(!plain.contains_key("rot_qw"), "a rotation that isn't a quaternion places nothing");
}
#[test]
fn an_environment_room_is_a_source_of_its_own_by_its_blur() {
// <Environment room blur> (three's RoomEnvironment, #165): no file, the engine makes it; the same room on two
// frames is one source, a file beside it another
let env = |p: serde_json::Value| Item { kind: "environment", props: Arc::new(p.as_object().unwrap().clone()), shape: None, material: None, at: None, world: Mat4::IDENTITY };
let frames = vec![
vec![cam(json!({ "position": [0, 0, 5] })), env(json!({ "room": true, "blur": 0.04, "intensity": 1 }))],
vec![cam(json!({ "position": [0, 0, 5] })), env(json!({ "room": true, "blur": 0.04, "intensity": 0.5 }))],
vec![cam(json!({ "position": [0, 0, 5] })), env(json!({ "src": "sky.hdr" }))],
vec![cam(json!({ "position": [0, 0, 5] })), env(json!({ "room": true }))],
];
let mut row: BTreeMap<String, V> = [("id".to_string(), V::S("cam".into()))].into();
camera(&frames, &mut Program::default(), &mut row);
let srcs: Vec<String> = match row.get("env_srcs") {
Some(V::S(s)) => serde_json::from_str(s).unwrap(),
other => panic!("no env_srcs: {other:?}"),
};
assert_eq!(srcs, ["room:0.04", "sky.hdr", "room:0"]);
}