Re: Problem with Texture Blending
- From: "Robert Dunlop [MS MVP]" <rdunlop@xxxxxxxx>
- Date: Sun, 4 Sep 2005 21:52:30 -0700
"Marco" <Marco@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:E5C2BFEB-544A-426A-BBFB-689A0C8C7A09@xxxxxxxxxxxxxxxx
> "Robert Dunlop [MS MVP]" wrote:
>>
>> out = current * (1-TFactor) + TFactor * (mask*TFactor +
>> texture*(1-TFactor))
>>
>
> Thank you. Now I can blend two textures with a factor. For example a 50:50
> blending. I think the main problem was the vertex format. I need two Tu/Tv
> pairs.
If you need different coordinates for each texture, then yes, this is the
case. If two or more textures can use the same coordinates, you can share
texture coordinates between them. The texture coordinate set that each
texture stage uses from the vertex format is specified by
TextureCoordinateIndex, which is a zero-based index and by default will be
set to assume separate texture coordinates for each stage, i.e.
TextureStageStates.TextureCoordinateIndex defaults to 0 for stage 0, 1 for
stage 1, etc. You could specify that both texture stages use the first set
of texture coordinates like this:
d3dDev.SetTextureStageState(0, TextureStageStates.TextureCoordinateIndex,
0);
d3dDev.SetTextureStageState(1, TextureStageStates.TextureCoordinateIndex,
0);
> But one thing want not work: I want to blend two textures with a custom
> mask. I have a mask, for example a black & white mask (also a texture) and
> two other textures. At the white pixels of the mask I want to see texture
> one
> and at the black pixels of the mask I want the texture 2. I think this is
> the
> formula:
> out = texture1 * mask + texture2 * (1 - mask)
> I tryed with the Lerp operation but it does not work.
You might be able to use lerp in this manner by using the temporary register
to load the mask color into, if the device supports
MiscCaps.SupportsTexturStageStateArgumentTemp and
TextureOperationCaps.SupportsLerp. You would load the mask into the
temporary texture register in the first stage, like this:
// mask texture
d3dDev.SetTextureStageState(0, TextureStageStates.ResultArgument,
(int)TextureArgument.Temp);
d3dDev.SetTextureStageState(0, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);
// texture 1
d3dDev.SetTextureStageState(1, TextureStageStates.ResultArgument,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(1, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);
// texture 2
d3dDev.SetTextureStageState(2, TextureStageStates.ResultArgument,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorOperation,
(int)TextureOperation.Lerp);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument1,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument2,
(int)TextureArgument.TextureColor);
// this is shown as Arg3 in lerp equation
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument0,
(int)TextureArgument.Temp);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);
Preferably, though, your mask texture should contain an alpha channel that
contains a blending value. If you are loading the mask from a file, you
could create an alpha-only texture file in DDS format using the texture tool
from the SDK, or use a color key value of 0xFF000000 with
TextureLoader.FromFile() to load the mask into a texture format containing
alpha, and set alpha to 0 for black pixels and 255 for non-black pixels.
With a mask texture containing the blending factors in the alpha channel,
you won't require temporary register support, because the blending can be
accomplished using the texture stage alpha channel:
// mask texture
d3dDev.SetTextureStageState(0, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(0, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);
// texture 1
d3dDev.SetTextureStageState(1, TextureStageStates.ColorOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.ColorArgument1,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(1, TextureStageStates.AlphaArgument1,
(int)TextureArgument.Current);
// texture 2
d3dDev.SetTextureStageState(2, TextureStageStates.ColorOperation,
(int)TextureOperation.BlendCurrentAlpha);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument1,
(int)TextureArgument.Current);
d3dDev.SetTextureStageState(2, TextureStageStates.ColorArgument2,
(int)TextureArgument.TextureColor);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaOperation,
(int)TextureOperation.SelectArg1);
d3dDev.SetTextureStageState(2, TextureStageStates.AlphaArgument1,
(int)TextureArgument.TextureColor);
--
Robert Dunlop
The X-Zone
http://www.directxzone.com/
Microsoft DirectX MVP
-------------
The opinions expressed in this message are my own personal views and do not
reflect the official views of the Microsoft Corporation.
The MVP program does not constitute employment or contractual obligation
with Microsoft.
.
- Prev by Date: Re: Problem displaying a second vertex buffer--TY (no text)
- Next by Date: RE: Managed Direct X Tutorial #6
- Previous by thread: Re: Problem displaying a second vertex buffer--TY (no text)
- Next by thread: N Patch - PatchMesh - how to use?
- Index(es):
Relevant Pages
|