I am making a game, in which I want to cover the background from the player and show just few fragments of the background - eg. area around the player and one or two brightened areas. So the masking process of covering texture has to be made at runtime (not once prepared/masked on game start or just used as a premade png files).
Game main class:
protected Pixmap circleShadowBigPixmap;
protected Pixmap pixmapBlackCover;
protected Texture pixmapTexture;
Update class:
arrowGame.lightX++;
arrowGame.pixmapBlackCover.setBlending(Pixmap.Blending.None); // before you start drawing pixels.
arrowGame.pixmapBlackCover.drawPixmap(arrowGame.circleShadowBigPixmap, arrowGame.lightX, 500);
arrowGame.pixmapBlackCover.setBlending(Pixmap.Blending.SourceOver);
arrowGame.pixmapTexture.draw(arrowGame.pixmapBlackCover, 0, 0);
Drawing class:
sb.draw(arrowGame.pixmapTexture, x, y); // sb - spritebatch
Preparing graphics class:
arrowGame.circleShadowBig = new Texture("shadowCircleBig.png");
arrowGame.circleShadowBig.getTextureData().prepare();
Screenshots: 1- unwanted mask fragment (this fragment shoud do nothing to the source pixmap, should be neither black nor transparent) 2- transparent area 3- black area (hidden)
Finally... Used shaders
Game main class:
ShaderProgram.pedantic = false;
mask = new Texture(Gdx.files.internal("bigSun1024violet.png"));
viewMask = new Texture(Gdx.files.internal("bigSun256violet.png"));
maskBuff = new FrameBuffer(Pixmap.Format.RGBA8888, frmSizeX, frmSizeY, false);
msk = new TextureRegion(maskBuff.getColorBufferTexture());
frmBuff = new FrameBuffer(Pixmap.Format.RGBA8888, frmSizeX, frmSizeY, false);
frm = new TextureRegion(frmBuff.getColorBufferTexture());
coverSprite = new Sprite(new Texture("violetCover.png"));
float donutX = 0;
float donutY = 0;
coverSprite.setPosition(donutX, donutY);
cam.update();
// shader = new ShaderProgram(VERT, FRAG);
shader = new ShaderProgram(Gdx.files.internal("vertex.glsl"), Gdx.files.internal("fragment-video.glsl"));
if (!shader.isCompiled()) {
System.err.println(shader.getLog());
System.exit(0);
}
if (!shader.getLog().isEmpty())
System.out.println(shader.getLog());
shader.bind();
shader.setUniformi("u_texture1", 1);
shader.setUniformi("u_mask", 2);
defaultShader = spriteBatch.getShader();
//bind mask to glActiveTexture(GL_TEXTURE2)
msk.getTexture().bind(2);
//bind dirt to glActiveTexture(GL_TEXTURE1)
// tex1.bind(1);
frm.getTexture().bind(1);
//now we need to reset glActiveTexture to zero!!!! since sprite batch does not do this for us
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0);
Update class:
arrowGame.lightX--;
arrowGame.maskBuff.begin();
Gdx.gl.glClearColor(255, 255, 255, 255);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
arrowGame.spriteBatch.begin();
arrowGame.spriteBatch.draw(arrowGame.mask, arrowGame.lightX, 0, 0, 0, arrowGame.frmSizeX, arrowGame.frmSizeY, 1, 1, 0, 0, 0, arrowGame.frmSizeX, arrowGame.frmSizeY, false, true);
arrowGame.spriteBatch.draw(arrowGame.viewMask, 500, 500, 0, 0, 256, 256, 1, 1, 0, 0, 0, 256, 256, false, true);
arrowGame.spriteBatch.end();
arrowGame.maskBuff.end();
Drawing class:
sb.setShader(arrowGame.shader);
arrowGame.coverSprite.draw(sb);
sb.setShader(arrowGame.defaultShader);