public void BuildTile(int x, int y) { RecastVertex bmin = Geometry.MinBounds; RecastVertex bmax = Geometry.MaxBounds; float tcs = Config.TileSize * Config.CellSize; RecastVertex tileBMin = new RecastVertex(); RecastVertex tileBMax = new RecastVertex(); tileBMin.X = bmin.X + x * tcs; tileBMin.Y = bmin.Y; tileBMin.Z = bmin.Z + y * tcs; tileBMax.X = bmin.X + (x + 1) * tcs; tileBMax.Y = bmax.Y; tileBMax.Z = bmin.Z + (y + 1) * tcs; var builder = BuildTileMesh(x, y, tileBMin, tileBMax); // remove/add new tile? if (builder != null) { Detour.NavMeshBuilder outBuilder; // nav mesh remove tile NavMesh.RemoveTile(NavMesh.GetTileRefAt(x, y, 0), out outBuilder); // nav mesh add tile long result = 0; NavMesh.AddTile(builder, NavMesh.TileFreeData, 0, ref result); } }
/// <summary> /// Builds a single polygon from the nav mesh /// </summary> /// <param name="navmesh">the entire navmesh which contains all polygon data</param> /// <param name="refId">id of the polygon that is to be drawn</param> /// <param name="color">Color to make the polygon</param> /// <param name="verts">List of Verts to add to</param> /// <param name="colors">List of colors for specific polygons</param> /// <param name="uvs">List of UVs for the polygons</param> /// <param name="tris">List of Triangles for the polygons</param> private void BuildNavMeshPoly(Detour.NavMesh navmesh, long refId, Color color, List <Vector3> verts, List <Color> colors, List <Vector2> uvs, List <int> tris) { MeshTile tile = null; Poly poly = null; if ((navmesh.GetTileAndPolyByRef(refId, ref tile, ref poly) & Status.Failure) != 0) { return; } long ip = 0; for (int i = 0; i < tile.Polys.Length; i++) { if (poly == tile.Polys[i]) { ip = i; } } if (poly.Type == Detour.NavMeshBuilder.PolyTypeOffMeshConnection) { // do nothing for now } else { PolyDetail pd = tile.DetailMeshes[ip]; for (int i = 0; i < pd.TriCount; i++) { int t = ((int)pd.TriBase + i) * 4; for (int j = 0; j < 3; j++) { if (tile.DetailTris[t + j] < poly.VertCount) { verts.Add(new Vector3(tile.Verts[poly.Verts[tile.DetailTris[t + j]] * 3 + 0], tile.Verts[poly.Verts[tile.DetailTris[t + j]] * 3 + 1], tile.Verts[poly.Verts[tile.DetailTris[t + j]] * 3 + 2])); } else { verts.Add( new Vector3(tile.DetailVerts[(pd.VertBase + tile.DetailTris[t + j] - poly.VertCount) * 3 + 0], tile.DetailVerts[(pd.VertBase + tile.DetailTris[t + j] - poly.VertCount) * 3 + 1], tile.DetailVerts[(pd.VertBase + tile.DetailTris[t + j] - poly.VertCount) * 3 + 2])); } uvs.Add(new Vector2()); colors.Add(color); tris.Add(tris.Count); } } } }
/// <summary> /// Builds the initial query, normally called by awake /// Can be callen manually with a different nav mesh. /// </summary> /// <param name="navMesh"></param> public void InitializeQuery(Detour.NavMesh navMesh) { _navMeshQuery = new NavMeshQuery(); _navMeshQuery.Init(navMesh, 2048); filter = new QueryFilter(); // These values need to me modifiable in the editor later using RecastArea filter.IncludeFlags = 15; filter.ExcludeFlags = 0; filter.SetAreaCost(1, 1.0f); filter.SetAreaCost(2, 10.0f); filter.SetAreaCost(3, 1.0f); filter.SetAreaCost(4, 1.0f); filter.SetAreaCost(5, 2); filter.SetAreaCost(6, 1.5f); }
public void RebuildTiles() { Progress = 0; IsBuilding = true; RecastVertex bmin = Geometry.MinBounds; RecastVertex bmax = Geometry.MaxBounds; RecastVertex tileBMin = new RecastVertex(); RecastVertex tileBMax = new RecastVertex(); float tcs = Config.TileSize * Config.CellSize; Total = TileWidth * TileHeight; for (int y = 0; y < TileHeight; y++) { for (int x = 0; x < TileWidth; x++) { Progress = y * TileWidth + x; tileBMin.X = bmin.X + x * tcs; tileBMin.Y = bmin.Y; tileBMin.Z = bmin.Z + y * tcs; tileBMax.X = bmin.X + (x + 1) * tcs; tileBMax.Y = bmax.Y; tileBMax.Z = bmin.Z + (y + 1) * tcs; EditorUtility.DisplayProgressBar("Generating...", "Generating Tile " + Progress + " of " + Total, Progress / (float)Total); var builder = BuildTileMesh(x, y, tileBMin, tileBMax); // remove/add new tile? if (builder != null) { Detour.NavMeshBuilder outBuilder; // nav mesh remove tile NavMesh.RemoveTile(NavMesh.GetTileRefAt(x, y, 0), out outBuilder); // nav mesh add tile long result = 0; NavMesh.AddTile(builder, NavMesh.TileFreeData, 0, ref result); } } } EditorUtility.ClearProgressBar(); IsBuilding = false; BuildGeometry(); }
public void BuildTile(int x, int y, RecastVertex tileBMin, RecastVertex tileBMax) { var builder = BuildTileMesh(x, y, tileBMin, tileBMax); // remove/add new tile? if (builder != null) { lock (this) { Detour.NavMeshBuilder outBuilder; // nav mesh remove tile NavMesh.RemoveTile(NavMesh.GetTileRefAt(x, y, 0), out outBuilder); // nav mesh add tile long result = 0; NavMesh.AddTile(builder, NavMesh.TileFreeData, 0, ref result); } } Progress++; }
public bool Init(string filePath) { FileStream f = null; try { f = File.OpenRead(filePath); } catch (System.Exception ex) { Log.Log.Common.Print(ex.ToString()); Log.Log.Common.Print(ex.StackTrace.ToString()); return(false); } BsonReader reader = new BsonReader(f); JsonSerializer serializer = new JsonSerializer(); Detour.NavMesh nav = serializer.Deserialize <NavMeshSerializer>(reader).Reconstitute(); Status ret = mNavMeshQuery.Init(nav, 2048); return(ret == Status.Success); }
/// <summary> /// Builds the NavMesh geometry into a Unity3d Mesh to display for debug /// </summary> public void BuildNavMeshGeometry() { if (NavMesh != null) { NavMeshTriangles = new List <int>(); NavMeshVerts = new List <Vector3>(); NavMeshUVs = new List <Vector2>(); NavMeshColors = new List <Color>(); for (int i = 0; i < NavMesh.GetMaxTiles(); i++) { MeshTile tile = NavMesh.GetTile(i); if (tile.Header == null) { continue; } long baseId = NavMesh.GetPolyRefBase(tile); for (int j = 0; j < tile.Header.PolyCount; j++) { BuildNavMeshPoly(baseId | (uint)j, duRGBA(0, 0, 64, 128), NavMeshVerts, NavMeshColors, NavMeshUVs, NavMeshTriangles); } } } }
public void RemoveTile(int x, int y) { Detour.NavMeshBuilder outBuilder; // nav mesh remove tile NavMesh.RemoveTile(NavMesh.GetTileRefAt(x, y, 0), out outBuilder); }