1
0
Fork 0

HDR: Simplify water shader

This commit is contained in:
Fernando García Liñán 2023-04-28 04:58:24 +02:00
parent afa8331299
commit 649e2a0574
4 changed files with 80 additions and 302 deletions

View file

@ -1553,6 +1553,7 @@
<vertex-shader>Shaders/HDR/water.vert</vertex-shader>
<fragment-shader>Shaders/HDR/water.frag</fragment-shader>
<fragment-shader>Shaders/HDR/normal_encoding.glsl</fragment-shader>
<fragment-shader>Shaders/HDR/normalmap.glsl</fragment-shader>
<fragment-shader>Shaders/HDR/color.glsl</fragment-shader>
</program>
<uniform>
@ -1575,46 +1576,6 @@
<type>sampler-2d</type>
<value type="int">3</value>
</uniform>
<uniform>
<name>WindE</name>
<type>float</type>
<value><use>windE</use></value>
</uniform>
<uniform>
<name>WindN</name>
<type>float</type>
<value><use>windN</use></value>
</uniform>
<uniform>
<name>WaveFreq</name>
<type>float</type>
<value><use>WaveFreq</use></value>
</uniform>
<uniform>
<name>WaveAmp</name>
<type>float</type>
<value><use>WaveAmp</use></value>
</uniform>
<uniform>
<name>WaveSharp</name>
<type>float</type>
<value><use>WaveSharp</use></value>
</uniform>
<uniform>
<name>WaveAngle</name>
<type>float</type>
<value><use>WaveAngle</use></value>
</uniform>
<uniform>
<name>WaveFactor</name>
<type>float</type>
<value><use>WaveFactor</use></value>
</uniform>
<uniform>
<name>WaveDAngle</name>
<type>float</type>
<value><use>WaveDAngle</use></value>
</uniform>
</pass>
</technique>

View file

