Skip to content

Game.Prefabs.Climate.ClimatePrefab

Assembly: Game
Namespace: Game.Prefabs.Climate

Type: class

Base: PrefabBase, IJsonWritable

Summary:
ClimatePrefab defines a procedural climate profile used by the game's climate system. It holds latitude/longitude, seasonal definitions and default weather prefabs, and generates time-based AnimationCurves for temperature, precipitation, cloudiness, aurora and fog. It provides utilities to rebuild those curves deterministically from a seed, query seasons by time, and serialize select data to JSON. The class also contains three small nested structs used to aggregate season-based curve inputs: SeasonTempCurves, SeasonPrecipCurves and SeasonAuroraCurves.


Fields

  • public float m_Latitude
    Latitude of the climate location (range -90 to 90). Used to represent the climate's position on the globe.

  • public float m_Longitude
    Longitude of the climate location (range -180 to 180).

  • public float m_FreezingTemperature
    Temperature threshold used for freezing logic (units as used by the game).

  • public AnimationCurve m_Temperature
    Generated 12-hour based (sampled at 288 samples) temperature curve for the climate.

  • public AnimationCurve m_Precipitation
    Generated precipitation amount curve (sampled at 1728 samples in reconstruction).

  • public AnimationCurve m_Cloudiness
    Generated cloudiness curve.

  • public AnimationCurve m_Aurora
    Generated aurora activity curve.

  • public AnimationCurve m_Fog
    Generated fog likelihood curve.

  • public WeatherPrefab m_DefaultWeather
    A default weather prefab reference.

  • public WeatherPrefab[] m_DefaultWeathers
    Array of default weather prefabs used by this climate.

  • public ClimateSystem.SeasonInfo[] m_Seasons
    Season definitions used to generate seasonal curves (start times, deviations, precipitation parameters, aurora settings, etc).

  • [HideInInspector] public int m_RandomSeed
    Random seed used when rebuilding the procedural curves. If zero, runtime time is used to seed.

  • public const int kYearDuration
    Constant (12) representing the atmospheric year length used in these curves.

  • private int[] m_SeasonsOrder
    Internal cached ordering of season indices sorted by start time.

  • private const float k90PercentileToStdDev
    Internal constant used when converting between min/max ranges and Gaussian standard deviation (value 0.78003126f).

Properties

  • public Bounds1 temperatureRange { get; }
    Computes and returns min/max bounds for the generated temperature curve by sampling m_Temperature at 288 points over the 12-unit year.

  • public float averageCloudiness { get; }
    Average value of m_Cloudiness sampled at 288 points.

  • public float averagePrecipitation { get; }
    Average value of m_Precipitation sampled at 288 points.

Constructors

  • public ClimatePrefab()
    No explicit constructor is defined in the source; the type uses the default parameterless constructor. Initialization of procedural curves is done via RebuildCurves().

