Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330
- uniform sampler2D refractionTex;
- uniform sampler2D reflectionTex;
- uniform sampler2D dudvMap;
- uniform sampler2D normalMap;
- uniform sampler2D depthMap;
- uniform vec3 lightColor;
- uniform float moveFactor;
- in vec4 clipSpace;
- in vec2 TexCoords0;
- in vec3 toCameraVector;
- in vec3 fromLightVector;
- out vec4 FragColor;
- const float waveStrength = 0.005;
- const float shineDamper = 20.0;
- const float reflectivity = 0.75;
- void main()
- {
- // Get Normalized Device Space (-1 to 1)
- vec2 ndc = clipSpace.xy / clipSpace.w;
- // Make it 0 to 1
- ndc = ndc / 2.0 + 0.5;
- vec2 refractUV = vec2(ndc.x, ndc.y);
- vec2 reflectUV = vec2(ndc.x, -ndc.y);
- float near = 0.1;
- float far = 1000.0;
- float depth = texture2D(depthMap, refractUV).r;
- float floorDistance = 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
- if (depth >= 1.0)
- discard;
- depth = gl_FragCoord.z;
- float waterDistance = 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
- float waterDepth = floorDistance - waterDistance;
- vec2 distortedUV = texture2D(dudvMap, vec2(TexCoords0.x + moveFactor, TexCoords0.y)).rg * waveStrength;
- distortedUV = TexCoords0 + vec2(distortedUV.x, distortedUV.y + moveFactor);
- vec2 totalDistortion = (texture2D(dudvMap, distortedUV).rg * 2.0 - 1.0) * waveStrength * clamp(waterDepth * 8.0, 0.0, 1.0);
- //vec2 distortion1 = (texture2D(dudvMap, vec2(TexCoords0.x + moveFactor, TexCoords0.y)).rg * 2.0 - 1.0) * waveStrength;
- //vec2 distortion2 = (texture2D(dudvMap, vec2(-TexCoords0.x + moveFactor, TexCoords0.y + moveFactor)).rg * 2.0 - 1.0) * waveStrength;
- //vec2 totalDistortion = distortion1 + distortion2;
- refractUV += totalDistortion;
- refractUV = clamp(refractUV, 0.001, 0.999);
- reflectUV += totalDistortion;
- reflectUV.x = clamp(reflectUV.x, 0.001, 0.999);
- reflectUV.y = clamp(reflectUV.y, -0.999, -0.001);
- vec4 refractColor = texture2D(refractionTex, refractUV);
- vec4 reflectColor = texture2D(reflectionTex, reflectUV);
- vec4 normalMapColor = texture2D(normalMap, distortedUV);
- vec3 normal = vec3(normalMapColor.r * 2.0 - 1.0, normalMapColor.b * 3.0, normalMapColor.g * 2.0 - 1.0);
- normal = normalize(normal);
- vec3 viewVector = normalize(toCameraVector);
- float refractiveFactor = dot(viewVector, normal); //vec3(0.0, 1.0, 0.0));
- // Program works without this line, but it makes it more reflective at angles
- refractiveFactor = clamp(pow(refractiveFactor, 2.0), 0.0, 1.0);
- vec3 reflectedLight = reflect(normalize(fromLightVector), normal);
- float specular = max(dot(reflectedLight, viewVector), 0.0);
- specular = pow(specular, shineDamper);
- vec3 specularHighlights = lightColor * specular * reflectivity * clamp(waterDepth * 8.0, 0.0, 1.0);;
- FragColor = mix(reflectColor, refractColor, refractiveFactor);
- FragColor = mix(FragColor, vec4(0.0, 0.1, 0.25, 1.0), 0.2) + vec4(specularHighlights, 0.0);
- FragColor.a = clamp(waterDepth * 16.0, 0.0, 1.0);
- FragColor.r = waterDepth;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement