Draw shape performance drop when iterating over children

Hi, i have a question regarding the way Processing draws 3D shapes.
I have a complex 3D model (121521 polygons), where the “top level” shape is a group and all children are triangles. When i draw this model with the built-in .shape() function i get high FPS, and when i try to iterate over the shape’s children and draw each also with the .shape() function, the framerate drops to 2 FPS.

        // 60 FPS
        g.shape(model);
        // 2 FPS
        for (int i = 0; i < model.getChildCount(); i++) {
            g.shape(model.getChild(i));
        }

Why does this happen? It’s important for me to have control over how each geometry of the model is draw, so i can choose color, stroke, etc.

Do you get better performance if you use getChildren() once and then iterate the PShape[] array as needed?

http://processing.github.io/processing-javadocs/core/processing/core/PShape.html#getChildren--

No, same behaviour :pensive:

for (PShape shape : model.getChildren()) {
        shape.draw(g);
}

I had a tip from someone: “Draw everything in once, or draw everything one by one?”

There’s probably some optimisation in the background that i cannot take advantage of if i draw shape by shape. My solution was to iterate over the child shapes, call .setFill(…) according to what i want and then draw everything at once using g.shape(model).

In hindsight this is probably the right way to do it all along. Set all colors and THEN draw, not set color-draw-repeat.

2 Likes