我需要绘制填充有颜色的多边形,但是我不知道如何正确创建纹理,以便批量绘制它 这是我的代码
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);
}
我还需要一些技巧,说明如何正确重用我的纹理,而不是总是重新创建它。谢谢
答案 0 :(得分:0)
好吧,我是通过从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可以起作用)