Sign in to follow this  
ChristianFrantz

Clipping planes in XNA 4

Recommended Posts

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 

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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 , ...

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

 

Cheers!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this