From 8caa5ff9c494046fca7d1d07e4f8481d6b0d3931 Mon Sep 17 00:00:00 2001 From: scrawl Date: Sun, 8 Apr 2012 15:03:09 +0200 Subject: [PATCH] fixed refraction and underwater distortion artifacts --- files/water/underwater.cg | 2 +- files/water/water.cg | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/files/water/underwater.cg b/files/water/underwater.cg index a734c316f..b853dd535 100644 --- a/files/water/underwater.cg +++ b/files/water/underwater.cg @@ -50,7 +50,7 @@ float4 main_fp (float2 iTexCoord : TEXCOORD0, { float4 normal = tex2D(NormalMap, noiseCoord) * 2 - 1; - float depth = tex2D(DepthMap, iTexCoord).r * far; + float depth = tex2D(DepthMap, iTexCoord + normal.xy * 0.015).r * far; depth = saturate(depth / 2000.f); float4 color = tex2D(RT, iTexCoord + normal.xy * 0.015) + diff --git a/files/water/water.cg b/files/water/water.cg index bbd42874b..7c7ec0780 100644 --- a/files/water/water.cg +++ b/files/water/water.cg @@ -62,7 +62,6 @@ void main_fp float depthTex = tex2D(depthMap, screenCoords).r; float depth1 = depthTex * far - iDepth; depth1 = saturate(depth1 / 500.f); - depth1 = (depthTex == 0 ? 1 : depth1); // Simple wave effect. to be replaced by something better float2 uv1 = iUv + time * float2(0.5, 0); @@ -74,7 +73,20 @@ void main_fp normal = 2*normal - 1; float2 screenCoords_reflect = screenCoords + normal.yx * 0.05; - float2 screenCoords_refract = screenCoords + normal.yx * 0.1 * depth1; + float2 screenCoords_refract = screenCoords + normal.yx * 0.05 * depth1; + + // Sample depth again with the refracted coordinates + depthTex = tex2D(depthMap, screenCoords_refract).r; + float depth2 = (depthTex * far - iDepth) / 500.f; + depth2 = (depthTex == 0 ? 1 : depth2); + // if depth2 is less than 0, this means we would refract something which is above water, + // which we don't want to - so in that case, don't refract + if (depth2 < 0.25) // delta due to inaccuracies + { + screenCoords_refract = screenCoords; + depth2 = depth1; + } + depth2 = saturate(depth2); float4 reflection = tex2D(reflectionMap, screenCoords_reflect); float4 refraction = tex2D(refractionMap, screenCoords_refract); @@ -93,7 +105,7 @@ void main_fp float3 halfVector = normalize(iEyeVector + lightDir); float specular = pow(max(dot(normal.xyz, halfVector.xyz), 0), 64); - float opacity = depth1 * saturate(reflectionFactor + specular); + float opacity = depth2 * saturate(reflectionFactor + specular); opacity *= (1-isUnderwater); reflection.xyz += lightSpecularColour0.xyz * specular;