A* Pathfinding Project  4.0.8
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
TileHandler Class Reference

Utility class for updating tiles of navmesh/recast graphs. More...

Detailed Description

Utility class for updating tiles of navmesh/recast graphs.

Primarily used by the TileHandlerHelper for navmesh cutting.

Most operations that this class does are asynchronous. They will be added as work items to the AstarPath class and executed when the pathfinding threads have finished calculating their current paths.

See Also
TileHandlerHelper

Classes

class  Cut
 Internal class describing a single NavmeshCut. More...
 
struct  CuttingResult
 Internal class representing a mesh which is the result of the CutPoly method. More...
 
class  TileType
 A template for a single tile in a navmesh/recast graph. More...
 

Public Types

enum  CutMode { CutAll = 1, CutDual = 2, CutExtra = 4 }
 

Public Member Functions

 TileHandler (NavmeshBase graph)
 
void ClearTile (int x, int z)
 Clear the tile at the specified tile coordinates.
 
void CreateTileTypesFromGraph ()
 
void EndBatchLoad ()
 
int GetActiveRotation (Int2 p)
 Rotation of the specified tile relative to the original rotation of the tile type.
 
TileType GetTileType (int index)
 
int GetTileTypeCount ()
 
void LoadTile (TileType tile, int x, int z, int rotation, int yoffset)
 Load a tile at tile coordinate x, z.
 
void OnRecalculatedTiles (NavmeshTile[] recalculatedTiles)
 Call to update the specified tiles with new information based on the navmesh/recast graph.
 
TileType RegisterTileType (Mesh source, Int3 centerOffset, int width=1, int depth=1)
 Register that a tile can be loaded from source.
 
void ReloadInBounds (Bounds bounds)
 Reloads all tiles intersecting with the specified bounds.
 
void ReloadInBounds (IntRect tiles)
 Reloads all tiles specified by the rectangle.
 
void ReloadTile (int x, int z)
 Reload tile at tile coordinate.
 
bool StartBatchLoad ()
 Start batch loading.
 

Public Attributes

readonly GridLookup
< NavmeshClipper
cuts
 NavmeshCut and NavmeshAdd components registered to this tile handler.
 
readonly NavmeshBase graph
 The underlaying graph which is handled by this instance.
 

Properties

bool isValid [get]
 True if the tile handler still has the same number of tiles and tile layout as the graph.
 

Private Member Functions

int ClipAgainstRectangle (Int3[] clipIn, Int3[] clipOut, Int2 size)
 Clips the input polygon against a rectangle with one corner at the origin and one at size in XZ space.
 
void CutAll (List< IntPoint > poly, List< int > intersectingCutIndices, List< Cut > cuts, Pathfinding.ClipperLib.PolyTree result)
 
void CutDual (List< IntPoint > poly, List< int > tmpIntersectingCuts, List< Cut > cuts, bool hasDual, List< List< IntPoint > > intermediateResult, Pathfinding.ClipperLib.PolyTree result)
 
void CutExtra (List< IntPoint > poly, List< IntPoint > extraClipShape, Pathfinding.ClipperLib.PolyTree result)
 
CuttingResult CutPoly (Int3[] verts, int[] tris, Int3[] extraShape, GraphTransform graphTransform, IntRect tiles, CutMode mode=CutMode.CutAll|CutMode.CutDual, int perturbate=-1)
 Cuts a piece of navmesh using navmesh cuts.
 
void DelaunayRefinement (Int3[] verts, int[] tris, ref int tCount, bool delaunay, bool colinear)
 Refine a mesh using delaunay refinement.
 
void UpdateTileType (NavmeshTile tile)
 

Static Private Member Functions

static void CopyMesh (Int3[] vertices, int[] triangles, List< Int3 > outVertices, List< int > outTriangles)
 Copy mesh from (vertices, triangles) to (outVertices, outTriangles)
 
static void PoolPolygon (Poly2Tri.Polygon polygon, Stack< Poly2Tri.Polygon > pool)
 
static List< CutPrepareNavmeshCutsForCutting (List< NavmeshCut > navmeshCuts, GraphTransform transform, IntRect cutSpaceBounds, int perturbate, bool anyNavmeshAdds)
 Generates a list of cuts from the navmesh cut components.
 
static int SampleYCoordinateInTriangle (Int3 p1, Int3 p2, Int3 p3, Int3 p)
 Sample Y coordinate of the triangle (p1, p2, p3) at the point p in XZ space.
 

Private Attributes

