Advertisement
Guest User

Water pass

a guest
Jan 29th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 450
  2.  
  3. #include "compiled.inc"
  4. #include "std/gbuffer.glsl"
  5. #include "std/math.glsl"
  6.  
  7. uniform sampler2D gbufferD;
  8. uniform sampler2D tex;
  9. uniform sampler2D sbase;
  10. uniform sampler2D sdetail;
  11. uniform sampler2D sfoam;
  12. #ifdef _Rad
  13. uniform sampler2D senvmapRadiance;
  14. #endif
  15.  
  16. uniform float time;
  17. uniform vec3 eye;
  18. uniform vec3 eyeLook;
  19. uniform vec2 cameraProj;
  20. uniform vec3 ld;
  21. uniform float envmapStrength;
  22.  
  23. in vec2 texCoord;
  24. in vec3 viewRay;
  25. out vec4 fragColor;
  26.  
  27. void main() {
  28.  
  29.     float gdepth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
  30.     if (gdepth == 1.0) {
  31.         fragColor = vec4(0.0);
  32.         return;
  33.     }
  34.  
  35.     // Eye below water
  36.     if (eye.z < waterLevel) {
  37.         fragColor = vec4(0.0);
  38.         return;
  39.     }
  40.  
  41.     // Displace surface
  42.     vec3 vray = normalize(viewRay);
  43.     vec3 p = getPos(eye, eyeLook, vray, gdepth, cameraProj);
  44.     float speed = time * 2.0 * waterSpeed;
  45.     p.z += sin(p.x * 10.0 / waterDisplace + speed) * cos(p.y * 10.0 / waterDisplace + speed) / 50.0 * waterDisplace;
  46.  
  47.     // Above water
  48.     if (p.z > waterLevel) {
  49.         fragColor = vec4(0.0);
  50.         return;
  51.     }
  52.  
  53.     // Hit plane to determine uvs
  54.     vec3 v = normalize(eye - p.xyz);
  55.     float t = -(dot(eye, vec3(0.0, 0.0, 1.0)) - waterLevel) / dot(v, vec3(0.0, 0.0, 1.0));
  56.     vec3 hit = eye + t * v;
  57.     hit.xy *= waterFreq;
  58.     hit.z += waterLevel;
  59.  
  60.     // Sample normal maps
  61.     vec2 tcnor0 = hit.xy / 3.0;
  62.     vec3 n0 = textureLod(sdetail, tcnor0 + vec2(speed / 60.0, speed / 120.0), 0.0).rgb;
  63.  
  64.     vec2 tcnor1 = hit.xy / 6.0 + n0.xy / 20.0;
  65.     vec3 n1 = textureLod(sbase, tcnor1 + vec2(speed / 40.0, speed / 80.0), 0.0).rgb;
  66.     vec3 n2 = normalize(((n1 + n0) / 2.0) * 2.0 - 1.0);
  67.  
  68.     float ddepth = textureLod(gbufferD, texCoord + (n2.xy * n2.z) / 40.0, 0.0).r * 2.0 - 1.0;
  69.     vec3 p2 = getPos(eye, eyeLook, vray, ddepth, cameraProj);
  70.     vec2 tc = p2.z > waterLevel ? texCoord : texCoord + (n2.xy * n2.z) / 30.0 * waterRefract;
  71.  
  72.     // Light
  73.     float fresnel = 1.0 - max(dot(n2, v), 0.0);
  74.     fresnel = pow(fresnel, 30.0) * 0.45;
  75.     vec3 r = reflect(-v, n2);
  76.     #ifdef _Rad
  77.     vec3 reflected =  textureLod(senvmapRadiance, envMapEquirect(r), 0).rgb;
  78.     #else
  79.     const vec3 reflected = vec3(0.5);
  80.     #endif
  81.     vec3 refracted = textureLod(tex, tc, 0.0).rgb;
  82.     fragColor.rgb = mix(refracted, reflected, fresnel * waterReflect);
  83.     fragColor.rgb *= waterColor;
  84.     fragColor.rgb += clamp(pow(max(dot(r, ld), 0.0), 200.0) * (200.0 + 8.0) / (PI * 8.0), 0.0, 2.0);
  85.     fragColor.rgb *= 1.0 - (clamp(-(p.z - waterLevel) * waterDensity, 0.0, 0.9));
  86.     fragColor.a = clamp(abs(p.z - waterLevel) * 5.0, 0.0, 1.0);
  87.  
  88.     // Foam
  89.     float fd = abs(p.z - waterLevel);
  90.     if (fd < 0.1) {
  91.         // Based on foam by Owen Deery
  92.         // http://fire-face.com/personal/water
  93.         vec3 foamMask0 = textureLod(sfoam, tcnor0 * 10, 0.0).rgb;
  94.         vec3 foamMask1 = textureLod(sfoam, tcnor1 * 11, 0.0).rgb;
  95.         vec3 foam = vec3(1.0) - foamMask0.rrr - foamMask1.bbb;
  96.         float fac = 1.0 - (fd * (1.0 / 0.1));
  97.         fragColor.rgb = mix(fragColor.rgb, clamp(foam, 0.0, 1.0), clamp(fac, 0.0, 1.0));
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement