[2.1] Unlit alpha blending not working

Problems building or running the engine, queries about how to use features etc.
Post Reply
Boost113
Kobold
Posts: 35
Joined: Sun Sep 08, 2013 6:39 pm
x 6

[2.1] Unlit alpha blending not working

Post by Boost113 »

Ogre Version: 2.1 (a6204308c4fa)
Operating System: Linux
Render System: OpenGL3+

I'm trying to do 2D rendering with the Unlit HLMS and an orthographic camera. I got it mostly working, but for some reason the alpha blending is not working between two meshes. I have a blendblock with "blendblock.setBlendType(Ogre::SBT_TRANSPARENT_ALPHA);" set on both the mesh with the wood texture and the cursor mesh, and I'm sure the cursor texture does include an alpha channel, but it looks like this:
Image

But the cursor mesh correctly blends with image content drawn in an earlier compositor workspace (the text with blue background in the top left):
Image

This is basically my code (the entire code is on github: https://github.com/hhyyrylainen/Leviath ... et.cpp#L31):

Code: Select all

    Ogre::HlmsManager* hlmsManager = Ogre::Root::getSingleton().getHlmsManager();
    Ogre::HlmsTextureManager* hlmsTextureManager = hlmsManager->getTextureManager();

    Ogre::HlmsUnlit* hlmsUnlit =
        static_cast<Ogre::HlmsUnlit*>(hlmsManager->getHlms(Ogre::HLMS_UNLIT));

    const auto datablockName = GetNameForDatablock();

    Ogre::HlmsBlendblock blendblock;
    blendblock.setBlendType(Ogre::SBT_TRANSPARENT_ALPHA);
    // blendblock.mSourceBlendFactor = Ogre::SBF_ONE;
    // blendblock.mDestBlendFactor = Ogre::SBF_ONE_MINUS_SOURCE_ALPHA;
    // blendblock.mSourceBlendFactor = Ogre::SBF_SOURCE_ALPHA;
    // blendblock.mDestBlendFactor = Ogre::SBF_ONE_MINUS_SOURCE_ALPHA;

    Ogre::HlmsUnlitDatablock* datablock =
        static_cast<Ogre::HlmsUnlitDatablock*>(hlmsUnlit->createDatablock(datablockName,
            datablockName, Ogre::HlmsMacroblock(), blendblock, Ogre::HlmsParamVec()));

    Ogre::HlmsTextureManager::TextureLocation texLocation =
        hlmsTextureManager->createOrRetrieveTexture(
            ImageName, Ogre::HlmsTextureManager::TEXTURE_TYPE_DIFFUSE);

    datablock->setTexture(0, texLocation.xIdx, texLocation.texture);

    Width = texLocation.texture->getWidth();
    Height = texLocation.texture->getHeight();

    QuadMesh = GeometryHelpers::CreateWidgetGeometry(
        "image_widget_" + std::to_string(ID) + "_mesh", Width, Height);

    QuadItem = ContainedIn->GetScene()->createItem(QuadMesh, Ogre::SCENE_DYNAMIC);
    QuadItem->setDatablock(datablock);

    Node = ContainedIn->GetParentForWidgets()->createChildSceneNode(Ogre::SCENE_DYNAMIC);

    Node->attachObject(QuadItem);
I see no errors in my ogre.log. I also tried a bunch of different blendblock configurations (commented out in the code) but they didn't help either.

Does anyone know what I'm doing wrong?
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: [2.1] Unlit alpha blending not working

Post by dark_sylinc »

For alpha blending to work correctly, things need to be drawn in order (back to front).

We try to sort it out for you but our algorithm works for 3D objects (and even for 3D objects, it's far from perfect).

Use setRenderQueueGroup and/or setRenderQueueSubGroup to have tight control control in which order things are rendered.

Alternatively you could try additive blending or modulative blending (Ogre::SBT_ADD and SBT_MODULATE respectively) which are order independent (because a + b = b + a; and a * b = b * a respectively)

You can also check on RenderDoc to see if indeed the texture's alpha is alright
Boost113
Kobold
Posts: 35
Joined: Sun Sep 08, 2013 6:39 pm
x 6

Re: [2.1] Unlit alpha blending not working

Post by Boost113 »

Thinking about this more I realized that the problem might have been with depth checking. So I disabled that and now the second drawn thing will overwrite the other one. So the pixels that were left black, that I thought weren't blending, was maybe caused by the other mesh not being drawn due to depth checking.

I'm trying to use Z position on the meshes to control the order they are drawn in but that doesn't work. Is there some way to do that without using the groups, which I want to avoid as I don't want to limit myself to 255 max nested objects (I'm building a GUI system)? It also seems that Ogre::Item has no method to set a sub group which would have given more values to work with.
Using RTT I can probably live with only 255 levels of different "Z" values, but I'd like to know if there is some other way to get the object render order fixed.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: [2.1] Unlit alpha blending not working

Post by dark_sylinc »

Ahhhh I see
Using RTT I can probably live with only 255 levels of different "Z" values, but I'd like to know if there is some other way to get the object render order fixed.
Actually if you combine renderqueue groups and subgroups you can get an effective 1024 levels of "Z" values.

However you mentioned something that I forgot:
I'm trying to use Z position on the meshes to control the order they are drawn in but that doesn't work
D'oh! You're right.
We perform rough Z sorting which uses the last 15 bits IIRC.

Try spacing objects apart by large Z values, i.e. pos.z = layer * constant; where constant is a large value like 512.0f. If it still doesn't work, try larger values like 1024.0f, 2048.0 and so on until it works as expected. If it works with 512, try smaller values until it stops working to get the best precision.

Cheers
Matias
Boost113
Kobold
Posts: 35
Joined: Sun Sep 08, 2013 6:39 pm
x 6

Re: [2.1] Unlit alpha blending not working

Post by Boost113 »

dark_sylinc wrote: Fri Apr 26, 2019 7:22 pm Ahhhh I see
Using RTT I can probably live with only 255 levels of different "Z" values, but I'd like to know if there is some other way to get the object render order fixed.
Actually if you combine renderqueue groups and subgroups you can get an effective 1024 levels of "Z" values.
I can use renderqueue groups but the subgroups don't work:

Code: Select all

/home/hhyyrylainen/Projects/Leviathan/Engine/GUI/Widgets/ImageWidget.cpp:75:15: virhe: ”class Ogre::Item” has no member named ”setRenderQueueSubGroup”; did you mean ”setRenderQueueGroup”?
     QuadItem->setRenderQueueSubGroup(5);
               ^~~~~~~~~~~~~~~~~~~~~~
               setRenderQueueGroup
dark_sylinc wrote: Fri Apr 26, 2019 7:22 pm Try spacing objects apart by large Z values, i.e. pos.z = layer * constant; where constant is a large value like 512.0f. If it still doesn't work, try larger values like 1024.0f, 2048.0 and so on until it works as expected. If it works with 512, try smaller values until it stops working to get the best precision.
I found that using 4096 seems to work fine, but only for small values. When I try to use 3 * 4096 as the Z position the object stops rendering. I've positioned my camera at 2049 * 4096 so it should be visible. I have near plane set to 1 and far plane set to 0 (infinite). Weirdly it seems that in the negative direction values of around -30 * 4096 work but they break sometime before -60 * 4096. So with the Z positions it seems I'm getting even less different levels than the 255 levels of render queue groups... Very strange. Renderdoc shows that when I go too far in the positive or negative direction the meshes are not rendered at all so culling done by Ogre must be the reason why I can't get a good number of levels. I tried setting the mesh extents to infinite but that didn't change anything.
Boost113
Kobold
Posts: 35
Joined: Sun Sep 08, 2013 6:39 pm
x 6

Re: [2.1] Unlit alpha blending not working

Post by Boost113 »

I decided to go with just the basic render queue groups. I probably need to use RTT anyway as I haven't found any easy way to do clipping in 2.1, so I'll need to do it anyway to be able to clip contents.
Post Reply