veloren/assets/voxygen/shaders/fluid-frag/shiny.glsl

175 lines
7.0 KiB
Plaintext
Raw Normal View History

#version 330 core
// https://www.shadertoy.com/view/XdsyWf
#include <globals.glsl>
#include <random.glsl>
in vec3 f_pos;
flat in uint f_pos_norm;
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-10-17 16:11:55 +00:00
uniform sampler2D t_waves;
out vec4 tgt_color;
#include <sky.glsl>
#include <light.glsl>
2020-04-12 22:29:59 +00:00
#include <lod.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-11-05 15:08:54 +00:00
float wave_height(vec3 pos) {
vec3 big_warp = (
texture(t_waves, fract(pos.xy * 0.03 + tick.x * 0.01)).xyz * 0.5 +
texture(t_waves, fract(pos.yx * 0.03 - tick.x * 0.01)).xyz * 0.5 +
vec3(0)
);
vec3 warp = (
2019-11-18 08:00:17 +00:00
texture(t_noise, fract(pos.yx * 0.1 + tick.x * 0.02)).xyz * 0.3 +
texture(t_noise, fract(pos.yx * 0.1 - tick.x * 0.02)).xyz * 0.3 +
2019-11-05 15:08:54 +00:00
vec3(0)
);
float height = (
2019-11-18 08:00:17 +00:00
(texture(t_noise, pos.xy * 0.03 + big_warp.xy + tick.x * 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 +
2019-11-05 15:08:54 +00:00
(texture(t_waves, pos.xy * 0.1 + warp.xy + tick.x * 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 +
2019-11-18 08:00:17 +00:00
(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.yx * 0.5 - tick.x * 0.1).x - 0.5) * 0.2 +
2019-11-18 10:02:05 +00:00
(texture(t_noise, pos.yx * 1.0 + warp.yx * 0.0 - tick.x * 0.1).x - 0.5) * 0.05 +
2019-11-05 15:08:54 +00:00
0.0
);
2019-11-18 08:00:17 +00:00
return pow(abs(height), 0.5) * sign(height) * 5.5;
2019-11-05 15:08:54 +00:00
}
void main() {
// First 3 normals are negative, next 3 are positive
2020-01-19 18:39:20 +00:00
vec3 normals[6] = vec3[](vec3(-1,0,0), vec3(1,0,0), vec3(0,-1,0), vec3(0,1,0), vec3(0,0,-1), 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-11-03 10:49:14 +00:00
vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
2020-04-12 22:29:59 +00:00
// vec4 vert_pos4 = view_mat * vec4(f_pos, 1.0);
// vec3 view_dir = normalize(-vec3(vert_pos4)/* / vert_pos4.w*/);
vec3 view_dir = -cam_to_frag;
2019-11-03 10:49:14 +00:00
float frag_dist = length(f_pos - cam_pos.xyz);
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);
2019-11-05 15:08:54 +00:00
float wave00 = wave_height(f_pos);
float wave10 = wave_height(f_pos + vec3(0.1, 0, 0));
float wave01 = wave_height(f_pos + vec3(0, 0.1, 0));
float slope = abs(wave00 - wave10) * abs(wave00 - wave01);
vec3 nmap = vec3(
-(wave10 - wave00) / 0.1,
-(wave01 - wave00) / 0.1,
0.1 / slope
);
nmap = mix(vec3(0, 0, 1), normalize(nmap), min(1.0 / pow(frag_dist, 0.75), 1));
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
2020-04-04 00:32:39 +00:00
vec4 _clouds;
vec3 reflect_ray_dir = reflect(cam_to_frag/*-view_dir*/, norm);
/* vec4 reflect_ray_dir4 = view_mat * vec4(reflect_ray_dir, 1.0);
reflect_ray_dir = normalize(vec3(reflect_ray_dir4) / reflect_ray_dir4.w); */
// vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
2020-04-04 00:32:39 +00:00
vec3 reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, f_pos, vec3(-100000), 0.25, false, _clouds) * f_light;
vec3 surf_color = /*srgb_to_linear*/(vec3(0.2, 0.5, 1.0));
2020-04-12 22:29:59 +00:00
vec3 sun_dir = get_sun_dir(time_of_day.x);
vec3 moon_dir = get_moon_dir(time_of_day.x);
float sun_shade_frac = horizon_at(/*f_shadow, f_pos.z, */f_pos, sun_dir);
float moon_shade_frac = horizon_at(/*f_shadow, f_pos.z, */f_pos, moon_dir);
float shade_frac = /*1.0;*/sun_shade_frac + moon_shade_frac;
vec3 k_a = vec3(0.5);
vec3 k_d = 0.5 * surf_color;
vec3 k_s = 2.0 * reflect_color;
2020-04-04 00:32:39 +00:00
float alpha = 0.255;
vec3 emitted_light, reflected_light;
// vec3 light, diffuse_light, ambient_light;
float point_shadow = shadow_at(f_pos, f_norm);
2020-04-12 22:29:59 +00:00
vec3 light_frac = /*vec3(1.0);*/light_reflection_factor(f_norm/*vec3(0, 0, 1.0)*/, view_dir, vec3(0, 0, -1.0), vec3(1.0), vec3(1.0), alpha);
2020-04-04 00:32:39 +00:00
// 0 = 100% reflection, 1 = translucent water
2020-04-12 22:29:59 +00:00
float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag/*view_dir*/), -cam_to_frag/*view_dir*/), 0.5);
get_sun_diffuse(norm, time_of_day.x, view_dir, k_a * f_light * point_shadow * (shade_frac * 0.5 + light_frac * 0.5), vec3(0.0), /*vec3(f_light * point_shadow)*//*reflect_color*/k_s * f_light * point_shadow * shade_frac, alpha, emitted_light, reflected_light);
vec3 diffuse_light_point = vec3(0.0);
lights_at(f_pos, norm, view_dir, k_a, vec3(1.0), vec3(0.0), alpha, emitted_light, diffuse_light_point);
2020-04-12 22:29:59 +00:00
vec3 dump_light = vec3(0.0);
vec3 specular_light_point = vec3(0.0);
lights_at(f_pos, norm, view_dir, vec3(0.0), vec3(0.0), vec3(1.0), alpha, dump_light, specular_light_point);
2020-04-12 22:29:59 +00:00
float reflected_light_point = length(diffuse_light_point/*.r*/) + f_light * point_shadow;
reflected_light += k_d * (diffuse_light_point + f_light * point_shadow * shade_frac) + k_s * specular_light_point;
/* vec3 point_light = light_at(f_pos, norm);
emitted_light += point_light;
reflected_light += point_light; */
2020-04-04 00:32:39 +00:00
// get_sun_diffuse(norm, time_of_day.x, light, diffuse_light, ambient_light, 0.0);
// diffuse_light *= f_light * point_shadow;
// ambient_light *= f_light * point_shadow;
// vec3 point_light = light_at(f_pos, norm);
// light += point_light;
// diffuse_light += point_light;
// reflected_light += point_light;
// vec3 surf_color = srgb_to_linear(vec3(0.2, 0.5, 1.0)) * light * diffuse_light * ambient_light;
surf_color = illuminate(surf_color * emitted_light, reflected_light);
2019-08-16 14:58:14 +00:00
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
2019-11-18 12:02:10 +00:00
vec4 clouds;
2020-04-12 22:29:59 +00:00
vec3 fog_color = get_sky_color(cam_to_frag/*-view_dir*/, time_of_day.x, cam_pos.xyz, f_pos, 0.25, true, clouds);
2020-04-04 00:32:39 +00:00
// vec3 reflect_ray_dir = reflect(cam_to_frag, norm);
2019-08-17 22:20:07 +00:00
// Hack to prevent the reflection ray dipping below the horizon and creating weird blue spots in the water
2020-04-04 00:32:39 +00:00
// reflect_ray_dir.z = max(reflect_ray_dir.z, 0.01);
2019-08-17 22:20:07 +00:00
2020-04-04 00:32:39 +00:00
// 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;
2019-11-18 10:02:05 +00:00
// Tint
2020-04-04 00:32:39 +00:00
// reflect_color = mix(reflect_color, surf_color, 0.6);
2020-04-04 00:32:39 +00:00
// vec4 color = mix(vec4(reflect_color * 2.0, 1.0), vec4(surf_color, 1.0 / (1.0 + /*diffuse_light*/(f_light * point_shadow + point_light) * 0.25)), passthrough);
// vec4 color = mix(vec4(reflect_color * 2.0, 1.0), vec4(surf_color, 1.0 / (1.0 + /*diffuse_light*/(/*f_light * point_shadow*/f_light * point_shadow + reflected_light_point/* + point_light*//*reflected_light*/) * 0.25)), passthrough);
// vec4 color = mix(vec4(surf_color, 1.0), vec4(surf_color, 0.0), passthrough);
//vec4 color = vec4(surf_color, 1.0);
2020-04-12 22:29:59 +00:00
vec4 color = vec4(surf_color, mix(1.0, 1.0 / (1.0 + /*0.25 * *//*diffuse_light*/(/*f_light * point_shadow*/reflected_light_point)), passthrough));
tgt_color = mix(mix(color, vec4(fog_color, 0.0), fog_level), vec4(clouds.rgb, 0.0), clouds.a);
}