GrassManager
A class in Root.Township, Alta.Environmental.Vegetation.
Networked wind for grass and clouds: scrolls a wind map and sets global shader values. The server syncs speed, strength, direction and offset to clients every 90 seconds. Use the static instance.
- Listed in World, chunks and saving
- Inherits or implements
NetworkEntityBehaviour - Attributes
ExecuteAlways
Constructors
| Member | What it is |
|---|---|
GrassManager() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
| Member | What it is |
|---|---|
static GrassManager instance | The last GrassManager to wake. |
20 private and internal fields
| Member | What it is |
|---|---|
private List<WindPattern> availablePatterns | Backing field for AvailablePatterns. |
private float cloudsRotation | Cloud rotation as a fraction of a turn, wrapping at 1. |
private float cloudsSpeedMultiplier | Scales how fast clouds turn with the wind. |
private float directionChangeLerpSpeed | Lerp amount per frame when turning; 1 snaps at once. |
private bool isCatchingUp | Client: true while moving toward the server's offset. |
private bool isLerpingDirection | True while turning toward newDirection. |
private float maxValueToLerp | Squared offset gap above which clients snap instead of catching up. |
private float networkCloudRotation | Cloud rotation from the last sync. |
private Vector2 newDirection | Direction being turned toward. |
private const float SendInterval | Seconds between syncs, 90. |
private MethodSyncStruct<bool> sync | Sync message for the wind state. |
private Vector2 windDirection | Backing field for WindDirection. |
private Texture2D windMap | Wind noise texture given to shaders as _WindMap. |
private Vector2 windMapOffset | Current scroll offset of the wind map. |
private Vector2 windMapOffsetNetwork | Wind map offset from the last sync. |
private float windMapScale | World size of one wind map tile; shaders get its inverse. |
private float windSpeed | Backing field for WindSpeed. |
private float windStrength | Backing field for WindStrength. |
private float worldPosition | Serialized world origin. Not read in this build. |
private float worldSize | World size used to turn wind movement into cloud rotation. |
Properties
| Member | What it is |
|---|---|
List<WindPattern> AvailablePatterns { get; } | Wind patterns that can be applied with ReadPattern. |
Vector2 WindDirection { get; set; } | Wind direction on the ground plane. Setting it updates the shader off headless. |
float WindSpeed { get; } | How fast the wind map scrolls. |
float WindStrength { get; } | How strongly grass bends. |
Methods
| Member | What it is |
|---|---|
override void Initialize() | Creates the sync message. |
override void InitializeAsServer(SpawnData spawnData) | Server: starts the 90-second sync loop. |
override void OnConnected(Player player) | Server: sends the wind state to a joining player with snapping on. |
void ReadPattern(WindPattern pattern) | Applies a pattern's speed and strength (not direction) and syncs it to clients. |
protected void Awake() | Sets instance and the global wind map shader values; on headless drops the wind map. |
9 private and internal methods
| Member | What it is |
|---|---|
private void ChangeWindDirection(Vector2 direction) | Starts turning toward a new (normalized) direction. Private, no callers here. |
private void ChangeWindSpeed(float speed) | Sets the wind speed, not below 0. |
private void ChangeWindStrength(float strength) | Sets the wind strength, not below 0, and updates the shader. |
private void OnValidate() | Editor: normalizes the direction and updates the shader. |
private void Sync(IPlayer player, Stream stream, bool isSnapping) | Writes or reads the wind state. Clients snap if told to or if far off, otherwise catch up smoothly. |
private IEnumerator SyncToClients() (iterator) | Server coroutine that syncs to all chunks every 90 seconds. |
private void Update() | Lerps the direction, scrolls the wind map and cloud rotation by speed, catches up to the server value, and updates shaders. |
private void UpdateShader() | Sets the global cloud rotation values from cloudsRotation, then the wind parameters. |
private void UpdateWindParameters() | Sets the global _WindParameters: wind map offset and direction times strength. |