Clipping planes in XNA 4

Started by
5 comments, last by ChristianFrantz 9 years, 7 months ago

Following Riemers tutorials for creating a water effect, I came across a function device.ClipPlane which does not exist in XNA 4. I googled a bit and found this effect that someone wrote for clipping a plane.


float4x4 World; 
float4x4 View; 
float4x4 Projection; 
 
float4 ClipPlane0; 
 
void vs(inout float4 position : POSITION0, out float4 clipDistances : TEXCOORD0) 
{ 

    clipDistances.y = 0; 
    clipDistances.z = 0; 
    clipDistances.w = 0; 
 
    position = mul(mul(mul(position, World), View), Projection); 

	    clipDistances.x = dot(position, ClipPlane0); 
} 
 
float4 ps(float4 clipDistances : TEXCOORD0) : COLOR0 
{ 
    clip(clipDistances); 
 
    return float4(0, 0, 0, 0); // TODO: whatever other shading logic you want 
} 
 
technique 
{ 
    pass 
    { 
        VertexShader = compile vs_2_0 vs(); 
        PixelShader = compile ps_2_0 ps(); 
    } 
} 

The result is supposed to show a saved picture of my refraction texture, but nothing is clipped.


        public void DrawRefractionMap(Effect clipEffect, Camera camera, GraphicsDevice device)
        {
            Plane refractionPlane = CreatePlane(waterHeight + 1.5f, new Vector3(0, -1, 0), camera, false);
            clipEffect.Parameters["ClipPlane0"].SetValue(new Vector4(refractionPlane.Normal, refractionPlane.D));

            device.SetRenderTarget(refractionRenderTarget);

            device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1, 0);

            foreach (EffectPass pass in clipEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                DrawTerrain(clipEffect, camera, device);
            }

            device.SetRenderTarget(null);
            refractionTexture = (Texture2D)refractionRenderTarget;

            FileStream stream = File.OpenWrite("Screenshot3333.png");
            
            refractionTexture.SaveAsJpeg(stream, refractionTexture.Width, refractionTexture.Height);
        }

Is there something wrong within the shader? I am not getting any errors or warnings

If you see a post from me, you can safely assume its C# and XNA :)

Advertisement
Well, one thing is your pixel shader is returning black. Anything drawn using that pixel shader will be black.

I guess I should ask, what are you seeing, and what exactly do you want to see? You said nothing is clipped, so your seeing the whole picture?

This is how I clipped pixels when I was drawing to reflection texture :


clip(dot(float4(input.WorldPos.xyz,1.0f),ClipPlane));

I simply passed the wolrd space position to the Pixel Shader. My Clip Plane is also built in this form :



ClipPlane = new Vector4( Plane.Normal , - Plane.Postion.Length );

Hope it helps , ...

This is what I'm getting

Screenshot3333.jpg

this is what im supposed to get

XNA_Tutorial_8_Refraction_map.jpg

So there is no clipping done at all.

@BlackBrain I'm talking about refraction, not reflection. Unless you just made a typo :P I'll give it a shot though

If you see a post from me, you can safely assume its C# and XNA :)

You want clipping plane , don't you ? Clipping plane is the same no matter it is reflection or refraction or anything else . Just set it up correctly and you should get correct results.

Just to make thing clear - you don't need clipping planes for refraction texture - just for the reflection texture

Cheers!

Feel free to point me to an easier way because I'm just following riemers tutorial on how to do it :P

If you see a post from me, you can safely assume its C# and XNA :)

This topic is closed to new replies.

Advertisement