flattenedClone() increases my draw call count, why?

After posting this question on Stack Overflow the other day, I've done some investigations with small numbers of nodes and my conclusion holds up. Calling .flattenedClone() on the node containing all my geometries and adding the result to the root node increases my draw call count!


What on earth is going on? Is .flattenedClone() somehow broken on iOS 12 or am I missing something?


This is representative of the code I'm testing with:


        let boardNode = SCNNode()
        (0...widthCellCount).forEach { widthIdx in
            (0...heightCellCount).forEach { heightIdx in
                let node = SCNNode(geometry: cellGeometry)
                node.position = SCNVector3(Double(widthIdx) * cellSize, Double(heightIdx) * cellSize, 0.0)
                boardNode.addChildNode(node)
            }
        }
// With widthCellCount = 4 / heightCellCount = 0 (i.e., 5 actual geometry nodes on screen)
// both these lines give me a draw call count of up to 12; without .flattenedClone() it starts smaller
//        scnScene.rootNode.addChildNode(boardNode.flattenedClone())
          scnScene.rootNode.addChildNode(boardNode)

Accepted Reply

I've fixed it—the problem was my materials .contents were CALayers, and if you do that .flattenedClone() does nothing. Setting my contents to a UIImage and then calling .flattenedClone() does decrease my draw call count and gets me ~60fps.

Replies

I've fixed it—the problem was my materials .contents were CALayers, and if you do that .flattenedClone() does nothing. Setting my contents to a UIImage and then calling .flattenedClone() does decrease my draw call count and gets me ~60fps.