1
0
Fork 0
fgdata/Shaders/HDR/water.frag

72 lines
2.5 KiB
GLSL
Raw Normal View History

2021-08-25 02:17:09 +00:00
#version 330 core
2023-04-28 02:58:24 +00:00
layout(location = 0) out vec4 out_gbuffer0;
layout(location = 1) out vec4 out_gbuffer1;
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
in VS_OUT {
float flogz;
2023-04-28 02:58:24 +00:00
vec2 water_texcoord;
vec2 topo_texcoord;
vec3 vertex_normal;
vec3 view_vector;
} fs_in;
2021-08-25 02:17:09 +00:00
uniform sampler2D perlin_normalmap;
uniform sampler2D water_dudvmap;
uniform sampler2D water_normalmap;
uniform sampler2D water_colormap;
uniform float osg_SimulationTime;
2023-04-28 02:58:24 +00:00
const vec2 sca = vec2(0.005);
const vec2 sca2 = vec2(0.02);
const vec2 tscale = vec2(0.25);
const float scale = 5.0;
const float mix_factor = 0.5;
2021-08-25 02:17:09 +00:00
// normal_encoding.glsl
vec2 encode_normal(vec3 n);
// color.glsl
vec3 eotf_inverse_sRGB(vec3 srgb);
2023-04-28 02:58:24 +00:00
// normalmap.glsl
mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv);
// logarithmic_depth.glsl
float logdepth_encode(float z);
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
void get_rotation_matrix(float angle, out mat2 rotmat)
2021-08-25 02:17:09 +00:00
{
2023-04-28 02:58:24 +00:00
rotmat = mat2(cos(angle), -sin(angle),
sin(angle), cos(angle));
2021-08-25 02:17:09 +00:00
}
void main()
{
2023-04-28 02:58:24 +00:00
vec2 disdis = texture(water_dudvmap, vec2(fs_in.water_texcoord * tscale) * scale).rg * 2.0 - 1.0;
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
vec3 N0 = vec3(texture(water_normalmap, vec2(fs_in.water_texcoord + disdis * sca2) * scale) * 2.0 - 1.0);
vec3 N1 = vec3(texture(perlin_normalmap, vec2(fs_in.water_texcoord + disdis * sca) * scale) * 2.0 - 1.0);
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
N0 += vec3(texture(water_normalmap, vec2(fs_in.water_texcoord * tscale) * scale) * 2.0 - 1.0);
N1 += vec3(texture(perlin_normalmap, vec2(fs_in.water_texcoord * tscale) * scale) * 2.0 - 1.0);
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
mat2 rotmatrix;
get_rotation_matrix(radians(2.0 * sin(osg_SimulationTime * 0.005)), rotmatrix);
N0 += vec3(texture(water_normalmap, vec2(fs_in.water_texcoord * rotmatrix * (tscale + sca2)) * scale) * 2.0 - 1.0);
N1 += vec3(texture(perlin_normalmap, vec2(fs_in.water_texcoord * rotmatrix * (tscale + sca2)) * scale) * 2.0 - 1.0);
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
get_rotation_matrix(radians(-4.0 * sin(osg_SimulationTime * 0.003)), rotmatrix);
N0 += vec3(texture(water_normalmap, vec2(fs_in.water_texcoord * rotmatrix + disdis * sca2) * scale) * 2.0 - 1.0);
N1 += vec3(texture(perlin_normalmap, vec2(fs_in.water_texcoord * rotmatrix + disdis * sca) * scale) * 2.0 - 1.0);
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
vec3 N = normalize(mix(N0, N1, mix_factor));
mat3 TBN = cotangent_frame(fs_in.vertex_normal, fs_in.view_vector, fs_in.water_texcoord);
N = normalize(TBN * N);
2021-08-25 02:17:09 +00:00
2023-04-28 02:58:24 +00:00
vec3 floor_color = eotf_inverse_sRGB(texture(water_colormap, fs_in.topo_texcoord).rgb);
2023-04-28 02:58:24 +00:00
out_gbuffer0.rg = encode_normal(N);
out_gbuffer1.rgb = floor_color;
gl_FragDepth = logdepth_encode(fs_in.flogz);
2021-08-25 02:17:09 +00:00
}