# Weird P3D Error

**URL:** <https://discourse.processing.org/t/weird-p3d-error/43220>\
**Category:** Coding Questions\
**Created:** [November 15, 2023, 10:22pm UTC](https://discourse.processing.org/t/weird-p3d-error/43220 "2023-11-15T22:22:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)\
**Post date:** [November 15, 2023, 10:22pm UTC](https://discourse.processing.org/t/weird-p3d-error/43220/1 "2023-11-15T22:22:22Z")

</div>

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.☹  
-Libby

> **Textures**
>
> ![stone](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/1/919c7e546c1b09712632f3d7db30fbe920dbccc4.png)  
> ![grass](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/9/e9684040c0860b7074aa2c40e562a54b5d8586ea.png)

> **Full code**
>
> ```auto
> 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;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [November 16, 2023, 3:52pm UTC](https://discourse.processing.org/t/weird-p3d-error/43220/2 "2023-11-16T15:52:32Z")

</div>

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](https://discourse.processing.org/t/transparency-issues-in-p3d/31784)

---

<div class="post-metadata">

**Author:** ![Noodlybanan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noodlybanan/32/18897_2.png) [@Noodlybanan](https://discourse.processing.org/u/Noodlybanan)\
**Post date:** [November 17, 2023, 3:56pm UTC](https://discourse.processing.org/t/weird-p3d-error/43220/3 "2023-11-17T15:56:37Z")

</div>

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

```auto
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);
}

```
