Добавить намокание грунта под дождём
This commit is contained in:
@@ -19,7 +19,7 @@ pub(crate) const VERSION: f32 = 1.0;
|
||||
/// The tail's size in `vec4` rows.
|
||||
pub(crate) const TAIL_ROWS: u64 = EXTENSION_TAIL_BYTES / 16;
|
||||
/// The rows this build writes; the rest of the tail stays zero.
|
||||
pub(crate) const ROWS: usize = 36;
|
||||
pub(crate) const ROWS: usize = 53;
|
||||
|
||||
/// The block's rows by owner.
|
||||
pub(crate) mod row {
|
||||
@@ -66,6 +66,10 @@ pub(crate) mod row {
|
||||
pub(crate) const GROUND_FOG: usize = 19;
|
||||
/// Sixteen rows of 8x8 terrain heights; missing samples carry a sentinel.
|
||||
pub(crate) const GROUND_HEIGHTS: usize = 20;
|
||||
/// Wet terrain strength and grid origin (`x`, `yz`) and spacing (`w`).
|
||||
pub(crate) const WET: usize = 36;
|
||||
/// Sixteen rows holding the 8x8 exposure samples.
|
||||
pub(crate) const WET_GRID: usize = 37;
|
||||
}
|
||||
|
||||
/// This frame's block, filled by the features and extracted to the render world.
|
||||
|
||||
@@ -41,6 +41,7 @@ mod static_casters;
|
||||
mod torch_shadows;
|
||||
mod water;
|
||||
mod water_scene;
|
||||
mod wet_terrain;
|
||||
mod wind;
|
||||
|
||||
use bevy::prelude::*;
|
||||
@@ -116,6 +117,7 @@ pub(crate) fn patches() -> Vec<ShaderPatch> {
|
||||
shadow::terrain_edits(TERRAIN.binding)
|
||||
.into_iter()
|
||||
.chain(point_px::terrain_edits(TERRAIN.binding))
|
||||
.chain(wet_terrain::terrain_edits())
|
||||
.collect(),
|
||||
),
|
||||
),
|
||||
@@ -168,6 +170,7 @@ impl Plugin for MoonwellGfxPlugin {
|
||||
ground_fog::publish.after(bevy::transform::TransformSystems::Propagate),
|
||||
wind::publish,
|
||||
water::publish,
|
||||
wet_terrain::publish.after(bevy::transform::TransformSystems::Propagate),
|
||||
point_px::publish.after(bevy::transform::TransformSystems::Propagate),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -291,6 +291,15 @@ const ITEMS: &[Item] = &[
|
||||
"Дальность, ярды",
|
||||
"Distance, yd",
|
||||
),
|
||||
section("Погода", "Weather"),
|
||||
slider(
|
||||
"weather.wetness",
|
||||
0.0,
|
||||
1.0,
|
||||
0.05,
|
||||
"Мокрая земля",
|
||||
"Wet ground",
|
||||
),
|
||||
section("Вода", "Water"),
|
||||
switch("water.enabled", "Новая вода", "Improved water"),
|
||||
slider(
|
||||
|
||||
@@ -29,6 +29,15 @@ pub struct GfxSettings {
|
||||
pub water: WaterSettings,
|
||||
pub lights: LightSettings,
|
||||
pub grass: GrassSettings,
|
||||
pub weather: WeatherSettings,
|
||||
}
|
||||
|
||||
/// Optional rain response for terrain; zero preserves the stock image.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct WeatherSettings {
|
||||
/// Strength of rain wetness, 0..1.
|
||||
pub wetness: f64,
|
||||
}
|
||||
|
||||
/// The ground cover: denser grass and flowers, drawn farther out than the reference's 70 yd.
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
//! Optional rain wetness on terrain. Stock weather (`0x67bcce`) supplies precipitation density;
|
||||
//! this material response and its coarse exposure grid are MoonWell's extension.
|
||||
|
||||
use benilla_world::collision::WorldCollision;
|
||||
use benilla_world::view::WorldCamera;
|
||||
use benilla_world::weather::{WeatherKind, WeatherState};
|
||||
use benilla_world::world_map::CurrentMap;
|
||||
use benilla_world::world_point::TerrainHeights;
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::frame::{row, GfxFrame};
|
||||
use crate::patch::Edit;
|
||||
use crate::settings::GfxSettings;
|
||||
|
||||
const SPACING: f32 = 12.0;
|
||||
|
||||
pub(crate) struct WetState {
|
||||
map: Option<u32>,
|
||||
amount: f32,
|
||||
key: Option<(u32, i32, i32)>,
|
||||
next_refresh: f64,
|
||||
samples: [f32; 64],
|
||||
}
|
||||
|
||||
impl Default for WetState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
map: None,
|
||||
amount: 0.0,
|
||||
key: None,
|
||||
next_refresh: 0.0,
|
||||
samples: [0.0; 64],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WetState {
|
||||
fn set_map(&mut self, map: Option<u32>) {
|
||||
if self.map != map {
|
||||
self.map = map;
|
||||
self.amount = 0.0;
|
||||
self.key = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn strength_setting(value: f64) -> f32 {
|
||||
if value.is_finite() {
|
||||
value.clamp(0.0, 1.0) as f32
|
||||
} else {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Move at a fixed rate so a full wetting takes 3 s and drying takes 20 s.
|
||||
fn advance(value: f32, target: f32, dt: f32) -> f32 {
|
||||
let step = dt.max(0.0) / if target > value { 3.0 } else { 20.0 };
|
||||
if target > value {
|
||||
(value + step).min(target)
|
||||
} else {
|
||||
(value - step).max(target)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn publish(
|
||||
settings: Res<GfxSettings>,
|
||||
weather: Res<WeatherState>,
|
||||
time: Res<Time<Real>>,
|
||||
camera: Query<&GlobalTransform, With<WorldCamera>>,
|
||||
map: Option<Res<CurrentMap>>,
|
||||
heights: TerrainHeights,
|
||||
collision: WorldCollision,
|
||||
mut state: Local<WetState>,
|
||||
mut frame: ResMut<GfxFrame>,
|
||||
) {
|
||||
state.set_map(map.as_deref().map(|m| m.0));
|
||||
let target = if weather.effect_kind == WeatherKind::Rain {
|
||||
weather.effect_density.clamp(0.0, 1.0)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
state.amount = advance(state.amount, target, time.delta_secs());
|
||||
frame.rows[row::WET] = [0.0; 4];
|
||||
let strength = strength_setting(settings.weather.wetness) * state.amount;
|
||||
let Ok(eye) = camera.single() else {
|
||||
state.key = None;
|
||||
return;
|
||||
};
|
||||
if strength <= 0.0 {
|
||||
state.key = None;
|
||||
return;
|
||||
}
|
||||
let Some(map) = map else {
|
||||
state.key = None;
|
||||
return;
|
||||
};
|
||||
let eye = eye.translation();
|
||||
let x = (eye.x / SPACING).floor() as i32 - 3;
|
||||
let z = (eye.z / SPACING).floor() as i32 - 3;
|
||||
let key = (map.0, x, z);
|
||||
if state.key != Some(key) || time.elapsed_secs_f64() >= state.next_refresh {
|
||||
for j in 0..8 {
|
||||
for i in 0..8 {
|
||||
let pos = Vec3::new(
|
||||
(x + i as i32) as f32 * SPACING,
|
||||
eye.y,
|
||||
(z + j as i32) as f32 * SPACING,
|
||||
);
|
||||
let exposed = heights.under(pos).is_some_and(|ground| {
|
||||
let ground_pos = Vec3::new(pos.x, ground, pos.z);
|
||||
collision
|
||||
.cast_camera(ground_pos + Vec3::Y * 50.0, Vec3::NEG_Y * 49.5, true)
|
||||
.is_none()
|
||||
});
|
||||
state.samples[j * 8 + i] = if exposed { 1.0 } else { 0.0 };
|
||||
}
|
||||
}
|
||||
state.key = Some(key);
|
||||
state.next_refresh = time.elapsed_secs_f64() + 1.0;
|
||||
}
|
||||
frame.rows[row::WET] = [strength, x as f32 * SPACING, z as f32 * SPACING, SPACING];
|
||||
let rows = state.samples.as_chunks::<4>().0;
|
||||
frame.rows[row::WET_GRID..row::WET_GRID + rows.len()].copy_from_slice(rows);
|
||||
}
|
||||
|
||||
pub(crate) fn terrain_edits() -> Vec<Edit> {
|
||||
vec![Edit::after(
|
||||
" var tuned = clamp(diffuse_term + spec_term, vec3<f32>(0.0), vec3<f32>(1.0));",
|
||||
r#" // MoonWell: rain darkens diffuse and adds a grazing ambient sheen on exposed ground.
|
||||
let wet = mw_lane(36u);
|
||||
if (mw_live() && wet.x > 0.0) {
|
||||
let grid = (in.world_position.xz - wet.yz) / wet.w;
|
||||
if (all(grid >= vec2<f32>(0.0)) && all(grid < vec2<f32>(7.0))) {
|
||||
let cell = vec2<u32>(floor(grid));
|
||||
let f = fract(grid);
|
||||
let a = cell.y * 8u + cell.x;
|
||||
let b = a + 8u;
|
||||
let lo = mix(mw_lane(37u + a / 4u)[a % 4u], mw_lane(37u + (a + 1u) / 4u)[(a + 1u) % 4u], f.x);
|
||||
let hi = mix(mw_lane(37u + b / 4u)[b % 4u], mw_lane(37u + (b + 1u) / 4u)[(b + 1u) % 4u], f.x);
|
||||
let edge = 1.0 - smoothstep(24.0, 32.0, distance(in.world_position.xz, view.world_position.xz));
|
||||
let amount = wet.x * mix(lo, hi, f.y) * edge;
|
||||
if (amount > 0.0) {
|
||||
let n = normalize(in.mw_normal);
|
||||
let v = normalize(view.world_position.xyz - in.world_position.xyz);
|
||||
let grazing = pow(1.0 - clamp(dot(n, v), 0.0, 1.0), 3.0);
|
||||
tuned = clamp(diffuse_term * (1.0 - 0.18 * amount) + spec_term + wow_light.light_ambient.rgb * (0.16 * grazing * amount), vec3<f32>(0.0), vec3<f32>(1.0));
|
||||
}
|
||||
}
|
||||
}"#,
|
||||
)]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn envelope_uses_elapsed_time() {
|
||||
let mut wet = 0.0;
|
||||
for _ in 0..30 {
|
||||
wet = advance(wet, 1.0, 0.1);
|
||||
}
|
||||
assert!((wet - 1.0).abs() < 0.0001);
|
||||
for _ in 0..200 {
|
||||
wet = advance(wet, 0.0, 0.1);
|
||||
}
|
||||
assert!(wet < 0.0001);
|
||||
assert_eq!(advance(0.0, 0.5, 30.0), 0.5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grid_fits_its_reserved_frame_rows() {
|
||||
assert_eq!(row::WET, 36);
|
||||
assert_eq!(row::WET_GRID, row::WET + 1);
|
||||
assert_eq!(row::WET_GRID + 64 / 4, crate::frame::ROWS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_change_clears_envelope_even_without_a_camera() {
|
||||
let mut state = WetState::default();
|
||||
state.set_map(Some(1));
|
||||
state.amount = 0.8;
|
||||
state.key = Some((1, 0, 0));
|
||||
state.set_map(Some(2));
|
||||
assert_eq!(state.amount, 0.0);
|
||||
assert_eq!(state.key, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nonfinite_strength_is_off() {
|
||||
assert_eq!(strength_setting(f64::NAN), 0.0);
|
||||
assert_eq!(strength_setting(f64::INFINITY), 0.0);
|
||||
assert_eq!(strength_setting(-1.0), 0.0);
|
||||
assert_eq!(strength_setting(2.0), 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_config_defaults_to_dry() {
|
||||
let settings: GfxSettings = toml::from_str("[fog]\nenabled = true\n").unwrap();
|
||||
assert_eq!(settings.weather.wetness, 0.0);
|
||||
assert_eq!(GfxSettings::default().weather.wetness, 0.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user