Skip to content

Instantly share code, notes, and snippets.

@Tunied
Created April 19, 2022 08:59
Show Gist options
  • Save Tunied/75b1ae21c2bdbb8685439ef4e9af3b15 to your computer and use it in GitHub Desktop.
Save Tunied/75b1ae21c2bdbb8685439ef4e9af3b15 to your computer and use it in GitHub Desktop.
生成优化过的Mesh
/// <summary>
/// 生成用于扫描的纹理
/// </summary>
private static Mesh GenNavMesh()
{
var walkAreaList = CEVoxelMeshGreedyOptimizer.Optimize(Game.Common.Map.MapSizeX, Game.Common.Map.MapSizeY, IsSameTile, IsHaveTile);
var vertices = new List<Vector3>();
var triangles = new List<int>();
var normals = new List<Vector3>();
walkAreaList.ForEach(vTile =>
{
var nowIndex = vertices.Count;
//V2-----V3
//| |
//| |
//V0-----V1
var V0 = GetPos(vTile.x, vTile.y);
var V1 = GetPos(vTile.x + vTile.sizeX, vTile.y);
var V2 = GetPos(vTile.x, vTile.y + vTile.sizeY);
var V3 = GetPos(vTile.x + vTile.sizeX, vTile.y + vTile.sizeY);
vertices.AddRange(new[] {V0, V1, V2, V3});
//0-2-3,0-3-1 顺时针
triangles.AddRange(new[]
{
nowIndex, nowIndex + 2, nowIndex + 3,
nowIndex, nowIndex + 3, nowIndex + 1
});
var normalDir = Vector3.up;
normals.AddRange(new[] {normalDir, normalDir, normalDir, normalDir});
});
var mesh = new Mesh();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.normals = normals.ToArray();
return mesh;
}
private static Vector3 GetPos(int _vX, int _vY) { return new Vector3(_vX * GameConst.CUBE_UNIT_SIZE, GameConst.CUBE_UNIT_SIZE, _vY * GameConst.CUBE_UNIT_SIZE); }
private static bool IsSameTile(int _aIndexX, int _aIndexY, int _bIndexX, int _bIndexY) { return true; }
private static bool IsHaveTile(int _tileX, int _tileY)
{
if (mIsFlyUnitNav)
{
return Game.Common.Map.Info.IsGenFlyNav(_tileX, _tileY);
}
return Game.Common.Map.Info.IsGenGroundNav(_tileX, _tileY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment