【问题标题】:Masking image that contains alpha makes the inner mask black包含 alpha 的遮罩图像使内部遮罩变黑
【发布时间】:2015-11-10 13:49:38
【问题描述】:

我正在尝试遮盖比遮罩小的背景图像。背景和蒙版之间的空间显示为黑色。

这是我正在使用的代码:

     batch.end();
     batch.begin();     
     Gdx.gl20.glColorMask(false, false, false, true);
     batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
     batch.draw(mask, getX(), getY());
     batch.flush();
     Gdx.gl20.glColorMask(true, true, true, true);      
     batch.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ONE_MINUS_DST_ALPHA);        
     batch.draw(texture, getX(), getY());       
     batch.flush();
     batch.setBlendFunction(GL20.GL_SRC_ALPHA,GL20.GL_ONE_MINUS_SRC_ALPHA);
     batch.end();
     batch.begin();

我尝试了所有类型的功能组合,但没有成功。可能我错过了一些东西。

更新

附上我构建的 src 和 dst 混合函数的所有可能(相关)结果的图表。幸运的是,以下方法都不起作用,而且我猜还需要做更多的事情才能达到结果。

     Gdx.gl20.glColorMask(true, true, true, true);      
     batch.setBlendFunction(src_func, dst_func);        
     batch.draw(texture, getX(), getY());       

【问题讨论】:

  • 这是一个非常详细且内容丰富的问题​​。

标签: opengl libgdx


【解决方案1】:

使用 FrameBuffer 解决了。

    batch.end();
    spriteBatch.begin();
        buffer.begin();
            Gdx.gl20.glColorMask(false, false, false, true);
            spriteBatch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
            spriteBatch.draw(mask, 0,0);
            Gdx.gl20.glColorMask(true, true, true, true);
            spriteBatch.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ZERO);
            spriteBatch.draw(texture, 0,0);
            spriteBatch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        buffer.end();
    spriteBatch.end();      
    batch.begin();
    batch.draw(buffer.getColorBufferTexture(), getX(), getY());

【讨论】:

    【解决方案2】:

    看看你的图片,它看起来像:

    • 掩码的 alpha 完全控制输出 alpha
    • 您希望同时考虑背景图像的 Alpha 值

    换句话说,您只需要两个图像都有 alpha 的 alpha。


    我还没有能够研究更多的混合功能以及它们是如何工作的,并且可能会更新更多关于如何实现你的目标的细节,但也许知道你想要完成什么会在我开始之前帮助你弄清楚。

    How to do blending in LibGDX - 这个问题看起来很有趣,将源图像和目标图像组合在一个网格中以显示效果。

    【讨论】:

    • 您好,谢谢您的回答。我知道我要完成什么我不知道该怎么做。我想我尝试了所有可能的混合功能。所以我想还有更多的事情需要做。在您发布的链接中,即使它得到了回答,也是一种不同的情况。正如你指定的那样。
    猜你喜欢
    • 2010-12-19
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2018-05-29
    • 2016-06-09
    • 2016-01-07
    相关资源
    最近更新 更多