Efficiently creating a mesh out of models

I’m creating a square grid of models “on the fly” for a minesweep type game. For graphics performance (to cut down on Geoms), I’m taking the whole grid and breaking it up into chunks of 10x10 squares. Each grid square is represented by a model, so each chunk is comprised of 100 models. Each chunk is created as a single node and flattened. When the user reveals a square, one or more chunks may need to be updated. I do this by deleting the old chunk and re-creating it. That part is working right, but there is a noticeable delay. On an fairly pokey Core 2 Duo 1.8GHz laptop, it can sometimes take up to 180ms to build a chunk, and when you need to do more than one the delay can be annoying.

I’m wondering if the method I’m using to build the chunks is as efficient as it could be. If it is, then I may have to consider smaller chunks.

Here’s the chunk creating part of the code:

   def createMeshFragment(self, fragmentX, fragmentY):
      fragWidth = GridFragmentManager.FRAGMENT_WIDTH
      fragHeight = GridFragmentManager.FRAGMENT_HEIGHT
      xoffset = fragmentX * fragWidth
      yoffset = fragmentY * fragHeight
      modelWidth = GridFragmentManager.MODEL_WIDTH
      modelHeight = GridFragmentManager.MODEL_HEIGHT
      newNode = NodePath("grid%d_%d" % (fragmentX, fragmentY))
      for z in range(fragHeight):
          for x in range(fragWidth):
             state, contents = self.gridModel.getSpaceData((x+xoffset,z+yoffset))
             if state == "hidden":             
                model = loader.loadModel("frustrum.egg")
                
             else:
                model = loader.loadModel("inv_frustrum.egg")
                if contents >= 1 and contents <= 3:
                   model.setTexture(self.textures[contents-1])
                   
             model.setPos(x*modelWidth, 0, z*modelHeight)
             model.reparentTo(newNode)
      
      newNode.clearModelNodes()
      newNode.flattenStrong()
      newNode.setPos(modelWidth*fragWidth*fragmentX, 0, modelHeight*fragHeight*fragmentY)
      return newNode

You can make that more efficient, but you’ll have to step into the lower-level functionality to do it. It won’t be simple.

Using smaller chunks might be a good answer. You can also try putting this work in a child thread, but there you will also be adding substantial complexity.

David