2019-08-06 02:50:49 +00:00
|
|
|
#version 330 core
|
|
|
|
|
|
|
|
#include <globals.glsl>
|
2019-08-15 01:35:56 +00:00
|
|
|
#include <random.glsl>
|
2019-08-06 02:50:49 +00:00
|
|
|
|
|
|
|
in vec3 f_pos;
|
2019-10-16 14:05:45 +00:00
|
|
|
flat in uint f_pos_norm;
|
2019-08-06 02:50:49 +00:00
|
|
|
in vec3 f_col;
|
|
|
|
in float f_light;
|
|
|
|
|
|
|
|
layout (std140)
|
|
|
|
uniform u_locals {
|
|
|
|
vec3 model_offs;
|
2019-09-24 15:43:51 +00:00
|
|
|
float load_time;
|
2019-08-06 02:50:49 +00:00
|
|
|
};
|
|
|
|
|
2019-10-17 16:11:55 +00:00
|
|
|
uniform sampler2D t_waves;
|
|
|
|
|
2019-08-06 02:50:49 +00:00
|
|
|
out vec4 tgt_color;
|
|
|
|
|
|
|
|
#include <sky.glsl>
|
|
|
|
#include <light.glsl>
|
|
|
|
|
2019-08-17 22:20:07 +00:00
|
|
|
vec3 warp_normal(vec3 norm, vec3 pos, float time) {
|
|
|
|
return normalize(norm
|
|
|
|
+ smooth_rand(pos * 1.0, time * 1.0) * 0.05
|
|
|
|
+ smooth_rand(pos * 0.25, time * 0.25) * 0.1);
|
|
|
|
}
|
|
|
|
|
2019-08-06 02:50:49 +00:00
|
|
|
void main() {
|
2019-10-16 14:05:45 +00:00
|
|
|
// 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) );
|
|
|
|
|
|
|
|
// TODO: last 3 bits in v_pos_norm should be a number between 0 and 5, rather than 0-2 and a direction.
|
|
|
|
uint norm_axis = (f_pos_norm >> 30) & 0x3u;
|
|
|
|
// Increase array access by 3 to access positive values
|
|
|
|
uint norm_dir = ((f_pos_norm >> 29) & 0x1u) * 3u;
|
|
|
|
// Use an array to avoid conditional branching
|
|
|
|
vec3 f_norm = normals[norm_axis + norm_dir];
|
|
|
|
|
2019-08-17 17:07:25 +00:00
|
|
|
/*
|
2019-08-17 22:20:07 +00:00
|
|
|
// Round the position to the nearest triangular grid cell
|
2019-08-17 17:07:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-08-17 22:20:07 +00:00
|
|
|
hex_pos = floor(hex_pos);
|
2019-08-17 17:07:25 +00:00
|
|
|
*/
|
|
|
|
|
2019-10-17 16:11:55 +00:00
|
|
|
vec3 b_norm;
|
|
|
|
if (f_norm.z > 0.0) {
|
|
|
|
b_norm = vec3(1, 0, 0);
|
|
|
|
} else if (f_norm.x > 0.0) {
|
|
|
|
b_norm = vec3(0, 1, 0);
|
|
|
|
} else {
|
|
|
|
b_norm = vec3(0, 0, 1);
|
|
|
|
}
|
|
|
|
vec3 c_norm = cross(f_norm, b_norm);
|
|
|
|
|
|
|
|
vec3 nmap = normalize(
|
2019-10-17 16:35:17 +00:00
|
|
|
(srgb_to_linear(texture(t_waves, fract(f_pos.xy * 0.3 + tick.x * 0.04)).rgb) - 0.0) * 0.05
|
|
|
|
+ (srgb_to_linear(texture(t_waves, fract(f_pos.xy * 0.1 - tick.x * 0.08)).rgb) - 0.0) * 0.1
|
|
|
|
+ (srgb_to_linear(texture(t_waves, fract(-f_pos.yx * 0.06 - tick.x * 0.1)).rgb) - 0.0) * 0.1
|
|
|
|
+ (srgb_to_linear(texture(t_waves, fract(-f_pos.yx * 0.03 - tick.x * 0.01)).rgb) - 0.0) * 0.2
|
|
|
|
+ vec3(0, 0, 0.0)
|
2019-10-17 16:11:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
vec3 norm = f_norm * nmap.z + b_norm * nmap.x + c_norm * nmap.y;
|
2019-08-16 14:58:14 +00:00
|
|
|
|
2019-09-24 09:01:50 +00:00
|
|
|
vec3 light, diffuse_light, ambient_light;
|
2019-10-08 11:07:10 +00:00
|
|
|
get_sun_diffuse(f_norm, time_of_day.x, light, diffuse_light, ambient_light, 0.0);
|
2019-09-25 12:00:00 +00:00
|
|
|
float point_shadow = shadow_at(f_pos, f_norm);
|
|
|
|
diffuse_light *= f_light * point_shadow;
|
|
|
|
ambient_light *= f_light, point_shadow;
|
2019-09-24 09:01:50 +00:00
|
|
|
vec3 point_light = light_at(f_pos, f_norm);
|
|
|
|
light += point_light;
|
|
|
|
diffuse_light += point_light;
|
2019-10-16 14:05:45 +00:00
|
|
|
vec3 surf_color = illuminate(srgb_to_linear(f_col), light, diffuse_light, ambient_light);
|
2019-08-06 02:50:49 +00:00
|
|
|
|
2019-08-16 14:58:14 +00:00
|
|
|
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
|
2019-08-20 15:26:12 +00:00
|
|
|
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, true);
|
2019-08-14 21:28:37 +00:00
|
|
|
|
2019-08-16 10:31:51 +00:00
|
|
|
vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
|
2019-08-17 22:20:07 +00:00
|
|
|
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
|
2019-08-17 17:07:25 +00:00
|
|
|
reflect_ray_dir.z = max(reflect_ray_dir.z, 0.05);
|
2019-08-17 22:20:07 +00:00
|
|
|
|
2019-08-20 15:26:12 +00:00
|
|
|
vec3 reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, false) * f_light;
|
2019-10-17 16:11:55 +00:00
|
|
|
//reflect_color = vec3(reflect_color.r + reflect_color.g + reflect_color.b) / 3.0;
|
2019-08-17 22:20:07 +00:00
|
|
|
// 0 = 100% reflection, 1 = translucent water
|
2019-10-17 16:35:17 +00:00
|
|
|
float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag), 0.3);
|
2019-08-14 21:28:37 +00:00
|
|
|
|
2019-10-17 16:35:17 +00:00
|
|
|
vec4 color = mix(vec4(reflect_color * 2.0, 1.0), vec4(surf_color, 4.0 / (1.0 + diffuse_light * 2.0)), passthrough);
|
2019-08-06 02:50:49 +00:00
|
|
|
|
2019-08-16 14:58:14 +00:00
|
|
|
tgt_color = mix(color, vec4(fog_color, 0.0), fog_level);
|
2019-08-06 02:50:49 +00:00
|
|
|
}
|