# How to add tiles randomly?

I made a set of 10 different "ocean" tiles, all interchangeable with one another, and I'd like to randomly attach these 10 different tiles together in different combinations over a large rectangular area -- what would be the best way to do so? I've thought of just adding each sprite into a script and then calling a for/foreach loop to add it into my world but that seems extremely expensive since I'd be adding over 1000 sprites. I don't want to reduce the number of sprites I have to add, I'm just seeing if there's a typical way people do this sort of thing.

• Your question is unclear. You want to randomize the tiles, or you want to reduce the number of sprites you'll add to your world? – Alexandre Vaillancourt Jun 6 '15 at 0:29
• I know how to randomize the tiles and add it to the world but it's inefficient and I'm wondering if there's a conventional way people do this since this is probably done quite often. tl;dr how do I generate a rectangular map randomly composed out of my 10 ocean tiles. – Jeff Burghess Jun 6 '15 at 1:01
• In what way is your method inefficient, memory footprint or time to generate the randomized map? – LLL79 Jul 6 '15 at 17:28

Another common technique is to pre-render your tiles (at runtime) into a large texture, or a set of large textures. This way, you can draw large swaths of the screen with a single draw call, at the cost of increased texture memory usage.

• Interesting -- I haven't taken a look at textures before (If you're talking about the specific unity implementation of them), so I'll read up on that. – Jeff Burghess Jun 6 '15 at 2:17

My answer is just based on imagination, no experience here.

this is for the case where you don't have to render all the ocean in one unique time (or maybe it would, its up to you)

Maybe you might want to make "false random generated" area. Let's say you want to display 20 by 20 tiles each time you render. let's say you have 1000 tiles total.

You could make a 20 by 20 generated content (matrix or whatever you implement) that repeat itself over the ocean.

Just make sure the repeatition is not obvious, and I think there won't be troubles.

A really great technique for tiled graphics that doesn't look tiled is called Wang tiling.

Basically, you design your tiles to have edge types and make it so if two tiles have the same edge type, they will look seamless if you put them next to each other, regardless of what else is going on in the tile

It looks great and also lends itself well to random generation / random placement.

To fill in a grid, you put down one tile in the upper left, then randomly select the neighbors from the tiles that have compatible edges. Rinse and repeat for those neighbors and on, until the grid is full. You can also do this procedurally so you don't actually need to store a map of what tiles go where - you define it mathematically!

Note that there's a link on that page to a Shadertoy pixel shader implementation that uses this to efficiently tile an infinite world.

• cool :) that's actually what I was doing but I didn't know what it was called! Guess I've been doing it right then – Jeff Burghess Jun 6 '15 at 2:16

Yet another way to do tiles graphics, but to hide the fact that its tiled is to use texture multisampling.

This is traditionally more useful for terrain, but might work for water as well.

The basic idea is that every pixel is some blend between multiple textures, and that the contribution from each texture varies based on some rule. Common rules include height of the vertex (to blend between rock, grass and snow for instance) but it could also be based on random numbers.

The reason this works well is because the images overlap and change per pixel not per tile, so makes tiling patterns much harder to detect.

You could generate a random number(eg.integer) and then use switch to check cases for how many numbers you want, to randomize numbers use Random.Range(float max, float min).

This is a little late, but...

This is an example from c++, written by me, but you could probably take it and write it out in unity.

You could take a random number using the rand() or any other random function. Then take that number, and as you place down each tile upon the map, use that number to determine what type of ocean tile it is going to be.

for(placingOceanTilesDownX) {
for(placingOceanTilesDownY) {
int randOceanTile = rand() % 4; // 0 - 3 different types

if(randOceanTile == 0)
oceanTileMap[x][y] = 0;

if(randOceanTile == 1)
oceanTileMap[x][y] = 1;

if(randOceanTile == 2)
oceanTileMap[x][y] = 2;

if(randOceanTile == 3)
oceanTileMap[x][y] = 3;

...
...
}
}


Or something like it.

Hope this helps.