- Home /
Draw 2D array of Tiles to texture
I'm not sure if this is the right approach to it but I'm generating a map procedurally. The map is essentially a 2d array of Tiles
private Tile[,] islandMap;
Each tile has a few different details such as whether the tile is a border (edge of map), ocean or land. I'm using a similar philosophy to Amitp's polygonal map generation - but restricted to a tiled grid in this case.
So what I'd like to accomplish here is represent my tile array of tiles and draw it as a single texture which is then applied to a Quad. I've done effectively everything but I'm stuck on how to draw the tiles to the texture - maybe there are more efficient, fast, and easy to present methods out there and my current approach is poor, but I tried to follow vaguely what AmitP did in his using textures.
public class MapTexture
{
int textureScale;
public MapTexture(int ts)
{
textureScale = ts;
}
public void AttachTexture(GameObject plane, Map map)
{
int textureWidth = (int)map.mapWidth * textureScale;
int textureHeight = (int)map.mapHeight * textureScale;
Texture2D texture = new Texture2D (textureWidth, textureHeight);
texture.SetPixels (Enumerable.Repeat (Color.magenta, textureWidth * textureHeight).ToArray ());
// TEXTURE EDITS HERE
Color[] colors = new Color[3];
colors [0] = Color.red;
colors [1] = Color.green;
colors [2] = Color.blue;
for (int x = 0; x < map.mapWidth; x++) {
for (int y = 0; y < map.mapHeight; y++) {
texture.SetPixels (x * textureScale, y * textureScale, textureScale, textureScale, colors);
}
}
texture.Apply ();
plane.GetComponent<Renderer> ().material.mainTexture = texture;
plane.transform.localPosition = new Vector3 (map.mapWidth / 2, map.mapHeight / 2, 1);
}
}
This is sort of my code at the moment, I obviously know that SetPixels is being used completely wrong, but despite reading documentation I still have no idea what it's supposed to be doing - the initial IEnumerable.Repeat was effectively recycled code which I didn't understand all that well either.
But the for loop is clearly not working and I ain't sure what the right approach should be here. I just want to neatly represent these tiles next to each other to see what I'm doing and keep the generation and drawing process fast and simple since that's not my focus at the moment
Answer by Luponius · Mar 28, 2016 at 02:46 PM
Texture2D texture = new Texture2D (textureWidth, textureHeight);
texture.SetPixels (Enumerable.Repeat (Color.magenta, textureWidth * textureHeight).ToArray ());
// Create color block to paint
for (int x = 0; x < map.mapWidth; x++) {
for (int y = 0; y < map.mapHeight; y++) {
Color[] colorBlock;
if (map.islandMap[x,y].ocean)
colorBlock = Enumerable.Repeat(Color.blue,textureScale*textureScale).ToArray();
else if (map.islandMap[x,y].coast)
colorBlock = Enumerable.Repeat(Color.yellow,textureScale*textureScale).ToArray();
else
colorBlock = Enumerable.Repeat(Color.grey,textureScale*textureScale).ToArray();
texture.SetPixels (x * textureScale, y * textureScale, textureScale, textureScale, colorBlock);
}
}
To answer my own question, this was basically what I ended up going with in the end. It's nothing special but it gets the job done. Takes a while to generate but it's a one time thing anyways.
This was later modified to be adapted to use with lists of Tiles, each referencing its own Vector2D "point" containing the x and y coordinates ins$$anonymous$$d of the 2d array values since it was easier to work with lists than arrays.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can you change the Texture Import Settings via C# Script? 1 Answer
How to assign texture from url 1 Answer
How to fill texture that would be avaliable to ither shaders? 1 Answer