Struct VoxelGrid
A 3D voxel grid that's easily extendable with out of the box support for various movement types.
Namespace: AnyPath.Graphs.VoxelGrid
Assembly: AnyPath.dll
Syntax
public struct VoxelGrid : IGraph<VoxelGridCell>, IDisposable
Constructors
VoxelGrid(int3, int3, DirCost[], Allocator, float, int, int)
Constructs a new grid.
Declaration
public VoxelGrid(int3 min, int3 max, VoxelGrid.DirCost[] directionsAndCost, Allocator allocator, float defaultCost = 0, int defaultFlags = 0, int capacity = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | min | Boundary min |
| int3 | max | Boundary max |
| DirCost[] | directionsAndCost | Supported movement directions. Use any of the following: |
| Allocator | allocator | Allocator to use |
| float | defaultCost | The default cost to use for cells that are not set with any value |
| int | defaultFlags | The default flags to use for cells that are not set with any value |
| int | capacity | Initial capacity of the internal hashmap. If you know how many cells you are going to add beforehand, set it to this value for faster creation. |
VoxelGrid(int3, int3, Allocator, float, int, int)
Constructs a new grid that supports straight 6 directional movement.
Declaration
public VoxelGrid(int3 min, int3 max, Allocator allocator, float defaultCost = 0, int defaultFlags = 0, int capacity = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | min | Boundary min |
| int3 | max | Boundary max |
| Allocator | allocator | Allocator to use |
| float | defaultCost | The default cost to use for cells that are not set with any value |
| int | defaultFlags | The default flags to use for cells that are not set with any value |
| int | capacity | Initial capacity of the internal hashmap. If you know how many cells you are going to add beforehand, set it to this value for faster creation. |
VoxelGrid(int3, int3, NativeArray<DirCost>, Allocator, float, int, int)
Constructs a new grid. Can be used inside of jobs.
Declaration
public VoxelGrid(int3 min, int3 max, NativeArray<VoxelGrid.DirCost> directionsAndCost, Allocator allocator, float defaultCost, int defaultFlags, int capacity)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | min | Boundary min |
| int3 | max | Boundary max |
| NativeArray<VoxelGrid.DirCost> | directionsAndCost | Supported movement directions + associated costs. The supplied nativearray is copied. |
| Allocator | allocator | Allocator to use |
| float | defaultCost | The default cost to use for cells that are not set with any value |
| int | defaultFlags | The default flags to use for cells that are not set with any value |
| int | capacity | Initial capacity of the internal hashmap. If you know how many cells you are going to add beforehand, set it to this value for faster creation. |
Fields
All26
All 26 directions are supported.
Declaration
public static readonly VoxelGrid.DirCost[] All26
Field Value
| Type | Description |
|---|---|
| DirCost[] |
Eight_Up_Down
Allowed to move diagonally only when not going up or down. Up and down movement is only supported straight.
Declaration
public static readonly VoxelGrid.DirCost[] Eight_Up_Down
Field Value
| Type | Description |
|---|---|
| DirCost[] |
Foward_Right_Back_Left_Down_StairsUp
Five straight directions: Forward, Right, Back, Left and Down Only allows upwards travel diagonally. Like walking "stairs".
Declaration
public static readonly VoxelGrid.DirCost[] Foward_Right_Back_Left_Down_StairsUp
Field Value
| Type | Description |
|---|---|
| DirCost[] |
Foward_Right_Back_Left_Down_StairsUp_StairsDown
Five straight directions: Forward, Right, Back, Left and Down Supports diagonal movement up and down.
Declaration
public static readonly VoxelGrid.DirCost[] Foward_Right_Back_Left_Down_StairsUp_StairsDown
Field Value
| Type | Description |
|---|---|
| DirCost[] |
Foward_Right_Back_Left_Up_Down
Default straight 6 directional movement.
Declaration
public static readonly VoxelGrid.DirCost[] Foward_Right_Back_Left_Up_Down
Field Value
| Type | Description |
|---|---|
| DirCost[] |
max
The boundary -max- position
Declaration
public readonly int3 max
Field Value
| Type | Description |
|---|---|
| int3 |
min
The boundary -min- position
Declaration
public readonly int3 min
Field Value
| Type | Description |
|---|---|
| int3 |
Properties
DefaultCost
What (enter) cost to use for cells that have not been explicity set. When this is zero, this means that all open cells are navigatable by default.
One use case here is to make this float.PositiveInfinity. Then by default, no cells are navigatable. Only cells that are explicity set (for example, with a cost of zero) would be navigatable. In essence creating a "cave".
Declaration
public float DefaultCost { get; }
Property Value
| Type | Description |
|---|---|
| float |
DefaultFlags
Flags to use for a cells that have not been explicity set.
Declaration
public int DefaultFlags { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
ClearCell(int3)
Clear a cell at a given position.
Declaration
public void ClearCell(int3 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | position |
Remarks
The cell will be considered as unset again and the default cost and flags will be used for this position.
Collect(VoxelGridCell, ref NativeList<Edge<VoxelGridCell>>)
Collects all neighbouring cells from a given location
Declaration
public void Collect(VoxelGridCell node, ref NativeList<Edge<VoxelGridCell>> edgeBuffer)
Parameters
| Type | Name | Description |
|---|---|---|
| VoxelGridCell | node | |
| NativeList<Edge<VoxelGridCell>> | edgeBuffer |
GetCell(int3)
Returns the cell at a given position.
Declaration
public VoxelGridCell GetCell(int3 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | position |
Returns
| Type | Description |
|---|---|
| VoxelGridCell |
GetCost(int3)
Returns the enter cost of a given position. Note that unset cells are considered open and have an entering cost of zero.
Declaration
public float GetCost(int3 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | position |
Returns
| Type | Description |
|---|---|
| float |
GetEnumerator()
Enumerates all of the cells in the grid, including unset ones. This can be used for constructing ALT heuristics.
Declaration
public VoxelGrid.Enumerator GetEnumerator()
Returns
| Type | Description |
|---|---|
| VoxelGrid.Enumerator |
Remarks
Be cautious as a 3D grid contains a lot of positions. I don't advise to use ALT heuristics on large 3D grids.
GetSetCells(Allocator)
Allocates an array containing all of the cells that are explicity set on this grid.
Declaration
public NativeArray<VoxelGridCell> GetSetCells(Allocator allocator)
Parameters
| Type | Name | Description |
|---|---|---|
| Allocator | allocator |
Returns
| Type | Description |
|---|---|
| NativeArray<VoxelGridCell> |
InBounds(int3)
Returns wether a certain position is within the bounds of the grid
Declaration
public bool InBounds(int3 position)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | position |
Returns
| Type | Description |
|---|---|
| bool |
SetCell(int3, float, int)
Sets the cost for a cell.
Declaration
public void SetCell(int3 position, float enterCost, int flags = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| int3 | position | Position to set |
| float | enterCost | Additional cost for walking this cell. Use float.PositiveInfinity to make this cell unwalkable |
| int | flags | Flags for this cell, this can be used in conjunction with FlagBitmask<TNode> to exclude certain areas. |
Remarks
No bounds checking is done on the position