Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330
- uniform sampler2D waterMap;
- uniform sampler2D noiseMap;
- uniform float animationFrame;
- in vec2 vTileCoords;
- out vec4 fragColor;
- vec4 cubic(float v){
- vec4 n = vec4(1.0, 2.0, 3.0, 4.0) - v;
- vec4 s = n * n * n;
- float x = s.x;
- float y = s.y - 4.0 * s.x;
- float z = s.z - 4.0 * s.y + 6.0 * s.x;
- float w = 6.0 - x - y - z;
- return vec4(x, y, z, w) * (1.0/6.0);
- }
- vec4 textureBicubic(sampler2D sampler, vec2 texCoords, vec2 texSize){
- vec2 invTexSize = 1.0 / texSize;
- texCoords = texCoords - 0.5;
- vec2 fxy = fract(texCoords);
- texCoords -= fxy;
- vec4 xcubic = cubic(fxy.x);
- vec4 ycubic = cubic(fxy.y);
- vec4 c = texCoords.xxyy + vec2(-0.5, +1.5).xyxy;
- vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw);
- vec4 offset = c + vec4(xcubic.yw, ycubic.yw) / s;
- offset *= invTexSize.xxyy;
- vec4 sample0 = texture(sampler, offset.xz);
- vec4 sample1 = texture(sampler, offset.yz);
- vec4 sample2 = texture(sampler, offset.xw);
- vec4 sample3 = texture(sampler, offset.yw);
- float sx = s.x / (s.x + s.y);
- float sy = s.z / (s.z + s.w);
- return mix(
- mix(sample3, sample2, sx), mix(sample1, sample0, sx)
- , sy);
- }
- float animationStrength = 0.2;
- float animationSpeed = 0.05;
- float animate(float frame){
- float wave = abs(sin(frame * animationSpeed)) - 1.0;
- return animationStrength * wave;
- }
- void main(){
- vec2 mapSize = vec2(textureSize(waterMap, 0));
- vec2 roundedCoords = floor(vTileCoords * 16.0) / 16.0;
- float baseWaterDepth = textureBicubic(waterMap, roundedCoords, mapSize).r;
- baseWaterDepth *= baseWaterDepth;
- float offset = texture(noiseMap, roundedCoords / mapSize * 0.1).r;
- float frame = animationFrame + offset*50;
- float waterDepth = baseWaterDepth + animate(frame);
- float waterDensity = 5.0;
- float waterExtinction = exp(-max(waterDepth, 0.0) * waterDensity);
- float wetness = 0.0;
- float waterThreshold = 0.10;
- if(waterDepth < waterThreshold){
- waterExtinction = 1.0;
- int iterations = 50;
- for(int i = 0; i < iterations; i++){
- int timeWarp = iterations - i - 1;
- float previousWaterDepth = baseWaterDepth + animate(frame - timeWarp);
- if(previousWaterDepth >= waterThreshold){
- wetness = float(i + 1) / float(iterations);
- }
- }
- fragColor = wetness * vec4(0.0, 0.0, 0.0, 0.4);
- }else{
- float alpha = 1.0 - waterExtinction;
- float wd1 = baseWaterDepth + animationStrength * (+sin(frame * animationSpeed) - 1.0);
- float wd2 = baseWaterDepth + animationStrength * (-sin(frame * animationSpeed) - 1.0);
- float foamAlpha1 = pow(clamp(1.0 - wd1 + waterThreshold, 0.0, 1.000001), 50);
- float foamAlpha2 = pow(clamp(1.0 - wd2 + waterThreshold, 0.0, 1.000001), 50);
- if(foamAlpha1 > 1.0){
- foamAlpha1 = 0.0;
- }else{
- foamAlpha1 = clamp(foamAlpha1 * 1.25, 0.0, 1.0);
- }
- if(foamAlpha2 > 1.0){
- foamAlpha2 = 0.0;
- }else{
- foamAlpha2 = clamp(foamAlpha2 * 1.25, 0.0, 1.0);
- }
- foamAlpha1 *= clamp(sin(frame * animationSpeed + 0.5)*0.9 + 0.5, 0.0, 1.0);
- foamAlpha2 *= clamp(-sin(frame * animationSpeed + 0.5)*0.9 + 0.5, 0.0, 1.0);
- float numEdges = 8.0;
- float power = 2.0;
- foamAlpha1 = pow(round(pow(foamAlpha1, 1.0/power)*numEdges) / numEdges, power);
- foamAlpha2 = pow(round(pow(foamAlpha2, 1.0/power)*numEdges) / numEdges, power);
- vec4 surfaceColor = mix(vec4(0.0, 0.15, 0.3, 0.4), vec4(0.9), clamp(foamAlpha1 + foamAlpha2, 0.0, 1.0));
- //INCORRECT CALCULATION OCCURS ON THIS LINE
- fragColor = vec4(surfaceColor.rgb, mix(surfaceColor.a, 1.0, alpha));
- //FIXED VERSION THAT WORKS AROUND THE ISSUE (mathematically identical)
- //fragColor = vec4(surfaceColor.rgb, mix(1.0, surfaceColor.a, waterExtinction));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement