Skip to content

Game.WaterRenderSystem

Assembly: Game
Namespace: Game.Rendering

Type: class

Base: GameSystemBase

Summary:
System responsible for preparing and updating water rendering data each frame. It bridges TerrainSystem, WaterSystem and RenderingSystem to populate per-surface material properties for all WaterSurface instances. Responsibilities include duplicating custom materials on creation (so instances can safely modify them), cleaning up those materials on destroy, feeding simulation/texture/state (water texture, flow texture, cascade / terrain data, time multipliers), and applying overlay textures / masks and shader keywords for custom water materials.


Fields

  • private TerrainRenderSystem m_TerrainRenderSystem
    Holds a reference to the TerrainRenderSystem (not assigned in the shown code, reserved for use if needed).

  • private TerrainSystem m_TerrainSystem
    Cached reference to the TerrainSystem acquired from the world in OnCreate; used to get cascade textures, scale/offset and cascade info.

  • private WaterSystem m_WaterSystem
    Cached reference to the WaterSystem acquired from the world in OnCreate; used to query water textures, flow textures and Loaded state.

  • private RenderingSystem m_RenderingSystem
    Cached reference to the RenderingSystem acquired from the world in OnCreate; used to read frameDelta used to compute per-surface timeMultiplier.

Properties

  • public Texture overrideOverlaymap { get; set; }
    Optional texture that, when set, is bound to the water surface material as the base color / overlay texture (shader property _BaseColorMap). If null the surface's normal overlay behavior remains.

  • public Texture overlayExtramap { get; set; }
    Optional extra overlay texture that can be assigned to shader property _OverlayExtra. If set, the system enables the "OVERRIDE_OVERLAY_EXTRA" shader keyword. If this texture equals the flow texture, a special shader float (_OverlayArrowSource) is set to 1 to indicate using the flow texture as source for overlay arrows.

  • public float4 overlayPollutionMask { get; set; }
    Vector mask passed to the shader (bound to TerrainRenderSystem.ShaderID._OverlayPollutionMask) to control pollution overlay blending.

  • public float4 overlayArrowMask { get; set; }
    Vector mask passed to the shader (bound to TerrainRenderSystem.ShaderID._OverlayArrowMask) to control arrow overlay blending.

  • public Texture waterTexture { get; }
    Read-only convenience property that returns the water render texture from the WaterSystem (m_WaterSystem.WaterRenderTexture).

  • public Texture flowTexture { get; }
    Read-only convenience property that returns the up-to-date flow texture from the WaterSystem (m_WaterSystem.FlowTextureUpdated).

  • public bool IsAsync { get; set; }
    A flag exposed on the system. In the provided code it is declared but not otherwise used; may be intended for future async behavior toggles.

Constructors

  • public WaterRenderSystem()
    Default constructor. The class relies on OnCreate to initialize its references and perform material duplication.

Methods

  • protected override void OnCreate() : System.Void
    Acquires TerrainSystem, WaterSystem and RenderingSystem from the world. Iterates WaterSurface.instances and, for any instance with a non-null customMaterial, replaces it with a new Material(instance.customMaterial) to create an instance-specific copy. Marked with [Preserve].

  • protected override void OnDestroy() : System.Void
    Iterates WaterSurface.instances and destroys any duplicated customMaterial via CoreUtils.Destroy to avoid leaking GPU resources, then calls base.OnDestroy(). Marked with [Preserve].

  • protected override void OnUpdate() : System.Void
    Per-frame update that:

  • Queries cascade info from TerrainSystem to determine LOD/cascade area and base LOD.
  • Iterates all WaterSurface.instances and sets:
    • timeMultiplier computed from rendering frameDelta and the world Time.DeltaTime (scaled by 60) so water animations scale correctly with game speed.
    • CascadeArea and WaterSimArea (a Vector4 derived from the cascade area depending on baseLOD).
    • TerrainScaleOffset and TerrainCascadeTexture from the TerrainSystem.
    • WaterSimulationTexture to either WaterSystem.WaterTexture (if m_WaterSystem.Loaded) or Texture2D.blackTexture otherwise.
  • For each instance with a customMaterial:
    • Binds overlayArrowMask and overlayPollutionMask to shader IDs.
    • If overrideOverlaymap is set, binds it to _BaseColorMap.
    • If overlayExtramap is set:
    • Ensures overrideOverlaymap at least defaults to white when overlayExtramap is used.
    • If overlayExtramap == flowTexture, sets shader float _OverlayArrowSource to 1 (use flow as arrow source).
    • Otherwise sets _OverlayExtra to overlayExtramap and sets _OverlayArrowSource to 0.
    • Enables keyword "OVERRIDE_OVERLAY_EXTRA".
    • If overlayExtramap is null, disables "OVERRIDE_OVERLAY_EXTRA".
  • The method reads m_WaterSystem.Loaded near the end (unused return), likely to ensure any side-effects or state reads.

Usage Example

// Example: access the system and configure overlays from another system or mod initializer
var waterSys = World.GetOrCreateSystemManaged<Game.Rendering.WaterRenderSystem>();
if (waterSys != null)
{
    // set a custom overlay texture and masks
    waterSys.overrideOverlaymap = myOverlayTexture;
    waterSys.overlayExtramap = myExtraOverlayTexture; // or waterSys.flowTexture to use flow as source
    waterSys.overlayPollutionMask = new float4(1,1,1,1);
    waterSys.overlayArrowMask = new float4(0,1,0,1);
    waterSys.IsAsync = false;
}

// Typical system lifecycle: OnCreate duplicates instance materials so they can be modified safely.
// OnDestroy will clean up Material copies created for WaterSurface instances.