I am working on a submarine game. I have a caustic effect and i want the effect to fade with increasing depth. I don't know much about hlsl programming only some basics that allows me to draw basic scene with some effects.

I was thinking about using vertex * worldmatrix y value to get the height but it doesn't work how i want.

void VertScene( float4 iPos : POSITION, float3 iNormal : NORMAL, float2 iTex : TEXCOORD0, out float4 wPos : POSITION1, out float4 oPos : POSITION, out float2 oTex : TEXCOORD0, out float oCamDist : TEXCOORD4, out float4 vPos : TEXCOORD1, out float3 vNormal : TEXCOORD2, out float4 vPosLight : TEXCOORD3, out float2 caustCoord : TEXCOORD5) { wPos = mul( iPos, g_mWorld ); vPos = mul( wPos, g_mView ); oPos = mul( vPos, g_mProj ); oTex = iTex; oCamDist=oPos.z; vNormal = mul(iNormal, (float3x3)mul(g_mWorld, g_mView)); vPosLight = mul( vPos, g_mViewToLightProj ); float2x2 rotation = {COS_0_15F, -SIN_0_15F, SIN_0_15F, COS_0_15F}; caustCoord=mul(wPos.xz, rotation); } /////// Pixel shader float2 movement = caustCoord.xy; movement.x = movement.x + cos(g_fTime * 0.2f*20) * 0.3f; movement.y = movement.y + sin(g_fTime * 0.3f*20) * 0.2f; float4 cautictex = tex2D(CausticTextureSampler, movement.xy * 0.07f); float dcaust=0.2*(1.0-saturate((400.5-wPos.y)/20.0)); float4 ps_LightAmbient = dcaust * cautictex+ 0.2 * g_LightAmbient + 0.6 * g_LightAmbient * saturate( dot( -normalize( float3( vPos - g_sun_poz ) ), normalize( vNormal ) ) );

saturate((400.5-wPos.y)/20.0) doesn't seem to work, (400.5 is the water surface position, 20.0 is the max depth at witch caustic effect should be visible), when i use constant 0 - 0.2 it works well.

What am i doing wrong?