Better reflections and water fog

This commit is contained in:
Joshua Barretto 2019-08-16 15:58:14 +01:00
parent 825d8bb632
commit f0e52e6002
12 changed files with 43 additions and 23 deletions

View File

@ -31,7 +31,7 @@ void main() {
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) + light_at(f_pos, f_norm);
vec3 surf_color = srgb_to_linear(model_col.rgb * f_col) * 4.0 * light;
float fog_level = fog(f_pos.xy, focus_pos.xy);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x);
vec3 color = mix(surf_color, fog_color, fog_level);

View File

@ -7,7 +7,6 @@ in vec3 f_pos;
flat in vec3 f_norm;
in vec3 f_col;
in float f_light;
in float f_opac;
layout (std140)
uniform u_locals {
@ -20,18 +19,19 @@ out vec4 tgt_color;
#include <light.glsl>
void main() {
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) * f_light + light_at(f_pos, f_norm);
vec3 warped_norm = normalize(f_norm + smooth_rand(f_pos * 0.35, tick.x) * 0.2);
vec3 light = get_sun_diffuse(warped_norm, time_of_day.x) * f_light + light_at(f_pos, warped_norm);
vec3 surf_color = f_col * light;
float fog_level = fog(f_pos.xy, focus_pos.xy);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x);
vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
vec3 warped_norm = normalize(f_norm + smooth_rand(f_pos * 0.35, tick.x) * 0.2);
vec3 reflect_color = get_sky_color(reflect(cam_to_frag, warped_norm), time_of_day.x) * light;
float passthrough = dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag);
vec3 reflect_color = get_sky_color(reflect(cam_to_frag, warped_norm), time_of_day.x) * f_light;
float passthrough = dot(faceforward(warped_norm, warped_norm, cam_to_frag), -cam_to_frag);
vec4 color = mix(vec4(reflect_color, 1.0), vec4(surf_color, f_opac), passthrough);
vec4 color = mix(vec4(reflect_color, 1.0), vec4(surf_color, 0.8 / (1.0 + light * 2.0)), passthrough);
tgt_color = mix(color, vec4(fog_color, 1.0), fog_level);
tgt_color = mix(color, vec4(fog_color, 0.0), fog_level);
}

View File

@ -14,7 +14,6 @@ out vec3 f_pos;
flat out vec3 f_norm;
out vec3 f_col;
out float f_light;
out float f_opac;
// First 3 normals are negative, next 3 are positive
vec3 normals[6] = vec3[]( vec3(-1,0,0), vec3(0,-1,0), vec3(0,0,-1), vec3(1,0,0), vec3(0,1,0), vec3(0,0,1) );
@ -43,8 +42,6 @@ void main() {
f_light = float(v_col_light & 0xFFu) / 255.0;
f_opac = 0.3;
gl_Position =
proj_mat *
view_mat *

View File

@ -9,4 +9,5 @@ uniform u_globals {
vec4 tick;
vec4 screen_res;
uvec4 light_count;
uvec4 medium;
};

View File

@ -144,11 +144,20 @@ vec3 get_sky_color(vec3 dir, float time_of_day) {
return sky_color + sun_light;
}
float fog(vec2 f_pos, vec2 focus_pos) {
float dist = distance(f_pos, focus_pos) / view_distance.x;
const float min_fog = 0.5;
const float max_fog = 1.0;
const float diff_fog = 0.5; // max - min
float fog(vec3 f_pos, vec3 focus_pos, uint medium) {
float fog_radius = view_distance.x;
float mist_radius = 10000000.0;
return pow(clamp((dist - min_fog) / (diff_fog), 0.0, 1.0), 1.7);
float min_fog = 0.5;
float max_fog = 1.0;
if (medium == 1u) {
mist_radius = 32.0;
min_fog = 0.0;
}
float fog = distance(f_pos.xy, focus_pos.xy) / fog_radius;
float mist = distance(f_pos, focus_pos) / mist_radius;
return pow(clamp((max(fog, mist) - min_fog) / (max_fog - min_fog), 0.0, 1.0), 1.7);
}

View File

@ -21,7 +21,7 @@ void main() {
vec3 light = get_sun_diffuse(f_norm, time_of_day.x) * f_light + light_at(f_pos, f_norm);
vec3 surf_color = f_col * light;
float fog_level = fog(f_pos.xy, focus_pos.xy);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x);
vec3 color = mix(surf_color, fog_color, fog_level);

View File

@ -17,6 +17,7 @@ use client::Client;
use common::{
comp::{humanoid, Body},
state::DeltaTime,
terrain::BlockKind,
};
use log::error;
use vek::*;
@ -102,6 +103,7 @@ impl Scene {
client.state().get_time(),
renderer.get_resolution(),
0,
BlockKind::Air,
)],
) {
error!("Renderer failed to update: {:?}", err);

View File

@ -18,7 +18,7 @@ fn block_shadow_density(kind: BlockKind) -> (f32, f32) {
BlockKind::Air => (0.0, 0.0),
BlockKind::Normal => (0.085, 0.3),
BlockKind::Dense => (0.3, 0.0),
BlockKind::Water => (0.08, 0.0),
BlockKind::Water => (0.15, 0.0),
}
}

