Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ git push origin v0.1.0
Changes that land in the main branch after phases C–E (`0.4.0`) go here.

### Added
- Ambient occlusion in the camera's picture (the manual's "The picture"): `picture.ssao: Some((quality, radius, thickness))`
in the `camera` content gives the world cameras Bevy's `ScreenSpaceAmbientOcclusion` (`SsaoTuning`, `SsaoQuality::{Low,
Medium, High, Ultra}`), with the depth and normal prepasses it needs and `Msaa::Off` while it is on (Bevy draws no SSAO
under MSAA); taking it out gives the camera back its own SSAO, MSAA and prepasses. The view model's camera follows the
world camera's MSAA and has no SSAO. Off by default.
- A shadow budget in views (the manual's "Lights and colour grading (F13)"): `graphics.shadow_views` (0 to 384, default 0)
counts the shadow budget in shadow views, a point light 6 and a spot 1, the nearest first, and replaces
`graphics.shadow_lights` while above 0. The `lights` query says the budget in views, how many are taken and, for a light
that missed out, what it takes and what was left; `ai/status` adds `light.budget_views` and `light.views_used`.
`GraphicsSettings::shadow_budget` and `ShadowBudget::{Lights, Views}`. At 0 nothing changes.
- Baked light for levels (the manual's "Levels from Blender (E4.2)", "Baked light"): level node properties `lightmap`,
`lightmap_rect`, `irradiance_volume`, `reflection_probe` and `probe_falloff`, and the picture-only content kind
`baked_light` (`BakedLightDef { sets, set, lightmap_exposure, irradiance_intensity, reflection_intensity, bicubic }`, one
per level id) that names the folders of the level's sets of baked images. A drawn level puts Bevy's `Lightmap` on the
meshes (by their second UV map; the check names a mesh without one and every file a set lacks) and a `LightProbe` with an
`IrradianceVolume` and/or an `EnvironmentMapLight` on the probe boxes; lightmapped meshes still take every dynamic light.
A game switches a level's set at run time with `shards::level::select_baked_set` (the resource `BakedLight`, the
picture's, in no snapshot); the tools `baked_light` (player) and `baked_light_set` (dev). A content light's new
`baked: true` lights no lightmapped mesh's diffuse (its light is in the bake). A level without a `baked_light` draws as
before.
- The game's own subtitle lines (the manual's "Subtitles"): `shards::audio::subtitles::say(world, Saying { sound, text,
speaker, secs, bus })` shows a line that no voice says, for the seconds the game gives, on the clock of the bus it names
(the simulation's, so a pause holds it; real time on `Ui`): words the game puts together, such as a tablet's new task as
Expand Down Expand Up @@ -383,6 +403,16 @@ Changes that land in the main branch after phases C–E (`0.4.0`) go here.
and the timing `field_timing` (`#[ignore]`) in `physics/world.rs`.

