4
\$\begingroup\$

In an hexgrid based game, I render all terrain tiles as a separate model, reusing the same texture objects on all similar tiles. The tiles is a pretty simple model.

When the terrain is set to be at 20x20 or more (so 400 tiles), the game really slows down. The "slowdown point" is about the same on my i5 laptop or my i7 desktop. The game takes around 30megs in the memory. Here is how the render looks like :

enter image description here

The way I render each tile is the following :

foreach (ModelMesh mesh in ModelSupport.Meshes)
{
    foreach (BasicEffect effect in mesh.Effects)
    {
        effect.EnableDefaultLighting();
        effect.World = transforms[mesh.ParentBone.Index] 
            * mScale * mRotationY * mPosition;
        effect.View = Camera.ViewMatrix;
        effect.Projection = Camera.ProjectionMatrix;
    }

    mesh.Draw();
}

Any suggestion of how I could optimize the rendering of a large amount of models?

\$\endgroup\$

2 Answers 2

10
\$\begingroup\$

I believe you want hardware instancing.

http://xbox.create.msdn.com/en-US/education/catalog/sample/mesh_instancing

\$\endgroup\$
4
  • \$\begingroup\$ I do believe it's exactly what I was looking for! I'll let you know once it's tested :-) \$\endgroup\$
    – Tipx
    Commented Jan 21, 2012 at 21:26
  • \$\begingroup\$ Worked like a charm! I now get the same lag with 250x250 that I use to get with 25x25 so it's 100 times faster. I just did a proof of concept the dirty way and I know I can optimize my code to get it like... over 9000 times faster!!! \$\endgroup\$
    – Tipx
    Commented Jan 24, 2012 at 4:53
  • \$\begingroup\$ Glad to help. There is overhead on each draw call. Both instancing and combining them into one vertex array do about the same thing as they get rid of that overhead which as you see is hefty when the models are very simple. No need to remove back facing tris though as XNA culls them by default. \$\endgroup\$ Commented Jan 24, 2012 at 5:16
  • \$\begingroup\$ It looks to me like the link above is 404'ing - I believe this is the equivalent. xbox.create.msdn.com/en-US/education/catalog/sample/… \$\endgroup\$ Commented Dec 21, 2013 at 14:45
1
\$\begingroup\$

If you're really rendering each tile as a static hexagonal model, then you're wasting a lot of resources on the side faces that will not be visible.

Think about doing it like Minecraft, instead: generate the side faces programmatically (or create them as separate models from the top), and include each face only if its tile is higher than its neighbor in that direction, and make them no taller than necessary. Take a reasonably-sized bunch of such computed tiles and put them all in one vertex array.

\$\endgroup\$
2
  • \$\begingroup\$ It's not a bad idea. At some point, the tiles will be moving up and down so it would make the thing a bit more complicated, especially since I've never experimented with dynamically creating models (which I'll do at some point). I'll try to implement @ClassicThunder's solution this week because even if I change the way I'm rendering tiles one day, I'll need to use what he pointed me to at some point for the units and other map components. \$\endgroup\$
    – Tipx
    Commented Jan 22, 2012 at 17:06
  • \$\begingroup\$ Doing it more than once isn't that much more complicated than doing it once :) — you just have to do the generation efficiently, and choose an appropriate chunk size (number of tiles per “model”) to trade off number of draw calls vs. time to rebuild a single model. \$\endgroup\$
    – Kevin Reid
    Commented Jan 23, 2012 at 1:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .