Weather System Architecture

Hi Guys,

I’m making a weather system. It’ll include warm/cold fronts, precipitation points, temperature, barometric pressure, day/night system, ect. Things like that.

I’ve begun making a little system, but I’m uncertain how to break it up into pieces, if at all. I was wondering could I get a little deliberation here? :slight_smile:

Here’s my class thus far:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(WindZone))]
public class EnvironmentManager : CDSFramework.CDSEnvironment.EnvironmentManagerFramework {

    // Light Vars
    [SerializeField] private Gradient nightDayColour;
    [SerializeField] private float minLightIntensity;
    [SerializeField] private float maxLightIntensity;
    [SerializeField] private float minAmbient;
    [SerializeField] private float maxAmbient;

    // Fog Vars
    [SerializeField] private Gradient nightDayFogColour;
    [SerializeField] private AnimationCurve fogDensityCurve;
    [SerializeField] private float fogScale;

    // Atmos Vars
    [SerializeField] private float minAtmosDensity;
    [SerializeField] private float maxAtmosDensity;

    // Time vars
    [SerializeField] [Range(0.1f,100)] private float dayRotateSpeed;
    [SerializeField] [Range(0.1f, 100)] private float nightRotateSpeed;
    [SerializeField] private Vector3 skySpeed;
    [SerializeField] private float renderTimeStep;

    // Humidity & Precipitation Vars
    [SerializeField] private float maxHumidity;
    [SerializeField] private float minHumidity;
    [SerializeField] private float maxAirTemperature;
    [SerializeField] private float minAirTemperature;
    [SerializeField] private float humiditySaturationPoint;

    // Air Pressure Vars
    [SerializeField] private float airPressure;
    private float precipitation = 0;

    // Instance Vars
    private float skyboxRotation = 0;
    private bool rendering = false;
    private Light directionalLight;
    private Skybox sky;
    private Material skyMat;

    protected override bool Initialise()
    {
        if((directionalLight = GetComponent<Light>()) && (skyMat = RenderSettings.skybox) )
        {
            rendering = true;
            StartCoroutine(TickRenderEnvironment());
            return base.Initialise();
        }
        return false;
    }
   
    // Update World Time, rotating 'Sun & Skies'
    void Update()
    {
        // World Time
        float dot = Mathf.Clamp01(Vector3.Dot(directionalLight.transform.forward, Vector3.down));

        Vector3 deltaRot = dot > 0 ? dayRotateSpeed * Time.deltaTime * skySpeed : nightRotateSpeed * Time.deltaTime * skySpeed;
        transform.Rotate(deltaRot);

        skyboxRotation += deltaRot.magnitude;
        RenderSettings.skybox.SetFloat("_Rotation", skyboxRotation);
    }


    // Ticks Rendering
    private IEnumerator TickRenderEnvironment()
    {
        while(rendering)
        {
            float dot = Mathf.Clamp01(Vector3.Dot(directionalLight.transform.forward, Vector3.down));
            float i = ((maxLightIntensity - minLightIntensity) * Mathf.Clamp01(dot)) + minLightIntensity;

            // Light Rendering
            directionalLight.color = nightDayColour.Evaluate(dot);
            RenderSettings.ambientLight = directionalLight.color;
            RenderSettings.ambientIntensity = ((maxAmbient - minAmbient) * dot) + minAmbient;
            directionalLight.intensity = i;

            // Fog Rendering
            RenderSettings.fogColor = nightDayFogColour.Evaluate(dot);
            RenderSettings.fogDensity = fogDensityCurve.Evaluate(dot) * fogScale;

            // Atmospheric Density Rendering
            i = ((maxAtmosDensity - minAtmosDensity) * dot) + minAtmosDensity;
            RenderSettings.skybox.SetFloat("_Exposure", i);

            // Performance Control
            yield return new WaitForSeconds(renderTimeStep);
        }
    }
}

This is only a very basic system and parts will need to depend on external methods already. Like fog will be dependent on the season, air temperature, humidity, air’s saturation point, current air pressure, incoming fronts, time of day. Blah de blah de blah. Point is there’ll be a fair few different things interacting with each other. I’m wondering whether I could get away with it being all in one class, which maybe references a ScriptableObject for world variables. Or whether I need to make a ‘Wind Module’, ‘Humidity Module’, and a master place where the world’s data is sustained to switch between states.

Any takers on advice for architecting this?

Personally, for ease of managing the code, and adding/removing newer weather conditions, I’d abstract each type of weather into its own class.

Something like:

WeatherManager - a component that handles the current weather conditions

IWeatherCondition - an interface for components of the possible weather conditions

I would probably structure it as a Parent GameObject and child GameObjects.

GameObject (WeatherManager)

  • GameObject (RainStorm)
  • GameObject (Sunny)
    …
  • GameObject (SnowStorm)

A weather condition could be singular. Or a composited weather condition.

CompositeWeatherCondition : IWeatherCondition

  • List = [ Fog, Drizzle ]

Each of these weather conditions could be considered a state. And have transitional information about them for when they’d become active. The WeatherManager uses this transitional information to determine when a different state should be entered or not.

1 Like

I’d also say that something like ‘night/day’ wouldn’t necessarily be part of the weather system, just an accent to it.

So you might have a NightDayController that is also attached to the same GameObject as the WeatherManager. This way any weather condition that depends on the night/day stats (accenting the night/day skybox for instance), can access it. But it’s still its own thing.

1 Like

I like your thinking @lordofduct … Looks like the way I’ll approach it then lol. Thanks!

You want to model out what you are building first. Are you building an actual atmospheric model, or are you simply faking things for ambience.

The structure for an actual atmopheric model would look very different.

It’s going to be as near a genuine simulation as I can model. It’ll be for the back end variables as much as the looks. So you reckon it’s worthwhile drawing out a little model on paper or something first to figure it out, but using something similar to loadofducts suggestion, yeah?

Also @lordofduct I do agree with your composition, but I had a question about it. Is there a specific reason why you’d suggest an interface over inheritance? I use interfaces sometimes, but I do tend to follow inheritance as my go to (possibly a bad habit I’m about to correct…?). Can you please share?