Skip to content

Masking

Masking is a technique used in graphics programming to control which parts of the screen are visible or hidden. Think of it as placing a stencil over your screen: only the parts that pass through the stencil will be drawn, while the rest will be ignored. Masks are extremely useful for creating effects like:

  • Cutting out parts of textures or shapes
  • Progress bars or health bars

The image below shows you conceptually how masks can be used to hide parts of a texture or shape.

Masking

Figure 1. Example of masking

In our game framework, masks need to be based on shapes (any of the supported shape types).

The color of what you draw matters conceptually:

  • White (or any drawn pixel): marks the area as visible.
  • Black (or absence of drawing): marks the area as hidden.

This means you can create a mask either by drawing the visible areas in white, or by drawing the hidden areas in black and then inverting the mask.

Masks are applied after they are defined. Once a mask is active, all rendering (shapes or textures) will be limited to the masked area. You can later clear the mask to draw normally again.

Important: Masks use OpenGL stencil buffers. While a mask is active, you can only draw in black or white to define the mask. This can be done by using the shape renderer. The mask will not be visible when you draw it.

Creating a mask

The GameApp.startMask() method can be used to start the masking mode. All subsequent shapes and textures will define the masking area.

Rules:

  • Must not be called while shape rendering is active.
  • Once active, all drawing defines the mask instead of being displayed.

Inverting masks

GameApp.invertMask() inverts the current mask. Areas that were previously visible will become hidden, and areas that were hidden will become visible.

End mask drawing

GameApp.endMask() ends the mask definition. Subsequent rendering will be masked according to the defined mask.

Example: Using a Mask

Below is an example of how to define a circular mask in the center of the screen and draw shapes and textures through it:

@Override
public void render(float delta) {
    super.render(delta);

    // Clear the screen
    GameApp.clearScreen();

    // Define a mask
    GameApp.startMask();

    // We are going to use the shape render to draw a circle mask
    GameApp.startShapeRenderingFilled();
    GameApp.drawCircle(GameApp.getWorldWidth()/2f, GameApp.getWorldHeight()/2f, 100, "white");
    GameApp.endShapeRendering();
    GameApp.endMask();

    // The mask is finished now
    // If we now start drawing (or rendering textures), the result will be masked
    GameApp.startShapeRenderingFilled();
    GameApp.drawRect(50, 50, 300, 200, "red");
    GameApp.drawCircle(200, 200, 100, "blue");
    GameApp.endShapeRendering();

    // Draw a texture that will also be masked
    GameApp.startSpriteRendering();
    GameApp.drawTexture(myTexture, 100, 100);
    GameApp.endSpriteRendering();

    // Clear the current mask, so that all the drawing that happens after is not masked anymore.
    GameApp.clearMask();

    // All the remaining drawing here is going to be unmasked!
    ...
}

Notes

  • Masks control visibility of all subsequent drawing until cleared.
  • Masks can be any shape (rectangle, circle, polygon, etc.).
  • You can invert masks for special effects.
  • Always end the mask with endMask() before doing normal rendering.
  • Only black/white drawing is allowed while defining the mask.
  • Don't forget to clear the mask at the end.