Hi to everybody
I’m trying to implement a simple 2D tile scroll (vertical) for a shoot em up prototype.
Basically the idea is to create a mesh of quads, like a checkboard, where every quad has one texture and the “mosaic” of all of them will compose the scrolling background.
iv’e a root node, called n0, then i’ve GRID_HEIGTH nodes, called bgr, that represents the GRID_HEIGTH row of my checkboard, and, at least a GRID_WIDTH * GRID_HEIGTH array of nodes, one for each quad.
The structure is
n0 —> gr0 → gn00,…gn0x
’ —> gr1 —>gr10…gr1x
’ —> …and so on
in the simpleUpdate method, i only update the y coordinate of the n0 node and all the tiles moves vertically.
When the last row disappears on the bottom, i move to the top (outside the camera) and change the texture inside it, so i should obtain an infinite scroll.
Now: the following code works (for now there’s no row movement but this is not a problem) but it is very cpu (or gpu?) killer.
With 32x256 quads the fps drops under 15 on a i7 with a AMD Radeon R7 200 Series, it surprise me a lot. Ok it is not the top-master-race card or the latest CPU but 33724 vertex should run at least 10 or more times faster in my computer.
Sorry for my english and here is the code: what is wrong?
private static final int GRID_WIDTH = 32;
private static final int GRID_HEIGTH = 256;
private Node[] bgr;
private Node[] bgn;
private Quad[] qn;
private Geometry[] gn;
private Node n0;
private Node n1;
private Node n2;
private Node n3;
private Quad q3;
private Geometry geom3;
Material mat3;
private boolean bla = true;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
public void simpleInitApp() {
bgr = new Node[GRID_HEIGTH];
bgn = new Node[GRID_WIDTH * GRID_HEIGTH];
qn = new Quad[GRID_WIDTH * GRID_HEIGTH];
gn = new Geometry[GRID_WIDTH * GRID_HEIGTH];
flyCam.setMoveSpeed(10);
DirectionalLight sunLight = new DirectionalLight();
sunLight.setDirection(new Vector3f(1, -1, -2).normalizeLocal());
sunLight.setColor(ColorRGBA.White);
rootNode.addLight(sunLight);
Material mat1 = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat1.setTexture("DiffuseMap", assetManager.loadTexture("Textures/mare.png"));
mat1.setColor("Diffuse", ColorRGBA.White);
mat1.setColor("Specular", ColorRGBA.White);
mat1.setColor("Ambient", ColorRGBA.Blue);
mat1.setBoolean("UseMaterialColors", true);
Material mat2 = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat2.setTexture("DiffuseMap", assetManager.loadTexture("Textures/bosco.png"));
mat2.setColor("Diffuse", ColorRGBA.White);
mat2.setColor("Specular", ColorRGBA.White);
mat2.setColor("Ambient", ColorRGBA.Blue);
mat2.setBoolean("UseMaterialColors", true);
mat3 = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat3.setTexture("DiffuseMap", assetManager.loadTexture("Textures/erba.png"));
mat3.setColor("Diffuse", ColorRGBA.White);
mat3.setColor("Specular", ColorRGBA.White);
mat3.setColor("Ambient", ColorRGBA.Blue);
mat3.setBoolean("UseMaterialColors", true);
n0 = new Node("n0");
for (int i = 0; i < GRID_HEIGTH; i++) {
bgr[i] = new Node();
for (int j = 0; j < GRID_WIDTH; j++) {
bgn[i * GRID_WIDTH + j] = new Node();
qn[i * GRID_WIDTH + j] = new Quad(1, 1);
gn[i * GRID_WIDTH + j] = new Geometry(i + "," + j, qn[i * GRID_WIDTH + j]);
int idx = (i + j) % 3;
Material m = null;
switch (idx) {
case 0:
m = mat1;
break;
case 1:
m = mat2;
break;
case 2:
m = mat3;
break;
}
gn[i * GRID_WIDTH + j].setMaterial(m);
bgn[i * GRID_WIDTH + j].attachChild(gn[i * GRID_WIDTH + j]);
bgn[i * GRID_WIDTH + j].setLocalTranslation(j, i, 0);
bgr[i].attachChild(bgn[i * GRID_WIDTH + j]);
}
n0.attachChild(bgr[i]);
}
rootNode.attachChild(n0);
}
@Override
public void simpleUpdate(float tpf) {
Vector3f local = n0.getLocalTranslation();
local.y -= tpf;
n0.setLocalTranslation(local);
}