屏蔽包含alpha的图像会使内部蒙版变黑

时间:2015-11-10 13:49:39

标签: opengl libgdx

我正在尝试屏蔽小于遮罩的背景图像。并且背景和面具之间的空间显示为黑色。

mask and background

enter image description here

这是我正在使用的代码:

     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());       

enter image description here

2 个答案:

答案 0 :(得分:3)

使用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());

答案 1 :(得分:0)

只是看看你的图片,看起来像是:

  • 蒙版的alpha完全控制输出alpha
  • 您还希望将背景图像的alpha值考虑在内

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

我还没有能够研究更多的混合功能及其工作方式,并且可能会更新有关如何实现目标的更多细节,但我可能知道你在尝试什么实现将帮助你在我达到目标之前解决它。

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

相关问题