/// <summary> /// Create marching squares /// </summary> /// <param name="map"></param> /// <param name="wallSize"></param> /// <param name="is3D"></param> static void CreateSquareGrid(IMarchingMap map, MapGenParams parameters) { if (map.MapData == null) return; MSNode[] allMeshNodes = new MSNode[map.MapData.NodeCount]; for (int row = 0; row < map.MapData.RowCount; row++) for (int col = 0; col < map.MapData.ColCount; col++) { allMeshNodes[row * map.MapData.ColCount + col] = new MSNode( map.MapData.GetNodePos(row, col) - Vector3.forward * parameters.wallSize, map.MapData.GetNode(row, col).m_type == MapNodeType.WALL, map.MapData.NodeSize ); } m_marchSquareGrid = new MarchingSquare[(map.MapData.RowCount - 1) * (map.MapData.ColCount - 1)]; for (int sq = 0; sq < (map.MapData.RowCount - 1) * (map.MapData.ColCount - 1); sq++) { int row = sq / (map.MapData.ColCount - 1); int col = sq % (map.MapData.ColCount - 1); m_marchSquareGrid[sq] = new MarchingSquare( allMeshNodes[(row + 1) * map.MapData.ColCount + col], allMeshNodes[(row + 1) * map.MapData.ColCount + col + 1], allMeshNodes[row * map.MapData.ColCount + col], allMeshNodes[row * map.MapData.ColCount + col + 1] ); } }
public static void CreateMesh(IMarchingMap map, MapGenParams parameters) { if (map == null || map.MapData == null || parameters == null) return; Restart(); CreateSquareGrid(map, parameters); MarchingSquaresToMeshes(); CreateFloorMesh(map.MapData); CreateRoofMesh(); map.RoofMesh.mesh = RoofMesh; map.FloorMesh.mesh = FloorMesh; CreateEdgePaths(); if (parameters.Is3D) { CreateWallMesh(parameters); map.WallMesh.mesh = WallMesh; } else { map.PolygonCollider.pathCount = EdgePaths.Count; for (int i = 0; i < EdgePaths.Count; i++) { List<Vector3> edges = EdgePaths[i]; Vector2[] vec2Edges = new Vector2[edges.Count]; for (int j = 0; j < edges.Count; j++) vec2Edges[j] = edges[j]; map.PolygonCollider.SetPath(i, vec2Edges); } } }