Search code examples
javalibgdxscene2d

Why actors with polygons do not filled normally in libgdx?


So here is two examples, in both I have two images in table but with different size. The only difference between examples that one uses Texture and other uses polygon.

  1. Texture x.png is 130x130 and y.png is 75x75

    Image image1 = new Image(new TextureRegionDrawable(new TextureRegion(new Texture("y.png"))));
    Image image2 = new Image(new TextureRegionDrawable(new TextureRegion(new Texture("x.png"))));
    mainTable.setFillParent(true);
    mainTable.row();
    mainTable.add(image1).uniform().fill();
    mainTable.add(image2).uniform();
    mainTable.pack();
    

output:

textures

As you can see first image is filled to second one size, but in base they have different sizes.


  1. Polygon

    Image image1 = new Image(new PolygonRegionDrawable(new PolygonRegion(getSolidTexture(Color.YELLOW), new float[]{0, 0, 75, 75, 0, 75, 75, 0}, new short[]{0, 1, 2, 0, 3, 2})));
    Image image2 = new Image(new PolygonRegionDrawable(new PolygonRegion(getSolidTexture(Color.YELLOW), new float[]{0, 0, 130, 130, 0, 130, 130, 0}, new short[]{0, 1, 2, 0, 3, 2})));
    mainTable.setFillParent(true);
    mainTable.row();
    mainTable.add(image1).uniform().fill();
    mainTable.add(image2).uniform();
    mainTable.pack();
    

where

    public TextureRegion getSolidTexture(Color color) {
        Pixmap p = new Pixmap(1, 1, RGBA8888);
        p.setColor(color);
        p.fill();
        Texture t = new Texture(p);
        return new TextureRegion(t);
    }

ouput:

polygon

As you can see first image isn't filled to second image size.

Actually why the behavior isn't the same for polygons?


Solution

  • As I figured out PolygonRegion must be the same size as vertices and only after that it normally scaled.

    In my code I just need to scale my vertices array to the same size as my pixmap.

        float[] vertices1 = {0, 0, 75, 75, 0, 75, 75, 0};
        vertices1 = GeometryUtils.scale(vertices1, 0, 0, 1, 1);
        Image image1 = new Image(new PolygonRegionDrawable(new PolygonRegion(resources.getSolidTexture(Color.YELLOW), vertices1, new short[]{0, 1, 2, 0, 3, 2})));
        float[] vertices2 = {0, 0, 130, 130, 0, 130, 130, 0};
        vertices2 = GeometryUtils.scale(vertices2, 0, 0, 1, 1);
        Image image2 = new Image(new PolygonRegionDrawable(new PolygonRegion(resources.getSolidTexture(Color.YELLOW), vertices2, new short[]{0, 1, 2, 0, 3, 2})));
        mainTable.setFillParent(true);
        mainTable.row();
        mainTable.add(image1).uniform().fill();
        mainTable.add(image2).uniform();
        mainTable.pack();
    

    At least for me it is very strange that I cannot find that info in Google or internet at all. After despair I found the solution while debugging.