### Fixed
- Colour grading said every field of a grade is drawn; Bevy draws only a grade's `exposure` on a camera that does not draw
in HDR (it grades in each material's shader there), and the rest (`temperature`, `tint`, `hue_deg`, `saturation`,
`contrast`, `lift`, `gamma`, `gain`) only in HDR, in its tone mapping pass. A grade tuned without HDR therefore changed
when a `picture.bloom` put the camera in HDR: a game's tunnels went black with red in them. Nothing is drawn
differently; the docs (`Grading`, `PictureTuning::grading`, the manual's "Colour grading") say so now, give
`temperature` and `tint` their real scale (an offset to the CIE 1931 white point: 0.02 to 0.05 is a clear cast, the old
examples' 0.35 takes the white point off the diagram) and say that `contrast` pivots on 0.5 of the linear picture before
the exposure (a dark scene that the grade's exposure lifts goes black under a contrast above 1). The `lights` query's
grading lines add `drawn: all of it (the camera draws in HDR)` or `drawn: its exposure alone; ...` with the fields left
out; `Grading::hdr_only` and `shards_motion::camera::grade_drawn_in_full`. An offscreen test pins Bevy's behaviour.
- A replay with a pause menu in it plays through (the manual's "Replays"). The menu takes effect after its frame's time is
counted, so the session may run that frame's tick under it and record the calls made in the menu one tick after the
call that opened it; played back, the opening call came inside a tick and paused the clock, which then never reached
Expand Down
122 changes: 122 additions & 0 deletions crates/sandbox/tests/sandbox/first_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,125 @@ fn a_mouse_move_turns_the_look_and_the_picture_in_its_frame_and_the_body_on_the_
}
eprintln!("{table}");
}

/// The camera content's SSAO drawn for real, through the first-person camera and its view model's camera: the picture
/// darkens where things meet, a prepass another module takes away comes back without a lost frame, and taking SSAO
/// out gives the cameras their MSAA back. Every capture must arrive: a camera whose prepasses, MSAA and SSAO disagree
/// is an error Bevy logs and draws nothing for, or a wgpu validation error that stops the drawing.
#[test]
#[ignore = "needs a GPU, or a software one (WGPU_FORCE_FALLBACK_ADAPTER=1): cargo test -p sandbox --test sandbox -- --ignored first_person"]
fn ssao_from_the_camera_content_draws_through_the_first_person_cameras_and_goes_again() {
use shards::bevy::core_pipeline::prepass::{DepthPrepass, NormalPrepass};
use shards::bevy::pbr::ScreenSpaceAmbientOcclusion;
use shards::bevy::render::view::Msaa;
use shards::motion::camera::SsaoTuning;

use crate::offscreen::{block, capture, mean_difference};

// A picture once the pipelines are compiled (a `shot` waits for them): a change of the camera's passes compiles new
// ones, and a frame drawn meanwhile leaves out what waits for them (a capture alike twice can still be one of those).
let drawn = |app: &mut App, picture: &Handle<Image>| {
shot_to(app, "ssao");
capture_stable(app, picture)
};

let mut app = offscreen_app();
let picture = app.world().resource::<Offscreen>().image.clone();
step(&mut app, 30);
look_down_through_the_eye(&mut app);
settle(&mut app, 200);
let plain = drawn(&mut app, &picture);
let world = app.world_mut();
let camera = world.query_filtered::<Entity, (With<FirstPersonCamera>, With<Camera3d>)>().single(world).unwrap();
let view_model = world.query_filtered::<Entity, With<shards::action::render::ViewModelCamera>>().single(world).unwrap();
let msaa = |app: &App, e: Entity| app.world().get::<Msaa>(e).copied();
let own = msaa(&app, camera);

app.world_mut().resource_mut::<CameraTuning>().picture.ssao = Some(SsaoTuning::default());
settle(&mut app, 30);
let occluded = drawn(&mut app, &picture);
let world = app.world();
assert!(world.get::<ScreenSpaceAmbientOcclusion>(camera).is_some() && world.get::<NormalPrepass>(camera).is_some());
assert_eq!((msaa(&app, camera), msaa(&app, view_model)), (Some(Msaa::Off), Some(Msaa::Off)), "both cameras without MSAA");
assert!(world.get::<ScreenSpaceAmbientOcclusion>(view_model).is_none(), "the arms have no SSAO of their own");
let luma = |[r, g, b]: [f64; 3]| 0.2126 * r + 0.7152 * g + 0.0722 * b;
let (w, h) = (plain.width(), plain.height());
let (before, after) = (luma(block(&plain, 0, 0, w, h)), luma(block(&occluded, 0, 0, w, h)));
let differ = mean_difference(&plain, &occluded);
eprintln!("SSAO in the sandbox's first person: luma {before:.2} -> {after:.2}, mean difference {differ:.3}");
assert!(differ > 0.05 && after < before, "SSAO darkens the picture: luma {before:.2} -> {after:.2}, difference {differ:.3}");

// A module takes the depth prepass away (the decals do when they end): it is back the next frame, and the frames go on.
app.world_mut().entity_mut(camera).remove::<DepthPrepass>();
for _ in 0..5 {
capture(&mut app, picture.clone());
}
assert!(app.world().get::<DepthPrepass>(camera).is_some(), "the prepass SSAO needs is put back");
let again = drawn(&mut app, &picture);
assert!(mean_difference(&occluded, &again) < 1.0, "and the picture is the occluded one: {:.3}", mean_difference(&occluded, &again));

// Out again: the cameras' own MSAA, no SSAO, the plain picture.
app.world_mut().resource_mut::<CameraTuning>().picture.ssao = None;
settle(&mut app, 30);
let back = drawn(&mut app, &picture);
assert!(app.world().get::<ScreenSpaceAmbientOcclusion>(camera).is_none());
assert_eq!((msaa(&app, camera), msaa(&app, view_model)), (own, own), "the MSAA the camera had, on both");
assert!(mean_difference(&plain, &back) < 1.0, "the plain picture again: {:.3}", mean_difference(&plain, &back));
}

/// How much of a grade Bevy draws: a camera in HDR is graded in the tone mapping pass, all of the grade; a camera that is
/// not is graded in each material's shader, where Bevy applies the exposure alone. So a white balance draws nothing until
/// a bloom puts the cameras in HDR, and an exposure draws the same either way; the `lights` query says which it is. (A
/// game tuned its grades without HDR, at a white balance far too strong, and its picture went black and red when it
/// turned a bloom on.) If Bevy starts to grade in full without HDR this fails, and the grading docs and the query's
/// `drawn:` line want changing.
#[test]
#[ignore = "needs a GPU, or a software one (WGPU_FORCE_FALLBACK_ADAPTER=1): cargo test -p sandbox --test sandbox -- --ignored first_person"]
fn a_grade_draws_its_exposure_alone_without_hdr_and_all_of_it_in_hdr() {
use shards::motion::camera::{BloomComposite, BloomTuning, Grading};

use crate::offscreen::block;

let mut app = offscreen_app();
let picture = app.world().resource::<Offscreen>().image.clone();
step(&mut app, 30);
look_down_through_the_eye(&mut app);
settle(&mut app, 200);
// The mean colour of the world above the arms, with this bloom and this grade (a `shot` first: it waits for the
// pipelines that a change to HDR compiles).
let world_with = |app: &mut App, name: &str, bloom: &Option<BloomTuning>, grade: Option<&Grading>| {
let mut tuning = app.world_mut().resource_mut::<CameraTuning>();
(tuning.picture.bloom, tuning.picture.grading) = (bloom.clone(), grade.cloned());
settle(app, 30);
shot_to(app, name);
let image = capture_stable(app, &picture);
block(&image, 0, 200, image.width(), 250)
};
let drawn = |app: &mut App| {
let said = ok(app, "lights", json!({})).result;
said.as_str().unwrap().lines().find(|l| l.starts_with(" drawn:")).map(str::to_owned)
};
let bloom = Some(BloomTuning { intensity: 0.1, threshold: 1.0, composite: BloomComposite::Additive, ..default() });
let warm = Grading { temperature: 0.05, ..default() };
let brighter = Grading { exposure: 1.7, ..default() };

let plain = world_with(&mut app, "grade-plain", &None, None);
let warm_ldr = world_with(&mut app, "grade-warm", &None, Some(&warm));
let said_ldr = drawn(&mut app);
let brighter_ldr = world_with(&mut app, "grade-brighter", &None, Some(&brighter));
let plain_hdr = world_with(&mut app, "grade-plain-hdr", &bloom, None);
let warm_hdr = world_with(&mut app, "grade-warm-hdr", &bloom, Some(&warm));
let said_hdr = drawn(&mut app);
let brighter_hdr = world_with(&mut app, "grade-brighter-hdr", &bloom, Some(&brighter));
eprintln!(
"the world's mean colour: plain {plain:.1?}, warm {warm_ldr:.1?}, brighter {brighter_ldr:.1?}; in HDR plain {plain_hdr:.1?}, warm {warm_hdr:.1?}, brighter {brighter_hdr:.1?}"
);
let alike = |a: [f64; 3], b: [f64; 3]| (0..3).all(|c| (a[c] - b[c]).abs() < 1.5);
assert!(alike(plain, warm_ldr), "without HDR a white balance draws nothing: {plain:.1?} and {warm_ldr:.1?}");
assert!(said_ldr.as_deref().is_some_and(|l| l.contains("its exposure alone") && l.contains("leaves out temperature")), "{said_ldr:?}");
assert!(brighter_ldr[1] > plain[1] + 20.0, "an exposure is drawn without HDR: {plain:.1?} and {brighter_ldr:.1?}");
assert!(alike(plain, plain_hdr), "this bloom leaves the world above the arms as it was: {plain:.1?} and {plain_hdr:.1?}");
assert!(warm_hdr[0] > plain_hdr[0] + 8.0 && warm_hdr[2] < plain_hdr[2] - 3.0, "in HDR it warms the picture: {plain_hdr:.1?} and {warm_hdr:.1?}");
assert_eq!(said_hdr.as_deref(), Some(" drawn: all of it (the camera draws in HDR)"));
assert!(alike(brighter_ldr, brighter_hdr), "and an exposure is drawn alike in both: {brighter_ldr:.1?} and {brighter_hdr:.1?}");
}
1 change: 1 addition & 0 deletions crates/sandbox/tests/sandbox/state_audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const OUTSIDE: &[(&str, &str)] = &[
// shards_hitstop
("shards_hitstop::", "the hit stop log: what was held and why, never changing the world; the holds themselves are ai_core::hold's"),
// shards_level
("shards_level::baked::BakedLight", "the baked set each level draws: the picture's, which a game sets from its own saved state"),
("shards_level::library::LevelLibrary", "loaded level files: what the world is built around, which a snapshot check guards"),
("shards_level::spawn::LevelInstances", "the levels spawned, made again by the snapshot prepares `levels` and `placed levels`"),
// shards_light
Expand Down
30 changes: 30 additions & 0 deletions crates/shards/tests/action_render/view_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,33 @@ fn a_world_camera_that_grades_in_its_shaders_hands_its_grade_to_the_view_model_c
app.update();
assert_eq!(grade_of(&app, vm), ColorGrading::default(), "under HDR the view model is drawn over the graded picture, ungraded");
}

#[test]
fn ssao_on_the_world_camera_turns_its_msaa_off_and_the_view_model_camera_follows_without_ssao_of_its_own() {
use shards::bevy::pbr::ScreenSpaceAmbientOcclusion;
use shards::bevy::render::view::Msaa;
use shards::motion::camera::{PictureTuning, SsaoTuning};

let World3 { mut app, camera, .. } = scene("action_render_view_model_ssao");
// A window's camera has MSAA (the render plugin's required component); the bare test app's does not, so give it one.
app.world_mut().entity_mut(camera).insert(Msaa::Sample4);
app.update();
app.update();
let vm = view_cameras(&mut app)[0];
let msaa = |app: &App, e: Entity| app.world().get::<Msaa>(e).copied();
assert_eq!(msaa(&app, vm), Some(Msaa::Sample4), "the two cameras share one main texture, so one sample count");

app.world_mut().resource_mut::<CameraTuning>().picture = PictureTuning { ssao: Some(SsaoTuning::default()), ..default() };
app.update();
app.update();
assert!(app.world().get::<ScreenSpaceAmbientOcclusion>(camera).is_some());
assert_eq!(msaa(&app, camera), Some(Msaa::Off), "Bevy draws no SSAO under MSAA");
assert_eq!(msaa(&app, vm), Some(Msaa::Off), "the view model's camera follows the world camera");
assert!(app.world().get::<ScreenSpaceAmbientOcclusion>(vm).is_none(), "the arms are drawn over the occluded world, with none of their own");

app.world_mut().resource_mut::<CameraTuning>().picture = PictureTuning::default();
app.update();
app.update();
assert!(app.world().get::<ScreenSpaceAmbientOcclusion>(camera).is_none());
assert_eq!((msaa(&app, camera), msaa(&app, vm)), (Some(Msaa::Sample4), Some(Msaa::Sample4)), "both get their MSAA back");
}
Loading
Loading