Nicer water shaders

This commit is contained in:
Joshua Barretto 2020-03-29 21:46:19 +01:00
parent a0dae82a2b
commit 16a175abe0
3 changed files with 31 additions and 28 deletions

View File

@ -28,30 +28,33 @@ vec3 warp_normal(vec3 norm, vec3 pos, float time) {
} }
float wave_height(vec3 pos) { float wave_height(vec3 pos) {
float timer = tick.x * 0.75;
pos *= 0.5;
vec3 big_warp = ( vec3 big_warp = (
texture(t_waves, fract(pos.xy * 0.03 + tick.x * 0.01)).xyz * 0.5 + texture(t_waves, fract(pos.xy * 0.03 + timer * 0.01)).xyz * 0.5 +
texture(t_waves, fract(pos.yx * 0.03 - tick.x * 0.01)).xyz * 0.5 + texture(t_waves, fract(pos.yx * 0.03 - timer * 0.01)).xyz * 0.5 +
vec3(0) vec3(0)
); );
vec3 warp = ( vec3 warp = (
texture(t_noise, fract(pos.yx * 0.1 + tick.x * 0.02)).xyz * 0.3 + texture(t_noise, fract(pos.yx * 0.1 + timer * 0.02)).xyz * 0.3 +
texture(t_noise, fract(pos.yx * 0.1 - tick.x * 0.02)).xyz * 0.3 + texture(t_noise, fract(pos.yx * 0.1 - timer * 0.02)).xyz * 0.3 +
vec3(0) vec3(0)
); );
float height = ( float height = (
(texture(t_noise, pos.xy * 0.03 + big_warp.xy + tick.x * 0.05).y - 0.5) * 1.0 + (texture(t_noise, pos.xy * 0.03 + big_warp.xy + timer * 0.05).y - 0.5) * 1.0 +
(texture(t_noise, pos.yx * 0.03 + big_warp.yx - tick.x * 0.05).y - 0.5) * 1.0 + (texture(t_noise, pos.yx * 0.03 + big_warp.yx - timer * 0.05).y - 0.5) * 1.0 +
(texture(t_waves, pos.xy * 0.1 + warp.xy + tick.x * 0.1).x - 0.5) * 0.5 + (texture(t_waves, pos.xy * 0.1 + warp.xy + timer * 0.1).x - 0.5) * 0.5 +
(texture(t_waves, pos.yx * 0.1 + warp.yx - tick.x * 0.1).x - 0.5) * 0.5 + (texture(t_waves, pos.yx * 0.1 + warp.yx - timer * 0.1).x - 0.5) * 0.5 +
(texture(t_noise, pos.yx * 0.3 + warp.xy * 0.5 + tick.x * 0.1).x - 0.5) * 0.2 + (texture(t_noise, pos.yx * 0.3 + warp.xy * 0.5 + timer * 0.1).x - 0.5) * 0.2 +
(texture(t_noise, pos.yx * 0.3 + warp.yx * 0.5 - tick.x * 0.1).x - 0.5) * 0.2 + (texture(t_noise, pos.yx * 0.3 + warp.yx * 0.5 - timer * 0.1).x - 0.5) * 0.2 +
(texture(t_noise, pos.yx * 1.0 + warp.yx * 0.0 - tick.x * 0.1).x - 0.5) * 0.05 + (texture(t_noise, pos.yx * 1.0 + warp.yx * 0.0 - timer * 0.1).x - 0.5) * 0.05 +
0.0 0.0
); );
return pow(abs(height), 0.5) * sign(height) * 5.5; return pow(abs(height), 0.5) * sign(height) * 10.5;
} }
void main() { void main() {
@ -101,7 +104,7 @@ void main() {
vec3 point_light = light_at(f_pos, norm); vec3 point_light = light_at(f_pos, norm);
light += point_light; light += point_light;
diffuse_light += point_light; diffuse_light += point_light;
vec3 surf_color = srgb_to_linear(vec3(0.2, 0.5, 1.0)) * light * diffuse_light * ambient_light; vec3 surf_color = srgb_to_linear(vec3(0.1)) * light * diffuse_light * ambient_light;
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x); float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec4 clouds; vec4 clouds;
@ -116,9 +119,9 @@ void main() {
// Tint // Tint
reflect_color = mix(reflect_color, surf_color, 0.6); reflect_color = mix(reflect_color, surf_color, 0.6);
// 0 = 100% reflection, 1 = translucent water // 0 = 100% reflection, 1 = translucent water
float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag), 0.5); float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag), 1.0);
vec4 color = mix(vec4(reflect_color * 2.0, 1.0), vec4(surf_color, 1.0 / (1.0 + diffuse_light * 0.25)), passthrough); vec4 color = mix(vec4(reflect_color, 1.0), vec4(surf_color, 1.0 / (1.0 + diffuse_light * 0.25)), passthrough);
tgt_color = mix(mix(color, vec4(fog_color, 0.0), fog_level), vec4(clouds.rgb, 0.0), clouds.a); tgt_color = mix(mix(color, vec4(fog_color, 0.0), fog_level), vec4(clouds.rgb, 0.0), clouds.a);
} }

