Alpha Test + Alpha Blending

Posted By: Uhrwerk

Alpha Test + Alpha Blending - 08/20/07 14:21

Hello Shader Gurus,

I have a question concerning alpha testing and alphablending. The problem is, that when I set a model with an alpha channel in my level I get sorting errors, as expected. When I now use AlphaTestEnable = true; in a fixed function effect the sorting gets corrected, as expected too. Now if I set AlphaBlendEnable = true; nothing happens. The hard edges of the alpha test remain.

In other words: I'd love to have a correctly sorted model (with AlphaTesting or whatever) and at the same time the alphatranspareny look with its soft edges, as if the AlpaDepthTest never happened. Is that possible?
Posted By: mpdeveloper_B

Re: Alpha Test + Alpha Blending - 08/20/07 17:26

use ZWriteEnable = true; the only problem is that if you use alot of items with tgas then you will get sorting errors between them, which i don't think can be fixed...
Posted By: Uhrwerk

Re: Alpha Test + Alpha Blending - 08/20/07 18:51

Without ZWriteEnable it wouldn't work either way, would it?!
Posted By: mpdeveloper_B

Re: Alpha Test + Alpha Blending - 08/21/07 17:37

use Zwriteenable and alphablendenable it should work just fine
Posted By: Uhrwerk

Re: Alpha Test + Alpha Blending - 08/21/07 17:52

I already tried that. It results ins polygons behind the object hidden not being rendered at all, independent rom the alpha channel.
Posted By: Matt_Aufderheide

Re: Alpha Test + Alpha Blending - 08/23/07 22:11

Quote:

In other words: I'd love to have a correctly sorted model (with AlphaTesting or whatever) and at the same time the alphatranspareny look with its soft edges, as if the AlpaDepthTest never happened. Is that possible?




Who wouldn't like this? It's not possible. Otherwise why do you suppose all games dont render this way?

You can in fact achieve something similar using some rendering tricks, such as two-pass rendering, alpha-to-coverage, or edge detection and blurring.
Posted By: Uhrwerk

Re: Alpha Test + Alpha Blending - 08/24/07 01:31

Quote:

two-pass rendering, alpha-to-coverage, or edge detection and blurring.



Could you explain this a little further? How can I achieve those things? In a pixel shader? Thanks for looking into this, Matt, your help is highly appreciated here.
Posted By: mpdeveloper_B

Re: Alpha Test + Alpha Blending - 08/24/07 14:18

what matt also said is that when you use alphablending and fix the zwriting of the model, then it will most definitely cut through all other tga sprites, or models using zwriting and alphablending, so it's not possible to fix this...
Posted By: Matt_Aufderheide

Re: Alpha Test + Alpha Blending - 08/24/07 22:23

Quote:

two-pass rendering, alpha-to-coverage, or edge detection and blurring.




These are three methods I know of to get soft edges and still correct sorting.

2-pass:
render the model once with alphatestenable=true and zwriteenable=false.
render it in a second pass with alphatestenable=false, alphablendenable=true and zwrite false.. also set zfunc=less

Drawback is that you must do two passes, not a problem unless you have a lot of models..

////////////////////////////////////////////////////////////////////////////////
alpha-to-coverage:
hardware method that requires anti-aliasing to be forced on, and hardawre support. you must simply enable the alpha-to-coverage render state, however you may have to do it in a dll usig d3d, very simple todo it though..this line does it:

pointertod3ddevice-> SetRenderState(D3DRS_ADAPTIVETESS_Y, (D3DFORMAT)MAKEFOURCC('A', 'T', 'O', 'C'));

////////////////////////////////////////////////////////////////////////////////

edge detection and blurring:

more complex and sophisticated method, requires knowledge of sahders and render-to-texture.

Grab the backbuffer into a texture that's larger than the screen resolution. Then render a screen-aligned-quad with a special shader. In this shader do a Sobel 6-tap edge detection filter, and also store the average of the rgb 6-tap (which is a blur) .. use the edge results to lerp the blur with the plain sample of the back buffer tex...then you have soft edge globally like super anti-aliasing.

...if you dont understand this last section, just ignore this option
Posted By: Uhrwerk

Re: Alpha Test + Alpha Blending - 08/24/07 23:10

Thank you for your explanations. That makes it all much clearer. As far as I can see the two pass rendering is the most sensefull method here. Writing a .ddl for a single line of code feels somehow ridiculous. Additionally the anti-aliasing could be a framerate killer and users with an older graphics card could get graphic errors?! Last method seems like a real framerate killer.

Thank you very much for helping me out here, Matt, it's a pitty I can rate you only one time 5 stars.

Edit: Works like a charm...
Code:
technique softveg
{
pass one
{
alphaBlendEnable = false;
alphaTestEnable = true;
zWriteEnable = true;
}
pass two
{
alphaBlendEnable = true;
alphaTestEnable = false;
zWriteEnable = false;
zFunc = less;
}
}


Posted By: William

Re: Alpha Test + Alpha Blending - 08/25/07 04:39

Yeah, thanks a bunch for this info Matt, and for the code Uhrwerk. I didn't know this could be done. It works well.
Posted By: frazzle

Re: Alpha Test + Alpha Blending - 08/26/07 13:00

Quote:

Yeah, thanks a bunch for this info Matt, and for the code Uhrwerk. I didn't know this could be done. It works well.




Indeed, Matt is a pro help when it comes to shaders
Btw, thanks for mentioning this very irritating problem Uhrwerk

Cheers

Frazzle
Posted By: Calined

Re: Alpha Test + Alpha Blending - 01/13/08 14:08

thanks alot from me too^^
© 2024 lite-C Forums