[Solved] WaterReflection problem

Im doing a water reflection shader and im getting a weird problem were as i decrease the height from the water or the plane its on. The water pulls sideways.
Im also seeing the bottom.

I checked the inflection point and it looks ok. But the behavior seems like it is that as i go to far down it flips.
So the back becomes the front. im not sure why its skewing like that either.

near the edge of the world

way down after i get the weird warping and then things seem to flip or something.

Maybe im not doing it right at all ?

My shader looks like this so far with comments.

//_______________________________________________________________
// technique 
// ReflectionWaterShader.  
//
// TextureCubeSampler
//_______________________________________________________________
// ToDo ....  
//__________________________________________________________

struct VsInReflectionWater
{
    float4 Position : POSITION0;
    float3 Normal : NORMAL0;
    float2 TexureCoordinateA : TEXCOORD0;
};
struct VsOutReflectionWater
{
    float4 Position : SV_Position;
    float2 TexureCoordinateA : TEXCOORD0;
    float4 Position3D    : TEXCOORD1;
    float3 Normal : TEXCOORD2;
    //float3 thisToPixel : TEXCOORD3;
};
// shaders
VsOutReflectionWater VsReflectionWater(VsInReflectionWater input)
{
    VsOutReflectionWater output;
    output.Position3D = mul(input.Position, World);
    //output.thisToPixel = output.Position3D - World._m30_m31_m32;
    float4x4 vp = mul(View, Projection);
    output.Position = mul(output.Position3D, vp);
    output.TexureCoordinateA = input.TexureCoordinateA;
    output.Normal = normalize(mul(input.Normal, World)); // Reminder, if we want to rotate a scaled world normal we need to renormalize it.
    return output;
}

float4 PsReflectionWater(VsOutReflectionWater input) : Color
{
    float3 cubeDir = normalize(input.Position3D - InflectionOrReflectionVector);
    float4 reflectionCubeColor = texCUBE(TextureCubeSampler, cubeDir);  // -input.cubeDir
    return float4(reflectionCubeColor.xyz, 1.0f);
}

technique ReflectionWaterShader
{
    pass
    {
        VertexShader = compile VS_SHADERMODEL VsReflectionWater();
        PixelShader = compile PS_SHADERMODEL PsReflectionWater();
    }
}

Here my calculation for the inflected camera position under the water plane.
It looks like it works when i draw lines to see it.

    Vector3 InflectionPositionFromPlane(Vector3 theCameraPostion, Vector3 thePlanesSurfaceNormal, Vector3 anyPositionOnThePlane)
    {
        var directionVector = thePlanesSurfaceNormal * -Vector3.Dot(thePlanesSurfaceNormal, (theCameraPostion - anyPositionOnThePlane));
        var pointOnSurface = directionVector + theCameraPostion;
        var surfaceDistance = Vector3.Distance(theCameraPostion, pointOnSurface);
        return pointOnSurface - theCameraPostion;
    }

I think your InflectionPositionFromPlane only works in specific cases.
This should always work:

    Vector3 InflectionPositionFromPlane(Vector3 theCameraPostion, Vector3 thePlanesSurfaceNormal, Vector3 anyPositionOnThePlane)
    {
        float camToPlaneDist = Vector3.Dot(thePlanesSurfaceNormal, theCameraPostion - anyPositionOnThePlane);
        return theCameraPostion - thePlanesSurfaceNormal  * camToPlaneDist * 2 ;
    }
1 Like

Edit2 yep that fixed it thank you markus.

Yep that got rid of the streching but i still have a wierd flip.
I can stop it half way thru the flip.

Its really weird i can’t wrap my head around what would trigger that.
Maybe thats elsewere its almost like the whole plane is shifting.
As i move down that brick moves up but that brick is actually just below the water line i think.

Edit :
lol oh never mind that is below the water line my brain is stuck in debug mode i crossed the plane and thats just the culling.

Dont mind the bends thats just because i didnt set the sphere to a high resolution.