Optimizing 3D Code

Hi all,
I just finished coding(from scratch :D) a 3d game, yet I get a mere 30fps on my iPad(have to check the version) and I was wondering if there was any way I could improve it, as in the future I will be adding many more calculations that will slow it down even more. Thanks! The code is here: https://gist.github.com/anonymous/c6deaf507d43594ab6b3

Hi @TheSpolderKing,

Just a minor change




which gives you better view, showing up all blocks. Now it looks like a maze, which I think was your objective.

Neat demo.


There are quite a few Lua specific optimisation tricks, especially for math ops and table iterators - I had the same problem for some games I’ve written and this seemed to help. I managed to get a good 5-10% performance increase by bearing in mind some of these.

Here’s a good starting point, although has generally been discussed at length on here over the years (so worth a search)


@TheSolderKing - for one thing, if you are using blocks for dungeon walls, you are drawing 6 times as many walls as you need to, in most cases. In my 3D dungeon, I just drew rects for all the walls, and the floor and ceiling were each one huge rect. Basically, the fewer vertices, the faster it will run.

Additionally, each block has its own mesh. Better to use a single mesh for the whole dungeon.

I discuss some of the techniques I used, in this thread, including using a shader for tiling walls and floor, and lighting for culling unwanted vertices

Regarding the number of meshes, you can have just one mesh for each type of object in your game, ie one wall mesh one crate mesh etc, and then you just translate/ rotate to each position (and set lighting shaders etc) and draw the same mesh over and over. I haven’t profiled this method to see if it’s faster, but it should certainly use way less memory than defining loads of identical meshes.

Yes, you only need one block, I should have said that.

But no blocks is better still.