Initial project import
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-06-05 21:44:45 -04:00
commit f29712d158
259 files changed
+52069

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
[package]
name = "settings"
version.workspace = true
edition.workspace = true
license.workspace = true
description = "Project-wide settings (rendering, physics, input) for the Bevy FPS foundation"
[dependencies]
bevy.workspace = true
serde = { workspace = true, features = ["derive"] }
ron = "0.8"
[dev-dependencies]
serde = { workspace = true, features = ["derive"] }
+264
View File
@@ -0,0 +1,264 @@
//! Serializable project settings loaded from `assets/project.ron`.
mod plugin;
mod rendering;
pub use plugin::{
save_project_settings, sync_sim_tuning, ProjectSettingsChanged, ProjectSettingsIo,
ProjectSettingsPlugin, SimTuning,
};
pub use rendering::{
effective_auto_exposure, resolve_effective_render_stack, resolve_gi_path,
write_effective_render_stack, ActiveCameraRenderProfile, ActiveVolumeContribution,
EffectiveRenderStack, ExposureMode, GiMode, GiPath, RenderFallbackReason,
RenderingCapabilities, VolumeContribution,
};
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
pub const DEFAULT_PROJECT_PATH: &str = "assets/project.ron";
pub const SETTINGS_VERSION: u32 = 1;
/// Marker for cameras that receive the shared project rendering stack.
#[derive(Component, Debug, Clone, Copy, Default)]
pub struct ProjectRenderCamera;
/// Root project manifest and tunable sections.
#[derive(Resource, Debug, Clone, Serialize, Deserialize, Reflect)]
#[reflect(Resource, Clone)]
pub struct ProjectSettings {
pub version: u32,
pub name: String,
pub default_level: String,
pub asset_roots: Vec<String>,
pub rendering: RenderingSettings,
pub physics: PhysicsSettings,
pub input: InputSettings,
}
impl Default for ProjectSettings {
fn default() -> Self {
Self {
version: SETTINGS_VERSION,
name: "Bevy FPS Foundation".into(),
default_level: "assets/levels/editor_scene.scn.ron".into(),
asset_roots: vec![
"assets/models".into(),
"assets/textures".into(),
"assets/materials".into(),
"assets/levels".into(),
],
rendering: RenderingSettings::default(),
physics: PhysicsSettings::default(),
input: InputSettings::default(),
}
}
}
/// Camera post-processing and world lighting defaults.
#[derive(Debug, Clone, Serialize, Deserialize, Reflect)]
#[reflect(Clone)]
pub struct RenderingSettings {
#[serde(default)]
pub gi_mode: GiMode,
pub hdr: bool,
#[serde(default)]
pub exposure_mode: ExposureMode,
pub exposure_ev100: f32,
#[serde(default = "default_auto_exposure_speed_brighten")]
pub auto_exposure_speed_brighten: f32,
#[serde(default = "default_auto_exposure_speed_darken")]
pub auto_exposure_speed_darken: f32,
pub tonemapping_aces: bool,
pub bloom: bool,
pub taa: bool,
pub ssao: bool,
pub atmosphere: bool,
pub fog_color: [f32; 3],
pub fog_density: f32,
pub ambient_color: [f32; 3],
pub ambient_brightness: f32,
pub sun_illuminance: f32,
pub shadow_cascades: u32,
pub shadow_first_cascade: f32,
pub shadow_max_distance: f32,
}
fn default_auto_exposure_speed_brighten() -> f32 {
3.0
}
fn default_auto_exposure_speed_darken() -> f32 {
1.0
}
impl Default for RenderingSettings {
fn default() -> Self {
Self {
gi_mode: GiMode::default(),
hdr: true,
exposure_mode: ExposureMode::default(),
exposure_ev100: 13.0,
auto_exposure_speed_brighten: default_auto_exposure_speed_brighten(),
auto_exposure_speed_darken: default_auto_exposure_speed_darken(),
tonemapping_aces: true,
bloom: true,
taa: true,
ssao: true,
atmosphere: true,
fog_color: [0.66, 0.75, 0.86],
fog_density: 0.0012,
ambient_color: [0.6, 0.7, 0.85],
ambient_brightness: 60.0,
sun_illuminance: 100_000.0,
shadow_cascades: 4,
shadow_first_cascade: 6.0,
shadow_max_distance: 220.0,
}
}
}
/// Gameplay movement and capsule constants (mirrors former `sim::tuning`).
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Reflect)]
#[reflect(Clone)]
pub struct PhysicsSettings {
pub player_radius: f32,
pub player_length: f32,
pub eye_offset_stand: f32,
pub eye_offset_crouch: f32,
pub gravity: f32,
pub walk_speed: f32,
pub run_speed: f32,
pub crouch_speed: f32,
pub jump_speed: f32,
pub ground_accel: f32,
pub air_accel: f32,
pub ground_check_dist: f32,
pub skin: f32,
pub max_slide_iter: u32,
pub ground_normal_y: f32,
pub coyote_time: f32,
pub jump_buffer: f32,
}
impl Default for PhysicsSettings {
fn default() -> Self {
Self {
player_radius: 0.4,
player_length: 1.0,
eye_offset_stand: 0.6,
eye_offset_crouch: 0.1,
gravity: 22.0,
walk_speed: 6.0,
run_speed: 9.5,
crouch_speed: 3.0,
jump_speed: 7.5,
ground_accel: 70.0,
air_accel: 16.0,
ground_check_dist: 0.2,
skin: 0.02,
max_slide_iter: 4,
ground_normal_y: 0.5,
coyote_time: 0.12,
jump_buffer: 0.12,
}
}
}
/// Input sensitivities and editor fly camera speeds.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Reflect)]
#[reflect(Clone)]
pub struct InputSettings {
pub yaw_sensitivity: f32,
pub pitch_sensitivity: f32,
pub editor_fly_speed: f32,
pub editor_look_sensitivity: f32,
pub editor_pan_speed: f32,
}
impl Default for InputSettings {
fn default() -> Self {
Self {
yaw_sensitivity: 0.003,
pitch_sensitivity: 0.0022,
editor_fly_speed: 14.0,
editor_look_sensitivity: 0.003,
editor_pan_speed: 0.015,
}
}
}
/// Loads settings from disk, falling back to defaults.
pub fn load_project_settings_from_path(path: &str) -> ProjectSettings {
match std::fs::read_to_string(path) {
Ok(contents) => match ron::from_str(&contents) {
Ok(settings) => settings,
Err(error) => {
warn!("Failed to parse {path}: {error}; using defaults");
ProjectSettings::default()
}
},
Err(error) => {
warn!("Could not read {path}: {error}; using defaults");
ProjectSettings::default()
}
}
}
/// Serializes settings to RON.
pub fn save_project_settings_to_string(settings: &ProjectSettings) -> Result<String, ron::Error> {
ron::ser::to_string_pretty(settings, ron::ser::PrettyConfig::default())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn settings_round_trip() {
let settings = ProjectSettings::default();
let text = save_project_settings_to_string(&settings).expect("serialize");
let parsed: ProjectSettings = ron::from_str(&text).expect("deserialize");
assert_eq!(parsed.name, settings.name);
assert_eq!(parsed.physics.walk_speed, settings.physics.walk_speed);
assert_eq!(parsed.rendering.gi_mode, settings.rendering.gi_mode);
}
#[test]
fn gi_mode_defaults_auto() {
assert_eq!(RenderingSettings::default().gi_mode, GiMode::Auto);
}
#[test]
fn exposure_mode_auto_round_trip() {
let mut settings = ProjectSettings::default();
settings.rendering.exposure_mode = ExposureMode::Auto;
settings.rendering.auto_exposure_speed_brighten = 4.0;
settings.rendering.auto_exposure_speed_darken = 0.5;
let text = save_project_settings_to_string(&settings).expect("serialize");
let parsed: ProjectSettings = ron::from_str(&text).expect("deserialize");
assert_eq!(parsed.rendering.exposure_mode, ExposureMode::Auto);
assert_eq!(parsed.rendering.auto_exposure_speed_brighten, 4.0);
assert_eq!(parsed.rendering.auto_exposure_speed_darken, 0.5);
}
#[test]
fn effective_auto_exposure_requires_hdr_and_compute() {
let mut profile = ActiveCameraRenderProfile::default();
profile.exposure_mode = ExposureMode::Auto;
profile.hdr = true;
let caps = RenderingCapabilities {
auto_exposure_supported: true,
..Default::default()
};
assert!(effective_auto_exposure(&profile, &caps));
profile.hdr = false;
assert!(!effective_auto_exposure(&profile, &caps));
profile.hdr = true;
let caps = RenderingCapabilities::default();
assert!(!effective_auto_exposure(&profile, &caps));
}
}
+112
View File
@@ -0,0 +1,112 @@
use bevy::prelude::*;
use crate::{
load_project_settings_from_path, save_project_settings_to_string, PhysicsSettings,
ProjectSettings, DEFAULT_PROJECT_PATH,
};
/// Runtime copy of physics tuning consumed by `sim` systems.
#[derive(Resource, Debug, Clone, Copy)]
pub struct SimTuning {
pub player_radius: f32,
pub player_length: f32,
pub eye_offset_stand: f32,
pub eye_offset_crouch: f32,
pub gravity: f32,
pub walk_speed: f32,
pub run_speed: f32,
pub crouch_speed: f32,
pub jump_speed: f32,
pub ground_accel: f32,
pub air_accel: f32,
pub ground_check_dist: f32,
pub skin: f32,
pub max_slide_iter: u32,
pub ground_normal_y: f32,
pub coyote_time: f32,
pub jump_buffer: f32,
}
impl SimTuning {
pub fn from_physics(physics: &PhysicsSettings) -> Self {
Self {
player_radius: physics.player_radius,
player_length: physics.player_length,
eye_offset_stand: physics.eye_offset_stand,
eye_offset_crouch: physics.eye_offset_crouch,
gravity: physics.gravity,
walk_speed: physics.walk_speed,
run_speed: physics.run_speed,
crouch_speed: physics.crouch_speed,
jump_speed: physics.jump_speed,
ground_accel: physics.ground_accel,
air_accel: physics.air_accel,
ground_check_dist: physics.ground_check_dist,
skin: physics.skin,
max_slide_iter: physics.max_slide_iter,
ground_normal_y: physics.ground_normal_y,
coyote_time: physics.coyote_time,
jump_buffer: physics.jump_buffer,
}
}
}
/// Tracks project settings file path and dirty state (editor persists via UI).
#[derive(Resource, Debug)]
pub struct ProjectSettingsIo {
pub path: String,
pub dirty: bool,
}
impl Default for ProjectSettingsIo {
fn default() -> Self {
Self {
path: DEFAULT_PROJECT_PATH.into(),
dirty: false,
}
}
}
/// Fired after project settings are saved or reloaded from disk.
#[derive(Message, Debug, Clone, Copy, Default)]
pub struct ProjectSettingsChanged;
pub struct ProjectSettingsPlugin;
impl Plugin for ProjectSettingsPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<ProjectSettingsIo>()
.register_type::<ProjectSettings>()
.register_type::<crate::GiMode>()
.register_type::<crate::ExposureMode>()
.register_type::<crate::ActiveCameraRenderProfile>()
.register_type::<crate::EffectiveRenderStack>()
.register_type::<crate::RenderFallbackReason>()
.register_type::<crate::RenderingCapabilities>()
.add_message::<ProjectSettingsChanged>()
.add_systems(PreStartup, load_settings);
}
}
fn load_settings(mut commands: Commands, io: Res<ProjectSettingsIo>) {
let settings = load_project_settings_from_path(&io.path);
let tuning = SimTuning::from_physics(&settings.physics);
commands.insert_resource(settings);
commands.insert_resource(tuning);
}
/// Refreshes [`SimTuning`] from the current [`ProjectSettings`] resource.
pub fn sync_sim_tuning(settings: &ProjectSettings, mut tuning: ResMut<SimTuning>) {
*tuning = SimTuning::from_physics(&settings.physics);
}
/// Persists settings to the path in [`ProjectSettingsIo`].
pub fn save_project_settings(
settings: &ProjectSettings,
io: &mut ProjectSettingsIo,
) -> Result<(), String> {
let text = save_project_settings_to_string(settings).map_err(|error| error.to_string())?;
std::fs::write(&io.path, text).map_err(|error| error.to_string())?;
io.dirty = false;
Ok(())
}
+263
View File
@@ -0,0 +1,263 @@
//! Runtime rendering profile types (no egui).
use bevy::prelude::*;
use serde::{Deserialize, Serialize};
use crate::RenderingSettings;
/// Global illumination policy from project settings.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Reflect)]
#[reflect(Default, Debug, PartialEq, Serialize, Deserialize)]
pub enum GiMode {
#[default]
Auto,
Forward,
Solari,
}
/// Resolved GI path for the active camera this frame.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Reflect)]
#[reflect(Default, Debug, PartialEq)]
pub enum GiPath {
#[default]
Forward,
SolariDeferred,
}
/// Reason the active camera is rendering with a different path than requested.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Debug, PartialEq)]
pub enum RenderFallbackReason {
RtUnsupported,
LocalShadowLights,
}
/// Unified render-stack decision consumed by game, editor, and diagnostics.
#[derive(Resource, Debug, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Resource, Debug, Clone, PartialEq)]
pub struct EffectiveRenderStack {
pub requested_gi_path: GiPath,
pub effective_gi_path: GiPath,
pub fallback_reason: Option<RenderFallbackReason>,
pub solari_ready: bool,
pub local_shadow_lights: bool,
}
impl Default for EffectiveRenderStack {
fn default() -> Self {
Self {
requested_gi_path: GiPath::Forward,
effective_gi_path: GiPath::Forward,
fallback_reason: None,
solari_ready: false,
local_shadow_lights: false,
}
}
}
/// Fixed EV100 on the camera, or Bevy histogram-based auto exposure.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, Reflect)]
#[reflect(Default, Debug, PartialEq, Serialize, Deserialize)]
pub enum ExposureMode {
#[default]
Manual,
Auto,
}
/// GPU/runtime GI capabilities detected at startup and updated when path changes.
#[derive(Resource, Debug, Clone, Reflect)]
#[reflect(Resource, Debug, Clone)]
pub struct RenderingCapabilities {
pub rt_supported: bool,
pub auto_exposure_supported: bool,
pub requested_gi_path: GiPath,
pub effective_gi_path: GiPath,
pub fallback_reason: Option<RenderFallbackReason>,
/// Deprecated compatibility field. Prefer [`RenderingCapabilities::effective_gi_path`].
pub active_gi_path: GiPath,
}
impl Default for RenderingCapabilities {
fn default() -> Self {
Self {
rt_supported: false,
auto_exposure_supported: false,
requested_gi_path: GiPath::Forward,
effective_gi_path: GiPath::Forward,
fallback_reason: None,
active_gi_path: GiPath::Forward,
}
}
}
/// Whether the resolved profile should drive [`bevy::post_process::auto_exposure::AutoExposure`].
pub fn effective_auto_exposure(
profile: &ActiveCameraRenderProfile,
caps: &RenderingCapabilities,
) -> bool {
profile.exposure_mode == ExposureMode::Auto && profile.hdr && caps.auto_exposure_supported
}
/// Resolved post-process + GI stack for the active camera (read-only for UI).
#[derive(Resource, Debug, Clone, PartialEq, Reflect)]
#[reflect(Resource, Debug, Clone, PartialEq)]
pub struct ActiveCameraRenderProfile {
pub gi_path: GiPath,
pub hdr: bool,
pub exposure_mode: ExposureMode,
pub exposure_ev100: f32,
pub auto_exposure_speed_brighten: f32,
pub auto_exposure_speed_darken: f32,
pub tonemapping_aces: bool,
pub bloom: bool,
pub taa: bool,
pub ssao: bool,
pub atmosphere: bool,
pub fog_color: [f32; 3],
pub fog_density: f32,
pub fullscreen_effect: Option<String>,
}
impl ActiveCameraRenderProfile {
pub fn from_project(rendering: &RenderingSettings, gi_path: GiPath) -> Self {
Self {
gi_path,
hdr: rendering.hdr,
exposure_mode: rendering.exposure_mode,
exposure_ev100: rendering.exposure_ev100,
auto_exposure_speed_brighten: rendering.auto_exposure_speed_brighten,
auto_exposure_speed_darken: rendering.auto_exposure_speed_darken,
tonemapping_aces: rendering.tonemapping_aces,
bloom: rendering.bloom,
taa: rendering.taa,
ssao: rendering.ssao,
atmosphere: rendering.atmosphere,
fog_color: rendering.fog_color,
fog_density: rendering.fog_density,
fullscreen_effect: None,
}
}
}
impl Default for ActiveCameraRenderProfile {
fn default() -> Self {
Self::from_project(&RenderingSettings::default(), GiPath::Forward)
}
}
/// One contributing post-process volume for editor HUD (max 3 stored).
#[derive(Debug, Clone, Reflect)]
#[reflect(Debug, Clone)]
pub struct VolumeContribution {
pub entity_bits: u64,
pub weight: f32,
pub label: String,
pub override_count: u32,
}
/// Top volume contributors at the active camera position (editor HUD).
#[derive(Resource, Debug, Clone, Default, Reflect)]
#[reflect(Resource, Debug, Clone, Default)]
pub struct ActiveVolumeContribution {
pub entries: Vec<VolumeContribution>,
}
impl ActiveVolumeContribution {
pub const MAX_ENTRIES: usize = 3;
pub fn set_entries(&mut self, entries: Vec<VolumeContribution>) {
self.entries = entries;
self.entries.truncate(Self::MAX_ENTRIES);
}
}
/// Resolves the requested GI path from project mode and capabilities.
pub fn resolve_gi_path(gi_mode: GiMode, caps: &RenderingCapabilities) -> GiPath {
match gi_mode {
GiMode::Forward => GiPath::Forward,
GiMode::Solari => {
if caps.rt_supported {
GiPath::SolariDeferred
} else {
GiPath::Forward
}
}
GiMode::Auto => {
if caps.rt_supported {
GiPath::SolariDeferred
} else {
GiPath::Forward
}
}
}
}
/// Resolves the effective camera GI path from project/volume intent and runtime state.
pub fn resolve_effective_render_stack(
requested_gi_path: GiPath,
caps: &RenderingCapabilities,
solari_ready: bool,
local_shadow_lights: bool,
) -> EffectiveRenderStack {
let (effective_gi_path, fallback_reason) = match requested_gi_path {
GiPath::Forward => (GiPath::Forward, None),
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),
};
EffectiveRenderStack {
requested_gi_path,
effective_gi_path,
fallback_reason,
solari_ready,
local_shadow_lights,
}
}
/// Mirrors the explicit stack decision into the capabilities resource for UI/readback.
pub fn write_effective_render_stack(caps: &mut RenderingCapabilities, stack: EffectiveRenderStack) {
caps.requested_gi_path = stack.requested_gi_path;
caps.effective_gi_path = stack.effective_gi_path;
caps.fallback_reason = stack.fallback_reason;
caps.active_gi_path = stack.effective_gi_path;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn effective_stack_uses_solari_when_ready() {
let caps = RenderingCapabilities {
rt_supported: true,
..Default::default()
};
let stack = resolve_effective_render_stack(GiPath::SolariDeferred, &caps, true, false);
assert_eq!(stack.effective_gi_path, GiPath::SolariDeferred);
assert_eq!(stack.fallback_reason, None);
}
#[test]
fn effective_stack_falls_back_for_local_shadow_lights() {
let caps = RenderingCapabilities {
rt_supported: true,
..Default::default()
};
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)
);
}
}