@ -1,218 +1,66 @@
#version 330 core
layout(location = 0) out vec4 outGBuffer0;
layout(location = 1) out vec4 outGBuffer1;
layout(location = 0) out vec4 out_gbuffer0;
layout(location = 1) out vec4 out_gbuffer1;
in vec4 waterTex1;
in vec4 waterTex2;
in mat3 TBN;
in vec3 relpos;
in vec2 TopoUV;
in VS_OUT {
vec2 water_texcoord;
vec2 topo_texcoord;
vec3 vertex_normal;
vec3 view_vector;
} fs_in;
uniform sampler2D perlin_normalmap;
uniform sampler2D water_dudvmap;
uniform sampler2D water_normalmap;
uniform sampler2D water_colormap;
uniform float WindE;
uniform float WindN;
uniform float WaveFreq;
uniform float WaveAmp;
uniform float WaveSharp;
uniform float WaveAngle;
uniform float WaveFactor;
uniform float WaveDAngle;
uniform float osg_SimulationTime;
uniform vec3 fg_SunDirection;
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;
// normal_encoding.glsl
vec2 encode_normal(vec3 n);
// color.glsl
vec3 eotf_inverse_sRGB(vec3 srgb);
// normalmap.glsl
mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv);
void rotationmatrix(float angle, out mat4 rotmat)
void get_rotation_matrix(float angle, out mat2 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
// wave functions ///////////////////////
struct Wave {
float freq; // 2*PI / wavelength
float amp; // amplitude
float phase; // speed * 2*PI / wavelength
vec2 dir;
};
Wave wave0 = Wave(1.0, 1.0, 0.5, vec2(0.97, 0.25));
Wave wave1 = Wave(2.0, 0.5, 1.3, vec2(0.97, -0.25));
Wave wave2 = Wave(1.0, 1.0, 0.6, vec2(0.95, -0.3));
Wave wave3 = Wave(2.0, 0.5, 1.4, vec2(0.99, 0.1));
float evaluateWave(in Wave w, vec2 pos, float t)
{
return w.amp * sin( dot(w.dir, pos) * w.freq + t * w.phase);
}
// derivative of wave function
float evaluateWaveDeriv(Wave w, vec2 pos, float t)
{
return w.freq * w.amp * cos( dot(w.dir, pos)*w.freq + t*w.phase);
}
// sharp wave functions
float evaluateWaveSharp(Wave w, vec2 pos, float t, float k)
{
return w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k);
}
float evaluateWaveDerivSharp(Wave w, vec2 pos, float t, float k)
{
return k*w.freq*w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k - 1) * cos( dot(w.dir, pos)*w.freq + t*w.phase);
}
void sumWaves(float angle, float dangle, float windScale, float factor, out float ddx, float ddy)
{
mat4 RotationMatrix;
float deriv;
vec4 P = waterTex1 * 1024;
rotationmatrix(radians(angle + dangle * windScale + 0.6 * sin(P.x * factor)), RotationMatrix);
P *= RotationMatrix;
P.y += evaluateWave(wave0, P.xz, osg_SimulationTime);
deriv = evaluateWaveDeriv(wave0, P.xz, osg_SimulationTime );
ddx = deriv * wave0.dir.x;
ddy = deriv * wave0.dir.y;
P.y += evaluateWave(wave1, P.xz, osg_SimulationTime);
deriv = evaluateWaveDeriv(wave1, P.xz, osg_SimulationTime);
ddx += deriv * wave1.dir.x;
ddy += deriv * wave1.dir.y;
P.y += evaluateWaveSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
deriv = evaluateWaveDerivSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
ddx += deriv * wave2.dir.x;
ddy += deriv * wave2.dir.y;
P.y += evaluateWaveSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
deriv = evaluateWaveDerivSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
ddx += deriv * wave3.dir.x;
ddy += deriv * wave3.dir.y;
rotmat = mat2(cos(angle), -sin(angle),
sin(angle), cos(angle));
}
void main()
{
const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005);
const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02);
const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25);
vec2 disdis = texture(water_dudvmap, vec2(fs_in.water_texcoord * tscale) * scale).rg * 2.0 - 1.0;
mat4 RotationMatrix;
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);
float windEffect = sqrt(WindE*WindE + WindN*WindN) * 0.6;
float windScale = 15.0/(3.0 + windEffect);
float windEffect_low = 0.3 + 0.7 * smoothstep(0.0, 5.0, windEffect);
float waveRoughness = 0.01 + smoothstep(0.0, 40.0, windEffect);
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);
float mixFactor = 0.2 + 0.02 * smoothstep(0.0, 50.0, windEffect);
mixFactor = clamp(mixFactor, 0.3, 0.8);
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);
// sine waves
float ddx, ddx1, ddx2, ddx3, ddy, ddy1, ddy2, ddy3;
float angle;
ddx = 0.0, ddy = 0.0;
ddx1 = 0.0, ddy1 = 0.0;
ddx2 = 0.0, ddy2 = 0.0;
ddx3 = 0.0, ddy3 = 0.0;
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);
// there's no need to do wave patterns or foam for pixels which are so
// far away that we can't actually see them
// we only need detail in the near zone or where the sun reflection is
int detail_flag;
float dist = length(relpos);
if ((dist > 15000.0) && (dot(normalize(vec3(fg_SunDirection.x, fg_SunDirection.y, 0.0) ), normalize(relpos)) < 0.7 )) {detail_flag = 0;}
else {detail_flag = 1;}
if (detail_flag == 1) {
angle = 0.0;
wave0.freq = WaveFreq ;
wave0.amp = WaveAmp;
wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
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);
angle -= 45;
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = WaveAmp * 1.25;
wave1.dir = vec2(0.70710, -0.7071); //vec2(cos(radians(angle)), sin(radians(angle)));
vec3 floor_color = eotf_inverse_sRGB(texture(water_colormap, fs_in.topo_texcoord).rgb);
angle += 30;
wave2.freq = WaveFreq * 3.5;
wave2.amp = WaveAmp * 0.75;
wave2.dir = vec2(0.96592, -0.2588);// vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 50;
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = WaveAmp * 0.75;
wave3.dir = vec2(0.42261, -0.9063); //vec2(cos(radians(angle)), sin(radians(angle)));
sumWaves(WaveAngle, -1.5, windScale, WaveFactor, ddx, ddy);
sumWaves(WaveAngle, 1.5, windScale, WaveFactor, ddx1, ddy1);
//reset the waves
angle = 0.0;
float waveamp = WaveAmp * 0.75;
wave0.freq = WaveFreq ;
wave0.amp = waveamp;
wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 20;
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = waveamp * 1.25;
wave1.dir = vec2(0.93969, -0.34202);// vec2(cos(radians(angle)), sin(radians(angle)));
angle += 35;
wave2.freq = WaveFreq * 3.5;
wave2.amp = waveamp * 0.75;
wave2.dir = vec2(0.965925, 0.25881); //vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 45;
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = waveamp * 0.75;
wave3.dir = vec2(0.866025, -0.5); //vec2(cos(radians(angle)), sin(radians(angle)));
sumWaves(WaveAngle + WaveDAngle, -1.5, windScale, WaveFactor, ddx2, ddy2);
sumWaves(WaveAngle + WaveDAngle, 1.5, windScale, WaveFactor, ddx3, ddy3);
}
vec4 disdis = texture(water_dudvmap, vec2(waterTex2 * tscale)* windScale) * 2.0 - 1.0;
vec3 N0 = vec3(texture(water_normalmap, vec2(waterTex1 + disdis * sca2) * windScale) * 2.0 - 1.0);
vec3 N1 = vec3(texture(perlin_normalmap, vec2(waterTex1 + disdis * sca) * windScale) * 2.0 - 1.0);
N0 += vec3(texture(water_normalmap, vec2(waterTex1 * tscale) * windScale) * 2.0 - 1.0);
N1 += vec3(texture(perlin_normalmap, vec2(waterTex2 * tscale) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(2.0 * sin(osg_SimulationTime * 0.005)), RotationMatrix);
N0 += vec3(texture(water_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
N1 += vec3(texture(perlin_normalmap, vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale) * 2.0 - 1.0);
rotationmatrix(radians(-4.0 * sin(osg_SimulationTime * 0.003)), RotationMatrix);
N0 += vec3(texture(water_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca2) * windScale) * 2.0 - 1.0);
N1 += vec3(texture(perlin_normalmap, vec2(waterTex1 * RotationMatrix + disdis * sca) * windScale) * 2.0 - 1.0);
N0 *= windEffect_low;
N1 *= windEffect_low;
N0.r += (ddx + ddx1 + ddx2 + ddx3);
N0.g += (ddy + ddy1 + ddy2 + ddy3);
vec3 N = normalize(mix(N0, N1, mixFactor) * waveRoughness);
vec3 floorColor = eotf_inverse_sRGB(texture(water_colormap, TopoUV).rgb);
outGBuffer0.rg = encode_normal(TBN * N);
outGBuffer1.rgb = floorColor;
out_gbuffer0.rg = encode_normal(N);
out_gbuffer1.rgb = floor_color;
}

View file

@ -3,15 +3,13 @@
layout(location = 0) in vec4 pos;
layout(location = 3) in vec4 multiTexCoord0;
out vec4 waterTex1;
out vec4 waterTex2;
out mat3 TBN;
out vec3 relpos;
out vec2 TopoUV;
out VS_OUT {
vec2 water_texcoord;
vec2 topo_texcoord;
vec3 vertex_normal;
vec3 view_vector;
} vs_out;
uniform float WindE, WindN;
uniform float osg_SimulationTime;
uniform mat4 osg_ModelViewMatrix;
uniform mat4 osg_ModelViewMatrixInverse;
uniform mat4 osg_ModelViewProjectionMatrix;
@ -24,56 +22,16 @@ const float squash = 0.9966471893352525192801545;
const float latAdjust = 0.9999074159800018; //geotiff source for the depth map
const float lonAdjust = 0.9999537058469516; //actual extents: +-180.008333333333326/+-90.008333333333340
void rotationmatrix(float angle, out mat4 rotmat)
void get_rotation_matrix(float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
rotmat = mat4(cos(angle), -sin(angle), 0.0, 0.0,
sin(angle), cos(angle), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0);
}
void main()
vec2 get_topo_coords(vec3 rawPos)
{
gl_Position = osg_ModelViewProjectionMatrix * pos;
// first current altitude of eye position in model space
vec4 ep = osg_ModelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0);
// and relative position to vector
relpos = pos.xyz - ep.xyz;
vec3 rawPos = (osg_ViewMatrixInverse * osg_ModelViewMatrix * pos).xyz;
// Using precalculated vectors
// vec3 T = normalize(osg_NormalMatrix * tangent);
// vec3 B = normalize(osg_NormalMatrix * binormal);
// vec3 N = normalize(osg_NormalMatrix * normal);
vec3 T = normalize(osg_NormalMatrix * vec3(0.0, -1.0, 0.0));
vec3 B = normalize(osg_NormalMatrix * vec3(1.0, 0.0, 0.0));
vec3 N = normalize(osg_NormalMatrix * vec3(0.0, 0.0, 1.0));
TBN = mat3(T, B, N);
mat4 RotationMatrix;
vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
float Angle;
float windFactor = sqrt(WindE * WindE + WindN * WindN) * 0.05;
if (WindN == 0.0 && WindE == 0.0) {
Angle = 0.0;
}else{
Angle = atan(-WindN, WindE) - atan(1.0);
}
rotationmatrix(Angle, RotationMatrix);
waterTex1 = multiTexCoord0 * RotationMatrix - t1 * windFactor;
rotationmatrix(Angle, RotationMatrix);
waterTex2 = multiTexCoord0 * RotationMatrix - t2 * windFactor;
// Geodesy lookup for depth map
float e2 = abs(1.0 - squash * squash);
float ra2 = 1.0/(a * a);
float e4 = e2 * e2;
@ -103,8 +61,20 @@ void main()
signT = 1.0;
float cosLon = dot(NormPosXY, vec2(1.0,0.0));
float cosLat = dot(abs(NormPosXZ), vec2(1.0,0.0));
TopoUV.s = signS * lonAdjust * degrees(acos(cosLon))/180.;
TopoUV.t = signT * latAdjust * degrees(acos(cosLat))/90.;
TopoUV.s = TopoUV.s * 0.5 + 0.5;
TopoUV.t = TopoUV.t * 0.5 + 0.5;
vec2 coord;
coord.s = signS * lonAdjust * degrees(acos(cosLon))/180.;
coord.t = signT * latAdjust * degrees(acos(cosLat))/90.;
return coord * 0.5 + 0.5;
}
void main()
{
gl_Position = osg_ModelViewProjectionMatrix * pos;
vs_out.water_texcoord = multiTexCoord0.st;
vs_out.vertex_normal = osg_NormalMatrix * vec3(0.0, 0.0, 1.0);
vs_out.view_vector = (osg_ModelViewMatrix * pos).xyz;
vec3 raw_pos = (osg_ViewMatrixInverse * vec4(vs_out.view_vector, 1.0)).xyz;
// Geodesy lookup for depth map
vs_out.topo_texcoord = get_topo_coords(raw_pos);
}