View File

@ -6,6 +6,7 @@ pub mod terrain;
pub mod ui;
use super::util::arr_to_mat;
use common::terrain::BlockKind;
use gfx::{
self,
gfx_constant_struct_meta,
@ -27,6 +28,7 @@ gfx_defines! {
tick: [f32; 4] = "tick",
screen_res: [f32; 4] = "screen_res",
light_count: [u32; 4] = "light_count",
medium: [u32; 4] = "medium",
}
constant Light {
@ -47,6 +49,7 @@ impl Globals {
tick: f64,
screen_res: Vec2<u16>,
light_count: usize,
medium: BlockKind,
) -> Self {
Self {
view_mat: arr_to_mat(view_mat.into_col_array()),
@ -58,6 +61,7 @@ impl Globals {
tick: [tick as f32; 4],
screen_res: Vec4::from(screen_res.map(|e| e as f32)).into_array(),
light_count: [light_count as u32; 4],
medium: [if medium.is_fluid() { 1 } else { 0 }; 4],
}
}
}
@ -74,6 +78,7 @@ impl Default for Globals {
0.0,
Vec2::new(800, 500),
0,
BlockKind::Air,
)
}
}

View File

@ -15,7 +15,7 @@ use crate::{
window::Event,
};
use client::Client;
use common::comp;
use common::{comp, terrain::BlockKind, vol::ReadVol};
use specs::Join;
use vek::*;
@ -194,6 +194,12 @@ impl Scene {
client.state().get_time(),
renderer.get_resolution(),
lights.len(),
client
.state()
.terrain()
.get(cam_pos.map(|e| e.floor() as i32))
.map(|b| b.kind())
.unwrap_or(BlockKind::Air),
)],
)
.expect("Failed to update global constants");

View File

@ -225,7 +225,7 @@ impl<'a> BlockGen<'a> {
// let sand = Block::new(1, Rgb::new(180, 150, 50));
// let warm_stone = Block::new(1, Rgb::new(165, 165, 130));
let water = Block::new(BlockKind::Water, Rgb::new(0, 24, 255));
let water = Block::new(BlockKind::Water, Rgb::new(60, 90, 190));
let grass_depth = 1.5 + 2.0 * chaos;
let block = if (wposf.z as f32) < height - grass_depth {

View File

@ -60,7 +60,7 @@ impl World {
pub fn generate_chunk(&self, chunk_pos: Vec2<i32>) -> (TerrainChunk, ChunkSupplement) {
let air = Block::empty();
let stone = Block::new(BlockKind::Dense, Rgb::new(200, 220, 255));
let water = Block::new(BlockKind::Water, Rgb::new(100, 150, 255));
let water = Block::new(BlockKind::Water, Rgb::new(60, 90, 190));
let chunk_size2d = Vec2::from(TerrainChunkSize::SIZE);
let (base_z, sim_chunk) = match self