We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
//
Answers
using in preferences 8000 MB (?) of Ram
Interesting.
So you have written another version of this code that uses an ArrayList -- and it runs faster?
yes, i have another version which runs fast with depth 0 to 2.
it is slow with depth 3 but working. It was so slow using an ArrayList that I attempted to write this new version with PShape but this seems to take a lot of memory.
@Chrisir -- I see. This slow-down seems expected -- a Menger sponge is 20^n, so depths are:
I wonder what the benchmark differences are between
To see a related approach to a Menger Sponge, see this PixelFlow library demo:
- https://forum.processing.org/two/discussion/comment/98724/#Comment_98724
- https://github.com/diwi/PixelFlow/blob/master/examples/Skylight_BulletPhysics_MengerSponge/Skylight_BulletPhysics_MengerSponge.java
thanks!
here is my version with an ArrayList of boxes.
With
(little different from yours)
Every step, multiply with 20.
With depth = 3 it's getting real slow.
Ah, yes. If you are numbering that way, then even moreso!
Yeah, I understand this.
My questions:
Do I have to use endShape? Do I use PShape correctly?
Are there more efficient ways to copy everything to my graphics card or so?
Does peasycam make it slower than necessary?
Grouped PShapes are not that bad, and have its advantages, e.g. if you want to modify each child in a later process (style, transformation, etc...).
But for a rather static Menger Sponge you can save a lot of time and memory if you create just one PShape and put all generated quads (cube-faces) in it.
shp_ms = createShape(); shp_ms.beginShape(QUADS); shp_ms.vertex(x,y,z); shp_ms.vertex(x,yz); ... shp_ms.endShape();
Another optimization is, to not generate invisible faces in the first place, ... and there are actually quite a lot of them.
Here is an older sketch, ... now updated for Processing 3. It is a bit complicated, but therefore creates and renders pretty fast.
To run level 6 (key '6') you probably need to increase the max available memory in the preferences.
further optimizations require to use low-level opengl render. E.g. Cube Instancing, Culling, and/or Implicit Vertex Data, etc...
2) PShape should be doing that.
3) shouldn't do. It's moving the camera, not the shape.
There are a lot of hidden and common faces in 160k cubes. And does PShape do backface culling? because that would probably help (given that boxes are convex and half the faces are pointing away)
(i have no idea why it is impossible to post code in this forum that doesnt get messed up, ... <= doesnt seem to work)
thats the cropped part