Well, with that inflammatory title, I begin.
The project I’m working on involves rendering tens of thousands of boxes. I’m currently testing it rendering 4097 boxes.
Naturally, this is quite slow (about 1.7 fps unflattened), so I started reading about performance optimization in Panda3D, and quickly came across node flattening; it sounds perfect as, at least for now, no boxes do anything independently at all except come into existence.
After the basic startup of getting a window up and running, I throw in a few pieces of code pertinent to Panda3D:
self.taskMgr.add(self.updateSceneTask, "UpdateSceneTask") self.block_root = self.render.attachNewNode("block_root") base.setFrameRateMeter(True) self.added_blocks=False self.first_time=True
All of this should be quite familiar, apart from the added_blocks and first_time variables, which are used to keep track of whether or not new blocks have been added in the updateSceneTask, which works like so:
def updateSceneTask(self, task): if self.first_time=True: self.first_time=False; for x in range(16): for y in range(16): for z in range(16): self.added_blocks=True; new_block = self.loader.loadModel("models/tribe/block") new_block.setScale(1,1,1) new_block.setPos(x*2-16,y*2+25,-2*z-5) new_block.reparentTo(self.block_root) if self.added_blocks=True: self.block_root.flattenStrong() self.added_blocks=False return Task.cont
Now, I’ve butchered that code a bit to remove a whole lot of stuff extraneous to this problem, so if that doesn’t make perfect sense, let me know and I’ll clarify.
Basically, though, the end result is that the task goes through and creates 4096 blocks one time, adds them to the self.block_root node, and attempts to flatten them, once. This completely butchers performance. Unflattened, I can kind of pan the camera around and get a look at things. Flattened, no such luck. The window all but hard locks. Anyone care to show me the error of my ways?