Mostly there but… colors don’t get updated in real time. Could you please have a look at the simpleupdate? Thanks!
public class TestBatch extends SimpleApplication {
public static void main(String[] args) {
TestBatch app = new TestBatch();
app.start(); // start the game
}
BatchNode bn = new BatchNode();
public static int SIZE = 2;
public static boolean USE_BATCHNODE = false;
public static boolean USE_CUSTOMMESH = true;
ColorRGBA[] allMyColors = new ColorRGBA[SIZE];
Quad allQuads[] = new Quad[SIZE];
VertexBuffer vb[] = new VertexBuffer[SIZE];
FloatBuffer colorBuffer;
Mesh mesh;
@Override
public void simpleInitApp() {
mesh = new Mesh();
Vector3f[] vertices = new Vector3f[40000];
/*vertices[0] = new Vector3f(0, 0, 0);
vertices[1] = new Vector3f(3, 0, 0);
vertices[2] = new Vector3f(0, 3, 0);
vertices[3] = new Vector3f(3, 3, 0);*/
Vector2f[] texCoord = new Vector2f[40000];
/*texCoord[0] = new Vector2f(0, 0);
texCoord[1] = new Vector2f(1, 0);
texCoord[2] = new Vector2f(0, 1);
texCoord[3] = new Vector2f(1, 1);*/
int[] indexes = {2, 0, 1, 1, 3, 2,
6, 4, 5, 5, 7, 6};
for (int i = 0; i < SIZE; i++) {
allMyColors[i] = ColorRGBA.randomColor();
if (USE_CUSTOMMESH) {
vertices[0 + i * 4] = new Vector3f(i * 4, 0, 0);
vertices[1 + i * 4] = new Vector3f(3 + i * 4, 0, 0);
vertices[2 + i * 4] = new Vector3f(i * 4, 3, 0);
vertices[3 + i * 4] = new Vector3f(3 + i * 4, 3, 0);
texCoord[0 + i * 4] = new Vector2f(0, 0);
texCoord[1 + i * 4] = new Vector2f(1 + i * 4, 0);
texCoord[2 + i * 4] = new Vector2f(0, 1 + i * 4);
texCoord[3 + i * 4] = new Vector2f(1 + i * 4, 1 + i * 4);
} else {
allQuads[i] = new Quad(.05f, .05f); // create cube shape
geom = new Geometry("Box", allQuads[i]); // create cube geometry from the shape
allQuads[i].setBuffer(VertexBuffer.Type.Color, 4, new float[]{allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a});
vb[i] = allQuads[i].getBuffer(Color);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setBoolean("VertexColor", true);
geom.setMaterial(mat); // set the cube's material
geom.move(-5, -5, 0);
int j = i / 100;
int k = i - j * 100;
geom.move(k / 10f, j / 10f, 0);
if (USE_BATCHNODE) {
bn.attachChild(geom); // make the cube appear in the scene
} else {
rootNode.attachChild(geom);
}
}
}
if (USE_CUSTOMMESH) {
mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
mesh.setBuffer(Type.Index, 3, BufferUtils.createIntBuffer(indexes));
mesh.updateBound();
int i = 0;
geom = new Geometry("Box", mesh);
mesh.setBuffer(VertexBuffer.Type.Color, 4, new float[16 * SIZE]);
colorBuffer = (FloatBuffer) mesh.getBuffer(Color).getData();
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // create a simple material
mat.setBoolean("VertexColor", true);
geom.scale(.05f);
geom.setMaterial(mat); // set the cube's material
rootNode.attachChild(geom);
}
if (USE_BATCHNODE) {
bn.batch();
rootNode.attachChild(bn);
}
}
Geometry geom;
float cc[] = new float[16];
public void simpleUpdate(float tpf) {
for (int i = 0; i < SIZE; i++) {
allMyColors[i] = ColorRGBA.randomColor();
if (USE_CUSTOMMESH) {
/*mesh.setBuffer(VertexBuffer.Type.Color, 4, new float[]{allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a});*/
colorBuffer.position(i * 16);
cc[0] = allMyColors[i].r;
cc[1] = allMyColors[i].g;
cc[2] = allMyColors[i].b;
cc[3] = allMyColors[i].a;
cc[4] = allMyColors[i].r;
cc[5] = allMyColors[i].g;
cc[6] = allMyColors[i].b;
cc[7] = allMyColors[i].a;
cc[8] = allMyColors[i].r;
cc[9] = allMyColors[i].g;
cc[10] = allMyColors[i].b;
cc[11] = allMyColors[i].a;
cc[12] = allMyColors[i].r;
cc[13] = allMyColors[i].g;
cc[14] = allMyColors[i].b;
cc[15] = allMyColors[i].a;
colorBuffer.put(cc, 0, 16);
} else {
allQuads[i].setBuffer(VertexBuffer.Type.Color, 4, new float[]{allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a});
}
//VertexBuffer vb[allQuads[i].getBuffer(Type.Color);
/*vb[i].setElementComponent(i, 4, new float[]{allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a,
allMyColors[i].r, allMyColors[i].g, allMyColors[i].b, allMyColors[i].a});*/
}
if (USE_BATCHNODE) {
bn.onGeometryUnassociated(geom);
bn.batch();
}
}
}