A* Pathfinding Project  4.3.7
The A* Pathfinding Project for Unity 3D
RetainedGizmos Class Reference

Helper for drawing Gizmos in a performant way. More...

Detailed Description

Helper for drawing Gizmos in a performant way.

This is a replacement for the Unity Gizmos class as that is not very performant when drawing very large amounts of geometry (for example a large grid graph). These gizmos can be persistent, so if the data does not change, the gizmos do not need to be updated.

How to use

  • Create a Hasher object and hash whatever data you will be using to draw the gizmos Could be for example the positions of the vertices or something. Just as long as if the gizmos should change, then the hash changes as well.
  • Check if a cached mesh exists for that hash
  • If not, then create a Builder object and call the drawing methods until you are done and then call Finalize with a reference to a gizmos class and the hash you calculated before.
  • Call gizmos.Draw with the hash.
  • When you are done with drawing gizmos for this frame, call gizmos.FinalizeDraw
var a = Vector3.zero;
var b = Vector3.one;
var color = Color.red;
var hasher = new RetainedGizmos.Hasher();
hasher.AddHash(a.GetHashCode());
hasher.AddHash(b.GetHashCode());
hasher.AddHash(color.GetHashCode());
if (!gizmos.Draw(hasher)) {
using (var helper = gizmos.GetGizmoHelper(active, hasher)) {
builder.DrawLine(a, b, color);
builder.Finalize(gizmos, hasher);
}
}

Classes

struct  BuilderData
 
struct  BuilderDataContainer
 
struct  Hasher
 Combines hashes into a single hash value. More...
 
struct  MeshWithHash
 
struct  NodeHasher
 Combines hashes into a single hash value. More...
 
struct  RedrawScope
 

Public Member Functions

void Dispose ()
 Destroys all cached meshes. More...
 
bool Draw (Hasher hasher)
 Schedules the meshes for the specified hash to be drawn. More...
 
void Draw (RedrawScope scope)
 Schedules all meshes that were drawn the last frame with this redraw scope to be drawn again. More...
 
CommandBuilder GetBuilder (Hasher hasher, RedrawScope redrawScope=default, bool isGizmos=true)
 
GraphGizmoHelper GetGizmoHelper (AstarPath active, Hasher hasher, RetainedGizmos.RedrawScope redrawScope)
 
GraphGizmoHelper GetSingleFrameGizmoHelper (AstarPath active, RetainedGizmos.RedrawScope redrawScope)
 
bool HasCachedMesh (Hasher hasher)
 True if there already is a mesh with the specified hash. More...
 
void Render (bool allowGizmos)
 Call after all Draw commands for the frame have been done to draw everything. More...
 
void TickFrame ()
 

Public Attributes

Material lineMaterial
 Material to use for the navmesh outline in the editor. More...
 
Material surfaceMaterial
 Material to use for the navmesh in the editor. More...
 

Package Functions

void AddMesh (MeshWithHash mesh)
 
Mesh GetMesh ()
 

Package Attributes

BuilderDataContainer data
 

Properties

int version [get, private set]
 

Private Member Functions

void DiscardData (Hasher hasher)
 
void PoolMesh (Mesh mesh)
 
void RemoveMeshesOlderThan (List< MeshWithHash > meshList, int versionThreshold)
 
void RemovePerCameraMeshes (List< MeshWithHash > meshList)
 
void RemoveUnusedMeshes (List< MeshWithHash > meshList)
 

Private Attributes

Stack< Mesh > cachedMeshes = new Stack<Mesh>()
 
Dictionary< Camera, Int2cameraVersions = new Dictionary<Camera, Int2>()
 
HashSet< ulong > existingHashes = new HashSet<ulong>()
 
Plane [] frustrumPlanes = new Plane[6]
 
int lastTickVersion = 1
 
int lastTickVersion2
 
List< MeshWithHashmeshes = new List<MeshWithHash>()
 
HashSet< ulong > usedHashes = new HashSet<ulong>()
 

Member Function Documentation

◆ AddMesh()

void AddMesh ( MeshWithHash  mesh)
package

◆ DiscardData()

void DiscardData ( Hasher  hasher)
private

◆ Dispose()

void Dispose ( )

Destroys all cached meshes.

Used to make sure that no memory leaks happen in the Unity Editor.

◆ Draw() [1/2]

bool Draw ( Hasher  hasher)

Schedules the meshes for the specified hash to be drawn.

Returns
False if there is no cached mesh for this hash, you may want to submit one in that case. The draw command will be issued regardless of the return value.

◆ Draw() [2/2]

void Draw ( RedrawScope  scope)

Schedules all meshes that were drawn the last frame with this redraw scope to be drawn again.

◆ GetBuilder()

CommandBuilder GetBuilder ( Hasher  hasher,
RedrawScope  redrawScope = default,
bool  isGizmos = true 
)

◆ GetGizmoHelper()

GraphGizmoHelper GetGizmoHelper ( AstarPath  active,
Hasher  hasher,
RetainedGizmos.RedrawScope  redrawScope 
)

◆ GetMesh()

Mesh GetMesh ( )
package

◆ GetSingleFrameGizmoHelper()

GraphGizmoHelper GetSingleFrameGizmoHelper ( AstarPath  active,
RetainedGizmos.RedrawScope  redrawScope 
)

◆ HasCachedMesh()

bool HasCachedMesh ( Hasher  hasher)

True if there already is a mesh with the specified hash.

◆ PoolMesh()

void PoolMesh ( Mesh  mesh)
private

◆ RemoveMeshesOlderThan()

void RemoveMeshesOlderThan ( List< MeshWithHash meshList,
int  versionThreshold 
)
private

◆ RemovePerCameraMeshes()

void RemovePerCameraMeshes ( List< MeshWithHash meshList)
private

◆ RemoveUnusedMeshes()

void RemoveUnusedMeshes ( List< MeshWithHash meshList)
private

◆ Render()

void Render ( bool  allowGizmos)

Call after all Draw commands for the frame have been done to draw everything.

◆ TickFrame()

void TickFrame ( )

Member Data Documentation

◆ cachedMeshes

Stack<Mesh> cachedMeshes = new Stack<Mesh>()
private

◆ cameraVersions

Dictionary<Camera, Int2> cameraVersions = new Dictionary<Camera, Int2>()
private

◆ data

BuilderDataContainer data
package

◆ existingHashes

HashSet<ulong> existingHashes = new HashSet<ulong>()
private

◆ frustrumPlanes

Plane [] frustrumPlanes = new Plane[6]
private

◆ lastTickVersion

int lastTickVersion = 1
private

◆ lastTickVersion2

int lastTickVersion2
private

◆ lineMaterial

Material lineMaterial

Material to use for the navmesh outline in the editor.

◆ meshes

List<MeshWithHash> meshes = new List<MeshWithHash>()
private

◆ surfaceMaterial

Material surfaceMaterial

Material to use for the navmesh in the editor.

◆ usedHashes

HashSet<ulong> usedHashes = new HashSet<ulong>()
private

Property Documentation

◆ version

int version
getprivate set

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