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

return tex2D(DiffuseSampler, input.texturecoord);

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

, , , , , ,

  1. #1 by womens ugg boots on sale on 10 de dezembro de 2016 - 3:22 am

    I think you have remarked some very interesting details , appreciate it for the post.
    womens ugg boots on sale

  2. #2 by balmain jacket sale on 10 de dezembro de 2016 - 3:45 am

    that you have been making here People are bound to find this really important. This is my very first comment, I think I like this!. in the future
    balmain jacket sale

  3. #3 by louis vuitton canada on 10 de dezembro de 2016 - 4:08 am

    My bro bookmarked this webpage for me and I have been reading through it for the past couple hrs. This is really going to benefit me and my friends for our class project. By the way, I enjoy the way you write.
    louis vuitton canada

  4. #4 by insurance quote on 10 de dezembro de 2016 - 4:23 am

    Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I ave been trying for a while but I never seem to get there! Many thanks

  5. #5 by louis vuitton outlet store new york on 10 de dezembro de 2016 - 4:26 am

    Thank you for another wonderful article. Where else could anyone get that type of info in such an ideal way of writing? I have a presentation next week, and I am on the look for such info.
    louis vuitton outlet store new york

  6. #6 by louis vuitton outlet usa on 10 de dezembro de 2016 - 4:31 am

    I was looking for essential information on this subject. The information was necessary as Im about to launch my very own portal. Thanks for providing a lacking link in my business. Anyway, in my language, there usually are not much good source like this.
    louis vuitton outlet usa

  7. #7 by the glades condo on 10 de dezembro de 2016 - 4:46 am

    Thanks-a-mundo for the post.Really looking forward to read more. Much obliged.

  8. #8 by vendita online moncler on 10 de dezembro de 2016 - 5:20 am

    Weight loss isnt always easy and it helps to learn what weight loss is all about.CLICK HERE
    vendita online moncler

  9. #9 by coach outlet online store on 10 de dezembro de 2016 - 5:38 am

    Im happy I found this weblog, I couldnt learn any info on this topic matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, im always appear for people to test out my site. Please stop by and leave a comment sometime!
    coach outlet online store

  10. #10 by adidas nmd on 10 de dezembro de 2016 - 5:51 am

    Thanks for your own labor on this blog. Kate delights in going through research and it’s really easy to understand why. We all learn all about the compelling method you make both useful and interesting tips and hints by means of the web blog and as well as foster participation from visitors about this content so our princess is truly discovering a lot of things. Have fun with the rest of the year. You are always carrying out a really good job.

  11. #11 by michael kors shop online on 10 de dezembro de 2016 - 6:10 am

    I Will have to visit again whenever my course load lets up however I am taking your Rss feed so i can read your web blog offline. Thanks.
    michael kors shop online

  12. #12 by coach us on 10 de dezembro de 2016 - 6:35 am

    Seriously thats precisely the advice that I have been hunting to get! You actually reached appreciate Google just for delivering us straight to your site!
    coach us

  13. #13 by ugg boots sale on 10 de dezembro de 2016 - 6:52 am

    Perfect piece of work you have done, this web site is really cool with good info .
    ugg boots sale

  14. #14 by north face outlet vacaville on 10 de dezembro de 2016 - 8:07 am

    this is really nice!
    north face outlet vacaville

  15. #15 by coach outlet online coupon on 10 de dezembro de 2016 - 8:14 am

    It is really amazing to see there is a lot of bullcrap commenting going on on the web these days. You have to ask yourself what really is the goal of thse robots who are doing this. It can be quite a mystery.
    coach outlet online coupon

1 195 196 197
(não será publicado)