Skip to content

Game.Simulation.NoisePollutionSystem

Assembly: Assembly-CSharp
Namespace: Game.Simulation

Type: class

Base: CellMapSystem, IJobSerializable

Summary:
NoisePollutionSystem maintains a 2D cell map of noise pollution for the simulation. It uses a fixed-size texture/grid (256×256) and updates the pollution field using Unity Jobs (Burst compiled) to compute diffusion and to clear temporary values. The system provides sampling utilities (bilinear interpolation) and integrates with the CellMapSystem framework (texture creation, update interval and writer dependency management). The OnUpdate method schedules two parallel jobs: a swap/diffuse job and a clear-temp job, and the system exposes a TextureSize property for consumers.


Fields

  • public static readonly int kTextureSize
    Returns the width/height of the square pollution texture: 256. Used throughout for indexing and neighbor lookups.

  • public static readonly int kUpdatesPerDay
    Number of discrete grid update steps considered per in-game day: 128. Used to compute the system update interval.

Properties

  • public int2 TextureSize => new int2(kTextureSize, kTextureSize)
    Returns the texture/grid dimensions as an int2 (kTextureSize × kTextureSize). Useful for code that needs the resolution of the pollution map.

Constructors

  • public NoisePollutionSystem()
    Default constructor. Marked with [Preserve] in the source to avoid stripping by Unity's managed code optimizers in some build configurations.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Calculates and returns the update interval (in simulation ticks) for the given system update phase. Implementation uses kUpdatesPerDay to compute the interval (262144 / kUpdatesPerDay), so updates are spread across the day.

  • public static float3 GetCellCenter(int index)
    Returns the world-space center position of the cell at the given flat index. Delegates to CellMapSystem.GetCellCenter with the known kTextureSize.

  • public static NoisePollution GetPollution(float3 position, NativeArray<NoisePollution> pollutionMap)
    Samples the pollution map at an arbitrary world position. Converts the position into cell coordinates, clamps to valid cell range, reads the four neighboring cell pollution values and performs bilinear interpolation to return a NoisePollution value for that position. Useful for querying local noise level in gameplay systems.

  • protected override void OnCreate()
    Called when the system is created. Calls base.OnCreate() and invokes CreateTextures(kTextureSize) to allocate and initialize the internal textures/maps used by the CellMapSystem.

  • protected override void OnUpdate()
    Schedules the pollution update jobs each simulation tick/interval:

  • Prepares a NoisePollutionSwapJob (IJobParallelFor, Burst compiled) that reads from the map and computes new m_Pollution from m_PollutionTemp and neighbors (weighted average: center/4, orthogonals/8, diagonals/16).
  • Schedules the swap job and then schedules a NoisePollutionClearJob (IJobParallelFor, Burst compiled) that zeroes m_PollutionTemp for all cells.
  • Uses NativeArray dependencies and calls AddWriter(dependencies) so other systems respect the write dependency.

  • private struct NoisePollutionSwapJob : IJobParallelFor (nested)
    Burst-compiled parallel job that computes the final m_Pollution for each cell from temporary values in m_PollutionTemp. It reads neighbor m_PollutionTemp values with bounds checks (based on kTextureSize) and applies a discrete diffusion kernel:

  • center contribution: m_PollutionTemp / 4
  • orthogonal neighbors: sum / 8
  • diagonal neighbors: sum / 16 Writes the computed m_Pollution back to the map. Uses [NativeDisableParallelForRestriction] on the NativeArray to allow safe parallel access patterns that the job relies on.

  • private struct NoisePollutionClearJob : IJobParallelFor (nested)
    Burst-compiled parallel job that sets m_PollutionTemp = 0 for every cell in the map. Run after the swap job to clear temporary buffers in preparation for the next accumulation phase.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // allocate textures/maps using the system's configured resolution (256x256)
    CreateTextures(NoisePollutionSystem.kTextureSize);
}

Notes and tips: - The system expects a fixed grid size (kTextureSize = 256). Index math in jobs assumes a square grid and uses modulo/divisions by kTextureSize. - Jobs are Burst compiled for performance; the swap job uses NativeDisableParallelForRestriction to handle parallel neighbor reads—ensure the underlying NativeArray usage matches expected patterns. - Use GetPollution when you need a smooth sampled value at a world position; it performs bilinear interpolation between four cells. - The system integrates with the CellMapSystem base methods (CreateTextures, GetCell, GetCellCenter, etc.), so other cell-based systems follow similar patterns for compatibility.