123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677 |
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.Profiling;
- namespace Pathfinding {
- using System.IO;
- using Pathfinding.Util;
- using Pathfinding.Serialization;
- using Math = System.Math;
- using System.Linq;
-
- public abstract class NavmeshBase : NavGraph, INavmesh, INavmeshHolder, ITransformedGraph
- , IRaycastableGraph {
- #if ASTAR_RECAST_LARGER_TILES
-
- public const int VertexIndexMask = 0xFFFFF;
- public const int TileIndexMask = 0x7FF;
- public const int TileIndexOffset = 20;
- #else
-
- public const int VertexIndexMask = 0xFFF;
- public const int TileIndexMask = 0x7FFFF;
- public const int TileIndexOffset = 12;
- #endif
-
- [JsonMember]
- public Vector3 forcedBoundsSize = new Vector3(100, 40, 100);
-
- public abstract float TileWorldSizeX { get; }
-
- public abstract float TileWorldSizeZ { get; }
-
-
-
-
-
- protected abstract float MaxTileConnectionEdgeDistance { get; }
-
- [JsonMember]
- public bool showMeshOutline = true;
-
- [JsonMember]
- public bool showNodeConnections;
-
- [JsonMember]
- public bool showMeshSurface = true;
-
- public int tileXCount;
-
- public int tileZCount;
-
-
-
-
-
- protected NavmeshTile[] tiles;
-
-
-
-
-
-
-
-
-
-
- [JsonMember]
- public bool nearestSearchOnlyXZ;
-
-
-
-
- [JsonMember]
- public bool enableNavmeshCutting = true;
-
-
-
-
-
- internal readonly NavmeshUpdates.NavmeshUpdateSettings navmeshUpdateData;
-
- bool batchTileUpdate;
-
- List<int> batchUpdatedTiles = new List<int>();
-
- List<MeshNode> batchNodesToDestroy = new List<MeshNode>();
-
-
-
-
- public GraphTransform transform = new GraphTransform(Matrix4x4.identity);
- GraphTransform ITransformedGraph.transform { get { return transform; } }
-
- protected abstract bool RecalculateNormals { get; }
-
-
-
-
-
- public abstract GraphTransform CalculateTransform();
-
-
-
-
-
-
-
- public System.Action<NavmeshTile[]> OnRecalculatedTiles;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public NavmeshTile GetTile (int x, int z) {
- return tiles[x + z * tileXCount];
- }
-
-
-
-
-
-
-
-
- public Int3 GetVertex (int index) {
- int tileIndex = (index >> TileIndexOffset) & TileIndexMask;
- return tiles[tileIndex].GetVertex(index);
- }
-
- public Int3 GetVertexInGraphSpace (int index) {
- int tileIndex = (index >> TileIndexOffset) & TileIndexMask;
- return tiles[tileIndex].GetVertexInGraphSpace(index);
- }
-
- public static int GetTileIndex (int index) {
- return (index >> TileIndexOffset) & TileIndexMask;
- }
- public int GetVertexArrayIndex (int index) {
- return index & VertexIndexMask;
- }
-
- public void GetTileCoordinates (int tileIndex, out int x, out int z) {
-
- z = tileIndex/tileXCount;
- x = tileIndex - z*tileXCount;
- }
-
-
-
-
- public NavmeshTile[] GetTiles () {
- return tiles;
- }
-
-
-
-
- public Bounds GetTileBounds (IntRect rect) {
- return GetTileBounds(rect.xmin, rect.ymin, rect.Width, rect.Height);
- }
-
-
-
-
- public Bounds GetTileBounds (int x, int z, int width = 1, int depth = 1) {
- return transform.Transform(GetTileBoundsInGraphSpace(x, z, width, depth));
- }
- public Bounds GetTileBoundsInGraphSpace (IntRect rect) {
- return GetTileBoundsInGraphSpace(rect.xmin, rect.ymin, rect.Width, rect.Height);
- }
-
- public Bounds GetTileBoundsInGraphSpace (int x, int z, int width = 1, int depth = 1) {
- var b = new Bounds();
- b.SetMinMax(
- new Vector3(x*TileWorldSizeX, 0, z*TileWorldSizeZ),
- new Vector3((x+width)*TileWorldSizeX, forcedBoundsSize.y, (z+depth)*TileWorldSizeZ)
- );
- return b;
- }
-
-
-
-
- public Int2 GetTileCoordinates (Vector3 position) {
- position = transform.InverseTransform(position);
- position.x /= TileWorldSizeX;
- position.z /= TileWorldSizeZ;
- return new Int2((int)position.x, (int)position.z);
- }
- protected override void OnDestroy () {
- base.OnDestroy();
-
- TriangleMeshNode.SetNavmeshHolder(active.data.GetGraphIndex(this), null);
- if (tiles != null) {
- for (int i = 0; i < tiles.Length; i++) {
- Pathfinding.Util.ObjectPool<BBTree>.Release(ref tiles[i].bbTree);
- }
- }
- }
- public override void RelocateNodes (Matrix4x4 deltaMatrix) {
- RelocateNodes(deltaMatrix * transform);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void RelocateNodes (GraphTransform newTransform) {
- transform = newTransform;
- if (tiles != null) {
-
- for (int tileIndex = 0; tileIndex < tiles.Length; tileIndex++) {
- var tile = tiles[tileIndex];
- if (tile != null) {
- tile.vertsInGraphSpace.CopyTo(tile.verts, 0);
-
- transform.Transform(tile.verts);
- for (int nodeIndex = 0; nodeIndex < tile.nodes.Length; nodeIndex++) {
- tile.nodes[nodeIndex].UpdatePositionFromVertices();
- }
- tile.bbTree.RebuildFrom(tile.nodes);
- }
- }
- }
- }
-
- protected NavmeshTile NewEmptyTile (int x, int z) {
- return new NavmeshTile {
- x = x,
- z = z,
- w = 1,
- d = 1,
- verts = new Int3[0],
- vertsInGraphSpace = new Int3[0],
- tris = new int[0],
- nodes = new TriangleMeshNode[0],
- bbTree = ObjectPool<BBTree>.Claim(),
- graph = this,
- };
- }
- public override void GetNodes (System.Action<GraphNode> action) {
- if (tiles == null) return;
- for (int i = 0; i < tiles.Length; i++) {
- if (tiles[i] == null || tiles[i].x+tiles[i].z*tileXCount != i) continue;
- TriangleMeshNode[] nodes = tiles[i].nodes;
- if (nodes == null) continue;
- for (int j = 0; j < nodes.Length; j++) action(nodes[j]);
- }
- }
-
-
-
-
- public IntRect GetTouchingTiles (Bounds bounds, float margin = 0) {
- bounds = transform.InverseTransform(bounds);
-
- var r = new IntRect(Mathf.FloorToInt((bounds.min.x - margin) / TileWorldSizeX), Mathf.FloorToInt((bounds.min.z - margin) / TileWorldSizeZ), Mathf.FloorToInt((bounds.max.x + margin) / TileWorldSizeX), Mathf.FloorToInt((bounds.max.z + margin) / TileWorldSizeZ));
-
- r = IntRect.Intersection(r, new IntRect(0, 0, tileXCount-1, tileZCount-1));
- return r;
- }
-
-
- public IntRect GetTouchingTilesInGraphSpace (Rect rect) {
-
- var r = new IntRect(Mathf.FloorToInt(rect.xMin / TileWorldSizeX), Mathf.FloorToInt(rect.yMin / TileWorldSizeZ), Mathf.FloorToInt(rect.xMax / TileWorldSizeX), Mathf.FloorToInt(rect.yMax / TileWorldSizeZ));
-
- r = IntRect.Intersection(r, new IntRect(0, 0, tileXCount-1, tileZCount-1));
- return r;
- }
-
-
-
-
-
- public IntRect GetTouchingTilesRound (Bounds bounds) {
- bounds = transform.InverseTransform(bounds);
-
- var r = new IntRect(Mathf.RoundToInt(bounds.min.x / TileWorldSizeX), Mathf.RoundToInt(bounds.min.z / TileWorldSizeZ), Mathf.RoundToInt(bounds.max.x / TileWorldSizeX)-1, Mathf.RoundToInt(bounds.max.z / TileWorldSizeZ)-1);
-
- r = IntRect.Intersection(r, new IntRect(0, 0, tileXCount-1, tileZCount-1));
- return r;
- }
- protected void ConnectTileWithNeighbours (NavmeshTile tile, bool onlyUnflagged = false) {
- if (tile.w != 1 || tile.d != 1) {
- throw new System.ArgumentException("Tile widths or depths other than 1 are not supported. The fields exist mainly for possible future expansions.");
- }
-
-
-
-
- for (int zo = -1; zo <= 1; zo++) {
- var z = tile.z + zo;
- if (z < 0 || z >= tileZCount) continue;
- for (int xo = -1; xo <= 1; xo++) {
- var x = tile.x + xo;
- if (x < 0 || x >= tileXCount) continue;
-
- if ((xo == 0) == (zo == 0)) continue;
- var otherTile = tiles[x + z*tileXCount];
- if (!onlyUnflagged || !otherTile.flag) {
- ConnectTiles(otherTile, tile);
- }
- }
- }
- }
- protected void RemoveConnectionsFromTile (NavmeshTile tile) {
- if (tile.x > 0) {
- int x = tile.x-1;
- for (int z = tile.z; z < tile.z+tile.d; z++) RemoveConnectionsFromTo(tiles[x + z*tileXCount], tile);
- }
- if (tile.x+tile.w < tileXCount) {
- int x = tile.x+tile.w;
- for (int z = tile.z; z < tile.z+tile.d; z++) RemoveConnectionsFromTo(tiles[x + z*tileXCount], tile);
- }
- if (tile.z > 0) {
- int z = tile.z-1;
- for (int x = tile.x; x < tile.x+tile.w; x++) RemoveConnectionsFromTo(tiles[x + z*tileXCount], tile);
- }
- if (tile.z+tile.d < tileZCount) {
- int z = tile.z+tile.d;
- for (int x = tile.x; x < tile.x+tile.w; x++) RemoveConnectionsFromTo(tiles[x + z*tileXCount], tile);
- }
- }
- protected void RemoveConnectionsFromTo (NavmeshTile a, NavmeshTile b) {
- if (a == null || b == null) return;
-
- if (a == b) return;
- int tileIdx = b.x + b.z*tileXCount;
- for (int i = 0; i < a.nodes.Length; i++) {
- TriangleMeshNode node = a.nodes[i];
- if (node.connections == null) continue;
- for (int j = 0;; j++) {
-
- if (j >= node.connections.Length) break;
- var other = node.connections[j].node as TriangleMeshNode;
-
- if (other == null) continue;
- int tileIdx2 = other.GetVertexIndex(0);
- tileIdx2 = (tileIdx2 >> TileIndexOffset) & TileIndexMask;
- if (tileIdx2 == tileIdx) {
- node.RemoveConnection(node.connections[j].node);
- j--;
- }
- }
- }
- }
- static readonly NNConstraint NNConstraintDistanceXZ = new NNConstraint { distanceXZ = true };
- public override NNInfoInternal GetNearest (Vector3 position, NNConstraint constraint, GraphNode hint) {
- return GetNearestForce(position, constraint != null && constraint.distanceXZ ? NNConstraintDistanceXZ : null);
- }
- public override NNInfoInternal GetNearestForce (Vector3 position, NNConstraint constraint) {
- if (tiles == null) return new NNInfoInternal();
- var tileCoords = GetTileCoordinates(position);
-
- tileCoords.x = Mathf.Clamp(tileCoords.x, 0, tileXCount-1);
- tileCoords.y = Mathf.Clamp(tileCoords.y, 0, tileZCount-1);
- int wmax = Math.Max(tileXCount, tileZCount);
- var best = new NNInfoInternal();
- float bestDistance = float.PositiveInfinity;
- bool xzSearch = nearestSearchOnlyXZ || (constraint != null && constraint.distanceXZ);
-
-
-
-
-
-
- for (int w = 0; w < wmax; w++) {
-
- if (bestDistance < (w-2)*Math.Max(TileWorldSizeX, TileWorldSizeX)) break;
- int zmax = Math.Min(w+tileCoords.y +1, tileZCount);
- for (int z = Math.Max(-w+tileCoords.y, 0); z < zmax; z++) {
-
-
- int originalDx = Math.Abs(w - Math.Abs(z-tileCoords.y));
- var dx = originalDx;
-
-
-
- do {
-
- int x = -dx + tileCoords.x;
- if (x >= 0 && x < tileXCount) {
- NavmeshTile tile = tiles[x + z*tileXCount];
- if (tile != null) {
- if (xzSearch) {
- best = tile.bbTree.QueryClosestXZ(position, constraint, ref bestDistance, best);
- } else {
- best = tile.bbTree.QueryClosest(position, constraint, ref bestDistance, best);
- }
- }
- }
- dx = -dx;
- } while (dx != originalDx);
- }
- }
- best.node = best.constrainedNode;
- best.constrainedNode = null;
- best.clampedPosition = best.constClampedPosition;
- return best;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public GraphNode PointOnNavmesh (Vector3 position, NNConstraint constraint) {
- if (tiles == null) return null;
- var tileCoords = GetTileCoordinates(position);
-
- if (tileCoords.x < 0 || tileCoords.y < 0 || tileCoords.x >= tileXCount || tileCoords.y >= tileZCount) return null;
- NavmeshTile tile = GetTile(tileCoords.x, tileCoords.y);
- if (tile != null) {
- GraphNode node = tile.bbTree.QueryInside(position, constraint);
- return node;
- }
- return null;
- }
-
- protected void FillWithEmptyTiles () {
- for (int z = 0; z < tileZCount; z++) {
- for (int x = 0; x < tileXCount; x++) {
- tiles[z*tileXCount + x] = NewEmptyTile(x, z);
- }
- }
- }
-
-
-
-
- protected static void CreateNodeConnections (TriangleMeshNode[] nodes) {
- List<Connection> connections = ListPool<Connection>.Claim();
- var nodeRefs = ObjectPoolSimple<Dictionary<Int2, int> >.Claim();
- nodeRefs.Clear();
-
- for (int i = 0; i < nodes.Length; i++) {
- TriangleMeshNode node = nodes[i];
- int av = node.GetVertexCount();
- for (int a = 0; a < av; a++) {
-
-
-
- var key = new Int2(node.GetVertexIndex(a), node.GetVertexIndex((a+1) % av));
- if (!nodeRefs.ContainsKey(key)) {
- nodeRefs.Add(key, i);
- }
- }
- }
- for (int i = 0; i < nodes.Length; i++) {
- TriangleMeshNode node = nodes[i];
- connections.Clear();
- int av = node.GetVertexCount();
- for (int a = 0; a < av; a++) {
- int first = node.GetVertexIndex(a);
- int second = node.GetVertexIndex((a+1) % av);
- int connNode;
- if (nodeRefs.TryGetValue(new Int2(second, first), out connNode)) {
- TriangleMeshNode other = nodes[connNode];
- int bv = other.GetVertexCount();
- for (int b = 0; b < bv; b++) {
-
- if (other.GetVertexIndex(b) == second && other.GetVertexIndex((b+1) % bv) == first) {
- connections.Add(new Connection(
- other,
- (uint)(node.position - other.position).costMagnitude,
- (byte)a
- ));
- break;
- }
- }
- }
- }
- node.connections = connections.ToArrayFromPool();
- node.SetConnectivityDirty();
- }
- nodeRefs.Clear();
- ObjectPoolSimple<Dictionary<Int2, int> >.Release(ref nodeRefs);
- ListPool<Connection>.Release(ref connections);
- }
-
-
-
-
- protected void ConnectTiles (NavmeshTile tile1, NavmeshTile tile2) {
- if (tile1 == null || tile2 == null) return;
- if (tile1.nodes == null) throw new System.ArgumentException("tile1 does not contain any nodes");
- if (tile2.nodes == null) throw new System.ArgumentException("tile2 does not contain any nodes");
- int t1x = Mathf.Clamp(tile2.x, tile1.x, tile1.x+tile1.w-1);
- int t2x = Mathf.Clamp(tile1.x, tile2.x, tile2.x+tile2.w-1);
- int t1z = Mathf.Clamp(tile2.z, tile1.z, tile1.z+tile1.d-1);
- int t2z = Mathf.Clamp(tile1.z, tile2.z, tile2.z+tile2.d-1);
- int coord, altcoord;
- int t1coord, t2coord;
- float tileWorldSize;
-
-
- if (t1x == t2x) {
- coord = 2;
- altcoord = 0;
- t1coord = t1z;
- t2coord = t2z;
- tileWorldSize = TileWorldSizeZ;
- } else if (t1z == t2z) {
- coord = 0;
- altcoord = 2;
- t1coord = t1x;
- t2coord = t2x;
- tileWorldSize = TileWorldSizeX;
- } else {
- throw new System.ArgumentException("Tiles are not adjacent (neither x or z coordinates match)");
- }
- if (Math.Abs(t1coord-t2coord) != 1) {
- throw new System.ArgumentException("Tiles are not adjacent (tile coordinates must differ by exactly 1. Got '" + t1coord + "' and '" + t2coord + "')");
- }
-
- int midpoint = (int)Math.Round((Math.Max(t1coord, t2coord) * tileWorldSize) * Int3.Precision);
- #if ASTARDEBUG
- Vector3 v1 = new Vector3(-100, 0, -100);
- Vector3 v2 = new Vector3(100, 0, 100);
- v1[coord] = midpoint*Int3.PrecisionFactor;
- v2[coord] = midpoint*Int3.PrecisionFactor;
- Debug.DrawLine(v1, v2, Color.magenta);
- #endif
- TriangleMeshNode[] nodes1 = tile1.nodes;
- TriangleMeshNode[] nodes2 = tile2.nodes;
-
-
- TriangleMeshNode[] closeToEdge = ArrayPool<TriangleMeshNode>.Claim(nodes2.Length);
- int numCloseToEdge = 0;
- for (int j = 0; j < nodes2.Length; j++) {
- TriangleMeshNode nodeB = nodes2[j];
- int bVertexCount = nodeB.GetVertexCount();
- for (int b = 0; b < bVertexCount; b++) {
- Int3 bVertex1 = nodeB.GetVertexInGraphSpace(b);
- Int3 bVertex2 = nodeB.GetVertexInGraphSpace((b+1) % bVertexCount);
- if (Math.Abs(bVertex1[coord] - midpoint) < 2 && Math.Abs(bVertex2[coord] - midpoint) < 2) {
- closeToEdge[numCloseToEdge] = nodes2[j];
- numCloseToEdge++;
- break;
- }
- }
- }
-
- for (int i = 0; i < nodes1.Length; i++) {
- TriangleMeshNode nodeA = nodes1[i];
- int aVertexCount = nodeA.GetVertexCount();
-
- for (int a = 0; a < aVertexCount; a++) {
-
- Int3 aVertex1 = nodeA.GetVertexInGraphSpace(a);
- Int3 aVertex2 = nodeA.GetVertexInGraphSpace((a+1) % aVertexCount);
-
- if (Math.Abs(aVertex1[coord] - midpoint) < 2 && Math.Abs(aVertex2[coord] - midpoint) < 2) {
- int minalt = Math.Min(aVertex1[altcoord], aVertex2[altcoord]);
- int maxalt = Math.Max(aVertex1[altcoord], aVertex2[altcoord]);
-
- if (minalt == maxalt) continue;
- for (int j = 0; j < numCloseToEdge; j++) {
- TriangleMeshNode nodeB = closeToEdge[j];
- int bVertexCount = nodeB.GetVertexCount();
- for (int b = 0; b < bVertexCount; b++) {
- Int3 bVertex1 = nodeB.GetVertexInGraphSpace(b);
- Int3 bVertex2 = nodeB.GetVertexInGraphSpace((b+1) % bVertexCount);
- if (Math.Abs(bVertex1[coord] - midpoint) < 2 && Math.Abs(bVertex2[coord] - midpoint) < 2) {
- int minalt2 = Math.Min(bVertex1[altcoord], bVertex2[altcoord]);
- int maxalt2 = Math.Max(bVertex1[altcoord], bVertex2[altcoord]);
-
- if (minalt2 == maxalt2) continue;
- if (maxalt > minalt2 && minalt < maxalt2) {
-
-
- if ((aVertex1 == bVertex1 && aVertex2 == bVertex2) || (aVertex1 == bVertex2 && aVertex2 == bVertex1) ||
- VectorMath.SqrDistanceSegmentSegment((Vector3)aVertex1, (Vector3)aVertex2, (Vector3)bVertex1, (Vector3)bVertex2) < MaxTileConnectionEdgeDistance*MaxTileConnectionEdgeDistance) {
- uint cost = (uint)(nodeA.position - nodeB.position).costMagnitude;
- nodeA.AddConnection(nodeB, cost, (byte)a);
- nodeB.AddConnection(nodeA, cost, (byte)b);
- }
- }
- }
- }
- }
- }
- }
- }
- ArrayPool<TriangleMeshNode>.Release(ref closeToEdge);
- }
-
-
-
-
-
-
- public void StartBatchTileUpdate () {
- if (batchTileUpdate) throw new System.InvalidOperationException("Calling StartBatchLoad when batching is already enabled");
- batchTileUpdate = true;
- }
-
-
-
-
-
- void DestroyNodes (List<MeshNode> nodes) {
- for (int i = 0; i < batchNodesToDestroy.Count; i++) {
- batchNodesToDestroy[i].TemporaryFlag1 = true;
- }
- for (int i = 0; i < batchNodesToDestroy.Count; i++) {
- var node = batchNodesToDestroy[i];
- for (int j = 0; j < node.connections.Length; j++) {
- var neighbour = node.connections[j].node;
- if (!neighbour.TemporaryFlag1) {
- neighbour.RemoveConnection(node);
- }
- }
-
-
- ArrayPool<Connection>.Release(ref node.connections, true);
- node.Destroy();
- }
- }
- void TryConnect (int tileIdx1, int tileIdx2) {
-
-
- if (tiles[tileIdx1].flag && tiles[tileIdx2].flag && tileIdx1 >= tileIdx2) return;
- ConnectTiles(tiles[tileIdx1], tiles[tileIdx2]);
- }
-
-
-
-
-
-
- public void EndBatchTileUpdate () {
- if (!batchTileUpdate) throw new System.InvalidOperationException("Calling EndBatchTileUpdate when batching had not yet been started");
- batchTileUpdate = false;
- DestroyNodes(batchNodesToDestroy);
- batchNodesToDestroy.ClearFast();
- for (int i = 0; i < batchUpdatedTiles.Count; i++) tiles[batchUpdatedTiles[i]].flag = true;
- for (int i = 0; i < batchUpdatedTiles.Count; i++) {
- int x = batchUpdatedTiles[i] % tileXCount, z = batchUpdatedTiles[i] / tileXCount;
- if (x > 0) TryConnect(batchUpdatedTiles[i], batchUpdatedTiles[i] - 1);
- if (x < tileXCount - 1) TryConnect(batchUpdatedTiles[i], batchUpdatedTiles[i] + 1);
- if (z > 0) TryConnect(batchUpdatedTiles[i], batchUpdatedTiles[i] - tileXCount);
- if (z < tileZCount - 1) TryConnect(batchUpdatedTiles[i], batchUpdatedTiles[i] + tileXCount);
- }
- for (int i = 0; i < batchUpdatedTiles.Count; i++) tiles[batchUpdatedTiles[i]].flag = false;
- batchUpdatedTiles.ClearFast();
- }
-
-
-
-
- protected void ClearTile (int x, int z) {
- if (!batchTileUpdate) throw new System.Exception("Must be called during a batch update. See StartBatchTileUpdate");
- var tile = GetTile(x, z);
- if (tile == null) return;
- var nodes = tile.nodes;
- for (int i = 0; i < nodes.Length; i++) {
- if (nodes[i] != null) batchNodesToDestroy.Add(nodes[i]);
- }
- ObjectPool<BBTree>.Release(ref tile.bbTree);
-
- tiles[x + z*tileXCount] = null;
- }
-
- Dictionary<int, int> nodeRecyclingHashBuffer = new Dictionary<int, int>();
-
-
-
-
-
-
-
-
-
-
- void PrepareNodeRecycling (int x, int z, Int3[] verts, int[] tris, TriangleMeshNode[] recycledNodeBuffer) {
- NavmeshTile tile = GetTile(x, z);
- if (tile == null || tile.nodes.Length == 0) return;
- var nodes = tile.nodes;
- var recycling = nodeRecyclingHashBuffer;
- for (int i = 0, j = 0; i < tris.Length; i += 3, j++) {
- recycling[verts[tris[i+0]].GetHashCode() + verts[tris[i+1]].GetHashCode() + verts[tris[i+2]].GetHashCode()] = j;
- }
- var connectionsToKeep = ListPool<Connection>.Claim();
- for (int i = 0; i < nodes.Length; i++) {
- var node = nodes[i];
- Int3 v0, v1, v2;
- node.GetVerticesInGraphSpace(out v0, out v1, out v2);
- var hash = v0.GetHashCode() + v1.GetHashCode() + v2.GetHashCode();
- int newNodeIndex;
- if (recycling.TryGetValue(hash, out newNodeIndex)) {
-
-
- if (verts[tris[3*newNodeIndex+0]] == v0 && verts[tris[3*newNodeIndex+1]] == v1 && verts[tris[3*newNodeIndex+2]] == v2) {
- recycledNodeBuffer[newNodeIndex] = node;
-
- nodes[i] = null;
-
-
- for (int j = 0; j < node.connections.Length; j++) {
- if (node.connections[j].node.GraphIndex != node.GraphIndex) {
- connectionsToKeep.Add(node.connections[j]);
- }
- }
- ArrayPool<Connection>.Release(ref node.connections, true);
- if (connectionsToKeep.Count > 0) {
- node.connections = connectionsToKeep.ToArrayFromPool();
- node.SetConnectivityDirty();
- connectionsToKeep.Clear();
- }
- }
- }
- }
- recycling.Clear();
- ListPool<Connection>.Release(ref connectionsToKeep);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public void ReplaceTile (int x, int z, Int3[] verts, int[] tris) {
- int w = 1, d = 1;
- if (x + w > tileXCount || z+d > tileZCount || x < 0 || z < 0) {
- throw new System.ArgumentException("Tile is placed at an out of bounds position or extends out of the graph bounds ("+x+", " + z + " [" + w + ", " + d+ "] " + tileXCount + " " + tileZCount + ")");
- }
- if (tris.Length % 3 != 0) throw new System.ArgumentException("Triangle array's length must be a multiple of 3 (tris)");
- if (verts.Length > VertexIndexMask) {
- Debug.LogError("Too many vertices in the tile (" + verts.Length + " > " + VertexIndexMask +")\nYou can enable ASTAR_RECAST_LARGER_TILES under the 'Optimizations' tab in the A* Inspector to raise this limit. Or you can use a smaller tile size to reduce the likelihood of this happening.");
- verts = new Int3[0];
- tris = new int[0];
- }
- var wasNotBatching = !batchTileUpdate;
- if (wasNotBatching) StartBatchTileUpdate();
- Profiler.BeginSample("Tile Initialization");
-
- var tile = new NavmeshTile {
- x = x,
- z = z,
- w = w,
- d = d,
- tris = tris,
- bbTree = ObjectPool<BBTree>.Claim(),
- graph = this,
- };
- if (!Mathf.Approximately(x*TileWorldSizeX*Int3.FloatPrecision, (float)Math.Round(x*TileWorldSizeX*Int3.FloatPrecision))) Debug.LogWarning("Possible numerical imprecision. Consider adjusting tileSize and/or cellSize");
- if (!Mathf.Approximately(z*TileWorldSizeZ*Int3.FloatPrecision, (float)Math.Round(z*TileWorldSizeZ*Int3.FloatPrecision))) Debug.LogWarning("Possible numerical imprecision. Consider adjusting tileSize and/or cellSize");
- var offset = (Int3) new Vector3((x * TileWorldSizeX), 0, (z * TileWorldSizeZ));
- for (int i = 0; i < verts.Length; i++) {
- verts[i] += offset;
- }
- tile.vertsInGraphSpace = verts;
- tile.verts = (Int3[])verts.Clone();
- transform.Transform(tile.verts);
- Profiler.BeginSample("Clear Previous Tiles");
-
- var nodes = tile.nodes = new TriangleMeshNode[tris.Length/3];
-
-
-
-
- PrepareNodeRecycling(x, z, tile.vertsInGraphSpace, tris, tile.nodes);
-
- ClearTile(x, z);
- Profiler.EndSample();
- Profiler.EndSample();
- Profiler.BeginSample("Assign Node Data");
-
- tiles[x + z*tileXCount] = tile;
- batchUpdatedTiles.Add(x + z*tileXCount);
-
- CreateNodes(nodes, tile.tris, x + z*tileXCount, (uint)active.data.GetGraphIndex(this));
- Profiler.EndSample();
- Profiler.BeginSample("AABBTree Rebuild");
- tile.bbTree.RebuildFrom(nodes);
- Profiler.EndSample();
- Profiler.BeginSample("Create Node Connections");
- CreateNodeConnections(tile.nodes);
- Profiler.EndSample();
- Profiler.BeginSample("Connect With Neighbours");
- if (wasNotBatching) EndBatchTileUpdate();
- Profiler.EndSample();
- }
- protected void CreateNodes (TriangleMeshNode[] buffer, int[] tris, int tileIndex, uint graphIndex) {
- if (buffer == null || buffer.Length < tris.Length/3) throw new System.ArgumentException("buffer must be non null and at least as large as tris.Length/3");
-
- tileIndex <<= TileIndexOffset;
-
- for (int i = 0; i < buffer.Length; i++) {
- var node = buffer[i];
-
- if (node == null) node = buffer[i] = new TriangleMeshNode(active);
-
- node.Walkable = true;
- node.Tag = 0;
- node.Penalty = initialPenalty;
- node.GraphIndex = graphIndex;
-
-
- node.v0 = tris[i*3+0] | tileIndex;
- node.v1 = tris[i*3+1] | tileIndex;
- node.v2 = tris[i*3+2] | tileIndex;
-
-
- if (RecalculateNormals && !VectorMath.IsClockwiseXZ(node.GetVertexInGraphSpace(0), node.GetVertexInGraphSpace(1), node.GetVertexInGraphSpace(2))) {
- Memory.Swap(ref tris[i*3+0], ref tris[i*3+2]);
- Memory.Swap(ref node.v0, ref node.v2);
- }
- node.UpdatePositionFromVertices();
- }
- }
- public NavmeshBase () {
- navmeshUpdateData = new NavmeshUpdates.NavmeshUpdateSettings(this);
- }
-
-
-
-
-
-
- public bool Linecast (Vector3 origin, Vector3 end) {
- return Linecast(origin, end, null);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public bool Linecast (Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit) {
- return Linecast(this, origin, end, hint, out hit, null);
- }
-
-
-
-
-
-
-
-
-
-
-
- public bool Linecast (Vector3 origin, Vector3 end, GraphNode hint) {
- GraphHitInfo hit;
- return Linecast(this, origin, end, hint, out hit, null);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool Linecast (Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace) {
- return Linecast(this, origin, end, hint, out hit, trace);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool Linecast (Vector3 origin, Vector3 end, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter) {
- return Linecast(this, origin, end, null, out hit, trace, filter);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit) {
- return Linecast(graph, origin, end, hint, out hit, null);
- }
-
- static readonly NNConstraint NNConstraintNoneXZ = new NNConstraint {
- constrainWalkability = false,
- constrainArea = false,
- constrainTags = false,
- constrainDistance = false,
- graphMask = -1,
- distanceXZ = true,
- };
-
- static readonly byte[] LinecastShapeEdgeLookup;
- static NavmeshBase () {
-
-
-
-
- LinecastShapeEdgeLookup = new byte[64];
- Side[] sideOfLine = new Side[3];
- for (int i = 0; i < LinecastShapeEdgeLookup.Length; i++) {
- sideOfLine[0] = (Side)((i >> 0) & 0x3);
- sideOfLine[1] = (Side)((i >> 2) & 0x3);
- sideOfLine[2] = (Side)((i >> 4) & 0x3);
- LinecastShapeEdgeLookup[i] = 0xFF;
-
- if (sideOfLine[0] != (Side)3 && sideOfLine[1] != (Side)3 && sideOfLine[2] != (Side)3) {
-
-
-
-
-
-
- int bestBadness = int.MaxValue;
- for (int j = 0; j < 3; j++) {
- if ((sideOfLine[j] == Side.Left || sideOfLine[j] == Side.Colinear) && (sideOfLine[(j+1)%3] == Side.Right || sideOfLine[(j+1)%3] == Side.Colinear)) {
- var badness = (sideOfLine[j] == Side.Colinear ? 1 : 0) + (sideOfLine[(j+1)%3] == Side.Colinear ? 1 : 0);
- if (badness < bestBadness) {
- LinecastShapeEdgeLookup[i] = (byte)j;
- bestBadness = badness;
- }
- }
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List<GraphNode> trace, System.Func<GraphNode, bool> filter = null) {
- hit = new GraphHitInfo();
- if (float.IsNaN(origin.x + origin.y + origin.z)) throw new System.ArgumentException("origin is NaN");
- if (float.IsNaN(end.x + end.y + end.z)) throw new System.ArgumentException("end is NaN");
- var node = hint as TriangleMeshNode;
- if (node == null) {
- node = graph.GetNearest(origin, NNConstraintNoneXZ).node as TriangleMeshNode;
- if (node == null) {
- Debug.LogError("Could not find a valid node to start from");
- hit.origin = origin;
- hit.point = origin;
- return true;
- }
- }
-
- var i3originInGraphSpace = node.ClosestPointOnNodeXZInGraphSpace(origin);
- hit.origin = graph.transform.Transform((Vector3)i3originInGraphSpace);
- if (!node.Walkable || (filter != null && !filter(node))) {
- hit.node = node;
- hit.point = hit.origin;
- hit.tangentOrigin = hit.origin;
- return true;
- }
- var endInGraphSpace = graph.transform.InverseTransform(end);
- var i3endInGraphSpace = (Int3)endInGraphSpace;
-
- if (i3originInGraphSpace == i3endInGraphSpace) {
- hit.point = hit.origin;
- hit.node = node;
- if (trace != null) trace.Add(node);
- return false;
- }
- int counter = 0;
- while (true) {
- counter++;
- if (counter > 2000) {
- Debug.LogError("Linecast was stuck in infinite loop. Breaking.");
- return true;
- }
- if (trace != null) trace.Add(node);
- Int3 a0, a1, a2;
- node.GetVerticesInGraphSpace(out a0, out a1, out a2);
- int sideOfLine = (byte)VectorMath.SideXZ(i3originInGraphSpace, i3endInGraphSpace, a0);
- sideOfLine |= (byte)VectorMath.SideXZ(i3originInGraphSpace, i3endInGraphSpace, a1) << 2;
- sideOfLine |= (byte)VectorMath.SideXZ(i3originInGraphSpace, i3endInGraphSpace, a2) << 4;
-
- int shapeEdgeA = (int)LinecastShapeEdgeLookup[sideOfLine];
-
- var sideNodeExit = VectorMath.SideXZ(shapeEdgeA == 0 ? a0 : (shapeEdgeA == 1 ? a1 : a2), shapeEdgeA == 0 ? a1 : (shapeEdgeA == 1 ? a2 : a0), i3endInGraphSpace);
- if (sideNodeExit != Side.Left) {
-
-
- hit.point = end;
- hit.node = node;
- var endNode = graph.GetNearest(end, NNConstraintNoneXZ).node as TriangleMeshNode;
- if (endNode == node || endNode == null) {
-
-
-
-
- return false;
- } else {
-
-
-
-
-
- return true;
- }
- }
- if (shapeEdgeA == 0xFF) {
-
-
- Debug.LogError("Line does not intersect node at all");
- hit.node = node;
- hit.point = hit.tangentOrigin = hit.origin;
- return true;
- } else {
- bool success = false;
- var nodeConnections = node.connections;
-
- for (int i = 0; i < nodeConnections.Length; i++) {
- if (nodeConnections[i].shapeEdge == shapeEdgeA) {
-
- var neighbour = nodeConnections[i].node as TriangleMeshNode;
- if (neighbour == null || !neighbour.Walkable || (filter != null && !filter(neighbour))) continue;
- var neighbourConnections = neighbour.connections;
- int shapeEdgeB = -1;
- for (int j = 0; j < neighbourConnections.Length; j++) {
- if (neighbourConnections[j].node == node) {
- shapeEdgeB = neighbourConnections[j].shapeEdge;
- break;
- }
- }
- if (shapeEdgeB == -1) {
-
-
- continue;
- }
- var side1 = VectorMath.SideXZ(i3originInGraphSpace, i3endInGraphSpace, neighbour.GetVertexInGraphSpace(shapeEdgeB));
- var side2 = VectorMath.SideXZ(i3originInGraphSpace, i3endInGraphSpace, neighbour.GetVertexInGraphSpace((shapeEdgeB+1) % 3));
-
- success = (side1 == Side.Right || side1 == Side.Colinear) && (side2 == Side.Left || side2 == Side.Colinear);
- if (!success) continue;
-
-
-
-
-
- node = neighbour;
- break;
- }
- }
- if (!success) {
-
-
- var hitEdgeStartInGraphSpace = (Vector3)(shapeEdgeA == 0 ? a0 : (shapeEdgeA == 1 ? a1 : a2));
- var hitEdgeEndInGraphSpace = (Vector3)(shapeEdgeA == 0 ? a1 : (shapeEdgeA == 1 ? a2 : a0));
- var intersectionInGraphSpace = VectorMath.LineIntersectionPointXZ(hitEdgeStartInGraphSpace, hitEdgeEndInGraphSpace, (Vector3)i3originInGraphSpace, (Vector3)i3endInGraphSpace);
- hit.point = graph.transform.Transform(intersectionInGraphSpace);
- hit.node = node;
- var hitEdgeStart = graph.transform.Transform(hitEdgeStartInGraphSpace);
- var hitEdgeEnd = graph.transform.Transform(hitEdgeEndInGraphSpace);
- hit.tangent = hitEdgeEnd - hitEdgeStart;
- hit.tangentOrigin = hitEdgeStart;
- return true;
- }
- }
- }
- }
- public override void OnDrawGizmos (Pathfinding.Util.RetainedGizmos gizmos, bool drawNodes) {
- if (!drawNodes) {
- return;
- }
- using (var helper = gizmos.GetSingleFrameGizmoHelper(active)) {
- var bounds = new Bounds();
- bounds.SetMinMax(Vector3.zero, forcedBoundsSize);
-
-
- helper.builder.DrawWireCube(CalculateTransform(), bounds, Color.white);
- }
- if (tiles != null && (showMeshSurface || showMeshOutline || showNodeConnections)) {
- var baseHasher = new RetainedGizmos.Hasher(active);
- baseHasher.AddHash(showMeshOutline ? 1 : 0);
- baseHasher.AddHash(showMeshSurface ? 1 : 0);
- baseHasher.AddHash(showNodeConnections ? 1 : 0);
- int startTileIndex = 0;
- var hasher = baseHasher;
- var hashedNodes = 0;
-
-
- for (int i = 0; i < tiles.Length; i++) {
-
-
-
- if (tiles[i] == null) continue;
-
- var nodes = tiles[i].nodes;
- for (int j = 0; j < nodes.Length; j++) {
- hasher.HashNode(nodes[j]);
- }
- hashedNodes += nodes.Length;
-
-
-
-
-
-
- if (hashedNodes > 1024 || (i % tileXCount) == tileXCount - 1 || i == tiles.Length - 1) {
- if (!gizmos.Draw(hasher)) {
- using (var helper = gizmos.GetGizmoHelper(active, hasher)) {
- if (showMeshSurface || showMeshOutline) {
- CreateNavmeshSurfaceVisualization(tiles, startTileIndex, i + 1, helper);
- CreateNavmeshOutlineVisualization(tiles, startTileIndex, i + 1, helper);
- }
- if (showNodeConnections) {
- for (int ti = startTileIndex; ti <= i; ti++) {
- if (tiles[ti] == null) continue;
- var tileNodes = tiles[ti].nodes;
- for (int j = 0; j < tileNodes.Length; j++) {
- helper.DrawConnections(tileNodes[j]);
- }
- }
- }
- }
- }
- gizmos.Draw(hasher);
- startTileIndex = i + 1;
- hasher = baseHasher;
- hashedNodes = 0;
- }
- }
- }
- if (active.showUnwalkableNodes) DrawUnwalkableNodes(active.unwalkableNodeDebugSize);
- }
-
- void CreateNavmeshSurfaceVisualization (NavmeshTile[] tiles, int startTile, int endTile, GraphGizmoHelper helper) {
- int numNodes = 0;
- for (int i = startTile; i < endTile; i++) if (tiles[i] != null) numNodes += tiles[i].nodes.Length;
-
- var vertices = ArrayPool<Vector3>.Claim(numNodes*3);
- var colors = ArrayPool<Color>.Claim(numNodes*3);
- int offset = 0;
- for (int i = startTile; i < endTile; i++) {
- var tile = tiles[i];
- if (tile == null) continue;
- for (int j = 0; j < tile.nodes.Length; j++) {
- var node = tile.nodes[j];
- Int3 v0, v1, v2;
- node.GetVertices(out v0, out v1, out v2);
- int index = offset + j*3;
- vertices[index + 0] = (Vector3)v0;
- vertices[index + 1] = (Vector3)v1;
- vertices[index + 2] = (Vector3)v2;
- var color = helper.NodeColor(node);
- colors[index + 0] = colors[index + 1] = colors[index + 2] = color;
- }
- offset += tile.nodes.Length * 3;
- }
- if (showMeshSurface) helper.DrawTriangles(vertices, colors, numNodes);
- if (showMeshOutline) helper.DrawWireTriangles(vertices, colors, numNodes);
-
- ArrayPool<Vector3>.Release(ref vertices);
- ArrayPool<Color>.Release(ref colors);
- }
-
- static void CreateNavmeshOutlineVisualization (NavmeshTile[] tiles, int startTile, int endTile, GraphGizmoHelper helper) {
- var sharedEdges = new bool[3];
- for (int i = startTile; i < endTile; i++) {
- var tile = tiles[i];
- if (tile == null) continue;
- for (int j = 0; j < tile.nodes.Length; j++) {
- sharedEdges[0] = sharedEdges[1] = sharedEdges[2] = false;
- var node = tile.nodes[j];
- for (int c = 0; c < node.connections.Length; c++) {
- var other = node.connections[c].node as TriangleMeshNode;
-
- if (other != null && other.GraphIndex == node.GraphIndex) {
- for (int v = 0; v < 3; v++) {
- for (int v2 = 0; v2 < 3; v2++) {
- if (node.GetVertexIndex(v) == other.GetVertexIndex((v2+1)%3) && node.GetVertexIndex((v+1)%3) == other.GetVertexIndex(v2)) {
-
- sharedEdges[v] = true;
- v = 3;
- break;
- }
- }
- }
- }
- }
- var color = helper.NodeColor(node);
- for (int v = 0; v < 3; v++) {
- if (!sharedEdges[v]) {
- helper.builder.DrawLine((Vector3)node.GetVertex(v), (Vector3)node.GetVertex((v+1)%3), color);
- }
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- protected override void SerializeExtraInfo (GraphSerializationContext ctx) {
- BinaryWriter writer = ctx.writer;
- if (tiles == null) {
- writer.Write(-1);
- return;
- }
- writer.Write(tileXCount);
- writer.Write(tileZCount);
- for (int z = 0; z < tileZCount; z++) {
- for (int x = 0; x < tileXCount; x++) {
- NavmeshTile tile = tiles[x + z*tileXCount];
- if (tile == null) {
- throw new System.Exception("NULL Tile");
-
-
- }
- writer.Write(tile.x);
- writer.Write(tile.z);
- if (tile.x != x || tile.z != z) continue;
- writer.Write(tile.w);
- writer.Write(tile.d);
- writer.Write(tile.tris.Length);
- for (int i = 0; i < tile.tris.Length; i++) writer.Write(tile.tris[i]);
- writer.Write(tile.verts.Length);
- for (int i = 0; i < tile.verts.Length; i++) {
- ctx.SerializeInt3(tile.verts[i]);
- }
- writer.Write(tile.vertsInGraphSpace.Length);
- for (int i = 0; i < tile.vertsInGraphSpace.Length; i++) {
- ctx.SerializeInt3(tile.vertsInGraphSpace[i]);
- }
- writer.Write(tile.nodes.Length);
- for (int i = 0; i < tile.nodes.Length; i++) {
- tile.nodes[i].SerializeNode(ctx);
- }
- }
- }
- }
- protected override void DeserializeExtraInfo (GraphSerializationContext ctx) {
- BinaryReader reader = ctx.reader;
- tileXCount = reader.ReadInt32();
- if (tileXCount < 0) return;
- tileZCount = reader.ReadInt32();
- transform = CalculateTransform();
- tiles = new NavmeshTile[tileXCount * tileZCount];
-
- TriangleMeshNode.SetNavmeshHolder((int)ctx.graphIndex, this);
- for (int z = 0; z < tileZCount; z++) {
- for (int x = 0; x < tileXCount; x++) {
- int tileIndex = x + z*tileXCount;
- int tx = reader.ReadInt32();
- if (tx < 0) throw new System.Exception("Invalid tile coordinates (x < 0)");
- int tz = reader.ReadInt32();
- if (tz < 0) throw new System.Exception("Invalid tile coordinates (z < 0)");
-
- if (tx != x || tz != z) {
- tiles[tileIndex] = tiles[tz*tileXCount + tx];
- continue;
- }
- var tile = tiles[tileIndex] = new NavmeshTile {
- x = tx,
- z = tz,
- w = reader.ReadInt32(),
- d = reader.ReadInt32(),
- bbTree = ObjectPool<BBTree>.Claim(),
- graph = this,
- };
- int trisCount = reader.ReadInt32();
- if (trisCount % 3 != 0) throw new System.Exception("Corrupt data. Triangle indices count must be divisable by 3. Read " + trisCount);
- tile.tris = new int[trisCount];
- for (int i = 0; i < tile.tris.Length; i++) tile.tris[i] = reader.ReadInt32();
- tile.verts = new Int3[reader.ReadInt32()];
- for (int i = 0; i < tile.verts.Length; i++) {
- tile.verts[i] = ctx.DeserializeInt3();
- }
- if (ctx.meta.version.Major >= 4) {
- tile.vertsInGraphSpace = new Int3[reader.ReadInt32()];
- if (tile.vertsInGraphSpace.Length != tile.verts.Length) throw new System.Exception("Corrupt data. Array lengths did not match");
- for (int i = 0; i < tile.verts.Length; i++) {
- tile.vertsInGraphSpace[i] = ctx.DeserializeInt3();
- }
- } else {
-
- tile.vertsInGraphSpace = new Int3[tile.verts.Length];
- tile.verts.CopyTo(tile.vertsInGraphSpace, 0);
- transform.InverseTransform(tile.vertsInGraphSpace);
- }
- int nodeCount = reader.ReadInt32();
- tile.nodes = new TriangleMeshNode[nodeCount];
-
- tileIndex <<= TileIndexOffset;
- for (int i = 0; i < tile.nodes.Length; i++) {
- var node = new TriangleMeshNode(active);
- tile.nodes[i] = node;
- node.DeserializeNode(ctx);
- node.v0 = tile.tris[i*3+0] | tileIndex;
- node.v1 = tile.tris[i*3+1] | tileIndex;
- node.v2 = tile.tris[i*3+2] | tileIndex;
- node.UpdatePositionFromVertices();
- }
- tile.bbTree.RebuildFrom(tile.nodes);
- }
- }
- }
- protected override void PostDeserialization (GraphSerializationContext ctx) {
-
- if (ctx.meta.version < AstarSerializer.V4_1_0 && tiles != null) {
- Dictionary<TriangleMeshNode, Connection[]> conns = tiles.SelectMany(s => s.nodes).ToDictionary(n => n, n => n.connections ?? new Connection[0]);
-
-
-
- foreach (var tile in tiles) CreateNodeConnections(tile.nodes);
- foreach (var tile in tiles) ConnectTileWithNeighbours(tile);
-
- GetNodes(node => {
- var triNode = node as TriangleMeshNode;
- foreach (var conn in conns[triNode].Where(conn => !triNode.ContainsConnection(conn.node)).ToList()) {
- triNode.AddConnection(conn.node, conn.cost, conn.shapeEdge);
- }
- });
- }
-
-
-
- transform = CalculateTransform();
- }
- }
- }
|