Weird P3D Error

Hello!
My P3D game has the function voxel(x, y, z, img_front, img_back, img_right, img_left, img_top, img_bottom). If i use the same image as input (for all voxel calls in a chunk), the framerate is very stable at 60fps, however when i generate the cunk with 2 different textures, the framerate drops down to 15fps. The textures are 8x8 pixels big. I don’t know why this happens, if anyone could help me out, it would be cool.:frowning:
-Libby

Textures

stone
grass

Full code
PShape chunk;
int data[][][] = new int[32][32][32];
PImage imgs[] = new PImage[16];

void setup() {
  size(960, 540, P3D);
  surface.setLocation(100, 100);

  imgs[0] = loadImage("stone.png");
  imgs[1] = loadImage("grass.png");

  chunk = createShape(GROUP);
  textureMode(NORMAL);
  ((PGraphicsOpenGL)g).textureSampling(3);
  noStroke();

  createChunk();
}

void draw() {
  background(#0080FF);
  
  translate(width/2, height/2);
  scale(100);
  rotateX(frameCount/(float)100);
  rotateY(frameCount/(float)100);
  rotateZ(frameCount/(float)100);
  shape(chunk);
  //box(100);
}

void createChunk() {
  //generate chunk data
  for (int x = 0; x < 31; x++) {
    for (int y = 0; y < 31; y++) {
      for (int z = 0; z < 31; z++) {
        data[x][y][z] = round(random(-1,1));
      }
    }
  }

  //mesh cunk
  int size = 32;
  for (int x = 0; x < size; x++) {
    for (int y = 0; y < size; y++) {
      for (int z = 0; z < size; z++) {
        if(data[x][y][z] != -1) chunk.addChild(voxel(x, y, z, imgs[data[x][y][z]], imgs[data[x][y][z]], imgs[data[x][y][z]], imgs[data[x][y][z]], imgs[data[x][y][z]], imgs[data[x][y][z]]));
      }
    }
  }
}

PShape voxel(int x, int y, int z, PImage front, PImage back, PImage right, PImage left, PImage top, PImage bottom) {
  PShape voxel = createShape(GROUP);
  voxel.translate(x, y, z);
  
  PShape quad;
  
  // Front face
  quad = createShape();
  quad.beginShape(QUADS);
  quad.texture(front);
  quad.vertex(0, 0, 0, 0, 0);
  quad.vertex(1, 0, 0, 1, 0);
  quad.vertex(1, 1, 0, 1, 1);
  quad.vertex(0, 1, 0, 0, 1);
  quad.endShape();
  
  voxel.addChild(quad);

  // Back face
  quad = createShape();
  quad.beginShape(QUADS);
  quad.texture(back);
  quad.vertex(0, 0, 1, 0, 0);
  quad.vertex(1, 0, 1, 1, 0);
  quad.vertex(1, 1, 1, 1, 1);
  quad.vertex(0, 1, 1, 0, 1);
  quad.endShape();
  
  voxel.addChild(quad);

  // Right face
  quad = createShape();
  quad.beginShape(QUADS);
  quad.texture(right);
  quad.vertex(1, 0, 0, 0, 0);
  quad.vertex(1, 0, 1, 1, 0);
  quad.vertex(1, 1, 1, 1, 1);
  quad.vertex(1, 1, 0, 0, 1);
  quad.endShape();
  
  voxel.addChild(quad);

  // Left face
  quad = createShape();
  quad.beginShape(QUADS);
  quad.texture(left);
  quad.vertex(0, 0, 0, 0, 0);
  quad.vertex(0, 0, 1, 1, 0);
  quad.vertex(0, 1, 1, 1, 1);
  quad.vertex(0, 1, 0, 0, 1);
  quad.endShape();
  
  voxel.addChild(quad);

  // Top face
  quad = createShape();
  quad.beginShape(QUADS);
  quad.texture(top);
  quad.vertex(0, 0, 0, 0, 0);
  quad.vertex(1, 0, 0, 1, 0);
  quad.vertex(1, 0, 1, 1, 1);
  quad.vertex(0, 0, 1, 0, 1);
  quad.endShape();
  
  voxel.addChild(quad);

  // Bottom face
  quad = createShape();
  quad.beginShape(QUADS);
  quad.texture(bottom);
  quad.vertex(0, 1, 0, 0, 0);
  quad.vertex(1, 1, 0, 1, 0);
  quad.vertex(1, 1, 1, 1, 1);
  quad.vertex(0, 1, 1, 0, 1);
  quad.endShape();
  
  voxel.addChild(quad);
  
  return voxel;
}
1 Like

Swapping between textures is a slow operation. Try using a single texture with the various “textures” in there, and change the texture coordinates instead.

EDIT - quick search for “texture atlas” on here finds eg. this thread which might be useful - Transparency issues in P3D

1 Like

Hi! For anyone looking for code, here is an example:

PShape quad;
PImage img;

void setup() {
  img = loadImage("image.png"); //load the image used for the map

  textureMode(NORMAL); //change texturing mode to fit
  ((PGraphicsOpenGL)g).textureSampling(3); //make textures be pixelated
  noStroke(); //make quads not have a border

  quad.beginShape(QUADS);
  chunk.texture(img);
  quad.vertex(1, 0, 0, 0, 0);
  quad.vertex(1, 0, 1, 1, 0);
  quad.vertex(1, 1, 1, 1, 1);
  quad.vertex(1, 1, 0, 0, 1);
  
  quad.vertex(0, 0, 0, 1, 0);
  quad.vertex(0, 0, 1, 2, 0);
  quad.vertex(0, 1, 1, 2, 1);
  quad.vertex(0, 1, 0, 1, 1);
  chunk.endShape();
}

void draw() {
shape(quad);
}