working on a P3D sketch with many semi-transparent, moving geometries distributed in space. finding transparency renders as desired on when i use
hint(ENABLE_DEPTH_SORT);
however, there seems to be some kind of buffering issue interfering with the sketch executing. after a certain number of frames the rendering progressively slows down, eventually grinding to a stop, and a lot of nasty looking triangular artifacts start flickering on the geometries. any ideas what i can do to alleviate this issue?
here’s the full code:
PGraphics img;
int[][] crystals;
int number = 5000;
int radius = 220;
int depth = 40;
int xRange = 20000;
int yRange = 7000;
int zRange = 17000;
float camPosX = 4000;
float camPosY = 2500;
float camPosZ = 5000;
float camSpeedZ = 7.0;
float cameraZ;
float wobbleDriver = 0;
float wobbleSpeed = 0.07;
float maxWobble = 10;
int cAlph = 50;
float cSat = 50.0;
void setup(){
size(1920, 1080, P3D);
cameraZ = (height/2.0) / tan(PI*60.0/360.0);
img = createGraphics(1920, 1080, P3D);
img.smooth(4);
img.hint(ENABLE_DEPTH_SORT);
randomSeed(0);
crystals = new int[number][6];
for(int x=0; x < number; x++ ){
crystals[x][0] = int(random(xRange));
crystals[x][1] = int(random(yRange));
crystals[x][2] = int(random(zRange));
crystals[x][3] = int(random(20, radius));
crystals[x][4] = int(random(20, depth));
crystals[x][5] = int(random(1, maxWobble));
}
}
void draw(){
background(0, 0);
img.beginDraw();
img.colorMode(HSB, 100);
img.camera(camPosX, camPosY, camPosZ, camPosX, camPosY + 300, -4000, 0, 1, 0);
img.perspective(PI/3.0, width/height, cameraZ/10.0, cameraZ*100.0);
img.background(0, 0);
for(int x = 0; x < number; x++) {
int crystalColor = 100 - int(map(crystals[x][1], 0, yRange, 0, 100));
img.fill(crystalColor, cSat, 100, cAlph);
img.pushMatrix();
img.translate(crystals[x][0] - (width), crystals[x][1] - (height), -crystals[x][2] + (zRange /4));
float angle = 0;
float angleIncrement = TWO_PI / 6;
img.rotateX(radians(cos(wobbleDriver) * crystals[x][5]));
img.rotateZ(radians(sin(wobbleDriver) * crystals[x][5]));
img.rotateY(radians(sin(wobbleDriver) * crystals[x][5]));
img.beginShape(QUAD_STRIP);
img.stroke(0);
for (int i = 0; i < 6 + 1; ++i) {
img.vertex(crystals[x][3]*cos(angle), 0 - crystals[x][4]/2, crystals[x][3]*sin(angle));
img.vertex(crystals[x][3]*cos(angle), crystals[x][4]/2, crystals[x][3]*sin(angle));
angle += angleIncrement;
}
img.endShape();
// top cap
angle = 0;
img.beginShape();
img.noStroke();
img.vertex(0, 0 - crystals[x][4]/2, 0);
for (int i = 0; i < 6 + 1; i++) {
img.vertex(crystals[x][3] * cos(angle), 0 - crystals[x][4]/2, crystals[x][3] * sin(angle));
angle += angleIncrement;
}
img.endShape();
// bottom cap
angle = 0;
img.beginShape(TRIANGLE_FAN);
img.vertex(0, crystals[x][4]/2, 0);
for (int i = 0; i < 6 + 1; i++) {
img.vertex(crystals[x][3] * cos(angle), crystals[x][4]/2, crystals[x][3] * sin(angle));
angle += angleIncrement;
}
img.endShape();
img.popMatrix();
}
img.endDraw();
image(img, 0, 0);
//img.save("wobbl_SP_" + nf(frameCount, 3) + ".png");
camPosZ -= camSpeedZ;
wobbleDriver += wobbleSpeed;
if(frameCount > 750){ stop(); }
}