0%

地图网格系统

根据War3地形拼接算法理论,我们需要在地形上绘制出整体的用于分区贴图的顶点网格

顶点

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
49
50
51
52
53
54
55
56
57
public class MapGrid
{
//顶点数据 键:屏幕空间坐标 值:世界空间坐标
public Dictionary<Vector2Int,MapVertex> vertexDic = new Dictionary<Vector2Int,MapVertex>();
public int MapHeight { get;private set; }
public int MapWidth { get; private set; }
public float CellSize { get; private set; }
public MapGrid(int mapHeight,int mapWidth,float cellSize)
{
MapHeight = mapHeight;
MapWidth = mapWidth;
CellSize = cellSize;
for(int x=1;x<mapWidth;x++)
{
for(int z=1;z<mapWidth;z++)
{
AddVertex(x,z);
}
}
#region 测试代码
foreach(var item in vertexDic.Values)
{
GameObject temp = GameObject.CreatePrimitive(PrimitiveType.Sphere);
temp.transform.position = item.Position;
temp.transform.localScale = Vector3.one*0.25f;
}
#endregion
}
public void AddVertex(int x,int y)
{
vertexDic.Add(new Vector2Int(x,y),new MapVertex()
{
Position = new Vector3(x*CellSize,0,y*CellSize)
});
}
public MapVertex GetVertex(Vector2Int index)
{
return vertexDic[index];
}
public MapVertex GetVertex(int x,int y)
{
return vertexDic[new Vector2Int(x,y)];
}
//通过世界坐标获取网格系统对应坐标
public MapVertex GetVertexByWorldPosition(Vector3 position)
{
int x =Mathf.Clamp(Mathf.RoundToInt(position.x / CellSize),0,MapWidth);
int y =Mathf.Clamp(Mathf.RoundToInt(position.z / CellSize),0,MapHeight);
return GetVertex(x, y);
}
}

//地图顶点
public class MapVertex
{
public Vector3 Position;
}

格子

格子中心和顶点坐标相差宽高各一半 格子会比顶点多一行一列

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
 public void AddCell(int x, int y)
{
float offset = CellSize / 2;
cellDic.Add(new Vector2Int(x, y), new MapCell()
{
Position = new Vector3(x * CellSize-offset, 0, y * CellSize-offset)
});
}
public class MapCell
{
public Vector3 Position;
}
for (int x = 1; x < mapWdith; x++)
{
for (int z = 1; z < mapWdith; z++)
{
AddVertex(x, z);
AddCell(x, z);
}
}
for(int x=1;x<=mapWdith; x++)
{
AddCell(x, MapHeight);
}
for (int z = 1; z < mapHeight; z++)
{
AddCell(MapWidth, z);
}
foreach(var item in cellDic.Values)
{
GameObject temp = GameObject.CreatePrimitive(PrimitiveType.Cube);
temp.transform.position = item.Position-new Vector3(0,0.49f,0);
temp.transform.localScale = new Vector3(cellSize,1,cellSize);
}

基于顶点找格子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public MapCell GetCell(Vector2Int index)
{
return cellDic[index];
}
public MapCell GetCell(int x, int y)
{
return GetCell(x, y);
}
public MapCell GetLeftBottomMapCell(Vector2Int vertex_Index)
{
return cellDic[vertex_Index];
}
public MapCell GetRightBottomMapCell(Vector2Int vector2_Index)
{
return cellDic[new Vector2Int(vector2_Index.x + 1, vector2_Index.y)];
}
public MapCell GetLeftTopMapCell(Vector2Int vector2_Index)
{
return cellDic[new Vector2Int(vector2_Index.x, vector2_Index.y + 1)];
}
public MapCell GetRightTopMapCell(Vector2Int vector2_Index)
{
return cellDic[new Vector2Int(vector2_Index.x + 1, vector2_Index.y + 1)];
}