3dexperiments/assets/shaders/water.fs

29 lines
907 B
Forth
Raw Normal View History

precision mediump float;
varying vec4 clipSpace;
uniform sampler2D reflectionTexture;
uniform sampler2D refractionTexture;
uniform sampler2D dudvMap;
varying vec2 uv;
const float waveStrength = 0.002;
void main() {
vec2 ndc = (clipSpace.xy / clipSpace.w) / 2.0 + 0.5;
vec2 refractTexCoords = vec2(ndc.x, ndc.y);
vec2 reflectTexCoords = vec2(ndc.x, 1.0 - ndc.y);
vec2 distortion1 = texture2D(dudvMap, uv).xy * 2.0 - 1.0 * waveStrength;
refractTexCoords += distortion1;
refractTexCoords = clamp(refractTexCoords, 0.001, 0.999);
reflectTexCoords += distortion1;
reflectTexCoords.x = clamp(reflectTexCoords.x, 0.001, 0.999);
reflectTexCoords.y = clamp(reflectTexCoords.y, -0.999, -0.001);
vec4 reflectColor = texture2D(reflectionTexture, reflectTexCoords);
vec4 refractColor = texture2D(refractionTexture, refractTexCoords);
gl_FragColor = mix(reflectColor, refractColor, 0.5);
}