Remove dead shader code, clean up weather.rs

This commit is contained in:
IsseW 2022-05-28 15:40:13 +02:00
parent dc8424c549
commit fd12c8abdb
5 changed files with 42 additions and 40 deletions

View File

@ -58,7 +58,7 @@ vec3 warp_normal(vec3 norm, vec3 pos, float time) {
+ smooth_rand(pos * 0.25, time * 0.25) * 0.1); + smooth_rand(pos * 0.25, time * 0.25) * 0.1);
} }
float wave_height(vec3 pos, vec3 surf_norm) { float wave_height(vec3 pos) {
float timer = tick.x * 0.75; float timer = tick.x * 0.75;
pos *= 0.5; pos *= 0.5;
@ -134,9 +134,9 @@ void main() {
vec3 wave_pos = mod(f_pos + focus_off.xyz, vec3(100.0)); vec3 wave_pos = mod(f_pos + focus_off.xyz, vec3(100.0));
float wave_sample_dist = 0.025; float wave_sample_dist = 0.025;
float wave00 = wave_height(wave_pos, surf_norm); float wave00 = wave_height(wave_pos);
float wave10 = wave_height(wave_pos + vec3(wave_sample_dist, 0, 0), surf_norm); float wave10 = wave_height(wave_pos + vec3(wave_sample_dist, 0, 0));
float wave01 = wave_height(wave_pos + vec3(0, wave_sample_dist, 0), surf_norm); float wave01 = wave_height(wave_pos + vec3(0, wave_sample_dist, 0));
// Possibility of div by zero when slope = 0, // Possibility of div by zero when slope = 0,
// however this only results in no water surface appearing // however this only results in no water surface appearing

View File

@ -113,11 +113,10 @@ layout(set = 0, binding = 12) uniform texture2D t_weather;
layout(set = 0, binding = 13) uniform sampler s_weather; layout(set = 0, binding = 13) uniform sampler s_weather;
vec4 sample_weather(vec2 wpos) { vec4 sample_weather(vec2 wpos) {
return textureLod(sampler2D(t_weather, s_weather), pos_to_uv(t_alt, s_alt, wpos - focus_off.xy), 0); // TODO: make this work for any world size return textureLod(sampler2D(t_weather, s_weather), pos_to_uv(t_alt, s_alt, wpos - focus_off.xy), 0);
} }
float cloud_tendency_at(vec2 pos) { float cloud_tendency_at(vec2 pos) {
//float nz = textureLod(sampler2D(t_noise, s_noise), (pos + wind_offset) / 60000.0 / cloud_scale, 0).x - 0.3;
return sample_weather(pos).r; return sample_weather(pos).r;
} }

View File

@ -162,6 +162,7 @@ impl WeatherLerp {
self.old = mem::replace(&mut self.new, (weather, Instant::now())); self.old = mem::replace(&mut self.new, (weather, Instant::now()));
} }
// TODO: Make impårovements to this interpolation, it's main issue is assuming that updates come at regular intervals.
fn update(&mut self, to_update: &mut WeatherGrid) { fn update(&mut self, to_update: &mut WeatherGrid) {
prof_span!("WeatherLerp::update"); prof_span!("WeatherLerp::update");
let old = &self.old.0; let old = &self.old.0;
@ -173,7 +174,7 @@ impl WeatherLerp {
*to_update = new.clone(); *to_update = new.clone();
} }
if old.size() == new.size() { if old.size() == new.size() {
// Assume updates are regular // Assumes updates are regular
let t = (self.new.1.elapsed().as_secs_f32() let t = (self.new.1.elapsed().as_secs_f32()
/ self.new.1.duration_since(self.old.1).as_secs_f32()) / self.new.1.duration_since(self.old.1).as_secs_f32())
.clamp(0.0, 1.0); .clamp(0.0, 1.0);
@ -182,7 +183,7 @@ impl WeatherLerp {
.iter_mut() .iter_mut()
.zip(old.iter().zip(new.iter())) .zip(old.iter().zip(new.iter()))
.for_each(|((_, current), ((_, old), (_, new)))| { .for_each(|((_, current), ((_, old), (_, new)))| {
*current = Weather::lerp(old, new, t); *current = Weather::lerp_unclamped(old, new, t);
}); });
} }
} }
@ -191,8 +192,8 @@ impl WeatherLerp {
impl Default for WeatherLerp { impl Default for WeatherLerp {
fn default() -> Self { fn default() -> Self {
Self { Self {
old: (WeatherGrid::new(Vec2::broadcast(0)), Instant::now()), old: (WeatherGrid::new(Vec2::zero()), Instant::now()),
new: (WeatherGrid::new(Vec2::broadcast(0)), Instant::now()), new: (WeatherGrid::new(Vec2::zero()), Instant::now()),
} }
} }
} }
@ -1461,6 +1462,7 @@ impl Client {
.map(|v| v.0) .map(|v| v.0)
} }
/// Returns Weather::default if no player position exists.
pub fn weather_at_player(&self) -> Weather { pub fn weather_at_player(&self) -> Weather {
self.position() self.position()
.map(|wpos| self.state.weather_at(wpos.xy())) .map(|wpos| self.state.weather_at(wpos.xy()))

View File

@ -20,24 +20,23 @@ impl Weather {
pub fn new(cloud: f32, rain: f32, wind: Vec2<f32>) -> Self { Self { cloud, rain, wind } } pub fn new(cloud: f32, rain: f32, wind: Vec2<f32>) -> Self { Self { cloud, rain, wind } }
pub fn get_kind(&self) -> WeatherKind { pub fn get_kind(&self) -> WeatherKind {
match ( // Over 24.5 m/s wind is a storm
(self.cloud * 10.0) as i32, if self.wind.magnitude_squared() >= 24.5f32.powi(2) {
(self.rain * 10.0) as i32, WeatherKind::Storm
(self.wind.magnitude() * 10.0) as i32, } else if (0.1..=1.0).contains(&self.rain) {
) { WeatherKind::Rain
// Over 24.5 m/s wind is a storm } else if (0.2..=1.0).contains(&self.cloud) {
(_, _, 245..) => WeatherKind::Storm, WeatherKind::Cloudy
(_, 1..=10, _) => WeatherKind::Rain, } else {
(4..=10, _, _) => WeatherKind::Cloudy, WeatherKind::Clear
_ => WeatherKind::Clear,
} }
} }
pub fn lerp(from: &Self, to: &Self, t: f32) -> Self { pub fn lerp_unclamped(from: &Self, to: &Self, t: f32) -> Self {
Self { Self {
cloud: f32::lerp(from.cloud, to.cloud, t), cloud: f32::lerp_unclamped(from.cloud, to.cloud, t),
rain: f32::lerp(from.rain, to.rain, t), rain: f32::lerp_unclamped(from.rain, to.rain, t),
wind: Vec2::<f32>::lerp(from.wind, to.wind, t), wind: Vec2::<f32>::lerp_unclamped(from.wind, to.wind, t),
} }
} }
@ -68,18 +67,18 @@ impl fmt::Display for WeatherKind {
} }
} }
// How many chunks wide a weather cell is.
// So one weather cell has (CHUNKS_PER_CELL * CHUNKS_PER_CELL) chunks.
pub const CHUNKS_PER_CELL: u32 = 16; pub const CHUNKS_PER_CELL: u32 = 16;
pub const CELL_SIZE: u32 = CHUNKS_PER_CELL * TerrainChunkSize::RECT_SIZE.x; pub const CELL_SIZE: u32 = CHUNKS_PER_CELL * TerrainChunkSize::RECT_SIZE.x;
/// How often the weather is updated, in seconds
pub const WEATHER_DT: f32 = 5.0;
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WeatherGrid { pub struct WeatherGrid {
weather: Grid<Weather>, weather: Grid<Weather>,
} }
/// Returns the center of the weather cell at the given position
fn to_cell_pos(wpos: Vec2<f32>) -> Vec2<f32> { wpos / CELL_SIZE as f32 - 0.5 } fn to_cell_pos(wpos: Vec2<f32>) -> Vec2<f32> { wpos / CELL_SIZE as f32 - 0.5 }
// TODO: Move consts from world to common to avoid duplication // TODO: Move consts from world to common to avoid duplication
@ -97,6 +96,7 @@ const LOCALITY: [Vec2<i32>; 9] = [
impl WeatherGrid { impl WeatherGrid {
pub fn new(size: Vec2<u32>) -> Self { pub fn new(size: Vec2<u32>) -> Self {
size.map(|e| debug_assert!(i32::try_from(e).is_ok()));
Self { Self {
weather: Grid::new(size.as_(), Weather::default()), weather: Grid::new(size.as_(), Weather::default()),
} }
@ -114,24 +114,24 @@ impl WeatherGrid {
/// interpolation between four cells. /// interpolation between four cells.
pub fn get_interpolated(&self, wpos: Vec2<f32>) -> Weather { pub fn get_interpolated(&self, wpos: Vec2<f32>) -> Weather {
let cell_pos = to_cell_pos(wpos); let cell_pos = to_cell_pos(wpos);
let rpos = cell_pos.map(|e| e.fract()); let rpos = cell_pos.map(|e| e.fract() + (1.0 - e.signum()) / 2.0);
let cell_pos = cell_pos.map(|e| e.floor()); let cell_pos = cell_pos.map(|e| e.floor());
let wpos = cell_pos.as_::<i32>(); let cpos = cell_pos.as_::<i32>();
Weather::lerp( Weather::lerp_unclamped(
&Weather::lerp( &Weather::lerp_unclamped(
self.weather.get(wpos).unwrap_or(&Weather::default()), self.weather.get(cpos).unwrap_or(&Weather::default()),
self.weather self.weather
.get(wpos + Vec2::unit_x()) .get(cpos + Vec2::unit_x())
.unwrap_or(&Weather::default()), .unwrap_or(&Weather::default()),
rpos.x, rpos.x,
), ),
&Weather::lerp( &Weather::lerp_unclamped(
self.weather self.weather
.get(wpos + Vec2::unit_x()) .get(cpos + Vec2::unit_y())
.unwrap_or(&Weather::default()), .unwrap_or(&Weather::default()),
self.weather self.weather
.get(wpos + Vec2::one()) .get(cpos + Vec2::one())
.unwrap_or(&Weather::default()), .unwrap_or(&Weather::default()),
rpos.x, rpos.x,
), ),

View File

@ -1,4 +1,4 @@
use common::weather::{CHUNKS_PER_CELL, WEATHER_DT}; use common::weather::CHUNKS_PER_CELL;
use common_ecs::{dispatch, System}; use common_ecs::{dispatch, System};
use common_state::State; use common_state::State;
use specs::DispatcherBuilder; use specs::DispatcherBuilder;
@ -16,12 +16,13 @@ pub fn add_server_systems(dispatch_builder: &mut DispatcherBuilder) {
} }
pub fn init(state: &mut State, world: &world::World) { pub fn init(state: &mut State, world: &world::World) {
// How many chunks wide a weather cell is.
// 16 here means that a weather cell is 16x16 chunks.
let weather_size = world.sim().get_size() / CHUNKS_PER_CELL; let weather_size = world.sim().get_size() / CHUNKS_PER_CELL;
let sim = sim::WeatherSim::new(weather_size, world); let sim = sim::WeatherSim::new(weather_size, world);
state.ecs_mut().insert(sim); state.ecs_mut().insert(sim);
// Tick weather every 2 seconds
/// How often the weather is updated, in seconds
pub const WEATHER_DT: f32 = 5.0;
state state
.ecs_mut() .ecs_mut()
.insert(SysScheduler::<tick::Sys>::every(Duration::from_secs_f32( .insert(SysScheduler::<tick::Sys>::every(Duration::from_secs_f32(