Game.Rendering.WindTextureSystem
Assembly: Assembly-CSharp
Namespace: Game.Rendering
Type: class (public)
Base: GameSystemBase
Summary:
Manages a Texture2D that stores the game's wind vector field as an RGFloat texture. The system reads the wind map from WindSystem and copies the wind vectors into a raw texture buffer using a Burst-compiled Unity Job (IJobFor). Updates are scheduled asynchronously and the texture is applied on completion. The system exposes a simple RequireUpdate/CompleteUpdate workflow so other systems can request an update and apply it when safe.
Fields
-
private WindSystem m_WindSystem
Holds a reference to the WindSystem instance that provides the wind map (NativeArray) and JobHandle dependencies. -
private Texture2D m_WindTexture
The Texture2D that stores wind vectors in RGFloat format. Created in OnCreate with size WindSystem.kTextureSize x WindSystem.kTextureSize, no mip chain, linear color space and hideFlags set to HideAndDontSave. -
private JobHandle m_UpdateHandle
JobHandle returned when scheduling WindTextureJob. Used to complete the job before applying the texture. -
private bool m_RequireUpdate
Flag set by RequireUpdate() to indicate that an update should be scheduled on the next system update. -
private bool m_RequireApply
Flag indicating that a scheduled job has completed yet the texture still needs Texture2D.Apply(). Controlled by CompleteUpdate(). -
private struct WindTextureJob : IJobFor
Burst-compiled job type used to copy wind vectors from the NativeArrayinto the texture's raw float2 buffer. -
public NativeArray<Wind> m_WindMap
(ReadOnly)
Source wind data for the job. -
public NativeArray<float2> m_WindTexture
Destination raw texture data (float2 per pixel) where the job writes the wind vector. -
public void Execute(int index)
Copies the wind vector from m_WindMap[index].m_Wind into m_WindTexture[index].
Properties
public Texture2D WindTexture { get; }
Returns the internal Texture2D instance used for the wind texture (m_WindTexture). Consumers can sample this texture in shaders or assign it to materials. The texture is updated asynchronously; call CompleteUpdate() to ensure the latest data has been applied.
Constructors
public WindTextureSystem()
Parameterless constructor with [Preserve] attribute. No custom construction logic beyond what GameSystemBase and OnCreate handle.
Methods
-
[Preserve] protected override void OnCreate()
Initializes the system: obtains the WindSystem reference from World, creates the WindTexture Texture2D (WindSystem.kTextureSize², TextureFormat.RGFloat, no mipmaps, linear) and configures name and hide flags. -
public void RequireUpdate()
Marks the system so that next OnUpdate will schedule a job to copy the wind map into the texture's raw buffer. Call this from other systems when a refresh of the texture is required. -
public void CompleteUpdate()
If a scheduled update was pending apply, this completes the JobHandle (m_UpdateHandle), clears the apply flag, and calls m_WindTexture.Apply() to upload the raw texture data to the GPU. Must be called on the main thread (Unity API). -
[Preserve] protected override void OnUpdate()
If RequireUpdate() was called, this schedules WindTextureJob to copy the NativeArrayinto the texture raw data. It: - Clears m_RequireUpdate and sets m_RequireApply.
- Calls m_WindSystem.GetMap(readOnly: true, out dependencies) to obtain the NativeArray
and any dependency JobHandle. - Gets raw texture buffer with m_WindTexture.GetRawTextureData
(). - Creates and schedules WindTextureJob with Schedule(count, dependencies).
-
Stores returned JobHandle in m_UpdateHandle and registers the handle with m_WindSystem.AddReader(m_UpdateHandle).
-
private struct WindTextureJob.Execute(int index)
Called per index by the job to copy the wind vector: m_WindTexture[index] = m_WindMap[index].m_Wind;
Usage Example
// Example: request an async update of the wind texture and apply it later (main thread).
var windTextureSystem = World.GetOrCreateSystemManaged<WindTextureSystem>();
// Somewhere when wind data has changed or you need the texture refreshed:
windTextureSystem.RequireUpdate();
// Later, on the main thread (for example end of frame or a safe point), ensure the job is complete and apply the texture:
windTextureSystem.CompleteUpdate();
// You can then use windTextureSystem.WindTexture (e.g., assign to a material).
Notes and tips:
- The job writes directly into the Texture2D raw buffer via GetRawTextureData