效果图
实现
@Overridepublic void update(float dt) {
batch.begin();
drawBackground(batch);
//画遮罩
drawAlphaMask(batch);
//画前景色
drawForeground(batch, 0, 0, mask.getWidth(), mask.getHeight());
batch.end();
}
画遮罩
private void drawAlphaMask(SpriteBatch batch) {
Gdx.gl.glColorMask(false, false, false, true);
//change the blending function for our alpha map
batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
//draw alpha mask sprite(s)
batch.draw(mask, 0, 0, mask.getWidth(), mask.getHeight());
//flush the batch to the GPU batch.flush();
}
画需要裁剪的sprite
private void drawForeground(SpriteBatch batch, int clipX, int clipY, int clipWidth, int clipHeight) {
Gdx.gl.glColorMask(true, true, true, true);
batch.setBlendFunction(GL20.GL_DST_ALPHA, GL20.GL_ONE_MINUS_DST_ALPHA);
Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST);
Gdx.gl.glScissor(clipX, clipY, clipWidth , clipHeight);
batch.draw(img, 240 - img.getWidth() / 2f, 160 - img.getHeight() / 2f);
batch.flush();
Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
}
代码
https://github.com/tianqiujie/gdxplayground/tree/master/core/src/org/flixel/screens/MaskTest.java