Code Snippet: Clipping Planes in Shader [HLSL]



In the XNA 4.0 we dont have anymore the Fixed function that let us specify a clipping plane. Now we have to do this in shaders =P. In this post i will show how to do this !

The following code shows how to implement ONE clipping plane in Projection Space. (same as the fixed pipeline)

If you want more than one plane, just use the Y,Z,W coordinates from the clipping variable to put the others values. If you want to use the plane in World or View space, just make the “dot” with the position in the right space.

The theory for this is simple. The dot operation evaluates the plane equation and the result give us in what side of the plane the position is. For more info check this.

float4x4 WVP;
float4 clippingPlane;
bool isClip;

Texture diffuse;
sampler DiffuseSampler = sampler_state
{
texture =  ;
magfilter = ANISOTROPIC;
minfilter = ANISOTROPIC;
mipfilter = LINEAR;
AddressU = wrap;
AddressV = wrap;
};

struct VertexShaderInput
{
float3 Position : POSITION0;
float4 texturecoord :  TEXCOORD0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 texturecoord :  TEXCOORD0;
float4 clipping :  TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = mul(float4(input.Position,1), WVP);
output.texturecoord = input.texturecoord;
output.clipping = 0;
float4 clp = output.Position;
output.clipping.x = dot(clp,clippingPlane) ;
return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
if(isClip)
clip(input.clipping.x);

return tex2D(DiffuseSampler, input.texturecoord);
}

technique Technique1
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}

, , , , , ,

  1. #1 by balmain outlet men on 17 de janeiro de 2017 - 3:56 am

    I found your entry interesting do I’ve added a Trackback to it on my weblog and my seotons
    balmain outlet men

  2. #2 by Sports on 17 de janeiro de 2017 - 3:56 am

    I do not even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you’re going to a famous blogger if you aren’t already 😉 Cheers!

  3. #3 by ugg bailey button triplet sale on 17 de janeiro de 2017 - 4:06 am

    Were a group of volunteers and opening a new scheme in our community. Your web site provided us with valuable info to work on. You have done an impressive job and our entire community will be grateful to you.
    ugg bailey button triplet sale http://www.znaturaloriginal.com/ugg/

  4. #4 by London doors on 17 de janeiro de 2017 - 4:13 am

    What web host are you using? Can I get your affiliate link to your host?

  5. #5 by посока самолетни билети on 17 de janeiro de 2017 - 4:40 am

    This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.

1 247 248 249
(não será publicado)