Changing alpha value of texture stages

What I want to do is pretty simple but I can’t figure it out.

from panda3d.core import *
import direct.directbase.DirectStart

cube = loader.loadModel('cube.egg')

tex1 = loader.loadTexture('tex1.jpg')
ts1 = TextureStage('ts1')
cube.setTexture(ts1, tex1)
tex2 = loader.loadTexture('tex2.jpg')
ts2 = TextureStage('ts2')
cube.setTexture(ts2, tex2)



How do I change the alpha value of ts2 to anywhere between 0 and 1?

Could you describe what you are trying to accomplish? Do you wish to blend the texture stages together using some constant factor? If so, you may want to look into the CMInterpolate combine mode.

Note that texture stages should always have a unique sort value, so that Panda knows the exact order in which to apply the textures.

I want tex2 to appear ontop of tex1 and tex2’s alpha value to change with a slider. So it would be more of a modulate or add mode I think. And for the colors to blend so if tex2 was blue and tex1 was red then the color would be purple at alpha 0.5

Based on your description, I’m inclined to second rdb’s suggestion of the “interpolate” mode. If I’m not much mistaken, with the “interpolate” mode a value of 0 will show only tex1; a value of 1 will show only tex2; and a value of 0.5 will show a blend of tex1 and tex2. I may be mistaken, but I would imagine that with a value of 0.5, a red tex1, and a blue tex2, the resulting output colour would indeed be purple.

I’m completely fresh to texturing. Can you show me how to use ts.setCombineRgb(TextureStage.CMInterpolate)? I have no idea what to do with the sources and operand and where to change the alpha value

For reference, I’m working from this manual page.

Looking at the manual, it indicates that the “interpolate” combine mode uses its third source to interpolate between its first two sources.

In your case, those first two sources would be your two textures–one coming from the previous texture-stage, and one set on the current texture-stage (i.e. the one that uses the “interpolate” mode).

Since you want to blend the colours of your textures (if I have it correctly), your operands would indicate that the texture-stage should fetch colours.

Now, you want to be able to vary the value used for interpolation (that is, the one given as the third source). Thus you probably don’t want to use a texture here. (I daresay that you could, but that seems like an awkward way to do it, it seems to me.)

Looking at the list of available sources given in the manual, two look promising: “TextureStage.CSConstant”, which uses the value set in “TextureStage.setColor”, and “TextureStage.CSConstantColorScale”, which uses the value set in “NodePath.setColorScale”. Either way, this source provides a constant colour that you can alter in code.

I’ve chosen “CSConstant” below, but either should work–go with whichever you prefer, I think.

Thus, if I’m not much mistaken:

  • Your first two sources would be the previous texture and the current texture (and thus “TextureStage.CSPrevious” and “TextureStage.CSTexture”, respectively)
  • Your operands would all be simply the colour of their respective sources (“TextureStage.COSrcColor”)
  • The final source, which is used to select between the first two sources, would be a constant value, applied via “TextureStage.setColor” (“TextureStage.CSConstant”)
                 TextureStage.CSPrevious, TextureStage.COSrcColor,
                 TextureStage.CSTexture, TextureStage.COSrcColor,
                 TextureStage.CSConstant, TextureStage.COSrcColor)

This doesn’t seem to work, am I doing something wrong? And is it possible to only change the alpha scale?

I think that the problem is that you’re changing the alpha-scale, and I’m not sure that it has an effect in this case.

This blending, if I’m not much mistaken, isn’t a matter of altering the alpha of the topmost texture; it doesn’t use transparency to interpolate between one texture and the other. Instead, it’s using the colour-value of the third source as a guide as to how to blend the colours of the textures in the first two sources.

In short, try putting “0.5” into the red, green, and blue channels of your call to “setColor”, rather than into the alpha channel.

Ohh okay okay I get it, your suggestion works.
Thank you! :smiley:

1 Like

A little curious about how the decal mode works, from it’s description it seems to have the function I’m looking for. Is it the same as CMInterpolate?

Hmm… I’m not familiar with those texture modes, I’m afraid. Looking at the manual, it seems that it uses the alpha value in the decal texture to determine how the two textures blend, rather than a third value; I don’t know offhand whether applying a colour to the TextureStage would have an effect–it might.