Conway’s Game of Life 3D – huge amount of objects in Unity

Conway's Game of Life 3D - huge amount of objects in Unity

Surely you have already heard of Conway‘s cellular automaton – the Game of Life.

For those who haven’t, it’s a simulation of cells interacting with their neighbors by following a simple rule set on an infinite, two dimensional grid.

So let’s add a dimension -> view the game of life in 3D!


The Game of Life has become famous not only in theoretical computer science, for it is Turing Complete, generates the most interesting patterns and is easy to implement.
In flash it can be done in ‘one line’ of code by means of a convolution filter.

So since 1970 the Game of Life has been explored by almost every programmer, it’s no surprise its “Glider” pattern became the official “Hacker Emblem” in 2003.

Customarily it’s visualized by a 2D representation of the board/grid – however, it can be applied to 1D and 3D space as well.

While I find 3D versions/rule sets to be interesting too, there is a much simpler way to lift the game into three dimensions that also generates more beautiful patterns:
Simply plotting the (visible) game grid´s state over time on the third axis perfectly visualizes the process and generates amazing shapes… or at least very interesting greebles :)

The draw call issue

Since I am generating a lot of cubes (cells) on every frame, Unity has to do a huge amount of draw calls – one for each object.

The frame rate on my machine (Q6600, GF8600) dropped right into the basement.

The simple solution to this issue was to generate the complete generation of cubes and to fuse them to one single mesh using Mesh.CombineMeshes.
Normally this is done on startup – for the exact purpose of reducing draw calls – and I thought it could also be done at runtime for this scenario.

And indeed it works and allowed me to easily push more than a million vertices.

Conclusion

The app now draws a 50*50 grid showing the last 200 generations – the game is restarted with a new seed if new generations eventually become too similar.

Admittedly, it takes some resources – a graphic adapter with at least 128MB is essential as the simulation might end up with 1.5 million vertices.

Hope you get to see some interesting patterns – have fun!

 

Update

The game now runs continuously – the VRAM size check was removed since it returned false-positives.

 

8 thoughts on “Conway’s Game of Life 3D – huge amount of objects in Unity”

  1. Greg Dunn

    That\’s brilliant! Did you end up using the Mesh.CombineMeshes on each generation or did you do sets of 10 generations or what? Do you mind sharing a bit more? :)

  2. flashgordon Post Author

    Of course I don´t :)

    Initially I wanted to create a continuous ‘stream’ of cubes, showing 200 states of a running GOL sim that would reset/seed once completely crystallized.

    I soon realized that this means an unfunny huge amount of draw calls so I tried combining each generation as you assumed correctly.

    That left me with max. 200 draw calls and ran quite smoothly but… after a while my browser crashed.

    I quickly found out that my setup was leaking VRam even if I destroyed obsolete objects and the generated meshes.

    It seams like an issue with Unity’s garbage collection, meshes generated on runtime are ignored.

    I don’t think that this is a huge problem in general, as calling Resources.UnloadUnusedAssets(); correctly removes generated meshes from memory, but it also makes the drawing stutter.

    So I had no choice but to give up on the idea of a constantly running simulation and made it stop after 200 generations.

    You might note that pressing the space bar causes Unity to lag a little – this is not because of the generation of the meshes, but because of the garbage collection.

    For the same reason I did not combine more than one generation – while this would certainly have improved the drawing speed.

    Come to think of it, I should just generate the meshes/combine them as far as the 65k limit allows then display the result.

    But right now it is still generated generation by generation right before your eyes :)

    So Mesh.CombineMeshes should be used when ever possible with generated scenery/levels and a forced garbage collection afterwards, but it might not always help with dynamic setups like this one.

    1. flashgordon Post Author

      Hi pepepaco,

      sorry it doesn’t work in your system, it looks like the Unityplayer detects a wrong VRAM size for your graphics adapter.
      This is the condition i wrote: if(SystemInfo.graphicsMemorySize < 128){....
      Strange... because it works on my setup which has similar specs.

      Anyway, thanks for reporting.

      Cheers!

  3. fimbox

    does not work here either, same problem with 128MB ram but have a GTX680

    why not just remove that check? people will notive themself if it is not playable..

  4. Anonymous

    Hi I’m so interested in your project, and I want to learn how to make one by myself, do you have any tutorial? Thanks a lot!

    1. flashgordon Post Author

      There are tons of implementations and documentation about Conway’s – and other – game of life(s) out there. Just google a bit and I’m sure you’ll find what you need.
      Good luck!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="">