We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys, is there any way to have different colors on the same PShape WITHOUT using children?
I'm building a chunk class for a voxel engine, and my chunk is being rendered as a single PShape. I'm looping through my blocks matrix (block[x][y][z]) and adding vertices for each block into the PShape. During the loop i assign different colors to the vertices, but i end up having a monochromatic PShape. Any suggestions?
void generateChunkMesh(){
c = createShape();
c.beginShape(QUADS);
for(int x = 0; x < CHUNK_SIZE; x++){
for(int y = 0; y < CHUNK_SIZE; y++){
for(int z = 0; z < CHUNK_SIZE; z++){
boolean positiveX = false, negativeX = false, positiveY = false, negativeY = false, positiveZ = false, negativeZ = false;
if( block[x][y][z].getState() ) { //Check if current block is active (should be rendered)
if( block[x][y][z].getBlockType() == blockType.GRASS) //HERE IS THE PROBLEM!!! I ASSIGN DIFFERENT COLORS TO DIFFERENT BLOCKS BUT IN THE END I HAVE ONLY ONE COLOR!
c.fill(#2eb82e);
if(block[x][y][z].getBlockType() == blockType.DIRT)
c.fill(#86592d);
if(block[x][y][z].getBlockType() == blockType.ROCK)
c.fill(#606060);
if(x > 0 && block[x - 1][y][z].isActive) //Check if some faces are occulted by other blocks
negativeX = true;
if(x < CHUNK_SIZE-1 && block[x+1][y][z].isActive)
positiveX = true;
if(y > 0 && block[x][y-1][z].isActive)
negativeY = true;
if(y < CHUNK_SIZE-1 && block[x][y+1][z].isActive)
positiveY = true;
if(z > 0 && block[x][y][z-1].isActive)
negativeZ = true;
if(z < CHUNK_SIZE-1 && block[x][y][z+1].isActive)
positiveZ = true;
//POSITIVE X
if(!positiveX){
c.vertex(x + 1, y, z + 1);
c.vertex(x + 1, y, z);
c.vertex(x + 1, y + 1, z);
c.vertex(x + 1, y + 1, z + 1);
}
//NEGATIVE X
if(!negativeX){
c.vertex(x, y, z + 1);
c.vertex(x, y + 1, z + 1);
c.vertex(x, y + 1, z);
c.vertex(x, y, z);
}
//POSITIVE Y
if(!positiveY){
c.vertex(x, y + 1, z + 1);
c.vertex(x + 1, y + 1, z + 1);
c.vertex(x + 1, y + 1, z);
c.vertex(x, y + 1, z);
}
//NEGATIVE Y
if(!negativeY){
c.vertex(x, y, z + 1);
c.vertex(x, y, z);
c.vertex(x + 1, y, z);
c.vertex(x + 1, y, z + 1);
}
//POSITIVE Z
if(!positiveZ){
c.vertex(x, y, z + 1);
c.vertex(x + 1, y, z + 1);
c.vertex(x + 1, y + 1, z + 1);
c.vertex(x, y + 1, z + 1);
}
//NEGATIVE Z
if(!negativeZ){
c.vertex(x,y,z);
c.vertex(x, y + 1, z);
c.vertex(x + 1, y + 1, z);
c.vertex(x + 1, y, z);
}
}
}
}
}
c.endShape(CLOSE);
}