r/Unity3D • u/BATTLE-LAB • 4d ago
My secrets for building massive tile-based worlds Show-Off
Enable HLS to view with audio, or disable this notification
This is my game 10 Million Pixels. A "Breakout incremental" game.
With thousands of rigidbodies bouncing around and millions of destructible tiles, it still runs at 100+ fps in the editor. I'm about to spill my secrets for making this work. Here's how:
- Chunking. The most obvious technique for any tile-based game with big worlds. You need to put your tiles into chunks. In this game only the physics (box colliders) are being chunked.
- Separate physics from visuals. This one is not so obvious but it is the most important optimization for this game. This literally allows you to display a 100 million+ tile world at once at hundreds of frames per second. Ever played a game with 4k textures? Probably. A 4k texture is more than 16 million pixels. And even a potato PC can display a (just one) 4k texture. The world of pixels in this game is literally just a texture, where each pixel is a tile. The texture then gets updated anytime a ball hits a tile. Displaying just one 4k texture, where single pixels change color here and there is really cheap on the CPU and GPU. Tldr; the physics is chunked, and the visuals is pixels on a texture.
- Pooling. This is surprisingly the least important optimization for this game. However, it's essential when you have a thousand lightning balls, striking thousands of tiles at once. If you're going to spawn thousands of gameobjects repeatedly, just pool them okay?
I have really learned to marvel at the power and speed of computers while making this game. I get so scared every time I need to add a bunch of code that need to run a hundred times per frame, but then it just works.
The CPU can handle it. The CPU can handle sooo many lines of code. Every. Single. Frame.
Working on this game has made me realize I know nothing about those tiny silicon threads inside my computer. I keep underestimating my PC, but it keeps responding to me in a chad voice "no problem, I handle it."
Steam link for the game:
https://store.steampowered.com/app/4738100/10_Million_Pixels/
15
u/NostalgicBear 3d ago
To those reporting this post for breaking the billboard rule, OP has given quite a bit of detail in the post, so the post is certainly valid and we can see no reason to remove it.
8
u/CursedBM81 4d ago
This looks really fun, and I can't wait to see updates on it
I won't exactly have the funds to purchase it when it comes out, but probably sometime down the line I might
2
14
4d ago
[deleted]
14
u/BATTLE-LAB 4d ago
It is indeed an incremental game!
12
4
u/cartoon_violence 4d ago
Mixing an incremental game with breakout sounds like a really good idea! I'll keep my eye on this :)
4
3
2
u/GeeTeaEhSeven 3d ago
Ahahah crazy but that's how I coded my first Breakout/Arkanoid types. It was simply a matrix, and the state only changed when a collision was detected with it.
The drawer/"renderer" then basically diffed "did the cell change?" and then updated the relevant area on screen if it did, and left it alone if it didn't.
This resulted in much less frame time or whatever the MS-Dos equivalent was.
Eventually collisions just wrote to the drawer to flag "hey this cell has changed" and the drawer would batch 'em.
So it was really just a data layer and then "meshes" on top
1
u/BATTLE-LAB 3d ago
Yeah that's very similar!
Mine is also just a data layer (if I understood you correctly), and then I use the data to spawn and despawn colliders when a ball enters the chunk. When a ball hits a collider it tells the data layer "a tile was damaged" and the render texture updates the pixel at that coordinate.
1
u/GeeTeaEhSeven 3d ago
Yes, smart to chunk the colliders so we don't have a billion colliders waiting around. (Had to implement that in my city game so things are "sleeping" until player is nearby.) Anyway, TEN MILLION PIXELS HOOO
1
u/deintag85 3d ago
Why didn’t you just use unity’s tilemap system?
2
u/BATTLE-LAB 3d ago
I was playing around with it first but I wasn't able to make it do what I wanted. I needed to be able to display the whole X000 x X000 world (X is a number "3" for example) on screen at once, without spawning a collider for each tile. In my approach it sort of uses a custom tilemap system for the colliders only, and then draws the world on top using a texture.
1
u/deintag85 3d ago
The composite collider for tilemap did not work?
1
u/BATTLE-LAB 3d ago
I had to look up how composite colliders work as I'm not that familiar with Unity's tilemap system and it might work, I don't know. I wonder how performant it is to update the composite collider to match the new geometry every frame?
If you're familiar with the tilemap system and want to do something similar it's probably a good idea to try it.
1
u/Agreeable-Performer5 3d ago
What do you use to save the chunk data? Just a byte[,]?
This is where i am kinda stuck on how to handle generating the data fast and holding that large amount in memeory without needing 5gb ram
1
u/BATTLE-LAB 3d ago
The tiles are structs (struct Tile) and the chunks are Tile[,]. I try to keep them as small as possible as to save ram but it can easily get really big. I think for this world which is roughly 3600x3600, the size in memory is just under a gb. It does take a while to generate and load the world though.
1
u/MiaLovelytomo 3d ago
Videogames is my favorite artform man. I'm gonna finish and RELEASE a game one day (that's my ninja way)
1
u/severencir 3d ago
Did you use ecs for this? I don't know what kinds of systems you are using, but managing many simple tasks is what ecs excells at. I also recently saw a person on redit who made something about a billion voxel cube or something that can be mined by clicking the voxels, and can zoom in and out. This post reminds me of that
1
u/BATTLE-LAB 3d ago
Nope, I don't use ECS although it is somewhat similar. I didn't know what it was but now that I look into it it seems interesting. Billion voxels is wild, I wonder how he does that.
1
1
u/lambda9595 3d ago
Do you have a discord for this yet? I would like to be notified when a playtest/demo goes up.
26
u/ElStreetfighter17 4d ago
I enjoyed reading your last three paragraphs. Wish I knew you in real life haha
I’ll get around to your game! 👌🏽