1
0
forked from 0ad/0ad

Updated water shader to support more settings (specular strength and reflection tint).

This was SVN commit r4317.
This commit is contained in:
Matei 2006-09-10 05:30:49 +00:00
parent aa50820d9a
commit 222d90727e

View File

@ -5,27 +5,28 @@ uniform vec3 cameraPos;
uniform sampler2D normalMap; uniform sampler2D normalMap;
uniform sampler2D reflectionMap; uniform sampler2D reflectionMap;
uniform sampler2D refractionMap; uniform sampler2D refractionMap;
uniform float shininess; uniform float shininess; // Blinn-Phong specular strength
uniform float waviness; uniform float specularStrength; // Scaling for specular reflection (specular color is (this,this,this))
uniform vec3 tint; uniform float waviness; // "Wildness" of the reflections and refractions; choose based on texture
uniform float murkiness; uniform vec3 tint; // Tint for refraction (used to simulate particles in water)
uniform float fullDepth; uniform float murkiness; // Amount of tint to blend in with the refracted colour
uniform float fullDepth; // Depth at which to use full murkiness (shallower water will be clearer)
uniform vec3 reflectionTint; // Tint for reflection (used for really muddy water)
uniform float reflectionTintStrength; // Strength of reflection tint (how much of it to mix in)
varying vec3 worldPos; varying vec3 worldPos;
varying float w; varying float w;
varying float waterDepth; varying float waterDepth;
varying float losMod; varying float losMod;
const vec3 specularColor = vec3(0.15, 0.15, 0.15);
void main() void main()
{ {
vec3 n, l, h, v; // Normal, light vector, half-vector and view vector (vector to eye) vec3 n, l, h, v; // Normal, light vector, half-vector and view vector (vector to eye)
float ndotl, ndoth, ndotv; float ndotl, ndoth, ndotv;
float fresnel; float fresnel;
float myMurkiness; // Murkiness and tint at this pixel (tweaked based on lighting and depth) float myMurkiness; // Murkiness and tint at this pixel (tweaked based on lighting and depth)
vec3 myTint; vec3 diffuse; // Diffuse colour at this pixel (ambient + diffuse terms)
float t; float t; // Temporary variable
vec2 reflCoords, refrCoords; vec2 reflCoords, refrCoords;
vec3 reflColor, refrColor, specular; vec3 reflColor, refrColor, specular;
@ -38,21 +39,25 @@ void main()
ndoth = dot(n, h); ndoth = dot(n, h);
ndotv = dot(n, v); ndotv = dot(n, v);
fresnel = pow(1.0 - ndotv, 0.8); // A rather random Fresnel approximation diffuse = ambient + ndotl * sunColor;
reflCoords = 0.5 * (gl_TexCoord[1].xy / gl_TexCoord[1].w) + 0.5; // Unbias texture coords myMurkiness = murkiness * min(waterDepth / fullDepth, 1.0);
reflCoords += waviness * n.xz / w;
fresnel = pow(1.0 - ndotv, 0.8); // A rather random Fresnel approximation
refrCoords = 0.5 * (gl_TexCoord[2].xy / gl_TexCoord[2].w) + 0.5; // Unbias texture coords refrCoords = 0.5 * (gl_TexCoord[2].xy / gl_TexCoord[2].w) + 0.5; // Unbias texture coords
refrCoords -= 0.8 * waviness * n.xz / w; // Refractions can be slightly less wavy refrCoords -= 0.8 * waviness * n.xz / w; // Refractions can be slightly less wavy
reflColor = texture2D(reflectionMap, reflCoords).rgb; reflCoords = 0.5 * (gl_TexCoord[1].xy / gl_TexCoord[1].w) + 0.5; // Unbias texture coords
reflCoords += waviness * n.xz / w;
myMurkiness = murkiness * min(waterDepth / fullDepth, 1.0); reflColor = mix(texture2D(reflectionMap, reflCoords).rgb, diffuse * reflectionTint,
myTint = (ambient + ndotl * sunColor) * tint; reflectionTintStrength);
refrColor = (0.6 + 0.4*ndotl) * mix(texture2D(refractionMap, refrCoords).rgb, myTint, myMurkiness);
specular = pow(max(0.0, ndoth), shininess) * sunColor * specularColor; refrColor = mix((0.6 + 0.4*ndotl) * texture2D(refractionMap, refrCoords).rgb, diffuse * tint,
myMurkiness);
specular = pow(max(0.0, ndoth), shininess) * sunColor * specularStrength;
gl_FragColor.rgb = mix(refrColor + 0.3*specular, reflColor + specular, fresnel) * losMod; gl_FragColor.rgb = mix(refrColor + 0.3*specular, reflColor + specular, fresnel) * losMod;