Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

newbie Alpha blend / transparency question.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 jkristia   Members   -  Reputation: 105

Like
0Likes
Like

Posted 14 October 2012 - 02:33 PM

Alpha blend / transparency question.

Hi, I'm completely new to DirectX (using SlimDX 11), and I have spend most evenings this past week simply trying to get something to show.
I'm trying to get alpha blending / transparency working, but I can can't figure out how to do it, and I can't find any exact example of what I'm trying to do.
Essentially I'm trying to be able to draw a box semi transparent. For test I have a blue triangle at z = -0.5, red at z = 0, green z = 0.5. I then rotate the view slightly and would now expect to see 'through' all the triangles, so I can see the green behind the red behind the blue.
In the shader I have just hardcoded the alpha value to 0.5f (this is just for testing)

float4 PShader(VertexOut vin) : SV_Target
{
vin.Color.a = 0.5f;
return vin.Color;
}


I then tried to set the BlendState through code, but kept getting a 'unspeificed error returned', so instead I added it to the technique

technique11 MyTest
{
pass P0
{
SetGeometryShader( 0 );
SetVertexShader( CompileShader( vs_4_0, VShader() ) );
SetPixelShader( CompileShader( ps_4_0, PShader() ) );
SetBlendState( SrcAlphaBlendingAdd, float4( 0.0f, 1.0f, 1.0f, 1.0f ), 0xFFFFFFFF );
}
}


Now, this almost works, I can see the red behind the blue (blue at z=-0.5, read at 0), but the red is completely opaque and I do not see the green.
Looking at some other examples I noticed that Depth is turned off, so I tried that too, but that doesn't work either. With depth turned off, I do see all 3 of them semi transparent, but the green is now always in front.
Any suggestions or links will be appreciated.

alpha issue.png

Jesper

Sponsor:

#2 jkristia   Members   -  Reputation: 105

Like
0Likes
Like

Posted 15 October 2012 - 07:27 PM

I have another question. As a test I changed the drawing order, then with depth enabled I get the expected behavior when viewed from one side, but all opaque when viewed from the other side. With depth disabled I 'kind of' get the correct behavior when viewed from both sides when I set the transparency to something like 10% for all 3 triangles, but any higher than that and it becomes ibvious that the triangle in the back is drawn on top.

So, do I have to recalculate the drawing order manually to achieve the transparent look, or is there some setting I need to enable for this?

Jesper

Edited by jkristia, 15 October 2012 - 07:38 PM.


#3 MJP   Moderators   -  Reputation: 5642

Like
1Likes
Like

Posted 15 October 2012 - 08:51 PM

With standard alpha blending, you need to draw primitives in back-to-front order to get the right result. It's a major limitation of rendering transparent geometry this way, and it's a huge pain in the ass to deal with in a production scenario. Most games just do some sort of coarse per-mesh or per-object sort by depth and hope for the best.

There are specialized rendering techniques that give you order-independent transparency, but they can be somewhat complex and have a performance cost associated with them.

Edited by MJP, 15 October 2012 - 11:21 PM.


#4 jkristia   Members   -  Reputation: 105

Like
0Likes
Like

Posted 15 October 2012 - 09:23 PM

Thank you very much, then I will not spend more time on this for now.

#5 Muzzy A   Members   -  Reputation: 322

Like
0Likes
Like

Posted 16 October 2012 - 12:00 AM

if there's nothing to blend with when you draw it, it wont look transparent. that's why you have to draw farthest transparent objects first, so the next transparent object can blend with it if it needs too and etc.

#6 jkristia   Members   -  Reputation: 105

Like
0Likes
Like

Posted 16 October 2012 - 07:22 AM

if there's nothing to blend with when you draw it, it wont look transparent. that's why you have to draw farthest transparent objects first, so the next transparent object can blend with it if it needs too and etc.

Ahh of course, that makes perfect sense, thank you




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS