|
|
@@ -1,23 +1,19 @@
|
|
|
#ifdef OPENGL_ES
|
|
|
-precision mediump float;
|
|
|
+precision highp float;
|
|
|
#endif
|
|
|
|
|
|
//////////////////////////
|
|
|
// Uniforms
|
|
|
uniform sampler2D u_refractionTexture;
|
|
|
uniform sampler2D u_reflectionTexture;
|
|
|
-
|
|
|
uniform sampler2D u_normalMap;
|
|
|
-
|
|
|
uniform float u_time;
|
|
|
|
|
|
//////////////////////////
|
|
|
// Varyings
|
|
|
varying vec4 v_vertexRefractionPosition;
|
|
|
varying vec4 v_vertexReflectionPosition;
|
|
|
-
|
|
|
varying vec2 v_texCoord;
|
|
|
-
|
|
|
varying vec3 v_eyePosition;
|
|
|
|
|
|
//////////////////////////
|
|
|
@@ -25,11 +21,9 @@ varying vec3 v_eyePosition;
|
|
|
const float distortAmount = 0.05;
|
|
|
const float specularAmount = 2.5;
|
|
|
const float textureRepeat = 2.0;
|
|
|
-
|
|
|
const vec4 tangent = vec4(1.0, 0.0, 0.0, 0.0);
|
|
|
const vec4 viewNormal = vec4(0.0, 1.0, 0.0, 0.0);
|
|
|
const vec4 bitangent = vec4(0.0, 0.0, 1.0, 0.0);
|
|
|
-
|
|
|
const vec4 waterColour = vec4(0.36, 0.32, 0.2,1.0);
|
|
|
|
|
|
vec2 fromClipSpace(vec4 position)
|
|
|
@@ -39,39 +33,40 @@ vec2 fromClipSpace(vec4 position)
|
|
|
|
|
|
void main()
|
|
|
{
|
|
|
- //get normal
|
|
|
+ // Get normal
|
|
|
vec4 normal = texture2D(u_normalMap, v_texCoord * textureRepeat + u_time);
|
|
|
normal = normalize(normal * 2.0 - 1.0);
|
|
|
|
|
|
- //distortion offset
|
|
|
+ // Distortion offset
|
|
|
vec4 dudv = normal * distortAmount;
|
|
|
|
|
|
- //refraction sample
|
|
|
+ // Refraction sample
|
|
|
vec2 textureCoord = fromClipSpace(v_vertexRefractionPosition) + dudv.rg;
|
|
|
textureCoord = clamp(textureCoord, 0.001, 0.999);
|
|
|
vec4 refractionColour = texture2D(u_refractionTexture, textureCoord) * waterColour;
|
|
|
|
|
|
- //calc fog distance
|
|
|
- //----version 1 (exponential)----//
|
|
|
+ // Calc fog distance
|
|
|
+
|
|
|
+ // Version 1 (linear)
|
|
|
+ float z = (gl_FragCoord.z / gl_FragCoord.w) / 300.0; //const is max fog distance
|
|
|
+ const float fogDensity = 6.0;
|
|
|
+ float fogAmount = z * fogDensity;
|
|
|
+ fogAmount = clamp(fogAmount, 0.0, 1.0);
|
|
|
+
|
|
|
+ // Version 2 (exponential)
|
|
|
//float z = gl_FragCoord.z / gl_FragCoord.w;
|
|
|
//const float fogDensity = 0.0005;
|
|
|
//float fogAmount = exp2(-fogDensity * fogDensity * z * z * 1.442695);
|
|
|
//fogAmount = clamp(fogAmount, 0.0, 0.7);
|
|
|
|
|
|
- //----version 2 (linear)----//
|
|
|
- float z = (gl_FragCoord.z / gl_FragCoord.w) / 300.0; //const is max fog distance
|
|
|
- const float fogDensity = 6.0;
|
|
|
- float fogAmount = z * fogDensity;
|
|
|
- fogAmount = clamp(fogAmount, 0.0, 1.0);
|
|
|
-
|
|
|
refractionColour = mix(refractionColour, waterColour, fogAmount);
|
|
|
|
|
|
- //reflection sample
|
|
|
+ // Sample reflection
|
|
|
textureCoord = fromClipSpace(v_vertexReflectionPosition) + dudv.rg;
|
|
|
textureCoord = clamp(textureCoord, 0.001, 0.999);
|
|
|
vec4 reflectionColour = texture2D(u_reflectionTexture, textureCoord);
|
|
|
|
|
|
- //put view in tangent space
|
|
|
+ // View in tangent space
|
|
|
vec4 viewDir = normalize(vec4(v_eyePosition, 1.0));
|
|
|
vec4 viewTanSpace = normalize(vec4(dot(viewDir, tangent), dot(viewDir, bitangent), dot(viewDir, viewNormal), 1.0));
|
|
|
vec4 viewReflection = normalize(reflect(-1.0 * viewTanSpace, normal));
|