Water.glsl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Fog.glsl"
  6. varying vec4 vScreenPos;
  7. varying vec2 vReflectUV;
  8. varying vec2 vWaterUV;
  9. varying vec3 vNormal;
  10. varying vec4 vEyeVec;
  11. #ifdef COMPILEVS
  12. uniform vec2 cNoiseSpeed;
  13. uniform float cNoiseTiling;
  14. #endif
  15. #ifdef COMPILEPS
  16. uniform float cNoiseStrength;
  17. uniform float cFresnelPower;
  18. uniform vec3 cWaterTint;
  19. #endif
  20. void VS()
  21. {
  22. mat4 modelMatrix = iModelMatrix;
  23. vec3 worldPos = GetWorldPos(modelMatrix);
  24. gl_Position = GetClipPos(worldPos);
  25. vScreenPos = GetScreenPos(gl_Position);
  26. // GetQuadTexCoord() returns a vec2 that is OK for quad rendering; multiply it with output W
  27. // coordinate to make it work with arbitrary meshes such as the water plane (perform divide in pixel shader)
  28. // Also because the quadTexCoord is based on the clip position, and Y is flipped when rendering to a texture
  29. // on OpenGL, must flip again to cancel it out
  30. vReflectUV = GetQuadTexCoord(gl_Position);
  31. vReflectUV.y = 1.0 - vReflectUV.y;
  32. vReflectUV *= gl_Position.w;
  33. vWaterUV = iTexCoord * cNoiseTiling + cElapsedTime * cNoiseSpeed;
  34. vNormal = GetWorldNormal(modelMatrix);
  35. vEyeVec = vec4(cCameraPos - worldPos, GetDepth(gl_Position));
  36. }
  37. void PS()
  38. {
  39. vec2 refractUV = vScreenPos.xy / vScreenPos.w;
  40. vec2 reflectUV = vReflectUV.xy / vScreenPos.w;
  41. vec2 noise = (texture2D(sNormalMap, vWaterUV).rg - 0.5) * cNoiseStrength;
  42. refractUV += noise;
  43. // Do not shift reflect UV coordinate upward, because it will reveal the clipping of geometry below water
  44. if (noise.y < 0.0)
  45. noise.y = 0.0;
  46. reflectUV += noise;
  47. float fresnel = pow(1.0 - clamp(dot(normalize(vEyeVec.xyz), vNormal), 0.0, 1.0), cFresnelPower);
  48. vec3 refractColor = texture2D(sEnvMap, refractUV).rgb * cWaterTint;
  49. vec3 reflectColor = texture2D(sDiffMap, reflectUV).rgb;
  50. vec3 finalColor = mix(refractColor, reflectColor, fresnel);
  51. gl_FragColor = vec4(GetFog(finalColor, GetFogFactor(vEyeVec.w)), 1.0);
  52. }