Methods

  • public void Write(IJsonWriter writer)
    Serializes selected climate properties (latitude, longitude, freezing temperature and seasons array) to a provided JSON writer. Useful for exporting climate configuration.

  • public void RebuildCurves()
    Regenerates all procedural curves (temperature, precipitation, aurora, fog) from the current m_RandomSeed (or a time-based seed if zero). Calls EnsureSeasonsOrder and per-curve rebuild methods.

  • internal void EnsureSeasonsOrder(bool force = false)
    Ensures m_SeasonsOrder is populated and sorted by each season's start time. If force is true, recomputes even if already present.

  • private static AnimationCurve GenCurveFromMinMax(int keyCount, AnimationCurve cmin, AnimationCurve cmax, uint seed, float minValue, float maxValue)
    Generates an AnimationCurve by sampling min/max curves, computing a Gaussian-distributed value for each sample between the min/max, and finally looping & clamping the curve to the provided range.

  • private void RebuildTemperatureCurves(uint seed)
    Creates daily (night/day) temperature component curves based on season temperature inputs, synthesizes a smooth 288-sample per-year temperature curve by blending night/day components with a cosine-based daily blend and Perlin-like noise, and stores result in m_Temperature.

  • private void RebuildPrecipitationCurves(uint seed)
    Synthesizes cloudiness and precipitation curves using seasonal precip parameters and turbulence. Cloudiness is built first (1728 samples) then precipitation is generated and modulated by cloudiness. Results stored in m_Cloudiness and m_Precipitation.

  • private static float SmoothNoise(float x, float y = 0f)
    Wrapper around noise.snoise to produce smooth noise used when procedurally perturbing generated curves.

  • private void RebuildAuroraCurves(uint seed)
    Generates aurora amount and chance curves (288 samples) from seasonal aurora settings and noise modulation, stored in m_Aurora.

  • private void RebuildFogCurves(uint seed)
    Generates a fog likelihood curve (288 samples) from current cloudiness, precipitation and recent temperature changes. Stores result in m_Fog.

  • private static float GaussianRandom(float mean, float dev, ref Unity.Mathematics.Random rng)
    Draws a value from an approximate Gaussian (Box–Muller transform) truncated around the mean (+/- 2*dev) with a retry loop.

  • public SeasonTempCurves CreateSeasonTemperatureCurves()
    Constructs four AnimationCurves (nightMin, nightMax, dayMin, dayMax) by sampling each season's temperature parameters at their mid-times, and prepares them for looping.

  • public (ClimateSystem.SeasonInfo, float) GetSeasonAndMidTime(int index)
    Returns the SeasonInfo of the requested season index (respecting the internal season ordering) and its mid-time (in the 12-unit scale).

  • public int CountElapsedSeasons(float startTime, float elapsedTime)
    Counts how many season boundaries are crossed when advancing elapsedTime from startTime. Useful for tracking season transitions over an interval.

  • private bool Intersect(float startTime, float elapsedTime, float seasonStart, float seasonEnd)
    Internal helper used by CountElapsedSeasons to test if a time interval intersects a season interval (handles wrap-around across year boundary).

  • public (ClimateSystem.SeasonInfo, float, float) FindSeasonByTime(float time)
    Finds which season contains the given time value (0..1 normalized across the full year) and returns (season, seasonStart, seasonEnd). Handles wrap-around.

  • private float GetSeasonMidTime(int index)
    Computes the mid-time (in 0..12) between season index and the next season.

  • public SeasonPrecipCurves CreateSeasonPrecipCurves()
    Builds the set of AnimationCurves used as inputs for precipitation generation (cloudChance, cloudAmountMin/Max, precipChance, precipAmountMin/Max, turbulence) from season definitions, and loops them.

  • private static void LoopCurve(AnimationCurve curve, float minValue = 0f, float maxValue = 1f)
    Sets wrap mode to Loop, smooths tangents, clamps key values to min/max and duplicates the first key at +12 to ensure seamless looping across the 12-unit year. Used by many builders.

  • public SeasonAuroraCurves CreateSeasonAuroraCurves()
    Constructs aurora amount and chance curves from season aurora parameters and loops/clamps them.

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Registers the components required by this prefab for entity conversion—adds ReadWrite in addition to base PrefabBase components.

  • public override void GetDependencies(List<PrefabBase> prefabs)
    Adds referenced prefabs (season prefabs, default weather prefabs and active entries in m_DefaultWeathers) to the dependency list so they are included when loading this prefab.

  • private static AnimationCurve GenCurveFromMinMax(...) and other private helpers (SmoothNoise, GaussianRandom)
    Various helpers to create realistic, noise-perturbed animation curves from seasonal min/max and chance parameters.

Usage Example

// Example: regenerate climate curves deterministically and write metadata to JSON.
ClimatePrefab climate = /* obtain ClimatePrefab instance from asset or editor */;
climate.m_RandomSeed = 12345;   // deterministic seed
climate.RebuildCurves();        // generate m_Temperature, m_Cloudiness, m_Precipitation, m_Aurora, m_Fog

// Inspect summary data
Bounds1 tempRange = climate.temperatureRange;
float avgCloud = climate.averageCloudiness;
float avgPrecip = climate.averagePrecipitation;

// Serialize core settings
IJsonWriter writer = /* create or obtain writer */;
climate.Write(writer);

Notes and tips: - RebuildCurves is the primary entry point to (re)generate all procedural data after editing seasons or seed. The generated AnimationCurves are sampled at game-run resolutions (temperature: 288 samples, precipitation/cloudiness: 1728 samples). - Seasons are defined in m_Seasons as ClimateSystem.SeasonInfo entries; EnsureSeasonsOrder sorts them by m_StartTime and is called automatically before rebuilding curves. - LoopCurve ensures all generated curves are seamless across the year boundary by duplicating the first key at time +12. - The class expects the seasonal inputs (amounts, chances, deviations, turbulence) to be percentages in some fields and converts them to normalized floats when generating curves.