View file

@ -11,11 +11,10 @@ uniform samplerCube prefiltered_envmap_tex;
uniform mat4 fg_ViewMatrixInverse;
uniform vec3 fg_SunDirection;
const float MAX_PREFILTERED_LOD = 4.0;
// math.glsl
float M_PI();
float M_1_PI();
float pow5(float x);
// normal_encoding.glsl
vec3 decode_normal(vec2 f);
// pos_from_depth.glsl
@ -26,9 +25,9 @@ vec3 get_sun_radiance_sea_level();
// exposure.glsl
vec3 apply_exposure(vec3 color);
float F_Schlick(float VdotH, float F0)
float F_Schlick(float VdotH, float f0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0);
return f0 + (1.0 - f0) * pow5(clamp(1.0 - VdotH, 0.0, 1.0));
}
float D_GGX(float NdotH, float a2)
@ -39,10 +38,9 @@ float D_GGX(float NdotH, float a2)
void main()
{
// Unpack G-Buffer
vec4 gbuffer0 = texture(gbuffer0_tex, texcoord);
vec4 gbuffer1 = texture(gbuffer1_tex, texcoord);
// Unpack G-Buffer
vec3 N = decode_normal(gbuffer0.rg);
vec3 sea_color = gbuffer1.rgb;
@ -55,18 +53,19 @@ void main()
vec3 ws_refl = (fg_ViewMatrixInverse * vec4(refl, 0.0)).xyz;
vec3 H = normalize(L + V);
float NdotL = clamp(dot(N, L), 0.0, 1.0);
float NdotV = clamp(abs(dot(N, V)), 0.001, 1.0);
float NdotH = clamp(dot(N, H), 0.0, 1.0);
float VdotH = max(dot(V, H), 1e-4);
float NdotL = max(dot(N, L), 1e-4);
float NdotV = max(dot(N, V), 1e-4);
float NdotH = max(dot(N, H), 1e-4);
const float f0 = 0.02; // For IOR=1.33
const float f0 = 0.02; // For IOR = 1.33
float fresnel = F_Schlick(NdotV, f0);
// Refracted light
vec3 Esky = textureLod(prefiltered_envmap_tex, ws_N, MAX_PREFILTERED_LOD).rgb;
vec3 refracted = sea_color * Esky * M_1_PI();
// Reflected sky light
vec3 reflected = textureLod(prefiltered_envmap_tex, ws_refl, 1.0).rgb;
// Refracted light, diffuse light coming from the sea floor
vec3 sky_color = textureLod(prefiltered_envmap_tex, ws_N, 4.0).rgb;
vec3 refracted = (1.0 - f0) * sea_color * sky_color * M_1_PI();
// Reflected light, specular light coming from the sky
vec3 reflected = textureLod(prefiltered_envmap_tex, ws_refl, 0.0).rgb * M_1_PI();
vec3 color = mix(refracted, reflected, fresnel);