[solved] Blend textures and alpha mask in material script?

Problems building or running the engine, queries about how to use features etc.
noobOmat
Halfling
Posts: 57
Joined: Thu May 07, 2009 12:23 pm
Location: Berlin, Germany

[solved] Blend textures and alpha mask in material script?

Post by noobOmat »

Hi,

I try to blend two textures with alpha channels in a material script and apply a kind of 'peekhole' via an alpha mask.

For example, take the following textures:

box1.png
Image

box2.png
Image

I need to combine those texture, so they look like this:
Image

After that is done, I want to apply an alpha mask to the material:
mask.png
Image

So that the final texture looks like this:
Image

Please note, that all textures are the same size (320x180).

I tried to do this with a material skript, but I was unable to achieve my goal. I didn't even manage to combine the textures box1 and box2 properly (which should be fairly easy), not to mention to apply the alpha mask. :(

Any assistance would be appreciated, as from this point on it will be trial and error for me, and with the extensive options the matarial scripts offer, that could take a while and would be very unsatisfying for me.

So if anyone could explain how to do this properly and/or provide a material script example, that would be great. :)

Edit:
With this material script I managed to display the combined textures box1.png and box2.png, but it needs two passes and the alpha mask is still missing. I'm sure it can be done in a single pass, using multi-textures, but I don't know how.

Code: Select all

material /test
{
    technique
    {
        pass
        {
            scene_blend alpha_blend
            
            texture_unit
            {
              texture box1.png
            }
        }
        pass
        {
            scene_blend alpha_blend
            
            texture_unit
            {
              texture box2.png
            }
        }
    }
}
Last edited by noobOmat on Mon May 27, 2013 10:39 am, edited 1 time in total.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: How to blend textures and alpha mask in material script?

Post by Kojack »

Off the top of my head it would be something like this (untested):

Code: Select all

material /test
{
    technique
    {
        pass
        {
            scene_blend alpha_blend
            
            texture_unit
            {
              texture box1.png
            }
            texture_unit
            {
              texture box2.png
              colour_op alpha_blend
            }
            texture_unit
            {
              texture mask.png
              colour_op_ex source1 src_current src_current
              alpha_op_ex modulate src_current src_texture
            }
        }
    }
}
This is what each texture unit is doing:
1 - gets the box1 texture
2 - alpha blends box2 texture with it. This is done within the texture units, it hasn't reached the screen yet.
3 - get the alpha channel of the mask and multiply it by the existing alpha channel from the previous two steps, but tells the colour channels to just copy the result of texture unit 2 (src_current, which is the result of the previous unit). So any colour info in the mask texture is ignored, the boxes aren't overwritten.

Then the final result (colour boxes with circular alpha mask combined with their own masks) is alpha blended onto the screen using the scene blend pass.

The important thing is that the mask.png has an alpha channel for the circular mask. You can't easily use just a black and white image to control alpha in ogre unless you write shaders, the fixed function pipeline doesn't like colour channels and alpha channels talking to each other.
noobOmat
Halfling
Posts: 57
Joined: Thu May 07, 2009 12:23 pm
Location: Berlin, Germany

Re: How to blend textures and alpha mask in material script?

Post by noobOmat »

Hi,

thank you for your explanation and the provided material script. Sadly the script is not working. :(

I fiddled around a little bit and it seems to me, that colour_op alpha_blend is not doing, what I would think it should do.

If I combine the two textures box1.png and box2.png the result shows only the overlapping part of both boxes. The third texture (the alpha mask) seems to have no effect at all. Even if I remove the alpha mask from the material, the result is still the same, as shown in the picture.

The grey background indicates, that the scene_blending of the pass works fine though.

Image

Code: Select all

material /test
{
    technique
    {
        pass
        {
            scene_blend alpha_blend
            
            texture_unit
            {
                texture box1.png
            }
            texture_unit
            {
                texture box2.png
                colour_op alpha_blend
            }
            texture_unit
            {
                texture mask.png
                colour_op_ex source1 src_current src_current
                alpha_op_ex modulate src_current src_texture
            }   
        }
    }
}
When I display the textures independently they are all displayed correctly, so I'm pretty sure they are all loaded properly.

I will have another look in the manual, but any help would still be appreciated. :)

BTW: I'm using Ogre 1.8.1, currently working with an Intel HD3000, but I can test again this evening with a Nvidia GTX260.
Last edited by noobOmat on Tue May 21, 2013 5:17 pm, edited 1 time in total.
noobOmat
Halfling
Posts: 57
Joined: Thu May 07, 2009 12:23 pm
Location: Berlin, Germany

Re: How to blend textures and alpha mask in material script?

Post by noobOmat »

I used some trial and error, starting from your material script, and came up with this
Image

using this material script

Code: Select all

material /test
{
    technique
    {
        pass
        {
            scene_blend alpha_blend
            
            texture_unit
            {
                texture box1.png
            }
            texture_unit
            {
                texture box2.png  
                colour_op_ex add src_current src_texture
                alpha_op_ex blend_texture_alpha src_texture src_current
            }
            texture_unit
            {
                texture alpha_mask.png  
                colour_op_ex source1 src_current src_current
                alpha_op_ex modulate src_current src_texture 
            }
            
        }
    }
}
and using, like you suggested, this alpha mask.

alpha_mask.png
Image

This looks pretty close to what I want to achieve, but the colour_op_ex add src_current src_texture statement adds the colors, where the boxes overlap. This comes as no big surprise, but all other modes I tested, were resulting in either both boxes being black or just one box colored and the other one black.

I just don't get it. It seems to me this shouldn't be that complicated and I'm totally missing something here. :(
noobOmat
Halfling
Posts: 57
Joined: Thu May 07, 2009 12:23 pm
Location: Berlin, Germany

Re: How to blend textures and alpha mask in material script?

Post by noobOmat »

noobOmat wrote:I just don't get it. It seems to me this shouldn't be that complicated and I'm totally missing something here. :(
I'm sorry that I have to bump this issue, but the problem still remains.

I tried several other combinations of color_op_ex and alpha_op_ex, but none of them got me the desired result. The alpha masking works prefectly now, but the two 'base' textures box1.png and box2.png are not blended to my satisfaction (see previous post).

It seems to me that the requested operation should be fairly simple and straight forward, but I can't get a grip on it. I'm sure there are a lot of people out there, who do the same thing, and I would appreciate any help I can get. :)
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Re: How to blend textures and alpha mask in material script?

Post by Kojack »

I tried for a while, but I couldn't get the alpha channels to work the exact way that was needed. I think there's just too much trying to be done there at once, the colour/alpha pipelines are very linear and simplistic. Using a shader would be much easier.
noobOmat
Halfling
Posts: 57
Joined: Thu May 07, 2009 12:23 pm
Location: Berlin, Germany

Re: How to blend textures and alpha mask in material script?

Post by noobOmat »

Hi Kojack,

I will use a shader then, although I have to admit I'm a little bit surprised, that this is not possible via the fixed function pipeline.

Nevertheless, thank you very much for your effort and time you spent.