многоугольник libgdx спрайт для рисования заполненный многоугольник - PullRequest
0 голосов
/ 18 октября 2019

Мне нужно нарисовать многоугольник, заполненный цветом, но я не знаю, как правильно создать свою текстуру, для того, чтобы нарисовать ее пакетно Вот мой код

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);

}

Также мне нужен совет о том, как правильно использовать мою текстуру, чтобы не всегда воссоздавать ее. Thx

1 Ответ

0 голосов
/ 19 октября 2019

Хорошо, я сделал это, унаследовав от 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();
    }
}

На самом деле было важно придать белый цветтекстуры, потому что белый цвет содержит все цвета, если я установлю красный цвет, там можно будет нарисовать только красный цвет, думаю, что это какой-то странный, но вуду сработало.)

...