View File

@ -187,7 +187,7 @@ vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float q
// Clouds // Clouds
clouds = get_cloud_color(dir, origin, time_of_day, f_dist, quality); clouds = get_cloud_color(dir, origin, time_of_day, f_dist, quality);
clouds.rgb *= get_sun_brightness(sun_dir) * (sun_halo * 1.5 + get_sun_color(sun_dir)) + get_moon_brightness(moon_dir) * (moon_halo * 80.0 + get_moon_color(moon_dir)); clouds.rgb *= get_sun_brightness(sun_dir) * (sun_halo * 1.5 + get_sun_color(sun_dir)) + get_moon_brightness(moon_dir) * (moon_halo * 80.0 + get_moon_color(moon_dir) + 0.25);
if (f_dist > 5000.0) { if (f_dist > 5000.0) {
sky_color += sun_light + moon_light; sky_color += sun_light + moon_light;

View File

@ -593,7 +593,7 @@ pub type Stocks<T> = MapVec<Stock, T>;
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
pub struct MapVec<K, T> { pub struct MapVec<K, T> {
stocks: HashMap<K, T>, entries: HashMap<K, T>,
zero: T, zero: T,
} }
@ -602,43 +602,43 @@ impl<K: Copy + Eq + Hash, T: Default + Clone> MapVec<K, T> {
where K: 'a, T: 'a where K: 'a, T: 'a
{ {
Self { Self {
stocks: i.into_iter().cloned().collect(), entries: i.into_iter().cloned().collect(),
zero: T::default(), zero: T::default(),
} }
} }
pub fn get_mut(&mut self, stock: K) -> &mut T { pub fn get_mut(&mut self, entry: K) -> &mut T {
self self
.stocks .entries
.entry(stock) .entry(entry)
.or_default() .or_default()
} }
pub fn get(&self, stock: K) -> &T { pub fn get(&self, entry: K) -> &T {
self.stocks.get(&stock).unwrap_or(&self.zero) self.entries.get(&entry).unwrap_or(&self.zero)
} }
pub fn map(mut self, mut f: impl FnMut(K, T) -> T) -> Self { pub fn map(mut self, mut f: impl FnMut(K, T) -> T) -> Self {
self.stocks.iter_mut().for_each(|(s, v)| *v = f(*s, std::mem::take(v))); self.entries.iter_mut().for_each(|(s, v)| *v = f(*s, std::mem::take(v)));
self self
} }
pub fn iter(&self) -> impl Iterator<Item=(K, &T)> + '_ { pub fn iter(&self) -> impl Iterator<Item=(K, &T)> + '_ {
self.stocks.iter().map(|(s, v)| (*s, v)) self.entries.iter().map(|(s, v)| (*s, v))
} }
pub fn iter_mut(&mut self) -> impl Iterator<Item=(K, &mut T)> + '_ { pub fn iter_mut(&mut self) -> impl Iterator<Item=(K, &mut T)> + '_ {
self.stocks.iter_mut().map(|(s, v)| (*s, v)) self.entries.iter_mut().map(|(s, v)| (*s, v))
} }
} }
impl<K: Copy + Eq + Hash, T: Default + Clone> std::ops::Index<K> for MapVec<K, T> { impl<K: Copy + Eq + Hash, T: Default + Clone> std::ops::Index<K> for MapVec<K, T> {
type Output = T; type Output = T;
fn index(&self, stock: K) -> &Self::Output { self.get(stock) } fn index(&self, entry: K) -> &Self::Output { self.get(entry) }
} }
impl<K: Copy + Eq + Hash, T: Default + Clone> std::ops::IndexMut<K> for MapVec<K, T> { impl<K: Copy + Eq + Hash, T: Default + Clone> std::ops::IndexMut<K> for MapVec<K, T> {
fn index_mut(&mut self, stock: K) -> &mut Self::Output { self.get_mut(stock) } fn index_mut(&mut self, entry: K) -> &mut Self::Output { self.get_mut(entry) }
} }