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
private void SetVertexType(Vector2Int vertexIndex,MapVertexType mapVertexType)
{
MapVertex vertex = GetVertex(vertexIndex);
if ((vertex.VertexType!=mapVertexType))
{
vertex.VertexType = mapVertexType;
//计算附近的贴图权重
if(vertex.VertexType == MapVertexType.Swamp)
{
MapCell tempCell = GetLeftBottomMapCell(vertexIndex);
if (tempCell != null)
{
tempCell.TextureIndex += 1;
}
tempCell = GetRightBottomMapCell(vertexIndex);
if (tempCell != null)
{
tempCell.TextureIndex += 2;
}
tempCell = GetLeftTopMapCell(vertexIndex);
if (tempCell != null)
{
tempCell.TextureIndex += 4;
}
tempCell = GetRightTopMapCell(vertexIndex);
if (tempCell != null)
{
tempCell.TextureIndex += 8;
}
}
}
}
1. 根据噪声图获取并设置到对应顶点的贴图类型 2. 给每个顶点周围贴图赋值增加1,2,4,8

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
public int[,] CalculateCellTextureIndex(float[,] noiseMap,float limit)
{
int width = noiseMap.GetLength(0);
int height = noiseMap.GetLength(1);
for(int x=1; x<=width;x++)
{
for (int z = 1; z <= height; z++)
{
if (noiseMap[x-1,z-1] >= limit)//大于边界设置为沼泽
{
SetVertexType(x, z, MapVertexType.Swamp);
}
else
{
SetVertexType(x, z, MapVertexType.Forest);
}
}
}
//到这里可以确定所有格子的贴图索引了
int[,] textureIndexMap = new int[width, height];
for(int x=0; x<width; x++)
{
for (int z = 0; z < height; z++)
{
MapCell cell = GetCell(x + 1, z + 1);
if (cell != null)
{
textureIndexMap[x, z] = cell.TextureIndex;
}
}
}
return textureIndexMap;
}