December 2nd, 2006

Alpha-Tested Shadows

One of the greatest drawbacks to stencil shadows is the inability to properly shadow alpha-tested objects; that is to say, an object with a transparent texture. Take, for example, a chain link fence. Many FPS game involve fences of some kind in one area or another, and in most cases these fences are nothing more than a simple quad with an alpha texture applied. It looks great, and it’s cheap, however it falls apart as soon as you attempt to shadow it. Stencils work by extruding the silloughete of a mesh, and since the transparency is a texture effect and not part of the mesh geometry, it isn’t possible to capture that in the stencil buffer.

However, this problem is resolved with shadowmapping. Because an object is draw into the depth buffer pixel by pixel, it is possible to do a lookup on the object’s diffuse texture, and determine whether or not the alpha component is solid or not. If it is, then continue to mark the depth. If not, kill the pixel shader (texkill is wonderful) and do not write out a depth (no shadow). It works great!

void VS(in float3 pos: POSITION, float2 texCoord: TEXCOORD0, out vsOut output) {

     output.pos = mul(float4(pos, 1.0), world);
     output.depth = length(output.pos.xyz - lightPos.xyz);
    output.pos = mul(output.pos, viewProj);

 output.texCoord = texCoord;

}

float4 PS(in float depth: TEXCOORD0, float2 texCoord: TEXCOORD1) : COLOR {
   
 clip(tex2D(DiffuseMapSampler, texCoord).a - 0.5f);

     return float4(depth, 0.0, 0.0, 1.0);
}

 As you can see here, it’s simple enough to implement. There is a rather major drawback though - texkill (clip in HLSL) interferes with a 3d card’s parallalism, and combined with an extra texture read, really chews up performance. However I’m not too worried about this - very few objects in the world will be required to use this special shader vs. the standard depth map shader.

22 Responses

  1. AndyTX Says:

    Just to note you can use alpha testing state directly instead of using a texkill instruction - this will give the hardware a bit better chance at optimizing, although naturally alpha testing can mess up early-z culling. Just output the alpha from your fragment program (in the alpha channel), enable alpha testing, and set the comparison value (also state).

    You mentioned variance shadow maps in a post earlier, so I figured I’d point out that if you are using them, you can actually use alpha *blending* (plus sorting properly of course) instead of alpha testing. This will give you nice and soft blended edges instead of the hard ones introduced by alpha testing.

  2. Аршавин Says:

    Полезно, спасибо

  3. games Says:

    Some believe in gaming as powerful stress busters and some consider
    it a great way to relax during free non-working days. Click Innovative Technique Settings hyperlink for
    the left hand facet. There are many advantages of Xbox 360
    Elite, for example, it has excellent selection of games, and
    each game is in native high-definition as well as user-friendly Dashboard
    interface.

  4. should i move to hawaii Says:

    If some one wishes expert view on the topic of blogging afterward
    i suggest him/her to pay a visit this webpage, Keep up the pleasant work.

  5. move to hawaii oahu Says:

    Hello to every one, the contents existing at this web page are really awesome
    for people experience, well, keep up the good work fellows.

  6. IMVU Credits Hack Says:

    You’re so awesome! I don’t believe I’ve read
    anything like this before. So good to discover someone with some
    unique thoughts on this subject. Really.. thank you for starting
    this up. This web site is something that is needed on the internet, someone with a bit
    of originality!

    My webpage … IMVU Credits Hack

  7. hay day cheat download Says:

    Have you ever considered creating an ebook or guest authoring
    on other sites? I have a blog centered on the same topics you discuss and would love to have
    you share some stories/information. I know
    my visitors would appreciate your work. If you are even remotely interested,
    feel free to send me an email.

    Here is my blog post … hay day cheat download

  8. How to hack Wifi 2014 Says:

    Amazing issues here. I am very glad to see your article.
    Thank you so much and I’m taking a look forward to contact you.

    Will you kindly drop me a e-mail?

    My blog post … How to hack Wifi 2014

  9. Little Tikes Trike Says:

    Hey! This is kind of off topic but I need some advice from an established blog.
    Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick.

    I’m thinking about making my own but I’m not sure where
    to start. Do you have any tips or suggestions? Many thanks

  10. Ali Says:

    Thanks , I’ve just been looking for info approximately this topic for
    a while and yours is the greatest I have discovered till now.
    But, what in regards to the bottom line? Are you sure concerning the source?

  11. hard drives Says:

    I was wondering if you ever considered changing the structure off your website?
    Its very well written; I love what youve got to say.
    But maybe you could a lttle more in tthe way off content so
    people could connect with it better. Youve got an awful loot of text for only having
    1 or 2 images. Maybe you could space it out better?

  12. Sylvester Says:

    Hi there colleagues, its enormous paragraph concerning teachingand completely defined, keep it up all
    the time.

  13. Kandace Says:

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

  14. Rowena Says:

    This piece of writing will help the internet users for building up new website or
    even a weblog from start to end.

  15. exterior home painting Says:

    I’ve been exploring for a little bit for any high quality articles or
    blog posts in this kind of space . Exploring in Yahoo I ultimately stumbled upon this site.
    Studying this info So i’m satisfied to exhibit that I’ve an incredibly good uncanny feeling I came upon just
    what I needed. I so much certainly will make sure to don?t omit this website and give it a look regularly.

  16. Jessica Says:

    You’ll acquire gemstones every time you go up a level
    if you integrate Facebook with the video game.
    Gamers likewise get five gemstones for logging in making use of Hay Day, discovering a motion picture ticket as well as seeing a trailer for another video game.

  17. Flash games Says:

    What’s up, its fastidious post on the topic of media
    print, we all be familiar with media is a fantastic source of information.

  18. KristenCTrembly Says:

    Your style is really unique in comparison to other
    people I have read stuff from. Thank you for posting when you have the
    opportunity, Guess I will just book mark this site.

  19. Jordan Jersey Says:

    Hi there, always i used to check weblog posts here in the early hours in the break of day, since i like to gain knowledge of more and more.

  20. العاب ماريو Says:

    May I simply say what a comfort to discover somebody who really
    understands what they are talking about on the net.

    You definitely know how to bring a problem to light and make it
    important. More and more people need to look at this and understand this side of your story.
    It’s surprising you are not more popular given that you surely have the gift.

  21. xxx.xn Says:

    It’s great that you are getting thoughts from this piece of
    writing as well as from our dialogue made at this time.

  22. 0bat pelangsing, Says:

    Fine way of describing, and pleasant paragraph to get facts regarding my presentation topic, which i am
    going to convey in school.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.