0%

生成地形贴图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
private Texture2D GenerateMapTexture(int[,] cellTextureIndexMap,Texture2D groundTexture, Texture2D[] marshTextures)
{
int mapWidth = cellTextureIndexMap.GetLength(0);
int mapHeight = cellTextureIndexMap.GetLength(1);
int textureCellSize = groundTexture.width;//假设地面贴图是正方形的
Texture2D mapTexture = new Texture2D(mapWidth*textureCellSize, mapHeight*textureCellSize);
//遍历每个格子,给每个格子赋予对应的贴图
for (int y = 0; y < mapHeight; y++)
{
int offsetY = y * textureCellSize;
for (int x = 0; x < mapWidth; x++)
{
int offsetX = x * textureCellSize;
int textureIndex = cellTextureIndexMap[x, y]-1;
// 绘制每一个格子内的像素
for (int y1=0;y1<textureCellSize;y1++)
{
for(int x1=0; x1 < textureCellSize; x1++)
{
//设置某个像素点的颜色
//确定是森林贴图还是沼泽贴图
//这个地方是森林||沼泽但是透明,需要绘制底面贴图
if (textureIndex <= 0)
{
Color color = groundTexture.GetPixel(x1, y1);
mapTexture.SetPixel(x1 + offsetX, y1 + offsetY, color);
}
else
{
Color color = marshTextures[textureIndex].GetPixel(x1, y1);
if (color.a==0)
{
mapTexture.SetPixel(x1 + offsetX, y1 + offsetY, groundTexture.GetPixel(x1,y1));
}
else
{
mapTexture.SetPixel(x1 + offsetX, y1 + offsetY, color);
}
}
}
}
}
}
mapTexture.filterMode = FilterMode.Point;
mapTexture.wrapMode = TextureWrapMode.Clamp;
mapTexture.Apply();
return mapTexture;
}