Hello. Can someone explain me how to do soft water edges, or give me a link of tutorial? I also tried to do this myself, but nothing happens. The only thing that happens, so water fragments that are more far form camera is more transparent and which fragments are near camera they are less transparent (see image below). If someone could help me, here is my current fragment shader's subroutine which is used while rendering water:
layout(binding = 1) uniform sampler2D waterReflection;
layout(binding = 2) uniform sampler2D waterRefraction;
layout(binding = 3) uniform sampler2D normalMap;
layout(binding = 4) uniform sampler2D dudvMap;
layout(binding = 5) uniform sampler2D sceneDepth;
layout(binding = 6) uniform sampler2D waterDepth;
in vec2 TexCoord;
uniform vec4 WaterColor;
uniform float kShine;
uniform float kDistortion;
uniform float kRefraction;
subroutine(RenderPassType)
void drawWater()
{
vec4 distOffset = texture2D(dudvMap, NormalUV) * kDistortion;
vec4 dudvColor = texture2D(dudvMap, RefractionUV + distOffset.xy);
dudvColor = normalize(dudvColor * 2.0 - 1.0) * kRefraction;
vec4 normalVector = texture2D(normalMap, RefractionUV + distOffset.xy);
normalVector = normalVector * 2.0 - 1.0;
normalVector.a = 0.0;
vec4 lightReflection = normalize(reflect(-LightTangentSpace, normalVector));
vec4 fresnelTerm = vec4(dot(normalVector, lightReflection));
vec4 invertedFresnel = 1.0 - fresnelTerm;
vec4 ViewCoordY = ViewCoord;
ViewCoordY.y = -ViewCoordY.y;
vec4 projCoord = ViewCoordY / ViewCoord.q;
projCoord = (projCoord + 1.0) * 0.5;
projCoord += dudvColor;
projCoord = clamp(projCoord, 0.001, 0.999);
vec4 reflectionColor = texture2D(waterReflection, projCoord.xy);
projCoord = ViewCoord / ViewCoord.q;
projCoord = (projCoord + 1.0) * 0.5;
projCoord += dudvColor;
projCoord = clamp(projCoord, 0.001, 0.999);
vec4 refractionColor = texture2D(waterRefraction, projCoord.xy);
float dColor = texture2D(sceneDepth, projCoord.xy).x;
vec4 depthValue = vec4(dColor, dColor, dColor, 1.0); //Scene depth
vec4 invDepth = 1.0 - depthValue;
refractionColor *= invertedFresnel * invDepth;
refractionColor += WaterColor * depthValue * invertedFresnel;
reflectionColor *= fresnelTerm;
vec4 localView = normalize(ViewTangentSpace);
float intensity = max(0.0, dot(lightReflection, localView));
vec4 specular = vec4(pow(intensity, kShine));
vec4 color = refractionColor + reflectionColor + specular;
dColor = texture2D(waterDepth, projCoord.xy).x;
vec4 vWaterDepth = vec4(dColor, dColor, dColor, 1.0);
color.a = (depthValue - vWaterDepth) * 2.5; //Subtract scene depth from water depth and scale the result
FragColor = color;
}
And here the image how it looks now: http://imageshack.com/a/img540/1930/Dfkzrr.jpg I actually have seen somewhere that I need to subtract scene's depth value from water depth value, but I don't know what texture coordinates I should use when getting depth value from depth texture using texture2D function. I have several choices: projCoord.xy, gl_FragCoord.xy, TexCoord, and maybe more.