Sign in to follow this  
spe

texture blending

Recommended Posts

Could someone please tell me how I can blend for example 3 textures together with each texture using an alpha texture? I tried a lot of ways to do this but I just can't get it working. Thanks, Spe

Share this post


Link to post
Share on other sites
Are you wanting to use the "Fixed Function Pipeline" or can this be done via the "Programmable Pipeline" (a.k.a. Pixel Shaders).

You'll get quite a different answer depending on which it is you want, so best ask first [smile]

hth
Jack

Share this post


Link to post
Share on other sites
How exactly do you want the blending done?

Three textures and two alpha textures
Three textures that contain alphas
Three textures, and an RGB texture to be treated as alphas

What hardware are we targetting? The first method will require a GeForceFX or better to render in 1 pass, and would need 3 passes on something like a RagePro.

Explain what you need in detail or nobody can give you a useful answer.

Share this post


Link to post
Share on other sites
Well, I was thinking of three textures with 3 alpha textures (for every texture there would be an alpha texture). And I want to do it without shaders. Any help is apriciated.

Share this post


Link to post
Share on other sites
Do you want to choose to mix between textures, but the result is solid (like a landscape), or do the 3 alpha textures somehow end up being an alpha used for frame buffer blending too?

If it's for a landscape, and you want 3 alpha textures, how do you want that to work? Will you guarantee the alphas sum to 1, and you want the 3 textures added? If you want to use lerp-style blending, what's the third alpha used for?

ie:
alpha1-3 = 0.33, 0.33, 0.33,
color = tex1*a1 + tex2*a2 + tex3 * a3
this is just asking for invalid alphas to creep in. What occurs if a1+a2+a3 < 1. You get dark areas. Maybe you want this for static shadows, maybe it's a possible bug.

or

alpha1-2 = 0.5, 0.33
color = lerp(tex1, tex2, a1)
color = lerp(color, tex3, a2)
alphas can be any values, and you can't produce invalid results.

Share this post


Link to post
Share on other sites
I want the result to be solid, actualy I want to use this for a landscape. I'd like to use the first method you described where the alphas have to sum up to 1. Or is there a better way to do this (with the fixed pipeline)? Firs I was thinking of vertex alphas, but this solution seemd to be simpler.

Share this post


Link to post
Share on other sites
// GET ALPHA0
0, D3DTSS_COLOROP, D3DTOP_SELECTARG1
0, D3DTSS_COLORARG1, D3DTA_DIFFUSE
0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE

// GET TEXTURE * ALPHA
1, D3DTSS_COLOROP, D3DTOP_MODULATE
1, D3DTSS_COLORARG1, D3DTA_TEXTURE
1, D3DTSS_COLORARG2, D3DTA_CURRENT | D3DTA_ALPHAREPLICATE
1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
1, D3DTSS_ALPHAARG1, D3DTA_CURRENT

// GET ALPHA1
2, D3DTSS_COLOROP, D3DTOP_SELECTARG1
2, D3DTSS_COLORARG1, D3DTA_CURRENT
2, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
2, D3DTSS_ALPHAARG1, D3DTA_TEXTURE

// COLOR += TEXTURE * ALPHA
3, D3DTSS_COLOROP, D3DTOP_MULTIPLYADD
3, D3DTSS_COLORARG1, D3DTA_TEXTURE
3, D3DTSS_COLORARG2, D3DTA_CURRENT | D3DTA_ALPHAREPLICATE
3, D3DTSS_COLORARG0, D3DTA_CURRENT
3, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
3, D3DTSS_ALPHAARG1, D3DTA_CURRENT

REPEAT STAGES 2 AND 3 AGAIN FOR THIRD SET OF TEXTURES. You probably want stage 6 as modulate with diffuse, and 7 as disable.

0, D3DTSS_TEXCOORDINDEX, 0
1, D3DTSS_TEXCOORDINDEX, 0
2, D3DTSS_TEXCOORDINDEX, 0
Repeat for all. Use different UV channels if your vertices have them.


As a shader, ps.1.4 (I think 1.4 support 6 textures) or 2.0

float4 shader(vertexoutput in) : COLOR
{
float4 color;
color.a = tex2D(alpha0sampler, in.texcoord0).a;
color.rgb = tex2D(color0sampler, in.texcoord0).rgb * color.a;
color.a = tex2D(alpha1sampler, in.texcoord0).a;
color.rgb += tex2D(color1sampler, in.texcoord0).rgb * color.a;
color.a = tex2D(alpha2sampler, in.texcoord0).a;
color.rgb += tex2D(color2sampler, in.texcoord0).rgb * color.a;
return color;
}

as a shader with single alpha texture (ps.2.0) and adding.

float4 shader(vertexoutput in) : COLOR
{
float4 color;
float4 alphas;
alphas = tex2D(alphasampler, in.texcoord0);
color.rgb = tex2D(color0sampler, in.texcoord0).rgb * alphas.r;
color.rgb += tex2D(color1sampler, in.texcoord0).rgb * alphas.g;
color.rgb += tex2D(color2sampler, in.texcoord0).rgb * alphas.b;
return color;
}

as a shader with single alpha texture, and lerping instead. By lerping we only need 2 alpha values. Luckily in ps.1.1 we can use .a or .b.

float4 shader(vertexoutput in) : COLOR
{
float4 color;
float4 alphas;
alphas = tex2D(alphasampler, in.texcoord0);
color.rgb = lerp(tex2D(color0sampler, in.texcoord0).rgb,
tex2D(color1sampler, in.texcoord0).rgb, alphas.a);
color.rgb = lerp(color.rgb, tex2D(color2sampler, in.texcoord0).rgb, alphas.b);
return color;
}

For a 4th texture you can use .r, or .g in ps.2.0, or use a dp3 with an .rgba write mask with either (1,0,0,0) or (0,1,0,0) to get red or green into the alpha channel.

float4 shader(vertexoutput in) : COLOR
{
float4 color;
float4 alphas;
alphas = tex2D(alphasampler, in.texcoord0);
color.rgb = lerp(tex2D(color0sampler, in.texcoord0).rgb,
tex2D(color1sampler, in.texcoord0).rgb, alphas.a);
color.rgb = lerp(color.rgb, tex2D(color2sampler, in.texcoord0).rgb, alphas.b);
float4 temp = dot(alphas.rgb, float3(1,0,0));
color.rgb = lerp(color.rgb, tex2D(color3sampler, in.texcoord0).rgb, temp.a);
return color;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this