Reputation:
I know how to cut Rectangles out of Images. But I would like to cut out a circle or a self-defined mask.
In some Games (Darkness Reborn) from Google Play there is this logo. How is this done?
class MyActor extends Actor {
Rectangle scissors = new Rectangle();
Rectangle clipBounds = new Rectangle(0,0,100,100);
Sprite sprite = new Sprite(new Texture(Gdx.files.internal("images/test2.png")));
public MyActor() {
sprite.getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
sprite.setScale(2f);
setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
setTouchable(Touchable.enabled);
}
@Override
public void draw(Batch batch, float parentAlpha) {
ScissorStack.calculateScissors(camera, batch.getTransformMatrix(), clipBounds, scissors);
ScissorStack.pushScissors(scissors);
sprite.draw(batch, parentAlpha);
}
}
Upvotes: 1
Views: 1057
Reputation: 25177
There is no direct way of doing non-rectangular masks in Libgdx (or OpenGL). There are a lot of alternative techniques, though.
Depending on your requirements, just doing the masking in software (generating a new Pixmap with the appropriate pixels set) might be sufficient.
Another approach is to use the Depth Buffer to mask texture elements. Clear the depth buffer to 1.0, draw your shapes in the buffer with 0.0, then render the texture with a depth test, so only the pixels corresponding to depth-buffer elements of 0.0 are rendered.
See mattdesl's LibGDX Masking wiki for more details, other options, and examples: https://github.com/mattdesl/lwjgl-basics/wiki/LibGDX-Masking#complex-masks
Upvotes: 1