A* Pathfinding Project
4.1.7
The A* Pathfinding Project for Unity 3D
|
Base class for RecastGraph and NavMeshGraph. More...
Base class for RecastGraph and NavMeshGraph.
Public Member Functions | |
abstract GraphTransform | CalculateTransform () |
Returns a new transform which transforms graph space to world space. More... | |
void | EndBatchTileUpdate () |
End batch updating of tiles. More... | |
override NNInfoInternal | GetNearest (Vector3 position, NNConstraint constraint, GraphNode hint) |
Returns the nearest node to a position using the specified NNConstraint. More... | |
override NNInfoInternal | GetNearestForce (Vector3 position, NNConstraint constraint) |
Returns the nearest node to a position using the specified constraint . More... | |
override void | GetNodes (System.Action< GraphNode > action) |
Calls a delegate with all nodes in the graph. More... | |
NavmeshTile | GetTile (int x, int z) |
Tile at the specified x, z coordinate pair. More... | |
Bounds | GetTileBounds (IntRect rect) |
Returns an XZ bounds object with the bounds of a group of tiles. More... | |
Bounds | GetTileBounds (int x, int z, int width=1, int depth=1) |
Returns an XZ bounds object with the bounds of a group of tiles. More... | |
Bounds | GetTileBoundsInGraphSpace (IntRect rect) |
Bounds | GetTileBoundsInGraphSpace (int x, int z, int width=1, int depth=1) |
Returns an XZ bounds object with the bounds of a group of tiles in graph space. More... | |
void | GetTileCoordinates (int tileIndex, out int x, out int z) |
Tile coordinates from a tile index. More... | |
Int2 | GetTileCoordinates (Vector3 position) |
Returns the tile coordinate which contains the specified position. More... | |
NavmeshTile [] | GetTiles () |
All tiles. More... | |
IntRect | GetTouchingTiles (Bounds bounds) |
Returns a rect containing the indices of all tiles touching the specified bounds. More... | |
IntRect | GetTouchingTilesInGraphSpace (Rect rect) |
Returns a rect containing the indices of all tiles touching the specified bounds. More... | |
IntRect | GetTouchingTilesRound (Bounds bounds) |
Returns a rect containing the indices of all tiles by rounding the specified bounds to tile borders. More... | |
Int3 | GetVertex (int index) |
Vertex coordinate for the specified vertex index. More... | |
int | GetVertexArrayIndex (int index) |
Int3 | GetVertexInGraphSpace (int index) |
Vertex coordinate in graph space for the specified vertex index. More... | |
bool | Linecast (Vector3 origin, Vector3 end) |
Returns if there is an obstacle between origin and end on the graph. More... | |
bool | Linecast (Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit) |
Returns if there is an obstacle between origin and end on the graph. More... | |
bool | Linecast (Vector3 origin, Vector3 end, GraphNode hint) |
Returns if there is an obstacle between origin and end on the graph. More... | |
bool | Linecast (Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List< GraphNode > trace) |
Returns if there is an obstacle between origin and end on the graph. More... | |
override void | OnDrawGizmos (Pathfinding.Util.RetainedGizmos gizmos, bool drawNodes) |
GraphNode | PointOnNavmesh (Vector3 position, NNConstraint constraint) |
Finds the first node which contains position. More... | |
override void | RelocateNodes (Matrix4x4 deltaMatrix) |
Moves the nodes in this graph. More... | |
void | RelocateNodes (GraphTransform newTransform) |
Moves the nodes in this graph. More... | |
void | ReplaceTile (int x, int z, Int3[] verts, int[] tris) |
Replace tile at index with nodes created from specified navmesh. More... | |
void | ReplaceTile (int x, int z, int w, int d, Int3[] verts, int[] tris) |
Replaces a tile with a new mesh. More... | |
void | StartBatchTileUpdate () |
Start batch updating of tiles. More... | |
Public Member Functions inherited from NavGraph | |
virtual int | CountNodes () |
Number of nodes in the graph. More... | |
NNInfoInternal | GetNearest (Vector3 position) |
Returns the nearest node to a position using the default NNConstraint. More... | |
NNInfoInternal | GetNearest (Vector3 position, NNConstraint constraint) |
Returns the nearest node to a position using the specified NNConstraint. More... | |
void | GetNodes (System.Func< GraphNode, bool > action) |
Calls a delegate with all nodes in the graph until the delegate returns false. More... | |
virtual void | OnDrawGizmos (RetainedGizmos gizmos, bool drawNodes) |
Draw gizmos for the graph. More... | |
void | RelocateNodes (Matrix4x4 oldMatrix, Matrix4x4 newMatrix) |
Moves nodes in this graph. More... | |
void | Scan () |
Scan the graph. More... | |
void | ScanGraph () |
Scan the graph. More... | |
void | SetMatrix (Matrix4x4 m) |
Use to set both matrix and inverseMatrix at the same time. More... | |
Static Public Member Functions | |
static int | GetTileIndex (int index) |
Tile index from a vertex index. More... | |
static bool | Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit) |
Returns if there is an obstacle between origin and end on the graph. More... | |
static bool | Linecast (NavmeshBase graph, Vector3 origin, Vector3 end, GraphNode hint, out GraphHitInfo hit, List< GraphNode > trace) |
Returns if there is an obstacle between origin and end on the graph. More... | |
Public Attributes | |
Vector3 | forcedBoundsSize = new Vector3(100, 40, 100) |
Size of the bounding box. More... | |
bool | nearestSearchOnlyXZ |
Perform nearest node searches in XZ space only. More... | |
System.Action< NavmeshTile[]> | OnRecalculatedTiles |
Called when tiles have been completely recalculated. More... | |
bool | showMeshOutline = true |
Show an outline of the polygons in the Unity Editor. More... | |
bool | showMeshSurface |
Show the surface of the navmesh. More... | |
bool | showNodeConnections |
Show the connections between the polygons in the Unity Editor. More... | |
const int | TileIndexMask = 0x7FFFF |
const int | TileIndexOffset = 12 |
int | tileXCount |
Number of tiles along the X-axis. More... | |
int | tileZCount |
Number of tiles along the Z-axis. More... | |
GraphTransform | transform = new GraphTransform(Matrix4x4.identity) |
Determines how the graph transforms graph space to world space. More... | |
const int | VertexIndexMask = 0xFFF |
Public Attributes inherited from NavGraph | |
AstarPath | active |
Reference to the AstarPath object in the scene. More... | |
bool | drawGizmos = true |
Enable to draw gizmos in the Unity scene view. More... | |
uint | graphIndex |
Index of the graph, used for identification purposes. More... | |
Guid | guid |
Used as an ID of the graph, considered to be unique. More... | |
bool | infoScreenOpen |
Used in the editor to check if the info screen is open. More... | |
uint | initialPenalty |
Default penalty to apply to all nodes. More... | |
Matrix4x4 | inverseMatrix = Matrix4x4.identity |
Inverse of matrix. More... | |
Matrix4x4 | matrix = Matrix4x4.identity |
A matrix for translating/rotating/scaling the graph. More... | |
string | name |
Name of the graph. More... | |
bool | open |
Is the graph open in the editor. More... | |
Protected Member Functions | |
void | ClearTiles (int x, int z, int w, int d) |
Clear all tiles within the rectangle with one corner at (x,z), width w and depth d. More... | |
void | ConnectTiles (NavmeshTile tile1, NavmeshTile tile2) |
Generate connections between the two tiles. More... | |
void | ConnectTileWithNeighbours (NavmeshTile tile, bool onlyUnflagged=false) |
TriangleMeshNode [] | CreateNodes (int[] tris, int tileIndex, uint graphIndex) |
override void | DeserializeExtraInfo (GraphSerializationContext ctx) |
Deserializes graph type specific node data. More... | |
void | FillWithEmptyTiles () |
Fills graph with tiles created by NewEmptyTile. More... | |
NavmeshTile | NewEmptyTile (int x, int z) |
Creates a single new empty tile. More... | |
override void | OnDestroy () |
Function for cleaning up references. More... | |
override void | PostDeserialization (GraphSerializationContext ctx) |
Called after all deserialization has been done for all graphs. More... | |
void | RemoveConnectionsFromTile (NavmeshTile tile) |
void | RemoveConnectionsFromTo (NavmeshTile a, NavmeshTile b) |
override void | SerializeExtraInfo (GraphSerializationContext ctx) |
Serializes Node Info. More... | |
Protected Member Functions inherited from NavGraph | |
virtual void | DeserializeSettingsCompatibility (GraphSerializationContext ctx) |
An old format for serializing settings. More... | |
virtual void | DestroyAllNodes () |
Destroys all nodes in the graph. More... | |
void | DrawUnwalkableNodes (float size) |
abstract IEnumerable< Progress > | ScanInternal () |
Internal method to scan the graph. More... | |
Static Protected Member Functions | |
static void | CreateNodeConnections (TriangleMeshNode[] nodes) |
Create connections between all nodes. More... | |
Protected Attributes | |
NavmeshTile [] | tiles |
All tiles. More... | |
Properties | |
abstract float | MaxTileConnectionEdgeDistance [get] |
Maximum (vertical) distance between the sides of two nodes for them to be connected across a tile edge. More... | |
abstract bool | RecalculateNormals [get] |
Determines how normals are calculated. More... | |
abstract float | TileWorldSizeX [get] |
Size of a tile in world units along the X axis. More... | |
abstract float | TileWorldSizeZ [get] |
Size of a tile in world units along the Z axis. More... | |
GraphTransform ITransformedGraph. | transform [get] |
Properties inherited from NavGraph | |
bool | exists [get] |
True if the graph exists, false if it has been destroyed. More... | |
Properties inherited from ITransformedGraph | |
GraphTransform | transform [get] |
Private Member Functions | |
void | CreateNavmeshSurfaceVisualization (NavmeshTile tile, GraphGizmoHelper helper) |
Creates a mesh of the surfaces of the navmesh for use in OnDrawGizmos in the editor. More... | |
Static Private Member Functions | |
static | NavmeshBase () |
static void | CreateNavmeshOutlineVisualization (NavmeshTile tile, GraphGizmoHelper helper) |
Creates an outline of the navmesh for use in OnDrawGizmos in the editor. More... | |
Private Attributes | |
bool | batchTileUpdate |
Currently updating tiles in a batch. More... | |
List< int > | batchUpdatedTiles = new List<int>() |
List of tiles updating during batch. More... | |
Static Private Attributes | |
static readonly byte [] | LinecastShapeEdgeLookup |
Used to optimize linecasts by precomputing some values. More... | |
static readonly NNConstraint | NNConstraintDistanceXZ = new NNConstraint { distanceXZ = true } |
static readonly NNConstraint | NNConstraintNone = NNConstraint.None |
Cached NNConstraint.None to reduce allocations. More... | |
|
staticprivate |
|
pure virtual |
Returns a new transform which transforms graph space to world space.
Does not update the transform field.
Implemented in RecastGraph, and NavMeshGraph.
|
protected |
Clear all tiles within the rectangle with one corner at (x,z), width w and depth d.
|
protected |
Generate connections between the two tiles.
The tiles must be adjacent.
|
protected |
|
staticprivate |
Creates an outline of the navmesh for use in OnDrawGizmos in the editor.
|
private |
Creates a mesh of the surfaces of the navmesh for use in OnDrawGizmos in the editor.
|
staticprotected |
Create connections between all nodes.
|
protected |
|
protectedvirtual |
void EndBatchTileUpdate | ( | ) |
End batch updating of tiles.
During batch updating, tiles will not be connected if they are updating with ReplaceTile. When ending batching, all affected tiles will be connected. This is faster than not using batching.
|
protected |
Fills graph with tiles created by NewEmptyTile.
|
virtual |
Returns the nearest node to a position using the specified NNConstraint.
position | The position to try to find a close node to |
hint | Can be passed to enable some graph generators to find the nearest node faster. |
constraint | Can for example tell the function to try to return a walkable node. If you do not get a good node back, consider calling GetNearestForce. |
Reimplemented from NavGraph.
|
virtual |
Returns the nearest node to a position using the specified constraint .
Reimplemented from NavGraph.
|
virtual |
Calls a delegate with all nodes in the graph.
This is the primary way of iterating through all nodes in a graph.
Do not change the graph structure inside the delegate.
If you want to store all nodes in a list you can do this
Implements NavGraph.
NavmeshTile GetTile | ( | int | x, |
int | z | ||
) |
Tile at the specified x, z coordinate pair.
The first tile is at (0,0), the last tile at (tileXCount-1, tileZCount-1).
Bounds GetTileBounds | ( | IntRect | rect | ) |
Returns an XZ bounds object with the bounds of a group of tiles.
The bounds object is defined in world units.
Bounds GetTileBounds | ( | int | x, |
int | z, | ||
int | width = 1 , |
||
int | depth = 1 |
||
) |
Returns an XZ bounds object with the bounds of a group of tiles.
The bounds object is defined in world units.
Bounds GetTileBoundsInGraphSpace | ( | IntRect | rect | ) |
Bounds GetTileBoundsInGraphSpace | ( | int | x, |
int | z, | ||
int | width = 1 , |
||
int | depth = 1 |
||
) |
Returns an XZ bounds object with the bounds of a group of tiles in graph space.
void GetTileCoordinates | ( | int | tileIndex, |
out int | x, | ||
out int | z | ||
) |
Tile coordinates from a tile index.
Implements INavmeshHolder.
Int2 GetTileCoordinates | ( | Vector3 | position | ) |
Returns the tile coordinate which contains the specified position.
It is not necessarily a valid tile (i.e it could be out of bounds).
|
static |
Tile index from a vertex index.
NavmeshTile [] GetTiles | ( | ) |
All tiles.
IntRect GetTouchingTiles | ( | Bounds | bounds | ) |
Returns a rect containing the indices of all tiles touching the specified bounds.
IntRect GetTouchingTilesInGraphSpace | ( | Rect | rect | ) |
Returns a rect containing the indices of all tiles touching the specified bounds.
rect | Graph space rectangle (in graph space all tiles are on the XZ plane regardless of graph rotation and other transformations, the first tile has a corner at the origin) |
IntRect GetTouchingTilesRound | ( | Bounds | bounds | ) |
Returns a rect containing the indices of all tiles by rounding the specified bounds to tile borders.
This is different from GetTouchingTiles in that the tiles inside the rectangle returned from this method may not contain the whole bounds, while that is guaranteed for GetTouchingTiles.
Int3 GetVertex | ( | int | index | ) |
Vertex coordinate for the specified vertex index.
IndexOutOfRangeException | if the vertex index is invalid. |
NullReferenceException | if the tile the vertex is in is not calculated. |
Implements INavmeshHolder.
int GetVertexArrayIndex | ( | int | index | ) |
Implements INavmeshHolder.
Int3 GetVertexInGraphSpace | ( | int | index | ) |
Vertex coordinate in graph space for the specified vertex index.
Implements INavmeshHolder.
bool Linecast | ( | Vector3 | origin, |
Vector3 | end | ||
) |
Returns if there is an obstacle between origin and end on the graph.
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions instead of checking for collider intersection.
Implements IRaycastableGraph.
bool Linecast | ( | Vector3 | origin, |
Vector3 | end, | ||
GraphNode | hint, | ||
out GraphHitInfo | hit | ||
) |
Returns if there is an obstacle between origin and end on the graph.
[in] | origin | Point to linecast from |
[in] | end | Point to linecast to |
[out] | hit | Contains info on what was hit, see GraphHitInfo |
[in] | hint | You need to pass the node closest to the start point |
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions instead of checking for collider intersection.
Implements IRaycastableGraph.
bool Linecast | ( | Vector3 | origin, |
Vector3 | end, | ||
GraphNode | hint | ||
) |
Returns if there is an obstacle between origin and end on the graph.
[in] | origin | Point to linecast from |
[in] | end | Point to linecast to |
[in] | hint | You need to pass the node closest to the start point |
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions instead of checking for collider intersection.
Implements IRaycastableGraph.
bool Linecast | ( | Vector3 | origin, |
Vector3 | end, | ||
GraphNode | hint, | ||
out GraphHitInfo | hit, | ||
List< GraphNode > | trace | ||
) |
Returns if there is an obstacle between origin and end on the graph.
[in] | origin | Point to linecast from |
[in] | end | Point to linecast to |
[out] | hit | Contains info on what was hit, see GraphHitInfo |
[in] | hint | You need to pass the node closest to the start point |
trace | If a list is passed, then it will be filled with all nodes the linecast traverses |
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions instead of checking for collider intersection.
Implements IRaycastableGraph.
|
static |
Returns if there is an obstacle between origin and end on the graph.
[in] | graph | The graph to perform the search on |
[in] | origin | Point to start from |
[in] | end | Point to linecast to |
[out] | hit | Contains info on what was hit, see GraphHitInfo |
[in] | hint | You need to pass the node closest to the start point, if null, a search for the closest node will be done |
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions instead of checking for collider intersection.
|
static |
Returns if there is an obstacle between origin and end on the graph.
[in] | graph | The graph to perform the search on |
[in] | origin | Point to start from. This point should be on the navmesh. It will be snapped to the closest point on the navmesh otherwise. |
[in] | end | Point to linecast to |
[out] | hit | Contains info on what was hit, see GraphHitInfo |
[in] | hint | If you already know the node which contains the origin point, you may pass it here for slighly improved performance. If null, a search for the closest node will be done. |
trace | If a list is passed, then it will be filled with all nodes along the line up until it hits an obstacle or reaches the end. |
This is not the same as Physics.Linecast, this function traverses the graph and looks for collisions instead of checking for collider intersections.
|
protected |
Creates a single new empty tile.
|
protectedvirtual |
Function for cleaning up references.
This will be called on the same time as OnDisable on the gameObject which the AstarPath script is attached to (remember, not in the editor). Use for any cleanup code such as cleaning up static variables which otherwise might prevent resources from being collected. Use by creating a function overriding this one in a graph class, but always call base.OnDestroy () in that function. All nodes should be destroyed in this function otherwise a memory leak will arise.
Reimplemented from NavGraph.
override void OnDrawGizmos | ( | Pathfinding.Util.RetainedGizmos | gizmos, |
bool | drawNodes | ||
) |
GraphNode PointOnNavmesh | ( | Vector3 | position, |
NNConstraint | constraint | ||
) |
Finds the first node which contains position.
"Contains" is defined as position is inside the triangle node when seen from above. So only XZ space matters. In case of a multilayered environment, which node of the possibly several nodes containing the point is undefined.
Returns null if there was no node containing the point. This serves as a quick check for "is this point on the navmesh or not".
Note that the behaviour of this method is distinct from the GetNearest method. The GetNearest method will return the closest node to a point, which is not necessarily the one which contains it in XZ space.
|
protectedvirtual |
Called after all deserialization has been done for all graphs.
Can be used to set up more graph data which is not serialized
Reimplemented from NavGraph.
|
virtual |
Moves the nodes in this graph.
Multiplies all node positions by deltaMatrix.
For example if you want to move all your nodes in e.g a point graph 10 units along the X axis from the initial position
Reimplemented from NavGraph.
void RelocateNodes | ( | GraphTransform | newTransform | ) |
Moves the nodes in this graph.
Moves all the nodes in such a way that the specified transform is the new graph space to world space transformation for the graph. You usually use this together with the CalculateTransform method.
So for example if you want to move and rotate all your nodes in e.g a recast graph you can do
This will move all the nodes to new positions as if the new graph settings had been there from the start.
|
protected |
|
protected |
void ReplaceTile | ( | int | x, |
int | z, | ||
Int3 [] | verts, | ||
int [] | tris | ||
) |
Replace tile at index with nodes created from specified navmesh.
void ReplaceTile | ( | int | x, |
int | z, | ||
int | w, | ||
int | d, | ||
Int3 [] | verts, | ||
int [] | tris | ||
) |
Replaces a tile with a new mesh.
This will create new nodes and link them to the adjacent tile (unless batching has been started in which case that will be done when batching ends).
The vertices are assumed to be in 'tile space', that is being in a rectangle with one corner at the origin and one at (TileWorldSizeX, 0, TileWorldSizeZ).
|
protectedvirtual |
Serializes Node Info.
Should serialize:
It is not necessary for this implementation to be forward or backwards compatible.
Reimplemented from NavGraph.
void StartBatchTileUpdate | ( | ) |
Start batch updating of tiles.
During batch updating, tiles will not be connected if they are updating with ReplaceTile. When ending batching, all affected tiles will be connected. This is faster than not using batching.
|
private |
Currently updating tiles in a batch.
|
private |
List of tiles updating during batch.
Vector3 forcedBoundsSize = new Vector3(100, 40, 100) |
Size of the bounding box.
|
staticprivate |
Used to optimize linecasts by precomputing some values.
bool nearestSearchOnlyXZ |
Perform nearest node searches in XZ space only.
Recomended for single-layered environments. Faster but can be inaccurate esp. in multilayered contexts. You should not use this if the graph is rotated since then the XZ plane no longer corresponds to the ground plane.
This can be important on sloped surfaces. See the image below:
You can also control this using a field on an NNConstraint object.
|
staticprivate |
|
staticprivate |
Cached NNConstraint.None to reduce allocations.
System.Action<NavmeshTile[]> OnRecalculatedTiles |
Called when tiles have been completely recalculated.
This is called after scanning the graph and after performing graph updates that completely recalculate tiles (not ones that simply modify e.g penalties). It is not called after NavmeshCut updates.
bool showMeshOutline = true |
Show an outline of the polygons in the Unity Editor.
bool showMeshSurface |
Show the surface of the navmesh.
bool showNodeConnections |
Show the connections between the polygons in the Unity Editor.
const int TileIndexMask = 0x7FFFF |
const int TileIndexOffset = 12 |
|
protected |
All tiles.
int tileXCount |
Number of tiles along the X-axis.
int tileZCount |
Number of tiles along the Z-axis.
GraphTransform transform = new GraphTransform(Matrix4x4.identity) |
Determines how the graph transforms graph space to world space.
const int VertexIndexMask = 0xFFF |
|
getprotected |
Maximum (vertical) distance between the sides of two nodes for them to be connected across a tile edge.
When tiles are connected to each other, the nodes sometimes do not line up perfectly so some allowance must be made to allow tiles that do not match exactly to be connected with each other.
|
getprotected |
Determines how normals are calculated.
Disable for spherical graphs or other complicated surfaces that allow the agents to e.g walk on walls or ceilings.
By default the normals of the mesh will be flipped so that they point as much as possible in the upwards direction. The normals are important when connecting adjacent nodes. Two adjacent nodes will only be connected if they are oriented the same way. This is particularly important if you have a navmesh on the walls or even on the ceiling of a room. Or if you are trying to make a spherical navmesh. If you do one of those things then you should set disable this setting and make sure the normals in your source mesh are properly set.
If you for example take a look at the image below. In the upper case then the nodes on the bottom half of the mesh haven't been connected with the nodes on the upper half because the normals on the lower half will have been modified to point inwards (as that is the direction that makes them face upwards the most) while the normals on the upper half point outwards. This causes the nodes to not connect properly along the seam. When this option is set to false instead the nodes are connected properly as in the original mesh all normals point outwards.
The default value of this field is true to reduce the risk for errors in the common case. If a mesh is supplied that has all normals pointing downwards and this option is false, then some methods like PointOnNavmesh will not work correctly as they assume that the normals point upwards. For a more complicated surface like a spherical graph those methods make no sense anyway as there is no clear definition of what it means to be "inside" a triangle when there is no clear up direction.
|
get |
Size of a tile in world units along the X axis.
|
get |
Size of a tile in world units along the Z axis.
|
getprivate |