Libgdx ScissorStack is not working as expected

I am trying to create a "progress bar" but clipping does not work as I expect. Am I doing something wrong or something I have misinterpreted?

The subroutine draw()

to anchor:

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        Rectangle scissors = new Rectangle();
        Rectangle clipBounds = new Rectangle(getX(), getY(), getWidth() * 0.75f, getHeight());

        ScissorStack.calculateScissors(
                getStage().getCamera(),
                getStage().getGutterWidth(),
                getStage().getGutterHeight(),
                getStage().getCamera().viewportWidth,
                getStage().getCamera().viewportHeight,
                batch.getTransformMatrix(),
                clipBounds, scissors);

        if (ScissorStack.pushScissors(scissors)) {
            super.draw(batch, parentAlpha);
            ScissorStack.popScissors();
        }
    }
}

      

Full sample code for a group ClipTest class TestScreen and screen capture.

enter image description here

  • ClipTest

    is a subclass of the group used to show the "error".
  • ClipImage

    is a subclass of Image that performs cropping on draw()

    .
  • ClipTest

    has 2 images, background and foreground.

The background is a black image and it should always be the full size of the progress bar.

The foreground is a white image and its width is cropped based on the percentage of the bar.

The weird result I found is that although the foreground uses a clipping class, the background image is actually clipped.

The expected result was generated using photoshop (as I was unable to create it using code).

Any idea what's wrong?

+2


source to share


1 answer


The actual drawing does not happen until Batch "flushes" is not a call draw

to wrap, as it is just a drawing order to be executed later.

You need to make sure OpenGL draw calls happen between turning your scissors on and off, so add flush

after draw

. See https://github.com/libgdx/libgdx/wiki/Clipping,-with-the-use-of-scissorstack



Since the call draw

can fail, you need to keep the calls draw

inside the scope of the active scissor. You may also need to flush or end the batch before starting the active scissor area to prevent queuing calls in the queue before the scissors start cleaning inside the active scissor area.

+5


source







All Articles