Advance M0 scene and rendering foundations
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-06-06 03:50:03 -04:00
1 parent 2fa64106c8
commit 2054fa2558
21 files changed
+711 -159

No files matched your search

@@ -476,7 +476,7 @@ fn labeled_asset_path(path: &str, label: &str) -> String {
}
fn studio_render_image() -> Image {
Image::new_target_texture(THUMB_SIZE, THUMB_SIZE, TextureFormat::Rgba8UnormSrgb, None)
Image::new_target_texture(THUMB_SIZE, THUMB_SIZE, TextureFormat::Rgba16Float, None)
}
fn finish_active_thumbnail(
+5 -5
View File
@@ -7,7 +7,7 @@ use bevy::scene::serde::SceneDeserializer;
use bevy::scene::DynamicScene;
use bevy::scene::DynamicSceneBuilder;
use bevy::window::PrimaryWindow;
use scene::{migrate_scene_text, stamp_schema_version, strip_schema_version, validate_level_text};
use scene::{document::SceneDocument, strip_schema_version, validate_level_text};
use serde::de::DeserializeSeed;
use shared::{
flush_level_object_hydration, infer_actor_kind, strip_hydrated_entity, validate_actor, ActorId,
@@ -495,8 +495,8 @@ fn save_entities(world: &mut World, path: &Path, entities: Vec<Entity>) -> Resul
std::fs::create_dir_all(parent)
.map_err(|err| format!("could not create {}: {err}", parent.display()))?;
}
let stamped = stamp_schema_version(&ron)?;
std::fs::write(path, stamped)
let document = SceneDocument::from_ron_text(&ron)?;
std::fs::write(path, document.to_ron_text()?)
.map_err(|err| format!("could not write {}: {err}", path.display()))?;
finalize_scene_load(world);
@@ -511,8 +511,8 @@ fn load_level(world: &mut World, path: &Path) -> Result<(), String> {
let text = std::fs::read_to_string(path)
.map_err(|err| format!("could not read {}: {err}", path.display()))?;
validate_level_text(&text)?;
let normalized = migrate_scene_text(&text)?;
let bevy_ron = strip_schema_version(&normalized)?;
let document = SceneDocument::from_ron_text(&text)?;
let bevy_ron = strip_schema_version(document.normalized_ron())?;
clear_loaded_scene_roots(world);
clear_level_objects(world);
+91 -70
View File
@@ -2415,78 +2415,87 @@ fn light_editor_ui(world: &mut World, ui: &mut egui::Ui, entity: Entity) {
}
});
});
let mut color = [light.color.r, light.color.g, light.color.b, light.color.a];
property_row(ui, "Color", |ui| {
if ui.color_edit_button_rgba_unmultiplied(&mut color).changed() {
light.color = ColorDesc {
r: color[0],
g: color[1],
b: color[2],
a: color[3],
};
changed = true;
let solari_disabled = solari_disables_local_light(world, &light);
if solari_disabled {
ui.colored_label(
egui::Color32::from_rgb(255, 180, 100),
"Point and spot lights are disabled while Solari is active. Use a directional light or emissive material, or switch GI to Forward.",
);
}
ui.add_enabled_ui(!solari_disabled, |ui| {
let mut color = [light.color.r, light.color.g, light.color.b, light.color.a];
property_row(ui, "Color", |ui| {
if ui.color_edit_button_rgba_unmultiplied(&mut color).changed() {
light.color = ColorDesc {
r: color[0],
g: color[1],
b: color[2],
a: color[3],
};
changed = true;
}
});
let intensity_label = match light.kind {
AuthoringLightKind::Directional => "Intensity (lux)",
_ => "Intensity (lumens)",
};
let intensity_max = match light.kind {
AuthoringLightKind::Directional => AUTHORING_DIRECTIONAL_LUX_MAX,
_ => AUTHORING_POINT_SPOT_LUMENS_MAX,
};
property_row(ui, intensity_label, |ui| {
ui.horizontal_wrapped(|ui| {
if ui
.add(
egui::Slider::new(&mut light.intensity, 0.0..=intensity_max)
.show_value(false),
)
.changed()
{
changed = true;
}
if ui
.add_sized(
[fit_width(ui, 72.0, 120.0), 20.0],
egui::DragValue::new(&mut light.intensity)
.range(0.0..=intensity_max)
.speed(intensity_max * 0.001),
)
.changed()
{
changed = true;
}
});
});
if matches!(
light.kind,
AuthoringLightKind::Point | AuthoringLightKind::Spot
) {
property_row(ui, "Range (m)", |ui| {
changed |= ui
.add(egui::Slider::new(&mut light.range, 0.0..=100.0))
.changed();
});
}
if matches!(light.kind, AuthoringLightKind::Spot) {
property_row(ui, "Inner angle", |ui| {
changed |= ui
.add(egui::Slider::new(&mut light.inner_angle_deg, 1.0..=80.0))
.changed();
});
property_row(ui, "Outer angle", |ui| {
changed |= ui
.add(egui::Slider::new(&mut light.outer_angle_deg, 1.0..=90.0))
.changed();
});
}
property_row(ui, "Shadows", |ui| {
changed |= ui.checkbox(&mut light.shadows, "Cast shadows").changed();
});
if matches!(light.kind, AuthoringLightKind::Directional) {
ui.small("Controls project sun while this directional exists.");
}
});
let intensity_label = match light.kind {
AuthoringLightKind::Directional => "Intensity (lux)",
_ => "Intensity (lumens)",
};
let intensity_max = match light.kind {
AuthoringLightKind::Directional => AUTHORING_DIRECTIONAL_LUX_MAX,
_ => AUTHORING_POINT_SPOT_LUMENS_MAX,
};
property_row(ui, intensity_label, |ui| {
ui.horizontal_wrapped(|ui| {
if ui
.add(
egui::Slider::new(&mut light.intensity, 0.0..=intensity_max)
.show_value(false),
)
.changed()
{
changed = true;
}
if ui
.add_sized(
[fit_width(ui, 72.0, 120.0), 20.0],
egui::DragValue::new(&mut light.intensity)
.range(0.0..=intensity_max)
.speed(intensity_max * 0.001),
)
.changed()
{
changed = true;
}
});
});
if matches!(
light.kind,
AuthoringLightKind::Point | AuthoringLightKind::Spot
) {
property_row(ui, "Range (m)", |ui| {
changed |= ui
.add(egui::Slider::new(&mut light.range, 0.0..=100.0))
.changed();
});
}
if matches!(light.kind, AuthoringLightKind::Spot) {
property_row(ui, "Inner angle", |ui| {
changed |= ui
.add(egui::Slider::new(&mut light.inner_angle_deg, 1.0..=80.0))
.changed();
});
property_row(ui, "Outer angle", |ui| {
changed |= ui
.add(egui::Slider::new(&mut light.outer_angle_deg, 1.0..=90.0))
.changed();
});
}
property_row(ui, "Shadows", |ui| {
changed |= ui.checkbox(&mut light.shadows, "Cast shadows").changed();
});
if matches!(light.kind, AuthoringLightKind::Directional) {
ui.small("Controls project sun while this directional exists.");
}
});
apply_component_card_response(world, entity, card_response);
if changed {
@@ -2494,6 +2503,18 @@ fn light_editor_ui(world: &mut World, ui: &mut egui::Ui, entity: Entity) {
}
}
fn solari_disables_local_light(world: &World, light: &LightDesc) -> bool {
matches!(
light.kind,
AuthoringLightKind::Point | AuthoringLightKind::Spot
) && world
.get_resource::<settings::ActiveCameraRenderProfile>()
.is_some_and(|profile| profile.gi_path == settings::GiPath::SolariDeferred)
&& world
.get_resource::<settings::RenderingCapabilities>()
.is_some_and(|caps| caps.rt_supported)
}
fn rigid_body_editor_ui(world: &mut World, ui: &mut egui::Ui, entity: Entity) {
let Some(mut body) = world.get::<RigidBodyDesc>(entity).copied() else {
return;
+7 -1
View File
@@ -82,6 +82,7 @@ struct RenderViewCache {
profile: Option<ActiveCameraRenderProfile>,
effective_gi: Option<GiPath>,
stack: Option<EffectiveRenderStack>,
local_shadow_lights: Option<bool>,
}
/// Ensures exactly one camera has the full project rendering stack for the current mode.
@@ -146,6 +147,8 @@ fn sync_project_render_view(
let effective_gi = stack.effective_gi_path;
let solari_readiness_changed = cache.effective_gi != Some(effective_gi);
cache.effective_gi = Some(effective_gi);
let local_shadow_lights_changed = cache.local_shadow_lights != Some(local_shadow_lights.0);
cache.local_shadow_lights = Some(local_shadow_lights.0);
let active_target = viewport_target.0.as_ref().map(|t| t.image.id());
let target_changed = cache.target != active_target;
@@ -156,6 +159,7 @@ fn sync_project_render_view(
&& !target_changed
&& !profile_changed
&& !solari_readiness_changed
&& !local_shadow_lights_changed
{
return;
}
@@ -183,9 +187,11 @@ fn sync_project_render_view(
|| target_changed
|| profile_changed
|| solari_readiness_changed
|| local_shadow_lights_changed
{
let force_full_apply = owner_changed || target_changed || solari_readiness_changed;
for entity in &active {
let apply = if owner_changed {
let apply = if force_full_apply {
ViewportStackApply::Full
} else if let Ok(state) = camera_fx.get(*entity) {
let snap = fx_snapshot(state);
@@ -84,9 +84,6 @@ fn active_camera_tab(world: &mut World, ui: &mut egui::Ui) {
) {
(GiPath::Forward, _, _) => "Forward PBR",
(GiPath::SolariDeferred, GiPath::SolariDeferred, _) => "Solari deferred",
(GiPath::SolariDeferred, _, Some(RenderFallbackReason::LocalShadowLights)) => {
"Solari requested, Forward (local shadow lights)"
}
(GiPath::SolariDeferred, _, Some(RenderFallbackReason::RtUnsupported)) => {
"Solari requested, Forward (RT unsupported)"
}
@@ -102,12 +99,7 @@ fn active_camera_tab(world: &mut World, ui: &mut egui::Ui) {
{
ui.colored_label(
egui::Color32::from_rgb(255, 180, 100),
"Solari deferred: directional lights and emissive meshes affect lighting; point/spot lights are Forward-only in Bevy 0.18.",
);
} else if stack.fallback_reason == Some(RenderFallbackReason::LocalShadowLights) {
ui.colored_label(
egui::Color32::from_rgb(255, 180, 100),
"Point/spot lights with shadows require Forward PBR; GI auto-falls back while they are in the scene.",
"Solari deferred: directional lights and emissive meshes affect lighting; point/spot LightDesc components are disabled in Bevy 0.18.",
);
} else if stack.fallback_reason == Some(RenderFallbackReason::RtUnsupported)
|| (!caps.rt_supported && profile.gi_path == GiPath::Forward)
@@ -176,7 +168,9 @@ fn active_camera_tab(world: &mut World, ui: &mut egui::Ui) {
ui.label(format!(
"Post-FX: hdr={} target_hdr={} bloom={} taa={} ssao={} atmosphere={} tonemap={}",
profile.hdr,
hdr_enabled_profile(&profile) || stack.effective_gi_path == GiPath::SolariDeferred,
hdr_enabled_profile(&profile)
|| profile.atmosphere
|| stack.effective_gi_path == GiPath::SolariDeferred,
profile.bloom,
profile.taa,
profile.ssao,
+1 -1
View File
@@ -72,7 +72,7 @@ mod hot {
ActiveCameraRenderProfile, ProjectRenderCamera, ProjectSettings, RenderingCapabilities,
SimTuning,
};
use shared::{LevelObject, LightDesc, ProjectSun};
use shared::{InspectorOrder, LevelObject, LightDesc, ProjectSun};
use sim::{
CameraSensitivity, Crouching, GameInputEnabled, GameInputFocused, Grounded, JumpState,
Player, PlayerCamera, PlayerVelocity, PlayerYawSensitivity,
+28 -7
View File
@@ -51,16 +51,24 @@ pub fn hdr_enabled_profile(profile: &ActiveCameraRenderProfile) -> bool {
}
}
/// True when the camera render target must be HDR for either authored HDR or Solari storage writes.
/// True when the camera render target must be HDR for authored HDR, atmosphere sky, or Solari storage writes.
pub fn hdr_required_for_effective_gi(
profile: &ActiveCameraRenderProfile,
effective_gi: GiPath,
) -> bool {
hdr_required_for_render_stack(hdr_enabled_profile(profile), effective_gi)
hdr_required_for_render_stack(
hdr_enabled_profile(profile),
profile.atmosphere,
effective_gi,
)
}
pub fn hdr_required_for_render_stack(hdr_enabled: bool, effective_gi: GiPath) -> bool {
hdr_enabled || effective_gi == GiPath::SolariDeferred
pub fn hdr_required_for_render_stack(
hdr_enabled: bool,
atmosphere_enabled: bool,
effective_gi: GiPath,
) -> bool {
hdr_enabled || atmosphere_enabled || effective_gi == GiPath::SolariDeferred
}
/// Applies the shared project rendering profile to all [`ProjectRenderCamera`] entities.
@@ -363,9 +371,22 @@ mod tests {
#[test]
fn solari_forces_hdr_render_target() {
assert!(hdr_required_for_render_stack(false, GiPath::SolariDeferred));
assert!(hdr_required_for_render_stack(true, GiPath::Forward));
assert!(!hdr_required_for_render_stack(false, GiPath::Forward));
assert!(hdr_required_for_render_stack(
false,
false,
GiPath::SolariDeferred
));
assert!(hdr_required_for_render_stack(true, false, GiPath::Forward));
assert!(!hdr_required_for_render_stack(
false,
false,
GiPath::Forward
));
}
#[test]
fn atmosphere_forces_hdr_render_target() {
assert!(hdr_required_for_render_stack(false, true, GiPath::Forward));
}
}
@@ -37,7 +37,7 @@ pub fn resolve_viewport_camera_owner(
}
}
/// True when any point or spot light in the world casts shadows (forces Forward GI).
/// True when any point or spot light in the world casts shadows.
pub fn has_local_shadow_lights(points: &Query<&PointLight>, spots: &Query<&SpotLight>) -> bool {
points.iter().any(|l| l.shadows_enabled) || spots.iter().any(|l| l.shadows_enabled)
}
@@ -57,7 +57,7 @@ pub fn world_has_local_shadow_lights(world: &mut World) -> bool {
false
}
/// Effective GI path for the main viewport, including auto-forward for local shadow lights.
/// Effective GI path for the main viewport.
pub fn effective_viewport_gi_path(
requested: GiPath,
caps: &RenderingCapabilities,
@@ -134,6 +134,7 @@ pub fn sync_viewport_camera_stack(
match mode {
ViewportStackApply::Full => {
strip_project_camera_fx(commands, entity);
commands.entity(entity).insert(ProjectRenderCamera);
apply_camera_render_profile(
commands,
@@ -208,7 +209,7 @@ mod tests {
use settings::RenderingCapabilities;
#[test]
fn effective_viewport_gi_forces_forward_when_local_shadow_lights_exist() {
fn effective_viewport_gi_keeps_solari_when_local_shadow_lights_exist() {
let caps = RenderingCapabilities {
rt_supported: true,
..Default::default()
@@ -221,7 +222,7 @@ mod tests {
});
assert_eq!(
effective_viewport_gi_path(GiPath::SolariDeferred, &caps, stats.as_ref(), true),
GiPath::Forward
GiPath::SolariDeferred
);
assert_eq!(
effective_viewport_gi_path(GiPath::SolariDeferred, &caps, stats.as_ref(), false),
+376
View File
@@ -0,0 +1,376 @@
//! Schema-aware scene document seam over the current DynamicScene RON format.
//!
//! This module intentionally stays Bevy-free. The editor can keep loading and
//! saving DynamicScene RON while tools start depending on stable actor/component
//! document concepts instead of runtime ECS entity IDs.
use std::collections::BTreeMap;
use ron::ser::PrettyConfig;
use serde::{Deserialize, Serialize};
use crate::{
migrate_scene_text, read_schema_version, stamp_schema_version, strip_schema_version,
validate_scene_authoring_only,
};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SceneDocument {
pub schema_version: u32,
pub entities: Vec<SceneEntity>,
normalized_ron: String,
}
impl SceneDocument {
pub fn from_ron_text(text: &str) -> Result<Self, String> {
let normalized = migrate_scene_text(text)?;
validate_scene_authoring_only(&normalized)?;
let body = strip_schema_version(&normalized)?;
let parsed: DynamicSceneBody = ron::from_str(&body)
.map_err(|err| format!("could not parse scene document body: {err}"))?;
let mut entities = Vec::with_capacity(parsed.entities.len());
for (index, (_source_key, entity)) in parsed.entities.into_iter().enumerate() {
let components = entity
.components
.into_iter()
.map(|(type_name, value)| SceneComponentBlob::from_value(type_name, value))
.collect::<Result<Vec<_>, _>>()?;
let actor_id = extract_actor_id(&components);
let name = extract_name(&components);
let document_id = actor_id
.as_ref()
.map(|id| SceneEntityId::ActorId(id.clone()))
.unwrap_or_else(|| SceneEntityId::Anonymous(format!("anonymous:{index}")));
entities.push(SceneEntity {
document_id,
actor_id,
name,
parent_actor_id: None,
components,
});
}
Ok(Self {
schema_version: read_schema_version(&normalized),
entities,
normalized_ron: normalized,
})
}
pub fn to_ron_text(&self) -> Result<String, String> {
stamp_schema_version(&self.normalized_ron)
}
pub fn normalized_ron(&self) -> &str {
&self.normalized_ron
}
pub fn find_entity(&self, id: &SceneEntityId) -> Option<&SceneEntity> {
self.entities
.iter()
.find(|entity| &entity.document_id == id)
}
pub fn apply_patch(&mut self, patch: ScenePatch) -> Result<(), String> {
match patch {
ScenePatch::SetComponent { target, component } => {
let entity = self
.find_entity_mut(&target)
.ok_or_else(|| format!("scene entity `{target}` not found"))?;
if let Some(existing) = entity
.components
.iter_mut()
.find(|existing| existing.type_name == component.type_name)
{
*existing = component;
} else {
entity.components.push(component);
}
}
ScenePatch::RemoveComponent {
target,
component_type,
} => {
let entity = self
.find_entity_mut(&target)
.ok_or_else(|| format!("scene entity `{target}` not found"))?;
entity
.components
.retain(|component| component.type_name != component_type);
}
ScenePatch::AddEntity { entity } => {
if self.find_entity(&entity.document_id).is_some() {
return Err(format!(
"scene entity `{}` already exists",
entity.document_id
));
}
self.entities.push(entity);
}
ScenePatch::DeleteEntity { target } => {
let before = self.entities.len();
self.entities.retain(|entity| entity.document_id != target);
if self.entities.len() == before {
return Err(format!("scene entity `{target}` not found"));
}
}
ScenePatch::Reparent {
target,
parent_actor_id,
} => {
let entity = self
.find_entity_mut(&target)
.ok_or_else(|| format!("scene entity `{target}` not found"))?;
entity.parent_actor_id = parent_actor_id;
}
ScenePatch::Rename { target, name } => {
let entity = self
.find_entity_mut(&target)
.ok_or_else(|| format!("scene entity `{target}` not found"))?;
entity.name = Some(name);
}
ScenePatch::SetTransform { target, transform } => {
let entity = self
.find_entity_mut(&target)
.ok_or_else(|| format!("scene entity `{target}` not found"))?;
let component =
SceneComponentBlob::from_serializable(TRANSFORM_COMPONENT, &transform)?;
if let Some(existing) = entity
.components
.iter_mut()
.find(|existing| existing.type_name == TRANSFORM_COMPONENT)
{
*existing = component;
} else {
entity.components.push(component);
}
}
}
Ok(())
}
fn find_entity_mut(&mut self, id: &SceneEntityId) -> Option<&mut SceneEntity> {
self.entities
.iter_mut()
.find(|entity| &entity.document_id == id)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum SceneEntityId {
ActorId(String),
Anonymous(String),
}
impl std::fmt::Display for SceneEntityId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ActorId(id) => write!(f, "actor:{id}"),
Self::Anonymous(id) => write!(f, "{id}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SceneEntity {
pub document_id: SceneEntityId,
pub actor_id: Option<String>,
pub name: Option<String>,
pub parent_actor_id: Option<String>,
pub components: Vec<SceneComponentBlob>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SceneComponentBlob {
pub type_name: String,
pub ron: String,
}
impl SceneComponentBlob {
pub fn from_serializable(
type_name: impl Into<String>,
value: &impl Serialize,
) -> Result<Self, String> {
let pretty = PrettyConfig::new();
let ron = ron::ser::to_string_pretty(value, pretty)
.map_err(|err| format!("could not serialize component blob: {err}"))?;
Ok(Self {
type_name: type_name.into(),
ron,
})
}
fn from_value(type_name: String, value: ron::Value) -> Result<Self, String> {
let pretty = PrettyConfig::new();
let ron = ron::ser::to_string_pretty(&value, pretty)
.map_err(|err| format!("could not serialize component value `{type_name}`: {err}"))?;
Ok(Self { type_name, ron })
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ScenePatch {
SetComponent {
target: SceneEntityId,
component: SceneComponentBlob,
},
RemoveComponent {
target: SceneEntityId,
component_type: String,
},
AddEntity {
entity: SceneEntity,
},
DeleteEntity {
target: SceneEntityId,
},
Reparent {
target: SceneEntityId,
parent_actor_id: Option<String>,
},
Rename {
target: SceneEntityId,
name: String,
},
SetTransform {
target: SceneEntityId,
transform: SceneTransform,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct SceneTransform {
pub translation: [f32; 3],
pub rotation: [f32; 4],
pub scale: [f32; 3],
}
impl Default for SceneTransform {
fn default() -> Self {
Self {
translation: [0.0, 0.0, 0.0],
rotation: [0.0, 0.0, 0.0, 1.0],
scale: [1.0, 1.0, 1.0],
}
}
}
#[derive(Debug, Deserialize)]
struct DynamicSceneBody {
#[serde(default)]
entities: BTreeMap<u64, DynamicSceneEntity>,
}
#[derive(Debug, Deserialize)]
struct DynamicSceneEntity {
#[serde(default)]
components: BTreeMap<String, ron::Value>,
}
const ACTOR_ID_COMPONENT: &str = "shared::components::ActorId";
const NAME_COMPONENT: &str = "bevy_ecs::name::Name";
pub const TRANSFORM_COMPONENT: &str = "bevy_transform::components::transform::Transform";
fn extract_name(components: &[SceneComponentBlob]) -> Option<String> {
components
.iter()
.find(|component| component.type_name == NAME_COMPONENT)
.map(|component| component.ron.trim().trim_matches('"').to_string())
.filter(|name| !name.is_empty())
}
fn extract_actor_id(components: &[SceneComponentBlob]) -> Option<String> {
let component = components
.iter()
.find(|component| component.type_name == ACTOR_ID_COMPONENT)?;
ron::from_str::<String>(&component.ron)
.ok()
.or_else(|| {
ron::from_str::<Vec<String>>(&component.ron)
.ok()
.and_then(|values| values.into_iter().next())
})
.filter(|value| !value.trim().is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_scene_document_without_exposing_runtime_entity_ids() {
let text = r#"(schema_version: 2,
resources: {},
entities: {
4294967133: (
components: {
"bevy_ecs::name::Name": "Crate",
"shared::components::ActorId": ("crate-01"),
"shared::components::LevelObject": (),
},
),
},
)"#;
let document = SceneDocument::from_ron_text(text).unwrap();
assert_eq!(document.schema_version, crate::CURRENT_SCENE_SCHEMA_VERSION);
assert_eq!(document.entities.len(), 1);
assert_eq!(document.entities[0].actor_id.as_deref(), Some("crate-01"));
assert_eq!(document.entities[0].name.as_deref(), Some("Crate"));
assert_eq!(
document.entities[0].document_id,
SceneEntityId::ActorId("crate-01".into())
);
assert!(!format!("{:?}", document.entities[0].document_id).contains("4294967133"));
}
#[test]
fn round_trips_normalized_ron_text() {
let body = "(resources: {}, entities: {})";
let document = SceneDocument::from_ron_text(body).unwrap();
let text = document.to_ron_text().unwrap();
assert!(text.starts_with("(schema_version: 2,"));
assert!(text.contains("entities: {}"));
}
#[test]
fn patch_rename_and_transform_are_representable() {
let text = r#"(schema_version: 2,
resources: {},
entities: {
1: (
components: {
"shared::components::ActorId": ("actor-a"),
"shared::components::LevelObject": (),
},
),
},
)"#;
let mut document = SceneDocument::from_ron_text(text).unwrap();
let target = SceneEntityId::ActorId("actor-a".into());
document
.apply_patch(ScenePatch::Rename {
target: target.clone(),
name: "Renamed".into(),
})
.unwrap();
document
.apply_patch(ScenePatch::SetTransform {
target: target.clone(),
transform: SceneTransform {
translation: [1.0, 2.0, 3.0],
..Default::default()
},
})
.unwrap();
let entity = document.find_entity(&target).unwrap();
assert_eq!(entity.name.as_deref(), Some("Renamed"));
assert!(entity
.components
.iter()
.any(|component| component.type_name == TRANSFORM_COMPONENT));
}
}
+1
View File
@@ -3,6 +3,7 @@
//! Bevy dynamic scenes are stored as RON tuples. On save the editor stamps
//! `schema_version` as the first root field; loaders strip it before deserializing.
pub mod document;
mod migrate;
use std::path::Path;
+4 -11
View File
@@ -29,7 +29,6 @@ pub enum GiPath {
#[reflect(Debug, PartialEq)]
pub enum RenderFallbackReason {
RtUnsupported,
LocalShadowLights,
}
/// Unified render-stack decision consumed by game, editor, and diagnostics.
@@ -204,10 +203,6 @@ pub fn resolve_effective_render_stack(
GiPath::SolariDeferred if !caps.rt_supported => {
(GiPath::Forward, Some(RenderFallbackReason::RtUnsupported))
}
GiPath::SolariDeferred if local_shadow_lights => (
GiPath::Forward,
Some(RenderFallbackReason::LocalShadowLights),
),
GiPath::SolariDeferred => (GiPath::SolariDeferred, None),
};
@@ -246,7 +241,7 @@ mod tests {
}
#[test]
fn effective_stack_falls_back_for_local_shadow_lights() {
fn effective_stack_keeps_solari_when_local_shadow_lights_exist() {
let caps = RenderingCapabilities {
rt_supported: true,
..Default::default()
@@ -254,10 +249,8 @@ mod tests {
let stack = resolve_effective_render_stack(GiPath::SolariDeferred, &caps, true, true);
assert_eq!(stack.effective_gi_path, GiPath::Forward);
assert_eq!(
stack.fallback_reason,
Some(RenderFallbackReason::LocalShadowLights)
);
assert_eq!(stack.effective_gi_path, GiPath::SolariDeferred);
assert_eq!(stack.fallback_reason, None);
assert!(stack.local_shadow_lights);
}
}
+71 -17
View File
@@ -2,7 +2,7 @@
use bevy::light::{CascadeShadowConfig, CascadeShadowConfigBuilder};
use bevy::prelude::*;
use settings::RenderingSettings;
use settings::{ActiveCameraRenderProfile, GiPath, RenderingCapabilities, RenderingSettings};
use crate::{
inspector_component_active, AuthoringLightKind, InspectorOrder, LevelObject, LightDesc,
@@ -29,26 +29,29 @@ pub fn cascade_config_from_rendering(rendering: &RenderingSettings) -> CascadeSh
pub fn hydrate_lights(
mut commands: Commands,
settings: Res<settings::ProjectSettings>,
lights: Query<
(Entity, &LightDesc, Option<&InspectorOrder>),
(
With<LevelObject>,
Or<(
Added<LightDesc>,
Changed<LightDesc>,
Changed<InspectorOrder>,
)>,
),
>,
profile: Option<Res<ActiveCameraRenderProfile>>,
caps: Option<Res<RenderingCapabilities>>,
lights: Query<(Entity, Ref<LightDesc>, Option<Ref<InspectorOrder>>), With<LevelObject>>,
) {
let rendering = &settings.rendering;
let render_stack_changed = profile.as_ref().is_some_and(|profile| profile.is_changed())
|| caps.as_ref().is_some_and(|caps| caps.is_changed());
let profile = profile.as_deref();
let caps = caps.as_deref();
for (entity, light, order) in &lights {
let mut entity_commands = commands.entity(entity);
strip_light_components(&mut entity_commands);
if !inspector_component_active(order, COMPONENT_LIGHT_DESC) {
let order_changed = order.as_ref().is_some_and(|order| order.is_changed());
if !light.is_changed() && !order_changed && !render_stack_changed {
continue;
}
insert_light_from_desc(&mut entity_commands, light, rendering);
let mut entity_commands = commands.entity(entity);
strip_light_components(&mut entity_commands);
if !inspector_component_active(order.as_deref(), COMPONENT_LIGHT_DESC) {
continue;
}
if local_light_disabled_in_solari(&light, profile, caps) {
continue;
}
insert_light_from_desc(&mut entity_commands, &light, rendering);
}
}
@@ -77,16 +80,27 @@ pub(crate) fn normalized_directional_lux(intensity: f32) -> f32 {
pub fn reconcile_missing_runtime_lights(
mut commands: Commands,
settings: Res<settings::ProjectSettings>,
profile: Option<Res<ActiveCameraRenderProfile>>,
caps: Option<Res<RenderingCapabilities>>,
lights: Query<(Entity, &LightDesc, Option<&InspectorOrder>), With<LevelObject>>,
points: Query<(), With<PointLight>>,
spots: Query<(), With<SpotLight>>,
directionals: Query<(), With<DirectionalLight>>,
) {
let rendering = &settings.rendering;
let profile = profile.as_deref();
let caps = caps.as_deref();
for (entity, light, order) in &lights {
if !inspector_component_active(order, COMPONENT_LIGHT_DESC) {
continue;
}
if local_light_disabled_in_solari(light, profile, caps) {
if points.contains(entity) || spots.contains(entity) || directionals.contains(entity) {
let mut entity_commands = commands.entity(entity);
strip_light_components(&mut entity_commands);
}
continue;
}
let missing = match light.kind {
AuthoringLightKind::Point => !points.contains(entity),
AuthoringLightKind::Spot => !spots.contains(entity),
@@ -101,6 +115,18 @@ pub fn reconcile_missing_runtime_lights(
}
}
pub(crate) fn local_light_disabled_in_solari(
light: &LightDesc,
profile: Option<&ActiveCameraRenderProfile>,
caps: Option<&RenderingCapabilities>,
) -> bool {
matches!(
light.kind,
AuthoringLightKind::Point | AuthoringLightKind::Spot
) && profile.is_some_and(|profile| profile.gi_path == GiPath::SolariDeferred)
&& caps.is_some_and(|caps| caps.rt_supported)
}
pub(crate) fn insert_light_from_desc(
entity: &mut EntityCommands<'_>,
light: &LightDesc,
@@ -156,7 +182,7 @@ mod tests {
use super::*;
use crate::{AuthoringLightKind, ColorDesc, LevelObject, LightDesc};
use bevy::ecs::world::CommandQueue;
use settings::RenderingSettings;
use settings::{ActiveCameraRenderProfile, GiPath, RenderingCapabilities, RenderingSettings};
#[test]
fn light_kind_swap_replaces_runtime_types() {
@@ -251,6 +277,34 @@ mod tests {
assert!(world.get::<Visibility>(spot_entity).is_some());
}
#[test]
fn solari_disables_point_and_spot_light_hydration() {
let profile = ActiveCameraRenderProfile {
gi_path: GiPath::SolariDeferred,
..Default::default()
};
let caps = RenderingCapabilities {
rt_supported: true,
..Default::default()
};
assert!(local_light_disabled_in_solari(
&LightDesc::for_kind(AuthoringLightKind::Point),
Some(&profile),
Some(&caps)
));
assert!(local_light_disabled_in_solari(
&LightDesc::for_kind(AuthoringLightKind::Spot),
Some(&profile),
Some(&caps)
));
assert!(!local_light_disabled_in_solari(
&LightDesc::for_kind(AuthoringLightKind::Directional),
Some(&profile),
Some(&caps)
));
}
#[test]
fn point_light_default_casts_shadows() {
let light = LightDesc::for_kind(AuthoringLightKind::Point);
+75 -7
View File
@@ -138,13 +138,6 @@ pub fn flush_level_object_hydration(world: &mut World) {
}
}
let lights: Vec<(Entity, crate::LightDesc)> = world
.query_filtered::<(Entity, &crate::LightDesc, Option<&InspectorOrder>), With<LevelObject>>()
.iter(world)
.filter(|(_, _, order)| inspector_component_active(*order, COMPONENT_LIGHT_DESC))
.map(|(entity, light, _)| (entity, light.clone()))
.collect();
let visibility_targets: Vec<(Entity, EditorVisibility)> = world
.query_filtered::<(Entity, &EditorVisibility), With<LevelObject>>()
.iter(world)
@@ -155,6 +148,22 @@ pub fn flush_level_object_hydration(world: &mut World) {
.get_resource::<settings::ProjectSettings>()
.map(|s| s.rendering.clone())
.unwrap_or_default();
let profile = world
.get_resource::<settings::ActiveCameraRenderProfile>()
.cloned();
let caps = world
.get_resource::<settings::RenderingCapabilities>()
.cloned();
let lights: Vec<(Entity, crate::LightDesc)> = world
.query_filtered::<(Entity, &crate::LightDesc, Option<&InspectorOrder>), With<LevelObject>>()
.iter(world)
.filter(|(_, light, order)| {
inspector_component_active(*order, COMPONENT_LIGHT_DESC)
&& !lights::local_light_disabled_in_solari(light, profile.as_ref(), caps.as_ref())
})
.map(|(entity, light, _)| (entity, light.clone()))
.collect();
let mut state: SystemState<(
Commands,
@@ -245,6 +254,65 @@ mod tests {
assert!(app.world().get::<PointLight>(entity).is_some());
}
#[test]
fn flush_hydration_skips_point_light_when_solari_is_active() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(AssetPlugin::default());
app.add_plugins(bevy::pbr::MaterialPlugin::<StandardMaterial>::default());
app.init_asset::<Mesh>();
app.world_mut()
.insert_resource(settings::ActiveCameraRenderProfile {
gi_path: settings::GiPath::SolariDeferred,
..Default::default()
});
app.world_mut()
.insert_resource(settings::RenderingCapabilities {
rt_supported: true,
..Default::default()
});
let entity = app
.world_mut()
.spawn((LevelObject, LightDesc::for_kind(AuthoringLightKind::Point)))
.id();
flush_level_object_hydration(app.world_mut());
assert!(app.world().get::<PointLight>(entity).is_none());
}
#[test]
fn flush_hydration_keeps_directional_light_when_solari_is_active() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(AssetPlugin::default());
app.add_plugins(bevy::pbr::MaterialPlugin::<StandardMaterial>::default());
app.init_asset::<Mesh>();
app.world_mut()
.insert_resource(settings::ActiveCameraRenderProfile {
gi_path: settings::GiPath::SolariDeferred,
..Default::default()
});
app.world_mut()
.insert_resource(settings::RenderingCapabilities {
rt_supported: true,
..Default::default()
});
let entity = app
.world_mut()
.spawn((
LevelObject,
LightDesc::for_kind(AuthoringLightKind::Directional),
))
.id();
flush_level_object_hydration(app.world_mut());
assert!(app.world().get::<DirectionalLight>(entity).is_some());
}
#[test]
fn flush_hydration_skips_inactive_light_desc() {
let mut app = App::new();