From 3429e8f0bdb907099afcbc685eac70ac9a57a3eb Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Sun, 26 Nov 2023 22:49:21 -0800 Subject: [PATCH] Parsing fog implemented. Now just need to use the fog values... --- assets/martian.tweak.toml | 13 +++++++- src/display3d.rs | 68 ++++++++++++++++++++++++++++++--------- src/main.rs | 2 +- src/tweak.rs | 2 ++ 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/assets/martian.tweak.toml b/assets/martian.tweak.toml index 454f0ec..4e92761 100644 --- a/assets/martian.tweak.toml +++ b/assets/martian.tweak.toml @@ -12,4 +12,15 @@ invalid = "/sfx/3D/3DInvalidMove" main = "/Music/Main Track2" [audio.menu] -select = "/SFX/MenuSelect" \ No newline at end of file +select = "/SFX/MenuSelect" + +[display3d.fog] +exponent = 2.0 +# https://docs.rs/bevy/latest/bevy/pbr/enum.FogFalloff.html +falloff = "Atmospheric (extinction 1.0 1.0 1.0) (inscattering 1.0 1.0 1.0)" + +[display3d.fog.color] +Rgba = { red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0 } + +[display3d.fog.light_color] +Rgba = { red = 1.0, green = 1.0, blue = 1.0, alpha = 1.0 } \ No newline at end of file diff --git a/src/display3d.rs b/src/display3d.rs index 0427295..e5e1d56 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -11,7 +11,7 @@ use bevy::{ }, window::PrimaryWindow, }; -use serde::{Deserialize, Deserializer}; +use serde::{Deserialize, Deserializer, de}; pub(crate) struct Display3dPlugin; @@ -83,13 +83,13 @@ impl Plugin for Display3dPlugin { } } -#[derive(Deserialize, Default)] -struct Display3dTweaks { +#[derive(Debug, Deserialize, Default)] +pub(crate) struct Display3dTweaks { #[serde(default)] fog: FogTweaks, } -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] struct FogTweaks { #[serde(default)] color: Color, @@ -101,6 +101,19 @@ struct FogTweaks { falloff: FogFalloff, } +impl Default for FogTweaks { + fn default() -> Self { + FogTweaks { + color: Color::WHITE, + light_color: Color::WHITE, + exponent: 1.0, + falloff: FogFalloff::Exponential { + density: 1.0, + } + } + } +} + fn default_fog_falloff() -> FogFalloff { FogFalloff::Exponential { density: 1.0, @@ -108,26 +121,51 @@ fn default_fog_falloff() -> FogFalloff { } fn deserialize_fog_falloff<'de, D: Deserializer<'de>>(deserializer: D) -> Result { - if let Ok(s) = String::deserailize(deserializer) { + if let Ok(s) = String::deserialize(deserializer) { // Linear [start f32] [end f32] if s.starts_with("Linear") { - todo!("Parse linear parameters"); - Ok(FogFalloff::Linear { ..default() }) + let mut parts = s.split(' '); + let start: f32 = parts.nth(1).expect("Fog Linear Start").parse().expect("Floating point number"); + let end: f32 = parts.nth(2).expect("Fog Linear End").parse().expect("Floating point number"); + Ok(FogFalloff::Linear { start, end }) // Exponential [density f32] } else if s.starts_with("Exponential") { - todo!("Parse exponential parameters"); - Ok(FogFalloff::Exponential { ..default() }) + let mut parts = s.split(' '); + let density: f32 = parts.nth(1).expect("Fog Exponential Density").parse().expect("Floating point number"); + Ok(FogFalloff::Exponential { density }) // ExponentialSquared [density f32] } else if s.starts_with("ExponentialSquared") { - todo!("Parse exponentialsquared parameters"); - Ok(FogFalloff::ExponentialSquared { ..default() }) + let mut parts = s.split(' '); + let density: f32 = parts.nth(1).expect("Fog Exponential Density Squared").parse().expect("Floating point number"); + Ok(FogFalloff::ExponentialSquared { density }) // Atmospheric (extinction [r: f32] [g: f32] [b: f32]) (inscattering [r: f32] [g: f32] [b: f32]) - } else if s.startswith("Atmospheric") { - todo!("Parse atmospheric parameters"); - Ok(FogFalloff::Atmospheric { ..default() }) + } else if s.starts_with("Atmospheric") { + let extinction = { + let start = s.find("extinction").expect("Extinction start"); + let end = s[start..].match_indices(')').find_map(|(i, _)| Some(i)).expect("Extinction end"); + let mut parts = s[start..start+end].split(' '); + let _ = parts.next(); + let r: f32 = parts.next().expect("red value").parse().expect("Floating point number"); + let g: f32 = parts.next().expect("green value").parse().expect("Floating point number"); + let b: f32 = parts.next().expect("blue value").parse().expect("Floating point number"); + Vec3::new(r, g, b) + }; + let inscattering = { + let start = s.find("inscattering").expect("Inscattering start"); + let end = s[start..].match_indices(')').find_map(|(i, _)| Some(i)).expect("Inscattering end"); + let mut parts = s[start..start+end].split(' '); + let _ = parts.next(); + let r: f32 = parts.next().expect("red value").parse().expect("Floating point number"); + let g: f32 = parts.next().expect("green value").parse().expect("Floating point number"); + let b: f32 = parts.next().expect("blue value").parse().expect("Floating point number"); + Vec3::new(r, g, b) + }; + Ok(FogFalloff::Atmospheric { extinction, inscattering }) } else { - Err(D::Error) + Err(de::Error::custom("Failed to parse fog value")) } + } else { + Err(de::Error::custom("Failed to parse fog value")) } } diff --git a/src/main.rs b/src/main.rs index d2ed3dc..4b5775c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use std::time::Duration; use bevy::{ asset::{ChangeWatcher, HandleId}, - input::ButtonState, pbr::FogPlugin, core_pipeline::fxaa::FxaaPlugin, + input::ButtonState, }; use crate::prelude::*; diff --git a/src/tweak.rs b/src/tweak.rs index b3a96dd..8599c39 100644 --- a/src/tweak.rs +++ b/src/tweak.rs @@ -34,6 +34,8 @@ struct GameTweakfile(Handle); pub(crate) struct Tweakfile { #[serde(default)] pub audio: audio::AudioTweaks, + #[serde(default)] + pub display3d: display3d::Display3dTweaks, }