Game.Simulation.WindSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: CellMapSystem
Summary:
WindSystem maintains a low-resolution (64x64) wind cell map used by rendering and gameplay systems. It copies wind data from the full WindSimulationSystem into a compact cell map that is sampled by other systems (e.g., WindTextureSystem). The system samples terrain heights to pick the appropriate vertical layer of the 3D wind simulation, interpolates between layers, and writes the resulting 2D wind vectors into a NativeArray
Fields
-
public static readonly int kTextureSize
Contains the width/height of the 2D wind texture (64). The wind map is a square grid with kTextureSize * kTextureSize cells. -
public static readonly int kUpdateInterval
Update interval used for GameSimulation phase (512). In non-GameSimulation phases the system updates every frame. -
public WindSimulationSystem m_WindSimulationSystem
Reference to the WindSimulationSystem (source of detailed 3D wind cells and the constantWind default). Used to obtain source wind cells and register read dependencies. -
public WindTextureSystem m_WindTextureSystem
Reference to WindTextureSystem. The WindSystem requests the WindTextureSystem to update whenever the 2D wind map changes. -
public TerrainSystem m_TerrainSystem
Reference to the TerrainSystem used to acquire TerrainHeightData for sampling heights when mapping 3D wind data into the 2D grid.
Properties
public int2 TextureSize { get; }
Returns the texture size as an int2: new int2(kTextureSize, kTextureSize). Useful when creating render targets or iterating the 2D grid.
Constructors
public WindSystem()
Default, preserved constructor. Most initialization happens in OnCreate (system retrieval, texture creation, and initial map population).
Methods
-
public override int GetUpdateInterval(SystemUpdatePhase phase)
Returns the update interval for the given update phase. For SystemUpdatePhase.GameSimulation it returns kUpdateInterval (512) to reduce frequency; for other phases it returns 1 (every frame). -
public static float3 GetCellCenter(int index)
Compute and return the world-space center of the cell at the provided linear index using the CellMapSystemhelper with kTextureSize. -
public static Wind GetWind(float3 position, NativeArray<Wind> windMap)
Sample the 2D wind map at a world position. This performs clamping and bilinear interpolation across the four neighboring texels and returns a Wind containing the interpolated wind vector. Intended for callers that have direct access to the NativeArraymap. -
public override JobHandle Deserialize<TReader>(EntityReaderData readerData, JobHandle inputDeps)
Custom deserialization logic that ensures compatibility with older saved versions that used a larger cell map length. It signals the WindTextureSystem to update, and for versions below Version.cellMapLengths it resizes internal m_Map temporarily to 65536 to match serialized layout, calls the base deserializer, completes the job, and then resizes m_Map back to kTextureSize * kTextureSize. For newer versions it delegates to base.Deserialize. -
public override JobHandle SetDefaults(Context context)
Populate the wind map with a default Wind value using WindSimulationSystem.constantWind. Also requests an update on WindTextureSystem. Returns a completed/default JobHandle as no asynchronous work is scheduled. -
protected override void OnCreate()
Initializes the system: retrieves WindSimulationSystem, WindTextureSystem and TerrainSystem from the world, creates textures at kTextureSize, and fills the internal map with the constantWind default. Marked with [Preserve] as in the original. -
protected override void OnUpdate()
Main update: obtains TerrainHeightData from TerrainSystem. If available, it schedules a Burst-compiled WindCopyJob (IJobFor) to copy and interpolate wind from WindSimulationSystem cells into the 2D map, using terrain height to pick vertical layers. The method wires job dependencies (TerrainSystem CPU reader, WindSimulationSystem reader, and its own read/write dependencies), sets base.Dependency, and marks WindTextureSystem for update. -
private struct WindCopyJob : IJobFor
(nested, BurstCompiled)
Job fields: - m_WindMap: NativeArray
target map to write into. - m_Source: ReadOnly NativeArray
source 3D wind cells. - m_TerrainHeightData: ReadOnly TerrainHeightData for sampling world heights.
Execute(index) behavior: - Computes the world center of the 2D cell. - Samples terrain height at that position and offsets upward (+25f) to determine which vertical layer to sample. - Calculates a vertical fractional layer index (clamped to available resolution), picks the current and next integer layers, reads center velocities from the WindSimulation source for both layers, linearly interpolates between them by the fractional part, and writes the resulting 2D wind vector into m_WindMap[index]. This job is scheduled with Burst and IJobFor parallelization for the whole grid.
Usage Example
// Example: sampling the 2D wind map from a known NativeArray<Wind> (e.g., inside another system/job)
float3 worldPos = new float3(512f, 0f, 1024f);
Wind sampled = WindSystem.GetWind(worldPos, windMap);
float2 windVelocity = sampled.m_Wind;
If you are writing a managed system and need to ensure the WindTexture is refreshed or to access the WindSystem, retrieve it from the world:
var windSystem = World.GetOrCreateSystemManaged<Game.Simulation.WindSystem>();
// Note: direct access to the internal wind NativeArray map is only possible from within systems/jobs