[Solved] Alpha Blending 3 Textures - C# Directx

Started by
5 comments, last by TheSkyline13 13 years, 6 months ago
I'm trying two blend two textures onto a base texture depending on the base textures alpha. So, Stage 1 would add to the diffuse of Stage 0, using Stage 0's. Stage 2 would then add Stage 0 as well but it would use the inverse of Stage 0's alpha. Essentially Stage 1 would render to the white part of Stage 0's alpha and Stage 2 would render to the black part of Stage 0's alpha.


The code I have here is trying to just get the first part of to work where Stage 1 only blends onto Stage 0 using Stage 0's alpha. It still renders all of Stage 1 though.

Device.SetTexture(0, texture[0]);Device.SetTexture(1, texture[1]);Device.RenderState.SourceBlend = Blend.SourceAlpha;Device.RenderState.DestinationBlend = Blend.InvSourceAlpha;Device.RenderState.AlphaBlendEnable = true;Device.TextureState[0].AlphaOperation = TextureOperation.Disable;//Disable blending for base textureDevice.TextureState[1].ColorOperation = TextureOperation.Add;Device.TextureState[1].AlphaOperation = TextureOperation.BlendCurrentAlpha;Device.TextureState[1].AlphaArgument0 = TextureArgument.Diffuse;


[Edited by - TheSkyline13 on October 16, 2010 10:22:20 PM]
Advertisement
Maybe I can give you some hints, without details and garantee, since my experience with the fixed function pipeline is rather small (I switched to shaders early, they are way more convenient).

- Stage 0: Output the texture to the temp register (to preserve the alpha for later), without any calculation (SelectArg1 if I remember correctly).
- Stage 1: Output the texture to current
- Stage 2: Combine the texture from the previous stage ("current") with the other texture with the alpha you still have in the temp

As stated: I'm not sure at all if this could work. Chances are you have to do a even more complex setup where you e.g. have to additionally use the D3DTA_COMPLEMENT flag.

By the way: I'm not sure you understand the alpha blend render states correctly here (SourceBlend/DestinationBlend etc): They are only applied after the texture stages, i.e. when the color is output to your frame-buffer/render target. They don't influence the texture stages at all.

Other than that: Could you clarify your setup with some images ("alpha" texture, texture A, texture B and your desired output), please ?
Well if it would be easier to do in hlsl, I've tried to grasp the concept of it but still don't know how it all comes together.

Diffuse: http://img99.imageshack.us/i/diffuse.png
Alpha: http://img836.imageshack.us/i/alphagu.png

Pattern 1: http://img219.imageshack.us/i/99312357.png
Pattern 2: http://img835.imageshack.us/i/71798308.png

Product: http://img100.imageshack.us/i/finald.png
Would you actually mind doing it with shaders (HLSL) ? Or are you bound somehow to the fixed function pipeline ? If not, definitively learn shaders, because once you got the hang of it, not only will it be far less tedious (read: easier) but you are no longer limited to the very few formulas the FFP provides.

From a quick glance you are looking for something called texture splatting, though in your case it has two levels.

Haven't got the time right now, sorry, but I think the formulas look more or less like this (using your image naming):

temp = lerp(pattern1, pattern2, alphagu)

and then either (modulate)

finald = temp * diffuse

or even simpler, just taking temp as the alpha for the diffuse.

Note: In HLSL this will almost look that simple, as soon as you have the sources in place (i.e. variables holding the sampled texture values).

I will give this a shot tomorrow, until then I recommend - given you can answer my first question with yes - playing with HLSL, just 2D and something simpler.

[Edited by - unbird on October 15, 2010 8:39:59 AM]
I just made a quick shader and set it up, worked exactly like I wanted. I guess I'll have have to look more into shaders, thanks for the hints.
That was quick, congrats. So you got a solution for your problem already ? If not I promise to write one tomorrow. You deserve it [grin]

Edit: If you got the solution, don't mind to post the source, so others can benefit. And for future reference, you can use ordinary HTML-tags to include screenshots/images, like here (Here I just copy-pasted the source ImageShack provided automatically):



[Edited by - unbird on October 15, 2010 8:57:59 AM]
color.rgb *= lerp(secondary.rgb, primary.rgb, color.a)


Was exactly what you said, really quite easy now that I know what that function does. Thanks again for the help :).

This topic is closed to new replies.

Advertisement