Class CellItemLayer
Represents a terrain layer that manages items placed within cells of a hex-based terrain system, providing functionality to track, manipulate, and retrieve item indices, states, positions, rotations, scales, and transformations for each cell.
Inherited Members
Namespace: Fwt.HexTerrains.CellItems.Data
Assembly: fwt.hexterrains.dll
Syntax
public class CellItemLayer : HexTerrainLayer, IDisposable, ISerializableTerrainLayer
Remarks
The CellItemLayer class enables dynamic placement and transformation of items relative to the terrain surface in a hexagonal grid layout. It supports efficient rendering and flexible item management by maintaining per-cell data for item properties and transformations. This class also provides methods for serializing and deserializing layer data, updating item transforms, and batch operations on cell items. Thread safety and job scheduling are considered for performance in large-scale terrain systems.
Fields
ItemIndexMap
Index of the item in each cell. Index = cellIndex, Value = item index.
Declaration
public ColorMapCellValueDataLayer_Int ItemIndexMap
Field Value
| Type | Description |
|---|---|
| ColorMapCellValueDataLayer_Int |
ItemPositionMap
Translation of the cell item within the cell. This allows for more dynamic and flexible placement of cell items, as they can be positioned relative to the terrain surface rather than having fixed world positions. Index = cellIndex, Value = local position of the item within the cell.
Declaration
public CellValueDataLayer<float3> ItemPositionMap
Field Value
| Type | Description |
|---|---|
| CellValueDataLayer<float3> |
ItemRotationMap
Rotation of the cell item within the cell. This allows for more dynamic and flexible placement of cell items, as they can be rotated relative to the terrain surface rather than having fixed world rotations. Index = cellIndex, Value = local rotation of the item within the cell (e.g., Euler angles or quaternion).
Declaration
public CellValueDataLayer<quaternion> ItemRotationMap
Field Value
| Type | Description |
|---|---|
| CellValueDataLayer<quaternion> |
ItemScaleMap
Scale of the cell item within the cell. This allows for more dynamic and flexible placement of cell items, as they can be scaled relative to the terrain surface rather than having fixed world scales. Index = cellIndex, Value = local scale of the item within the cell.
Declaration
public CellValueDataLayer<float3> ItemScaleMap
Field Value
| Type | Description |
|---|---|
| CellValueDataLayer<float3> |
ItemStateMap
State of the item in each cell. State can be used to represent different variations of the same item, or different stages of an item's lifecycle (e.g., growth stage of a plant). Index = cellIndex, Value = item state.
Declaration
public CellValueDataLayer<int> ItemStateMap
Field Value
| Type | Description |
|---|---|
| CellValueDataLayer<int> |
ItemTransformMap
Calculated local transform matrix of the cell item within the cell, combining the local position, rotation, and scale. This can be used for efficient rendering, as it provides a single matrix that represents the complete transformation of the cell item relative to the terrain surface. Index = cellIndex, Value = local transform matrix of the item within the cell.
Declaration
public CellValueDataLayer<Matrix4x4> ItemTransformMap
Field Value
| Type | Description |
|---|---|
| CellValueDataLayer<Matrix4x4> |
OverlapSurfaceLayerReference
Represents a reference to the terrain layer that overlaps the SurfaceLayerReference surface layer.
Declaration
public HexTerrainLayerReference OverlapSurfaceLayerReference
Field Value
| Type | Description |
|---|---|
| HexTerrainLayerReference |
Remarks
This field can be used to identify or interact with the terrain layer that is positioned above or otherwise intersects the primary surface layer in a hex-based terrain system.
SurfaceLayerReference
Reference to a surface layer that holds a cell positions. Used to calculate the world position of the cell item based on the cell position and the local position of the item within the cell. This allows for more dynamic and flexible placement of cell items, as they can be positioned relative to the terrain surface rather than having fixed world positions.
Declaration
public HexTerrainLayerReference SurfaceLayerReference
Field Value
| Type | Description |
|---|---|
| HexTerrainLayerReference |
Properties
ItemTransformSettings
Settings related to transform of the CellObject within the cell
Declaration
public virtual CellItemTransformSettings ItemTransformSettings { get; set; }
Property Value
| Type | Description |
|---|---|
| CellItemTransformSettings |
Methods
CalculateColorMap(JobHandle)
Schedules the calculation of the color map, ensuring it is executed after the specified job dependency completes.
Declaration
public override JobHandle CalculateColorMap(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency | A job handle representing the dependency that must be completed before the color map calculation begins. |
Returns
| Type | Description |
|---|---|
| JobHandle | A job handle that can be used to track the completion of the color map calculation job. |
Overrides
Cleanup()
Every frame there is a cleanup stage. This function is called. Usually used to clean the dirty flags.
Declaration
public override void Cleanup()
Overrides
CleanupAsync(JobHandle)
Async version of Cleanup() Cleanup() Difference between regular Cleanup() is that it schedules jobs that clean up data layers, so you don't have to wait for the results and can continue working. Every frame there is a cleanup stage. This function is called. Usually used to clean the dirty flags.
Declaration
public override JobHandle CleanupAsync(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency |
Returns
| Type | Description |
|---|---|
| JobHandle |
Overrides
CompleteAllJobs()
Completes all jobs that affect data layers.
Declaration
public override void CompleteAllJobs()
Overrides
DeserializeLayer(BinaryReader, HexTerrainSettings)
Requests a deserialization of the layer data. Returns false if the layer does not support deserialization.
Declaration
public virtual bool DeserializeLayer(BinaryReader reader, HexTerrainSettings terrainSettings)
Parameters
| Type | Name | Description |
|---|---|---|
| BinaryReader | reader | binary reader to deserialize data from |
| HexTerrainSettings | terrainSettings | deserialized terrain settings (layer was serialized with this settings) |
Returns
| Type | Description |
|---|---|
| bool |
Dispose()
Disposes all allocated native memory.
Declaration
public override void Dispose()
Overrides
GetCellItemIndex(int)
Retrieves the index of the item associated with the specified cell index, if it exists.
Declaration
public virtual int? GetCellItemIndex(int cellIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell for which to retrieve the item index. Must be within the valid range of the item index map. |
Returns
| Type | Description |
|---|---|
| int? | The index of the item associated with the specified cell index, or null if the cell index is not found. |
Remarks
This method opens the item index map for reading before attempting to retrieve the item index. Ensure that the item index map is properly initialized before calling this method.
GetCellItemLocalPosition(int)
Retrieves the local position of the item located at the specified cell index, if available.
Declaration
public virtual float3? GetCellItemLocalPosition(int cellIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell for which to obtain the item's local position. Must be within the valid range of the item position map. |
Returns
| Type | Description |
|---|---|
| float3? | A nullable |
GetCellItemLocalRotation(int)
Retrieves the local rotation of the cell item at the specified index, if available.
Declaration
public virtual quaternion? GetCellItemLocalRotation(int cellIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell item whose local rotation is to be retrieved. Must be within the valid range of the item rotation map. |
Returns
| Type | Description |
|---|---|
| quaternion? | A nullable quaternion representing the local rotation of the specified cell item, or null if the item does not exist or the rotation is unavailable. |
Remarks
If the specified index does not correspond to a valid cell item, the method returns null.
GetCellItemLocalScale(int)
Retrieves the local scale of the item at the specified cell index, if available.
Declaration
public virtual float3? GetCellItemLocalScale(int cellIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell for which to retrieve the local scale. Must be within the valid range of the item scale map. |
Returns
| Type | Description |
|---|---|
| float3? | A nullable |
Remarks
If the item scale map is not available or the specified index is out of range, the method returns null.
GetCellItemLocalTransform(int)
Retrieves the local transformation matrix for the specified cell item index.
Declaration
public virtual Matrix4x4? GetCellItemLocalTransform(int cellIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The index of the cell item for which to retrieve the local transformation. Must be a valid index within the bounds of the item transform map. |
Returns
| Type | Description |
|---|---|
| Matrix4x4? | A nullable UnityEngine.Matrix4x4 representing the local transformation matrix of the specified cell item, or null if the item does not exist. |
Remarks
If the item transform map is not initialized or the specified index is invalid, the method returns null.
GetCellItemState(int)
Retrieves the state of the item at the specified cell index, if available.
Declaration
public virtual int? GetCellItemState(int cellIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell for which to retrieve the item state. Must be within the valid range of the item state map. |
Returns
| Type | Description |
|---|---|
| int? | The state of the item at the specified cell index, or null if the index is invalid or the item state is not found. |
Remarks
If the item state map is not initialized or the specified index is out of range, this method returns null.
Init(HexTerrainSettings)
Initializes/Reinitializes the area data.
Declaration
public override void Init(HexTerrainSettings settings)
Parameters
| Type | Name | Description |
|---|---|---|
| HexTerrainSettings | settings | new terrain settings |
Overrides
Init<TInitArgs>(HexTerrainSettings, TInitArgs)
Initializes the terrain system using the specified settings and initialization arguments.
Declaration
public override void Init<TInitArgs>(HexTerrainSettings settings, TInitArgs initArgs)
Parameters
| Type | Name | Description |
|---|---|---|
| HexTerrainSettings | settings | The configuration settings to apply when initializing the terrain system. Cannot be null. |
| TInitArgs | initArgs | Additional arguments required for initialization. The expected type and usage depend on the implementation. |
Type Parameters
| Name | Description |
|---|---|
| TInitArgs | The type of the initialization arguments to be used during terrain system setup. |
Overrides
SerializeLayer(BinaryWriter)
Requests a serialization of the layer data. Returns false if the layer does not support serialization.
Declaration
public virtual bool SerializeLayer(BinaryWriter writer)
Parameters
| Type | Name | Description |
|---|---|---|
| BinaryWriter | writer | binary writer to serialize data by |
Returns
| Type | Description |
|---|---|
| bool | True if serialized, false if not serialized/not supported/not implemented |
SetAllCellsItemIndex(int)
Sets the specified item index for all cells in the collection.
Declaration
public virtual bool SetAllCellsItemIndex(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The item index value to assign to every cell. Must be a non-negative integer. |
Returns
| Type | Description |
|---|---|
| bool | true if all cells were successfully updated with the specified index; otherwise, false. |
Remarks
If the map is not available, the method returns false without making any changes.
SetAllCellsItemState(int)
Sets the state of all cells in the item state map to the specified value.
Declaration
public virtual bool SetAllCellsItemState(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The integer value to assign to each cell's state. |
Returns
| Type | Description |
|---|---|
| bool | true if the operation succeeds and all cell states are updated; otherwise, false. |
Remarks
This method attempts to open the item state map for writing before updating cell states. If the item state map is null, the method returns false and no changes are made.
SetAllCellsLocalPosition(float3)
Sets the local position of all cells to the specified value.
Declaration
public virtual bool SetAllCellsLocalPosition(float3 value)
Parameters
| Type | Name | Description |
|---|---|---|
| float3 | value | The new local position to assign to all cells, represented as a |
Returns
| Type | Description |
|---|---|
| bool | true if the operation succeeds and all cell positions are updated; otherwise, false. |
Remarks
This method opens the item position map for writing before updating cell positions. Ensure that the item position map is initialized before calling this method.
SetAllCellsLocalRotation(quaternion)
Sets the local rotation for all cells to the specified quaternion value.
Declaration
public virtual bool SetAllCellsLocalRotation(quaternion value)
Parameters
| Type | Name | Description |
|---|---|---|
| quaternion | value | The quaternion representing the local rotation to apply to all cells. |
Returns
| Type | Description |
|---|---|
| bool | true if the rotation was successfully applied to all cells; otherwise, false. |
Remarks
This method attempts to open the ItemRotationMap for writing and fills it with the provided rotation value. Ensure that the ItemRotationMap is initialized before calling this method.
SetAllCellsLocalScale(float3)
Sets the local scale for all cells in the item scale map to the specified value.
Declaration
public virtual bool SetAllCellsLocalScale(float3 value)
Parameters
| Type | Name | Description |
|---|---|---|
| float3 | value | The new local scale to apply to each cell. This value determines the size and appearance of all cells in the map. |
Returns
| Type | Description |
|---|---|
| bool | true if the operation succeeds and all cells are updated; otherwise, false. |
Remarks
The method attempts to open the item scale map for writing before applying the new scale. Ensure that the item scale map is initialized and available before calling this method.
SetAllCellsLocalTransform(Matrix4x4)
Sets the local transformation matrix for all cells in the item transform map.
Declaration
public virtual bool SetAllCellsLocalTransform(Matrix4x4 value)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix4x4 | value | The transformation matrix to apply to all cells. This matrix defines the local position, rotation, and scale of the cells. |
Returns
| Type | Description |
|---|---|
| bool | true if the transformation was successfully applied to all cells; otherwise, false. |
Remarks
This method opens the item transform map for writing before applying the transformation. If the item transform map is null, the method returns false.
SetAllDirty(bool)
Sets all data layers dirty value. If isDirty is true, all data layers will be marked as dirty and will be recalculated on the next frame. If isDirty is false, all data layers will be marked as clean and will not be recalculated on the next frame.
Declaration
public override void SetAllDirty(bool isDirty)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | isDirty |
Overrides
SetCellItemIndex(int, int)
Sets the item index for a specified cell in the item index map.
Declaration
public virtual bool SetCellItemIndex(int cellIndex, int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell to update. Must be within the valid range of the item index map. |
| int | value | The item index value to assign to the specified cell. |
Returns
| Type | Description |
|---|---|
| bool | true if the item index was successfully set; otherwise, false. |
Remarks
If the item index map is null, the method returns false and no changes are made. The item index map is opened for writing before attempting to set the value.
SetCellItemLocalPosition(int, float3)
Sets the local position of a cell item at the specified index within the item position map.
Declaration
public virtual bool SetCellItemLocalPosition(int cellIndex, float3 value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell item whose local position is to be set. Must be within the valid range of the item position map. |
| float3 | value | The new local position to assign to the cell item, represented as a Unity.Mathematics.float3 structure. |
Returns
| Type | Description |
|---|---|
| bool | true if the local position was successfully set; otherwise, false. |
Remarks
If the item position map is null, the method returns false and no changes are made. The item position map is opened for writing before attempting to set the position.
SetCellItemLocalRotation(int, quaternion)
Sets the local rotation of a cell item at the specified index.
Declaration
public virtual bool SetCellItemLocalRotation(int cellIndex, quaternion value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell item whose local rotation is to be set. Must be within the valid range of the item rotation map. |
| quaternion | value | The quaternion value representing the desired local rotation to assign to the cell item. |
Returns
| Type | Description |
|---|---|
| bool | true if the local rotation was successfully set; otherwise, false. |
Remarks
If the item rotation map is null, the method returns false and no rotation is set. The item rotation map is opened for writing before attempting to set the rotation.
SetCellItemLocalScale(int, float3)
Sets the local scale for the specified cell item in the item scale map.
Declaration
public virtual bool SetCellItemLocalScale(int cellIndex, float3 value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell item whose local scale is to be set. Must be within the valid range of the item scale map. |
| float3 | value | The new local scale to assign to the cell item, represented as a float3 structure. |
Returns
| Type | Description |
|---|---|
| bool | true if the local scale was successfully set; otherwise, false. |
Remarks
The method attempts to open the item scale map for writing before setting the scale. If the item scale map is null, the method returns false and no changes are made.
SetCellItemLocalTransform(int, Matrix4x4)
Sets the local transformation matrix for the specified cell item.
Declaration
public virtual bool SetCellItemLocalTransform(int cellIndex, Matrix4x4 value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell item whose local transformation is to be updated. Must be within the valid range of the item transform map. |
| Matrix4x4 | value | The transformation matrix to assign to the specified cell item. This matrix defines the new local transformation. |
Returns
| Type | Description |
|---|---|
| bool | true if the transformation was successfully set; otherwise, false. |
Remarks
This method attempts to open the item transform map for writing before setting the transformation. If the item transform map is null, the method returns false.
SetCellItemState(int, int)
Sets the state value for the specified cell item.
Declaration
public virtual bool SetCellItemState(int cellIndex, int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | cellIndex | The zero-based index of the cell item whose state is to be set. Must be within the valid range of the item state map. |
| int | value | The new state value to assign to the cell item. The value should be within the acceptable range defined by the application. |
Returns
| Type | Description |
|---|---|
| bool | true if the state was successfully set; otherwise, false. |
Remarks
If the item state map is null, the method returns false and no changes are made. The item state map is opened for writing before attempting to set the state.
UpdateCellItems(JobHandle)
Updates the state of the layer, including calculating the local transform of each cell item based on the current state of the terrain and the layer settings.
Declaration
public virtual JobHandle UpdateCellItems(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency |
Returns
| Type | Description |
|---|---|
| JobHandle |
UpdateLocalTransforms(JobHandle)
Updates the local transform of each cell item based on the current state of the terrain and the layer settings. This is a separate step from UpdateCellItems to allow for better scheduling and potential reuse of the local transform data in other systems.
Declaration
public virtual JobHandle UpdateLocalTransforms(JobHandle dependency)
Parameters
| Type | Name | Description |
|---|---|---|
| JobHandle | dependency |
Returns
| Type | Description |
|---|---|
| JobHandle |
Exceptions
| Type | Condition |
|---|---|
| NotImplementedException |