Better water shaders

This commit is contained in:
Joshua Barretto 2020-03-27 15:13:36 +00:00
parent 63d5e196e9
commit 3a989f061f
5 changed files with 18 additions and 25 deletions

View File

@ -34,16 +34,6 @@ void main() {
vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz); vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
/*
// Round the position to the nearest triangular grid cell
vec3 hex_pos = f_pos * 2.0;
hex_pos = hex_pos + vec3(hex_pos.y * 1.4 / 3.0, hex_pos.y * 0.1, 0);
if (fract(hex_pos.x) > fract(hex_pos.y)) {
hex_pos += vec3(1.0, 1.0, 0);
}
hex_pos = floor(hex_pos);
*/
vec3 light, diffuse_light, ambient_light; vec3 light, diffuse_light, ambient_light;
get_sun_diffuse(f_norm, time_of_day.x, light, diffuse_light, ambient_light, 0.0); get_sun_diffuse(f_norm, time_of_day.x, light, diffuse_light, ambient_light, 0.0);
float point_shadow = shadow_at(f_pos,f_norm); float point_shadow = shadow_at(f_pos,f_norm);
@ -52,7 +42,7 @@ void main() {
vec3 point_light = light_at(f_pos, f_norm); vec3 point_light = light_at(f_pos, f_norm);
light += point_light; light += point_light;
diffuse_light += point_light; diffuse_light += point_light;
vec3 surf_color = illuminate(srgb_to_linear(vec3(0.2, 0.2, 1.0)), light, diffuse_light, ambient_light); vec3 surf_color = srgb_to_linear(vec3(0.4, 0.7, 2.0)) * 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;

View File

@ -68,16 +68,6 @@ void main() {
vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz); vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
float frag_dist = length(f_pos - cam_pos.xyz); float frag_dist = length(f_pos - cam_pos.xyz);
/*
// Round the position to the nearest triangular grid cell
vec3 hex_pos = f_pos * 2.0;
hex_pos = hex_pos + vec3(hex_pos.y * 1.4 / 3.0, hex_pos.y * 0.1, 0);
if (fract(hex_pos.x) > fract(hex_pos.y)) {
hex_pos += vec3(1.0, 1.0, 0);
}
hex_pos = floor(hex_pos);
*/
vec3 b_norm; vec3 b_norm;
if (f_norm.z > 0.0) { if (f_norm.z > 0.0) {
b_norm = vec3(1, 0, 0); b_norm = vec3(1, 0, 0);
@ -111,7 +101,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 = illuminate(srgb_to_linear(f_col), light, diffuse_light, ambient_light); vec3 surf_color = srgb_to_linear(vec3(0.2, 0.5, 1.0)) * 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;
@ -119,7 +109,7 @@ void main() {
vec3 reflect_ray_dir = reflect(cam_to_frag, norm); vec3 reflect_ray_dir = reflect(cam_to_frag, norm);
// Hack to prevent the reflection ray dipping below the horizon and creating weird blue spots in the water // Hack to prevent the reflection ray dipping below the horizon and creating weird blue spots in the water
reflect_ray_dir.z = max(reflect_ray_dir.z, 0.05); reflect_ray_dir.z = max(reflect_ray_dir.z, 0.01);
vec4 _clouds; vec4 _clouds;
vec3 reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, f_pos, vec3(-100000), 0.25, false, _clouds) * f_light; vec3 reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, f_pos, vec3(-100000), 0.25, false, _clouds) * f_light;

View File

@ -27,6 +27,9 @@ void main() {
f_pos.z *= min(1.0001 - 0.02 / pow(tick.x - load_time, 10.0), 1.0); f_pos.z *= min(1.0001 - 0.02 / pow(tick.x - load_time, 10.0), 1.0);
f_pos.z -= 25.0 * pow(distance(focus_pos.xy, f_pos.xy) / view_distance.x, 20.0); f_pos.z -= 25.0 * pow(distance(focus_pos.xy, f_pos.xy) / view_distance.x, 20.0);
// Small waves
f_pos.z -= 0.05 + 0.05 * (sin(tick.x * 2.0 + f_pos.x * 2.0 + f_pos.y * 2.0) + 1.0) * 0.5;
f_col = vec3( f_col = vec3(
float((v_col_light >> 8) & 0xFFu), float((v_col_light >> 8) & 0xFFu),
float((v_col_light >> 16) & 0xFFu), float((v_col_light >> 16) & 0xFFu),

View File

@ -21,6 +21,8 @@ const vec3 SKY_NIGHT_MID = vec3(0.001, 0.005, 0.02);
const vec3 SKY_NIGHT_BOT = vec3(0.002, 0.004, 0.004); const vec3 SKY_NIGHT_BOT = vec3(0.002, 0.004, 0.004);
const vec3 NIGHT_LIGHT = vec3(0.002, 0.01, 0.03); const vec3 NIGHT_LIGHT = vec3(0.002, 0.01, 0.03);
const float UNDERWATER_MIST_DIST = 100.0;
vec3 get_sun_dir(float time_of_day) { vec3 get_sun_dir(float time_of_day) {
const float TIME_FACTOR = (PI * 2.0) / (3600.0 * 24.0); const float TIME_FACTOR = (PI * 2.0) / (3600.0 * 24.0);
@ -202,7 +204,7 @@ float fog(vec3 f_pos, vec3 focus_pos, uint medium) {
float max_fog = 1.0; float max_fog = 1.0;
if (medium == 1u) { if (medium == 1u) {
mist_radius = 96.0; mist_radius = UNDERWATER_MIST_DIST;
min_fog = 0.0; min_fog = 0.0;
} }

View File

@ -14,5 +14,13 @@ out vec4 tgt_color;
void main() { void main() {
vec4 _clouds; vec4 _clouds;
tgt_color = vec4(get_sky_color(normalize(f_pos), time_of_day.x, cam_pos.xyz, vec3(-100000), 1.0, true, _clouds), 1.0); float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
float dist = 100000.0;
if (medium.x == 1u) {
dist = UNDERWATER_MIST_DIST;
}
vec3 wpos = cam_pos.xyz + normalize(f_pos) * dist;
tgt_color = vec4(get_sky_color(normalize(f_pos), time_of_day.x, cam_pos.xyz, wpos, 1.0, true, _clouds), 1.0);
} }