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 star wars characters on 30 de abril de 2017 - 6:55 pm

    Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet explorer. I’m not sure if this is a format issue or something to do with internet browser compatibility but I figured I’d post to let you know. The style and design look great though! Hope you get the issue resolved soon. Many thanks

  2. #2 by Tabung Pemadam api on 30 de abril de 2017 - 7:33 pm

    As soon as I found this website I went on reddit to share some of the love with them.

  3. #3 by gsa ser lists on 30 de abril de 2017 - 7:39 pm

    Greetings! Very helpful advice in this particular article!
    It’s the little changes that make the greatest changes.
    Thanks for sharing!

  4. #4 by buy louis vuitton online store on 30 de abril de 2017 - 8:12 pm

    glory glory to all the football fans its the best sport in the world!! glory glory gloryyy gloryy !
    buy louis vuitton online store http://www.handbagsoutlet.online

  5. #5 by mulberry handbags sale on 30 de abril de 2017 - 8:27 pm

    Hi, I just read texts on your blog and I became interested in the topic. I like your article and I am thinking whether I could use your words in my work? Would it be probable? If yes, please contact with me. Thank you.
    mulberry handbags sale http://www.cybermondaysale.online

  6. #6 by keen factory store on 30 de abril de 2017 - 9:57 pm

    Valuable info. Lucky me I found your website by accident, and Im shocked why this accident didnt happened earlier! I bookmarked it.
    keen factory store http://www.keenstore.online

1 373 374 375
(não será publicado)