readonly int[] activeTileOffsets
 Offsets along the Y axis of the active tiles.
 
readonly int[] activeTileRotations
 Rotations of the active tiles.
 
readonly TileType[] activeTileTypes
 Which tile type is active on each tile index.
 
readonly Dictionary< Int2, int > cached_Int2_int_dict = new Dictionary<Int2, int>()
 Cached dictionary to avoid excessive allocations.
 
readonly Clipper clipper = new Clipper()
 Handles polygon clipping operations.
 
bool isBatching
 True while batching tile updates.
 
readonly bool[] reloadedInBatch
 A flag for each tile that is set to true if it has been reloaded while batching is in progress.
 
readonly
Pathfinding.Voxels.VoxelPolygonClipper 
simpleClipper
 Utility for clipping polygons to rectangles.
 
readonly int tileXCount
 Number of tiles along the x axis.
 
readonly int tileZCount
 Number of tiles along the z axis.
 

Member Enumeration Documentation

enum CutMode
Enumerator:
CutAll 

Cut holes in the navmesh.

CutDual 

Cut the navmesh but do not remove the interior of the cuts.

CutExtra 

Also cut using the extra shape that was provided.

Constructor & Destructor Documentation

Member Function Documentation

void ClearTile ( int  x,
int  z 
)

Clear the tile at the specified tile coordinates.

int ClipAgainstRectangle ( Int3[]  clipIn,
Int3[]  clipOut,
Int2  size 
)
private

Clips the input polygon against a rectangle with one corner at the origin and one at size in XZ space.

Parameters
clipInInput vertices
clipOutOutput vertices. This buffer must be large enough to contain all output vertices.
sizeThe clipping rectangle has one corner at the origin and one at this position in XZ space.
Returns
Number of output vertices
static void CopyMesh ( Int3[]  vertices,
int[]  triangles,
List< Int3 outVertices,
List< int >  outTriangles 
)
staticprivate

Copy mesh from (vertices, triangles) to (outVertices, outTriangles)

void CreateTileTypesFromGraph ( )
void CutAll ( List< IntPoint >  poly,
List< int >  intersectingCutIndices,
List< Cut cuts,
Pathfinding.ClipperLib.PolyTree  result 
)
private
void CutDual ( List< IntPoint >  poly,
List< int >  tmpIntersectingCuts,
List< Cut cuts,
bool  hasDual,
List< List< IntPoint > >  intermediateResult,
Pathfinding.ClipperLib.PolyTree  result 
)
private
void CutExtra ( List< IntPoint >  poly,
List< IntPoint >  extraClipShape,
Pathfinding.ClipperLib.PolyTree  result 
)
private
CuttingResult CutPoly ( Int3[]  verts,
int[]  tris,
Int3[]  extraShape,
GraphTransform  graphTransform,
IntRect  tiles,
CutMode  mode = CutMode.CutAll | CutMode.CutDual,
int  perturbate = -1 
)
private

Cuts a piece of navmesh using navmesh cuts.

Note
I am sorry for the really messy code in this method. It really needs to be refactored.
Parameters
vertsVertices that are going to be cut. Should be in graph space.
trisTriangles describing a mesh using the vertices.
extraShapeIf supplied the resulting mesh will be the intersection of the input mesh and this mesh.
graphTransformTransform mapping graph space to world space.
See Also
NavmeshBase.transform
Parameters
tilesTiles in the recast graph which the mesh covers.
mode
See Also
CutMode
Parameters
perturbateMove navmesh cuts around randomly a bit, the larger the value the more they are moved around. Used to prevent edge cases that can cause the clipping to fail.
void DelaunayRefinement ( Int3[]  verts,
int[]  tris,
ref int  tCount,
bool  delaunay,
bool  colinear 
)
private

Refine a mesh using delaunay refinement.

Loops through all pairs of neighbouring triangles and check if it would be better to flip the diagonal joining them using the delaunay criteria.

Does not require triangles to be clockwise, triangles will be checked for if they are clockwise and made clockwise if not. The resulting mesh will have all triangles clockwise.

See Also
https://en.wikipedia.org/wiki/Delaunay_triangulation
void EndBatchLoad ( )
int GetActiveRotation ( Int2  p)

Rotation of the specified tile relative to the original rotation of the tile type.

A result N means that the tile has been rotated N*90 degrees.

TileType GetTileType ( int  index)
int GetTileTypeCount ( )
void LoadTile ( TileType  tile,
int  x,
int  z,
int  rotation,
int  yoffset 
)

