1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
|
/**
* @project Spring RTS
* @file bumpWaterFS.glsl
* @brief An extended bumpmapping water shader
* @author jK
*
* Copyright (C) 2008-2012. Licensed under the terms of the
* GNU GPL, v2 or later.
*/
#define CausticDepth 0.5
#define CausticRange 0.45
#define WavesLength 0.15
#ifdef opt_texrect
// ancient
#extension GL_ARB_texture_rectangle : enable
#else
#define sampler2DRect sampler2D
#endif
//////////////////////////////////////////////////
// Uniforms + Varyings
uniform sampler2D normalmap;
uniform sampler2D heightmap;
uniform sampler2D caustic;
uniform sampler2D foam;
uniform sampler2D reflection;
uniform sampler2DRect refraction;
uniform sampler2D coastmap;
uniform sampler2DRect depthmap;
uniform sampler2D waverand;
#ifdef opt_infotex
uniform sampler2D infotex;
#endif
#ifdef opt_shadows
uniform sampler2DShadow shadowmap;
uniform mat4 shadowMatrix;
// added via SetupUniforms or as a constant definition
// uniform float shadowDensity;
#endif
uniform mat4 projMatrix;
uniform float frame;
// neither added via SetupUniforms nor as a constant definition
uniform float gammaExponent;
uniform vec3 eyePos;
uniform vec3 fogColor;
uniform vec3 fogParams;
in vec3 eyeVec;
in vec3 sunVec;
in vec3 worldPos;
in vec2 clampedPotCoords;
in vec4 texCoord0;
in vec4 texCoord1;
in vec4 texCoord2;
in vec4 texCoord3;
in vec4 texCoord4;
in vec4 texCoord5;
in float fogCoord;
layout(location = 0) out vec4 fragColor;
vec2 clampedWorldPos = clamp(worldPos.xz, vec2(0.0), (vec2(1.0) / TexGenPlane.xy));
//////////////////////////////////////////////////
// Screen Coordinates (normalized and screen dimensions)
#ifdef opt_texrect
vec2 screencoord = gl_FragCoord.xy - ViewPos;
vec2 reftexcoord = screencoord * ScreenInverse;
#else
vec2 screencoord = (gl_FragCoord.xy - ViewPos) * ScreenTextureSizeInverse;
vec2 reftexcoord = (gl_FragCoord.xy - ViewPos) * ScreenInverse;
#endif
//////////////////////////////////////////////////
// Depth conversion
#ifdef opt_depth
float ConvertDepthToEyeZ(float d) {
float pm14 = projMatrix[3].z;
float pm10 = projMatrix[2].z;
return (pm14 / (d * -2.0 + 1.0 - pm10));
}
#endif
//////////////////////////////////////////////////
// shorewaves functions
#ifdef opt_shorewaves
const float InvWavesLength = 1.0 / WavesLength;
float smoothlimit(const float x, const float edge) {
float limitcurv = edge - (mod(x,edge) * edge) / (1.0 - edge);
return mix(x, limitcurv, step(edge, x));
}
vec4 waveIntensity(const vec4 v) {
vec4 front = vec4(1.0)-(abs(v - vec4(0.85)))/vec4(1.0-0.85);
bvec4 bs = lessThan(v, vec4(0.85));
front = max(front, vec4(bs) * v * 0.5);
return front;
}
#endif
//////////////////////////////////////////////////
// Shadow
float GetShadowOcclusion(vec3 worldPos) {
#ifdef opt_shadows
vec4 vertexShadowPos = shadowMatrix * vec4(worldPos, 1.0);
return mix(1.0, textureProj(shadowmap, vertexShadowPos), shadowDensity);
#endif
return 1.0;
}
//////////////////////////////////////////////////
// infotex
vec3 GetInfoTex(float outside) {
vec3 info = vec3(0.0);
#ifdef opt_infotex
vec2 clampedPotCoords = ShadingPlane.xy * clampedWorldPos;
info = (texture(infotex, clampedPotCoords).rgb - 0.5) * 0.5;
info = mix(info, vec3(0.0), outside);
#endif
return info;
}
//////////////////////////////////////////////////
// Helpers
vec3 GetWaterHeight(inout vec2 coastdist)
{
vec3 depths = vec3(0.0, 0.0, 0.0);
#ifdef opt_shorewaves
vec3 coast = texture(coastmap, texCoord0.st).rgb;
coastdist = coast.rg;
depths.y = coast.b;
#else
depths.y = texture(heightmap, texCoord5.st).a; // heightmap in alpha channel
#endif
#ifdef opt_endlessocean
depths.z = min(1.0, distance(clampedWorldPos, worldPos.xz));
depths.y = mix(depths.y, 1.0, step(0.5, depths.z));
#endif
depths.x = 1.0 - depths.y;
return depths;
}
vec3 GetNormal(out vec3 octave)
{
vec3 octave1 = texture(normalmap, texCoord1.st).rgb;
vec3 octave2 = texture(normalmap, texCoord1.pq).rgb;
vec3 octave3 = texture(normalmap, texCoord2.st).rgb;
vec3 octave4 = texture(normalmap, texCoord2.pq).rgb;
float a = PerlinAmp;
octave1 = (octave1 * 2.0 - 1.0) * a;
octave2 = (octave2 * 2.0 - 1.0) * a * a;
octave3 = (octave3 * 2.0 - 1.0) * a * a * a;
octave4 = (octave4 * 2.0 - 1.0) * a * a * a * a;
vec3 normal = normalize(octave1 + octave2 + octave3 + octave4);
octave = octave3;
return normal.xzy;
}
float GetWaterDepthFromDepthBuffer(float waterdepth)
{
#ifdef opt_depth
// calculate difference between texel-z and fragment-z; convert
// since both are non-linear mappings from 0=dr.min to 1=dr.max
// absolute differences larger than 3 elmos are clamped to 1
float texZ = ConvertDepthToEyeZ(texture2DRect(depthmap, screencoord).r);
float fragZ = ConvertDepthToEyeZ(gl_FragCoord.z);
float diffZ = abs(texZ - fragZ) * 0.333;
return clamp(diffZ, 0.0, 1.0);
#else
return waterdepth;
#endif
}
vec3 GetShorewaves(vec2 coast, vec3 octave, float waterdepth , float invwaterdepth)
{
vec3 color = vec3(0.0, 0.0, 0.0);
#ifdef opt_shorewaves
float coastdist = coast.g + octave.x * 0.1;
if (coastdist > 0.0) {
// no shorewaves/foam under terrain (is 0.0 underground, 1.0 else)
float underground = 1.0 - step(1.0, invwaterdepth);
vec3 wavefoam = texture(foam, texCoord3.st).rgb;
wavefoam += texture(foam, texCoord3.pq).rgb;
wavefoam *= 0.5;
// shorewaves
vec4 waverands = texture(waverand, texCoord4.pq);
vec4 fi = vec4(0.25, 0.50, 0.75, 1.00);
vec4 f = fract(fi + frame * 50.0);
f = f * 1.4 - vec4(coastdist);
f = vec4(1.0) - f * InvWavesLength;
f = clamp(f, 0.0, 1.0);
f = waveIntensity(f);
float intensity = dot(f, waverands) * coastdist;
float iwd = smoothlimit(invwaterdepth, 0.8);
intensity *= iwd * 1.5;
color += wavefoam * underground * intensity;
// cliff foam
color += (wavefoam * wavefoam) * (underground * 5.5 * (coast.r * coast.r * coast.r) * (coastdist * coastdist * coastdist * coastdist));
}
#endif
return color;
}
vec4 GetReflection(vec3 normal, float angle)
{
vec4 reflColor = vec4(0.0, 0.0, 0.0, 0.0);
#ifdef opt_reflection
// we have to mirror the Y-axis
reftexcoord = vec2(reftexcoord.x, 1.0 - reftexcoord.y);
reftexcoord += vec2(0.0, 3.0 * ScreenInverse.y) + normal.xz * 0.09 * ReflDistortion;
reflColor.rgb = texture(reflection, reftexcoord).rgb;
#ifdef opt_blurreflection
vec2 v = BlurBase;
float s = BlurExponent;
reflColor.rgb += texture(reflection, reftexcoord.st + v).rgb;
reflColor.rgb += texture(reflection, reftexcoord.st + v *s).rgb;
reflColor.rgb += texture(reflection, reftexcoord.st + v *s*s).rgb;
reflColor.rgb += texture(reflection, reftexcoord.st + v *s*s*s).rgb;
reflColor.rgb += texture(reflection, reftexcoord.st + v *s*s*s*s).rgb;
reflColor.rgb += texture(reflection, reftexcoord.st + v *s*s*s*s*s).rgb;
reflColor.rgb += texture(reflection, reftexcoord.st + v *s*s*s*s*s*s).rgb;
reflColor.rgb *= 0.125;
#endif
// fresnel term
reflColor.a = FresnelMin + FresnelMax * pow(angle, FresnelPower);
#endif
return reflColor;
}
//////////////////////////////////////////////////
// MAIN()
void main()
{
#ifdef dbg_coastmap
fragColor = vec4(texture(coastmap, texCoord0.st).g);
return;
#endif
// only rendered with blending iff !opt_refraction
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
// GET WATERDEPTH
//
// depths.z := outside
// depths.x := waterdepth
// depths.y := invwaterdepth
vec2 coast = vec2(0.0, 0.0);
vec3 depths = GetWaterHeight(coast);
float shallowScale = GetWaterDepthFromDepthBuffer(depths.x);
// NORMALMAP
vec3 octave;
vec3 normal = GetNormal(octave);
vec3 eyeDir = normalize(eyeVec);
vec3 reflectDir = reflect(normalize(-sunVec), normal);
vec3 SunLow = SunDir * vec3(1.0, 0.1, 1.0);
float eyeNormalCos = dot(-eyeDir, normal);
float angle = (1.0 - abs(eyeNormalCos));
// SHADOW
float shadowOcc = GetShadowOcclusion(worldPos + vec3(normal.x, 0.0, normal.z) * 10.0);
// AMBIENT & DIFFUSE
float diffuse = pow(max(dot(normal, SunLow), 0.0), 3.0) * DiffuseFactor;
float ambient = smoothstep(-1.3, 0.0, eyeNormalCos) * AmbientFactor;
float specular = angle * pow(max(dot(reflectDir, eyeDir), 0.0), SpecularPower) * SpecularFactor * shallowScale;
float surfaceMix = (SurfaceColor.a + diffuse) * shallowScale;
float refractDistortion = 60.0 * (1.0 - pow(gl_FragCoord.z, 80.0)) * shallowScale;
// NB: DiffuseColor (water.diffuseColor) and DiffuseFactor (water.diffuseFactor) default to 1
vec3 waterSurface = SurfaceColor.rgb + DiffuseColor * diffuse + vec3(ambient);
// INFOTEX
waterSurface += GetInfoTex(depths.z);
{
// REFRACTION
#ifdef opt_refraction
#ifdef opt_texrect
vec3 refrColor = texture(refraction, screencoord + normal.xz * refractDistortion).rgb;
#else
vec3 refrColor = texture(refraction, screencoord + normal.xz * refractDistortion * ScreenInverse).rgb;
#endif
fragColor.rgb = mix(refrColor, waterSurface, 0.1 + surfaceMix);
#else
fragColor.rgb = waterSurface;
fragColor.a = surfaceMix + specular;
#endif
}
// CAUSTICS
if (depths.x > 0.0) {
vec3 caust = texture(caustic, texCoord0.pq * 75.0).rgb;
#ifdef opt_refraction
float caustBlend = smoothstep(CausticRange, 0.0, abs(depths.x - CausticDepth));
fragColor.rgb += caust * caustBlend * 0.08;
#else
fragColor.a *= min(depths.x * 4.0, 1.0);
fragColor.rgb += caust * (1.0 - depths.x) * 0.6;
#endif
}
{
// SHORE WAVES
fragColor.rgb += shadowOcc * GetShorewaves(coast, octave, depths.x, depths.y);
}
{
// REFLECTION
// Schlick's approx. for Fresnel term
vec4 reflColor = GetReflection(normal, angle);
fragColor.rgb = mix(fragColor.rgb, reflColor.rgb, reflColor.a * shallowScale);
}
{
// SPECULAR
fragColor.rgb += (shadowOcc * specular * SpecularColor);
}
{
// FOG
float fogRange = (fogParams.y - fogParams.x) * fogParams.z;
float fogDepth = (fogCoord - fogParams.x * fogParams.z) / fogRange;
// float fogDepth = (fogParams.y * fogParams.z - fogCoord) / fogRange;
float fogFactor = 1.0 - clamp(fogDepth, 0.0, 1.0);
// float fogFactor = clamp(fogDepth, 0.0, 1.0);
fragColor.rgb = mix(fogColor.rgb, fragColor.rgb, fogFactor);
}
fragColor.rgb = pow(fragColor.rgb, vec3(gammaExponent));
}
|