Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #version 330
  2.  
  3.  
  4. uniform sampler2D waterMap;
  5. uniform sampler2D noiseMap;
  6.  
  7.  
  8. uniform float animationFrame;
  9.  
  10. in vec2 vTileCoords;
  11.  
  12. out vec4 fragColor;
  13.  
  14.  
  15. vec4 cubic(float v){
  16. vec4 n = vec4(1.0, 2.0, 3.0, 4.0) - v;
  17. vec4 s = n * n * n;
  18. float x = s.x;
  19. float y = s.y - 4.0 * s.x;
  20. float z = s.z - 4.0 * s.y + 6.0 * s.x;
  21. float w = 6.0 - x - y - z;
  22. return vec4(x, y, z, w) * (1.0/6.0);
  23. }
  24.  
  25. vec4 textureBicubic(sampler2D sampler, vec2 texCoords, vec2 texSize){
  26.  
  27. vec2 invTexSize = 1.0 / texSize;
  28.  
  29. texCoords = texCoords - 0.5;
  30.  
  31.  
  32. vec2 fxy = fract(texCoords);
  33. texCoords -= fxy;
  34.  
  35. vec4 xcubic = cubic(fxy.x);
  36. vec4 ycubic = cubic(fxy.y);
  37.  
  38. vec4 c = texCoords.xxyy + vec2(-0.5, +1.5).xyxy;
  39.  
  40. vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw);
  41. vec4 offset = c + vec4(xcubic.yw, ycubic.yw) / s;
  42.  
  43. offset *= invTexSize.xxyy;
  44.  
  45. vec4 sample0 = texture(sampler, offset.xz);
  46. vec4 sample1 = texture(sampler, offset.yz);
  47. vec4 sample2 = texture(sampler, offset.xw);
  48. vec4 sample3 = texture(sampler, offset.yw);
  49.  
  50. float sx = s.x / (s.x + s.y);
  51. float sy = s.z / (s.z + s.w);
  52.  
  53. return mix(
  54. mix(sample3, sample2, sx), mix(sample1, sample0, sx)
  55. , sy);
  56. }
  57.  
  58. float animationStrength = 0.2;
  59. float animationSpeed = 0.05;
  60.  
  61. float animate(float frame){
  62.  
  63. float wave = abs(sin(frame * animationSpeed)) - 1.0;
  64.  
  65. return animationStrength * wave;
  66. }
  67.  
  68. void main(){
  69.  
  70. vec2 mapSize = vec2(textureSize(waterMap, 0));
  71.  
  72. vec2 roundedCoords = floor(vTileCoords * 16.0) / 16.0;
  73.  
  74. float baseWaterDepth = textureBicubic(waterMap, roundedCoords, mapSize).r;
  75. baseWaterDepth *= baseWaterDepth;
  76.  
  77. float offset = texture(noiseMap, roundedCoords / mapSize * 0.1).r;
  78.  
  79. float frame = animationFrame + offset*50;
  80.  
  81. float waterDepth = baseWaterDepth + animate(frame);
  82.  
  83. float waterDensity = 5.0;
  84. float waterExtinction = exp(-max(waterDepth, 0.0) * waterDensity);
  85.  
  86. float wetness = 0.0;
  87.  
  88. float waterThreshold = 0.10;
  89.  
  90.  
  91. if(waterDepth < waterThreshold){
  92. waterExtinction = 1.0;
  93.  
  94. int iterations = 50;
  95. for(int i = 0; i < iterations; i++){
  96.  
  97. int timeWarp = iterations - i - 1;
  98.  
  99. float previousWaterDepth = baseWaterDepth + animate(frame - timeWarp);
  100.  
  101. if(previousWaterDepth >= waterThreshold){
  102. wetness = float(i + 1) / float(iterations);
  103. }
  104. }
  105.  
  106. fragColor = wetness * vec4(0.0, 0.0, 0.0, 0.4);
  107. }else{
  108.  
  109. float alpha = 1.0 - waterExtinction;
  110.  
  111. float wd1 = baseWaterDepth + animationStrength * (+sin(frame * animationSpeed) - 1.0);
  112. float wd2 = baseWaterDepth + animationStrength * (-sin(frame * animationSpeed) - 1.0);
  113.  
  114. float foamAlpha1 = pow(clamp(1.0 - wd1 + waterThreshold, 0.0, 1.000001), 50);
  115. float foamAlpha2 = pow(clamp(1.0 - wd2 + waterThreshold, 0.0, 1.000001), 50);
  116. if(foamAlpha1 > 1.0){
  117. foamAlpha1 = 0.0;
  118. }else{
  119. foamAlpha1 = clamp(foamAlpha1 * 1.25, 0.0, 1.0);
  120. }
  121. if(foamAlpha2 > 1.0){
  122. foamAlpha2 = 0.0;
  123. }else{
  124. foamAlpha2 = clamp(foamAlpha2 * 1.25, 0.0, 1.0);
  125. }
  126.  
  127. foamAlpha1 *= clamp(sin(frame * animationSpeed + 0.5)*0.9 + 0.5, 0.0, 1.0);
  128. foamAlpha2 *= clamp(-sin(frame * animationSpeed + 0.5)*0.9 + 0.5, 0.0, 1.0);
  129.  
  130. float numEdges = 8.0;
  131. float power = 2.0;
  132. foamAlpha1 = pow(round(pow(foamAlpha1, 1.0/power)*numEdges) / numEdges, power);
  133. foamAlpha2 = pow(round(pow(foamAlpha2, 1.0/power)*numEdges) / numEdges, power);
  134.  
  135. vec4 surfaceColor = mix(vec4(0.0, 0.15, 0.3, 0.4), vec4(0.9), clamp(foamAlpha1 + foamAlpha2, 0.0, 1.0));
  136.  
  137. //INCORRECT CALCULATION OCCURS ON THIS LINE
  138. fragColor = vec4(surfaceColor.rgb, mix(surfaceColor.a, 1.0, alpha));
  139.  
  140. //FIXED VERSION THAT WORKS AROUND THE ISSUE (mathematically identical)
  141. //fragColor = vec4(surfaceColor.rgb, mix(1.0, surfaceColor.a, waterExtinction));
  142.  
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement