java – 用图像掩盖的Libgdx

我有一个帧缓冲区,其中一些形状是使用ShapeRenderer绘制的.现在我想用图像掩码来掩盖这个帧缓冲区.在此之前,我使用ShapeRenderer绘制的简单圆形蒙版进行处理.但我需要使用更复杂的蒙版,所以我必须使用图像.面膜是黑色面具和透明背景的png.这是我的代码:

   @Override
   public void draw(Batch batch, float parentAlpha) {

      //disable RGB color, only enable ALPHA to the frame buffer
      Gdx.gl.glColorMask(false, false, false, true);

      //change the blending function for our alpha map
      batch.setBlendFunction(GL20.GL_ZERO, GL20.GL_SRC_ALPHA);

      //draw alpha mask sprite(s)
      batch.draw(maskTexture, MASK_OFFSET_X + getX(), MASK_OFFSET_Y + getY());   

      //flush the batch to the GPU
      batch.flush();

      Gdx.gl.glColorMask(true, true, true, true);
      batch.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ONE_MINUS_DST_ALPHA);

      //The scissor test is optional, but it depends 
      Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST);
      Gdx.gl.glScissor(MASK_OFFSET_X + (int) getX(), MASK_OFFSET_Y + (int) getY(), maskTexture.getWidth(), maskTexture.getHeight());

      //draw framebuffer to be masked
      batch.draw(frm, getX(), getY(), frmSizeX, frmSizeY);

      //remember to flush before changing GL states again
      batch.flush();

      //disable scissor before continuing
      Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);

      //set default blend function
      batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
   }

我的图像确实被掩盖了,但是掩模图像有一个黑色背景(它应该是透明的).
它现在看起来像这样:

《java – 用图像掩盖的Libgdx》

并且它应该看起来像这样的例子(除了这个例子没有面具所以ofc paint不应该在头外):

《java – 用图像掩盖的Libgdx》

另请注意,涂料是半透明的. (我不知道它是否会改变一些代码).

我正在使用RGBA8888格式,这里是初始化代码:

frmBuff = new FrameBuffer(Format.RGBA8888, frmSizeX, frmSizeY, false);
frm = new TextureRegion(frmBuff.getColorBufferTexture());
frmCam = new OrthographicCamera(frmSizeX, frmSizeY);
frmCam.translate(frmSizeX / 2, frmSizeY / 2);

maskTexture = game.manager.get("my_mask.png", Texture.class);
maskTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

我在搞乱setBlendFunction并取得了截然不同的结果,但其中没有一个是正确的.

我怎样才能解决这个问题?

顺便说一句,我的代码基于这个例子:
https://gist.github.com/mattdesl/6076846

我也读过这个:
https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Masking

最佳答案 我知道这是一个老帖子.但是我对这个问题的所有研究只引出了我的观点.我在
https://gist.github.com/mattdesl/6076846上阅读了这篇文章,并意识到问题在于你只考虑了面具的alpha,但实际上你需要面具和前景产品的alpha(紫色油漆).

//disable RGB color, only enable ALPHA to the frame buffer
Gdx.gl.glColorMask(false, false, false, true);

//change the blending function for our alpha map
batch.setBlendFunction(GL20.GL_ZERO, GL20.GL_SRC_ALPHA);

//draw alpha mask sprite(s)
batch.draw(maskTexture, MASK_OFFSET_X + getX(), MASK_OFFSET_Y + getY());

//change the function for our source
batch.setBlendFunction(GL20.GL_ONE_MINUS_SRC_ALPHA, GL20.GL_SRC_ALPHA);

//draw the source alpha
river_sprite.draw(batch);

//flush the batch to the GPU
batch.flush();
点赞