Skip to content

Game.Simulation.WaterSurfaceData

Assembly:
Namespace: Game.Simulation

Type: struct

Base: System.ValueType

Summary:
Represents a 3D grid of surface-water samples used by the water simulation. The struct wraps a NativeArray containing per-cell depth data together with spatial metadata (resolution, scale and offset) so simulation code and jobs can map array indices to world-space positions. Note that WaterSurfaceData does not implement IDisposable — the caller is responsible for allocating and disposing the underlying NativeArray to avoid leaks and double-dispose problems.


Fields

  • This struct does not declare explicit fields in source; it uses auto-implemented properties whose backing fields are generated by the compiler. The important storage is the NativeArray exposed via the depths property.

Properties

  • public NativeArray<SurfaceWater> depths { get; private set; }
    Holds the linearized buffer of surface-water samples. The NativeArray must be allocated by the caller (e.g., new NativeArray(count, allocator)). Disposing the NativeArray is the caller's responsibility. Copying this struct copies the NativeArray handle (shallow copy of the reference to the same buffer) — be careful to avoid double-dispose.

  • public int3 resolution { get; private set; }
    3D integer resolution of the sample grid (x, y, z). The array length is expected to match resolution.x * resolution.y * resolution.z.

  • public float3 scale { get; private set; }
    Scale applied to grid indices to convert to world-space distances (cell size in each axis).

  • public float3 offset { get; private set; }
    World-space offset of the grid origin.

  • public bool isCreated => depths.IsCreated
    Convenience property that returns true when the underlying NativeArray has been allocated.

Constructors

  • public WaterSurfaceData(NativeArray<SurfaceWater> _depths, int3 _resolution, float3 _scale, float3 _offset)
    Initializes the struct with the provided NativeArray and spatial metadata. The constructor does not take ownership semantics beyond storing the provided NativeArray; the caller remains responsible for correct allocation (choose an appropriate Allocator: Temp/TempJob/Persistent) and eventual disposal.

Notes: - Ensure the provided NativeArray length equals resolution.x * resolution.y * resolution.z. - For data passed into Unity Jobs/Burst, use an allocator and lifetime that match job scheduling (e.g., Allocator.TempJob for job-local arrays, Allocator.Persistent for long-lived data).

Methods

  • This type defines no methods beyond the property accessors and the constructor.

Usage Example

using Unity.Collections;
using Unity.Mathematics;
using Game.Simulation;

// allocate a 2D grid (z = 1) as an example
int3 resolution = new int3(512, 512, 1);
float3 scale = new float3(1f, 1f, 1f);
float3 offset = new float3(0f, 0f, 0f);
int total = resolution.x * resolution.y * resolution.z;

// allocate the buffer (choose allocator based on lifetime)
var depths = new NativeArray<SurfaceWater>(total, Allocator.Persistent);

// construct the wrapper
var waterSurface = new WaterSurfaceData(depths, resolution, scale, offset);

if (waterSurface.isCreated)
{
    // read/write samples via waterSurface.depths
    // e.g., waterSurface.depths[0] = new SurfaceWater { depth = 0.5f };
}

// when finished, dispose the underlying NativeArray
if (waterSurface.depths.IsCreated)
{
    waterSurface.depths.Dispose();
}

Tips and caveats: - Do not rely on this struct to free memory automatically — manage the NativeArray lifecycle explicitly. - Copying WaterSurfaceData will copy the NativeArray handle (shallow). Ensure only one code path calls Dispose() for the buffer to avoid ObjectDisposedExceptions or crashes. - When passing the NativeArray into Unity Jobs or Burst code, ensure the allocator and scheduling are compatible (TempJob for short-lived job allocations, Persistent for longer-lived data).