libgdx 多边形精灵批量绘制填充多边形

libgdx polygon sprite batch draw filled polygon

我需要绘制填充颜色的多边形,但我不知道如何正确创建我的纹理,以便批量绘制它 这是我的代码

public class VisualComponent implements Component, Pool.Poolable {
       public float[] vertices = new float[1];
       public short[] triangles = new short[1];

       @Override
       public void reset() {
           vertices = new float[1];
           triangles = new short[1];
       }
}

这里我正在创建我想要绘制的实体

    Entity source = engine.createEntity();
    TransformComponent transformComponent = engine.createComponent(TransformComponent.class);
    VisualComponent visualComponent = engine.createComponent(VisualComponent.class);
    float redColorBits = Color.RED.toFloatBits();
    visualComponent.vertices = new float[]{
            0, 0, redColorBits,
            10, 0, redColorBits,
            10, 10, redColorBits,
            0, 10, redColorBits
    };

    visualComponent.triangles = new short[]{
            0, 1, 2,
            0, 2, 3
    };

    source.add(transformComponent);
    source.add(visualComponent);
    engine.addEntity(source);

这里是渲染系统的processEntity方法

@Override
protected void processEntity(Entity entity, float deltaTime) {
    TransformComponent transformComponent = tcm.get(entity);
    VisualComponent visualComponent = vcm.get(entity);

    batch.draw(new Texture(1, 1, Pixmap.Format.RGBA8888),
            visualComponent.vertices, 0, visualComponent.vertices.length,
            visualComponent.triangles, 0, visualComponent.triangles.length);

}

此外,我还需要有关如何正确重用我的纹理的提示,而不是总是重新创建它。谢谢

好的,我通过继承 PolygonSpriteBatch 并为绘图功能添加 1x1 白色像素纹理来做到这一点,这是工作正常的代码。

public class TexturedPolygonSpriteBatch extends PolygonSpriteBatch {
private Texture texture;

    public TexturedPolygonSpriteBatch() {
        super();
        initTexture();
    }

    public void draw(float[] polygonVertices, int verticesOffset, int verticesCount, 
short[] polygonTriangles,
                     int trianglesOffset, int trianglesCount) {
        super.draw(texture, polygonVertices, verticesOffset, verticesCount, 
polygonTriangles, trianglesOffset, trianglesCount);
    }

    @Override
    public void dispose() {
        texture.dispose();
        super.dispose();
    }

    private void initTexture() {
        Logger.log(getClass(), "Initializing 1x1 white texture");
        Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.WHITE);
        pixmap.fill();
        texture = new Texture(pixmap);
        pixmap.dispose();
    }
}

其实给纹理赋予白色很重要,因为白色包含所有颜色,如果我设置红色,那里只能绘制红色,觉得有点奇怪,但是woodoo 成功了。)