Game.Simulation.TerrainHeightData
Assembly:
{{ Game assembly containing simulation types (e.g. the game's main assembly). }}
Namespace: Game.Simulation
Type: struct
Base: System.ValueType
Summary:
TerrainHeightData is a lightweight value-type container used to carry terrain height information for simulation systems. It wraps a Unity.Collections.NativeArray
Fields
-
private Unity.Collections.NativeArray<ushort> <heights>k__BackingField
Backing field for the heights auto-property. Holds packed height samples as unsigned 16-bit values (NativeArray). -
private int3 <resolution>k__BackingField
Backing field for the resolution auto-property. Contains the integer grid resolution (x,y,z) of the height data. -
private float3 <scale>k__BackingField
Backing field for the scale auto-property. Scale applied to convert sample indices to world/local space. -
private float3 <offset>k__BackingField
Backing field for the offset auto-property. Offset applied to position the height samples in world/local space.
Properties
-
public Unity.Collections.NativeArray<ushort> heights { get; private set }
NativeArray holding the height samples as ushorts. The NativeArray must be allocated (for example with Allocator.Persistent or Allocator.TempJob) by the caller. The setter is private — only the struct constructor assigns it. -
public int3 resolution { get; private set }
Three-component integer resolution describing how many samples are present along each axis. -
public float3 scale { get; private set }
Scale vector used to convert sample indices to world/local coordinates (multiplicative factor). -
public float3 offset { get; private set }
Offset vector applied after scaling to position samples in world/local coordinates. -
public bool isCreated => heights.IsCreated
Read-only convenience property that returns true if the underlying NativeArray has been allocated.
Constructors
public TerrainHeightData(NativeArray<ushort> _heights, int3 _resolution, float3 _scale, float3 _offset)
Constructs a TerrainHeightData instance by taking ownership of (or referencing) the provided NativeArray and storing the resolution, scale and offset. Note: the struct does not take responsibility for disposing the NativeArray; the caller that allocated it should dispose it when no longer needed.
Methods
- None (no instance methods; only auto-properties and a computed isCreated property).
Usage Example
using Unity.Collections;
using Unity.Mathematics;
using Game.Simulation;
// Example: create a simple flat terrain height dataset
int3 resolution = new int3(256, 1, 256); // x * y * z samples
int sampleCount = resolution.x * resolution.y * resolution.z;
// Allocate a NativeArray holding ushort height samples
NativeArray<ushort> heights = new NativeArray<ushort>(sampleCount, Allocator.Persistent);
// Fill heights (example: flat plane at some encoded height)
ushort encodedHeight = 1024; // interpretation depends on scale/offset
for (int i = 0; i < heights.Length; i++)
{
heights[i] = encodedHeight;
}
float3 scale = new float3(1.0f, 0.01f, 1.0f); // example: y-scale converts ushort to meters
float3 offset = float3.zero;
// Construct the TerrainHeightData
TerrainHeightData terrainData = new TerrainHeightData(heights, resolution, scale, offset);
// Use it in simulation or pass to systems
if (terrainData.isCreated)
{
// Read/inspect heights, or pass to a job/system that accepts NativeArray<ushort>
}
// When done, dispose the NativeArray (the struct itself does not dispose it)
if (heights.IsCreated)
{
heights.Dispose();
}
Notes and tips: - Be explicit about the Allocator used when allocating the NativeArray (Persistent, TempJob, etc.) depending on lifetime and whether you plan to access it from jobs. - When using in jobs, ensure the NativeArray is allocated with a compatible allocator and proper safety attributes are respected. - The mapping from ushort sample values to world heights depends on the provided scale and offset — agree on encoding/decoding conventions in your mod/system.