Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/shaders/glsl/model_water.fs
#version 120 | #version 120 | ||||
#include "common/los_fragment.h" | |||||
#include "common/shadows_fragment.h" | #include "common/shadows_fragment.h" | ||||
uniform sampler2D baseTex; | uniform sampler2D baseTex; | ||||
uniform sampler2D losTex; | |||||
uniform sampler2D aoTex; | uniform sampler2D aoTex; | ||||
uniform sampler2D normTex; | uniform sampler2D normTex; | ||||
uniform sampler2D specTex; | uniform sampler2D specTex; | ||||
uniform sampler2D waterTex; | uniform sampler2D waterTex; | ||||
uniform samplerCube skyCube; | uniform samplerCube skyCube; | ||||
#if USE_OBJECTCOLOR | #if USE_OBJECTCOLOR | ||||
Show All 12 Lines | |||||
uniform float specularStrength; | uniform float specularStrength; | ||||
uniform float waviness; | uniform float waviness; | ||||
uniform vec3 waterTint; | uniform vec3 waterTint; | ||||
uniform float murkiness; | uniform float murkiness; | ||||
uniform vec3 reflectionTint; | uniform vec3 reflectionTint; | ||||
uniform float reflectionTintStrength; | uniform float reflectionTintStrength; | ||||
float waterDepth = 4.0; | float waterDepth = 4.0; | ||||
float fullDepth = 5.0; // Depth at which to use full murkiness (shallower water will be clearer) | float fullDepth = 5.0; // Depth at which to use full murkiness (shallower water will be clearer) | ||||
varying vec4 worldPos; | varying vec4 worldPos; | ||||
varying vec4 v_tex; | varying vec4 v_tex; | ||||
varying vec2 v_los; | |||||
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 t; // Temporary variable | float t; // Temporary variable | ||||
vec2 reflCoords, refrCoords; | vec2 reflCoords, refrCoords; | ||||
vec3 reflColor, refrColor, specular; | vec3 reflColor, refrColor, specular; | ||||
float losMod; | |||||
//vec4 wtex = textureGrad(waterTex, vec3(fract(v_tex.xy), v_tex.z), dFdx(v_tex.xy), dFdy(v_tex.xy)); | //vec4 wtex = textureGrad(waterTex, vec3(fract(v_tex.xy), v_tex.z), dFdx(v_tex.xy), dFdy(v_tex.xy)); | ||||
vec4 wtex = texture2D(waterTex, fract(v_tex.xy)); | vec4 wtex = texture2D(waterTex, fract(v_tex.xy)); | ||||
n = normalize(wtex.xzy - vec3(0.5, 0.5, 0.5)); | n = normalize(wtex.xzy - vec3(0.5, 0.5, 0.5)); | ||||
l = -sunDir; | l = -sunDir; | ||||
v = normalize(cameraPos - worldPos.xyz); | v = normalize(cameraPos - worldPos.xyz); | ||||
h = normalize(l + v); | h = normalize(l + v); | ||||
ndotl = dot(n, l); | ndotl = dot(n, l); | ||||
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 | fresnel = pow(1.0 - ndotv, 0.8); // A rather random Fresnel approximation | ||||
//refrCoords = (0.5*gl_TexCoord[2].xy - 0.8*waviness*n.xz) / gl_TexCoord[2].w + 0.5; // Unbias texture coords | //refrCoords = (0.5*gl_TexCoord[2].xy - 0.8*waviness*n.xz) / gl_TexCoord[2].w + 0.5; // Unbias texture coords | ||||
//reflCoords = (0.5*gl_TexCoord[1].xy + waviness*n.xz) / gl_TexCoord[1].w + 0.5; // Unbias texture coords | //reflCoords = (0.5*gl_TexCoord[1].xy + waviness*n.xz) / gl_TexCoord[1].w + 0.5; // Unbias texture coords | ||||
//vec3 dir = normalize(v + vec3(waviness*n.x, 0.0, waviness*n.z)); | //vec3 dir = normalize(v + vec3(waviness*n.x, 0.0, waviness*n.z)); | ||||
vec3 eye = reflect(v, n); | vec3 eye = reflect(v, n); | ||||
vec3 tex = textureCube(skyCube, eye).rgb; | vec3 tex = textureCube(skyCube, eye).rgb; | ||||
reflColor = mix(tex, sunColor * reflectionTint, | reflColor = mix(tex, sunColor * reflectionTint, | ||||
reflectionTintStrength); | reflectionTintStrength); | ||||
//waterDepth = 4.0 + 2.0 * dot(abs(v_tex.zw - 0.5), vec2(0.5)); | //waterDepth = 4.0 + 2.0 * dot(abs(v_tex.zw - 0.5), vec2(0.5)); | ||||
waterDepth = 4.0; | waterDepth = 4.0; | ||||
//refrColor = (0.5 + 0.5*ndotl) * mix(texture2D(refractionMap, refrCoords).rgb, sunColor * tint, | //refrColor = (0.5 + 0.5*ndotl) * mix(texture2D(refractionMap, refrCoords).rgb, sunColor * tint, | ||||
refrColor = (0.5 + 0.5*ndotl) * mix(vec3(0.3), sunColor * waterTint, | refrColor = (0.5 + 0.5*ndotl) * mix(vec3(0.3), sunColor * waterTint, | ||||
murkiness * clamp(waterDepth / fullDepth, 0.0, 1.0)); // Murkiness and tint at this pixel (tweaked based on lighting and depth) | murkiness * clamp(waterDepth / fullDepth, 0.0, 1.0)); // Murkiness and tint at this pixel (tweaked based on lighting and depth) | ||||
specular = pow(max(0.0, ndoth), 150.0f) * sunColor * specularStrength; | specular = pow(max(0.0, ndoth), 150.0f) * sunColor * specularStrength; | ||||
losMod = texture2D(losTex, v_los).a; | |||||
#if USE_SHADOW | #if USE_SHADOW | ||||
float shadow = get_shadow(); | float shadow = get_shadow(); | ||||
float fresShadow = mix(fresnel, fresnel*shadow, dot(sunColor, vec3(0.16666))); | float fresShadow = mix(fresnel, fresnel*shadow, dot(sunColor, vec3(0.16666))); | ||||
#else | #else | ||||
float fresShadow = fresnel; | float fresShadow = fresnel; | ||||
#endif | #endif | ||||
vec3 color = mix(refrColor + 0.3*specular, reflColor + specular, fresShadow); | vec3 color = mix(refrColor + 0.3*specular, reflColor + specular, fresShadow); | ||||
gl_FragColor.rgb = color * losMod; | gl_FragColor.rgb = color * getLOS(); | ||||
//gl_FragColor.rgb = mix(refrColor + 0.3*specular, reflColor + specular, fresnel) * losMod; | |||||
// Make alpha vary based on both depth (so it blends with the shore) and view angle (make it | // Make alpha vary based on both depth (so it blends with the shore) and view angle (make it | ||||
// become opaque faster at lower view angles so we can't look "underneath" the water plane) | // become opaque faster at lower view angles so we can't look "underneath" the water plane) | ||||
t = 18.0 * max(0.0, 0.7 - v.y); | t = 18.0 * max(0.0, 0.7 - v.y); | ||||
gl_FragColor.a = 0.15 * waterDepth * (1.2 + t + fresnel); | gl_FragColor.a = 0.15 * waterDepth * (1.2 + t + fresnel); | ||||
} | } | ||||
Wildfire Games · Phabricator