Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Using PShape Group with Menger Sponge

$
0
0

Hello all,

I have this little code from the forum and I thought it would be faster to add all boxes to a group PShape instead of storing all boxes in an ArrayList.

It works up to 2 but when I go to recursion depth of 3 it totally slows everything down.

What do I do wrong?

Do I have to use endShape?

Thanks !

Best, Chrisir ;-)

import peasy.*;

PeasyCam cam;
PShape shape1;

// ------------------------------------------------

void setup() {
  size(1200, 1000, P3D);
  cam = new PeasyCam(this, 0, 0, 0, 500);
  println("working");
  shape1 = createShape(GROUP);

  generate(0, 0, 0,
    2,
    167);

  println("done ");
}

void draw() {
  background(0);
  avoidClipping();
  lights();
  shape(shape1, 0, 0);
}

// ------------------------------------------------------

void generate(float x2, float y2, float z2,
  int depth,
  float r) {

  PVector pos=new PVector(x2, y2, z2);
  int sum ;

  for (int x = -1; x < 2; x++) {
    for (int y = -1; y < 2; y++) {
      for (int z = -1; z < 2; z++) {

        sum = abs(x) + abs(y) + abs(z);

        float newR = r/3;

        if (sum > 1) {
          if (depth==0) {
            // end of recursion
            Box b = new Box(pos.x + x*newR,
              pos.y + y*newR,
              pos.z + z*newR,
              newR,
              1); // 1 or k

            shape1.addChild(b.getShape());
          } else
          {
            //recursion
            generate(pos.x + x*newR,
              pos.y + y*newR,
              pos.z + z*newR,
              depth-1,
              newR);
          }
        }
      }
    }
  }
  // return boxes;
}//func

void avoidClipping() {
  // avoid clipping (at camera):
  // https : //
  // forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
  perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func


// ========================================================

class Box {

  PVector pos;
  float r;
  //  int index;

  Box(float x, float y, float z,
    float r_,
    int index_) {

    pos = new PVector(x, y, z);
    r = r_;
  }

  PShape getShape() {

    PShape s = createShape(BOX, r);

    s.setFill(color(255));
    s.setStroke(color(111));
    s.translate(pos.x, pos.y, pos.z);

    //  s.endShape();

    return s;
  }
  //
}//class
//

Viewing all articles
Browse latest Browse all 2896

Trending Articles