Load a tile at tile coordinate x, z.

Parameters
tileTile type to load
xTile x coordinate (first tile is at (0,0), second at (1,0) etc.. ).
zTile z coordinate.
rotationRotate tile by 90 degrees * value.
yoffsetOffset Y coordinates by this amount. In Int3 space, so if you have a world space offset, multiply by Int3.Precision and round to the nearest integer before calling this function.
void OnRecalculatedTiles ( NavmeshTile[]  recalculatedTiles)

Call to update the specified tiles with new information based on the navmesh/recast graph.

This is usually called right after a navmesh/recast graph has recalculated some tiles and thus some calculations need to be done to take navmesh cutting into account as well.

Will reload all tiles in the list.

static void PoolPolygon ( Poly2Tri.Polygon  polygon,
Stack< Poly2Tri.Polygon >  pool 
)
staticprivate
static List<Cut> PrepareNavmeshCutsForCutting ( List< NavmeshCut navmeshCuts,
GraphTransform  transform,
IntRect  cutSpaceBounds,
int  perturbate,
bool  anyNavmeshAdds 
)
staticprivate

Generates a list of cuts from the navmesh cut components.

Each cut has a single contour (NavmeshCut components may contain multiple).

transform should transform a point from cut space to world space.

TileType RegisterTileType ( Mesh  source,
Int3  centerOffset,
int  width = 1,
int  depth = 1 
)

Register that a tile can be loaded from source.

Parameters
centerOffsetAssumes that the mesh has its pivot point at the center of the tile. If it has not, you can supply a non-zero centerOffset to offset all vertices.
widthwidth of the tile. In base tiles, not world units.
depthdepth of the tile. In base tiles, not world units.
sourceSource mesh, must be readable.
Returns
Identifier for loading that tile type
void ReloadInBounds ( Bounds  bounds)

Reloads all tiles intersecting with the specified bounds.

void ReloadInBounds ( IntRect  tiles)

Reloads all tiles specified by the rectangle.

void ReloadTile ( int  x,
int  z 
)

Reload tile at tile coordinate.

The last tile loaded at that position will be reloaded (e.g to account for moved NavmeshCut components)

static int SampleYCoordinateInTriangle ( Int3  p1,
Int3  p2,
Int3  p3,
Int3  p 
)
staticprivate

Sample Y coordinate of the triangle (p1, p2, p3) at the point p in XZ space.

The y coordinate of p is ignored.

Returns
The interpolated y coordinate unless the triangle is degenerate in which case a DivisionByZeroException will be thrown
See Also
https://en.wikipedia.org/wiki/Barycentric_coordinate_system
bool StartBatchLoad ( )

Start batch loading.

Returns
True if batching wasn't started yet, and thus EndBatchLoad should be called, False if batching was already started by some other part of the code and you should not call EndBatchLoad
void UpdateTileType ( NavmeshTile  tile)
private

Member Data Documentation

readonly int [] activeTileOffsets
private

Offsets along the Y axis of the active tiles.

readonly int [] activeTileRotations
private

Rotations of the active tiles.

readonly TileType [] activeTileTypes
private

Which tile type is active on each tile index.

This array will be tileXCount*tileZCount elements long.

readonly Dictionary<Int2, int> cached_Int2_int_dict = new Dictionary<Int2, int>()
private

Cached dictionary to avoid excessive allocations.

readonly Clipper clipper = new Clipper()
private

Handles polygon clipping operations.

readonly GridLookup<NavmeshClipper> cuts

NavmeshCut and NavmeshAdd components registered to this tile handler.

This is updated by the TileHandlerHelper component.

See Also
TileHandlerHelper
readonly NavmeshBase graph

The underlaying graph which is handled by this instance.

bool isBatching
private

True while batching tile updates.

Batching tile updates has a positive effect on performance

readonly bool [] reloadedInBatch
private

A flag for each tile that is set to true if it has been reloaded while batching is in progress.

readonly Pathfinding.Voxels.VoxelPolygonClipper simpleClipper
private

Utility for clipping polygons to rectangles.

Implemented as a struct and not a bunch of static methods because it needs some buffer arrays that are best cached to avoid excessive allocations

readonly int tileXCount
private

Number of tiles along the x axis.

readonly int tileZCount
private

Number of tiles along the z axis.

Property Documentation

bool isValid
get

True if the tile handler still has the same number of tiles and tile layout as the graph.

If the graph is rescanned the tile handler will get out of sync and needs to be recreated.


The documentation for this class was generated from the following file: