Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #version 330
  2.  
  3. uniform sampler2D refractionTex;
  4. uniform sampler2D reflectionTex;
  5. uniform sampler2D dudvMap;
  6. uniform sampler2D normalMap;
  7. uniform sampler2D depthMap;
  8.  
  9. uniform vec3 lightColor;
  10.  
  11. uniform float moveFactor;
  12.  
  13. in vec4 clipSpace;
  14. in vec2 TexCoords0;
  15. in vec3 toCameraVector;
  16. in vec3 fromLightVector;
  17.  
  18. out vec4 FragColor;
  19.  
  20. const float waveStrength = 0.005;
  21. const float shineDamper = 20.0;
  22. const float reflectivity = 0.75;
  23.  
  24. void main()
  25. {
  26. // Get Normalized Device Space (-1 to 1)
  27. vec2 ndc = clipSpace.xy / clipSpace.w;
  28. // Make it 0 to 1
  29. ndc = ndc / 2.0 + 0.5;
  30.  
  31. vec2 refractUV = vec2(ndc.x, ndc.y);
  32. vec2 reflectUV = vec2(ndc.x, -ndc.y);
  33.  
  34. float near = 0.1;
  35. float far = 1000.0;
  36. float depth = texture2D(depthMap, refractUV).r;
  37. float floorDistance = 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
  38. if (depth >= 1.0)
  39. discard;
  40.  
  41. depth = gl_FragCoord.z;
  42. float waterDistance = 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
  43. float waterDepth = floorDistance - waterDistance;
  44.  
  45.  
  46.  
  47. vec2 distortedUV = texture2D(dudvMap, vec2(TexCoords0.x + moveFactor, TexCoords0.y)).rg * waveStrength;
  48. distortedUV = TexCoords0 + vec2(distortedUV.x, distortedUV.y + moveFactor);
  49. vec2 totalDistortion = (texture2D(dudvMap, distortedUV).rg * 2.0 - 1.0) * waveStrength * clamp(waterDepth * 8.0, 0.0, 1.0);
  50.  
  51. //vec2 distortion1 = (texture2D(dudvMap, vec2(TexCoords0.x + moveFactor, TexCoords0.y)).rg * 2.0 - 1.0) * waveStrength;
  52. //vec2 distortion2 = (texture2D(dudvMap, vec2(-TexCoords0.x + moveFactor, TexCoords0.y + moveFactor)).rg * 2.0 - 1.0) * waveStrength;
  53. //vec2 totalDistortion = distortion1 + distortion2;
  54.  
  55. refractUV += totalDistortion;
  56. refractUV = clamp(refractUV, 0.001, 0.999);
  57.  
  58. reflectUV += totalDistortion;
  59. reflectUV.x = clamp(reflectUV.x, 0.001, 0.999);
  60. reflectUV.y = clamp(reflectUV.y, -0.999, -0.001);
  61.  
  62. vec4 refractColor = texture2D(refractionTex, refractUV);
  63. vec4 reflectColor = texture2D(reflectionTex, reflectUV);
  64.  
  65. vec4 normalMapColor = texture2D(normalMap, distortedUV);
  66. vec3 normal = vec3(normalMapColor.r * 2.0 - 1.0, normalMapColor.b * 3.0, normalMapColor.g * 2.0 - 1.0);
  67. normal = normalize(normal);
  68.  
  69. vec3 viewVector = normalize(toCameraVector);
  70. float refractiveFactor = dot(viewVector, normal); //vec3(0.0, 1.0, 0.0));
  71. // Program works without this line, but it makes it more reflective at angles
  72. refractiveFactor = clamp(pow(refractiveFactor, 2.0), 0.0, 1.0);
  73.  
  74. vec3 reflectedLight = reflect(normalize(fromLightVector), normal);
  75. float specular = max(dot(reflectedLight, viewVector), 0.0);
  76. specular = pow(specular, shineDamper);
  77. vec3 specularHighlights = lightColor * specular * reflectivity * clamp(waterDepth * 8.0, 0.0, 1.0);;
  78.  
  79. FragColor = mix(reflectColor, refractColor, refractiveFactor);
  80. FragColor = mix(FragColor, vec4(0.0, 0.1, 0.25, 1.0), 0.2) + vec4(specularHighlights, 0.0);
  81. FragColor.a = clamp(waterDepth * 16.0, 0.0, 1.0);
  82. FragColor.r = waterDepth;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement