using Unity.Netcode; using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; namespace GrassCutter { /// /// The sun goes round the ball, the same for everyone: it stands overhead at the base at phase 0 and its path leans toward /// the north, so the day at the base runs a little longer than the night. Each place has its own time of day. /// public static class Sun { const float Lean = 25f * Mathf.Deg2Rad; /// The time of day before the crew has one: the morning light of the main menu. const float MenuPhase = 0.9f; public static float Phase { get; private set; } = MenuPhase; /// Which way the sun is, from anywhere. public static Vector3 Dir { get; private set; } = Vector3.up; /// Test bots hold the sun still so their pictures compare. Below 0: it moves. public static float Pinned = -1f; /// 1 with the sun overhead, 0 on the horizon, below that it's night. public static float Height(Vector3 up) => Vector3.Dot(up, Dir); public static void Tick() { var ts = TeamState.Instance; var net = NetworkManager.Singleton; if (Pinned >= 0f) Phase = Pinned; else if (ts && ts.IsSpawned && net) Phase = Mathf.Repeat(ts.DayOffset.Value + (float)(net.ServerTime.Time / GameDefs.DayLength % 1.0), 1f); else Phase = MenuPhase; var a = new Vector3(Mathf.Cos(Lean), Mathf.Sin(Lean), 0f); var b = new Vector3(-Mathf.Sin(Lean), Mathf.Cos(Lean), 0f); float t = Phase * Mathf.PI * 2f; Dir = (Mathf.Cos(Lean) * (Mathf.Cos(t) * b + Mathf.Sin(t) * Vector3.forward) + Mathf.Sin(Lean) * a).normalized; } /// What the time of day has to be for the given offset of the clock to read this phase. public static float OffsetFor(float phase) { var net = NetworkManager.Singleton; return Mathf.Repeat(phase - (net ? (float)(net.ServerTime.Time / GameDefs.DayLength % 1.0) : 0f), 1f); } } /// /// Sets the light from the sun's height over the camera, carries the headlamp, and turns the night vision on. Grass and soil /// have shaders of their own that only know the sun, so the lamp is also handed to them directly. /// public class DayNight : MonoBehaviour { public const int Off = 0, Lamp = 1, NightVision = 2; const float LampRange = 42f; public static int View { get; private set; } = Lamp; /// 0 in daylight, 1 in the dark, where the camera is. public static float Dark { get; private set; } /// 1 at sunrise and sunset, when the sun sits on the horizon; 0 the rest of the time. public static float Dusk { get; private set; } static readonly int DuskId = Shader.PropertyToID("_GcDusk"); static readonly Color DuskFog = new Color(0.98f, 0.6f, 0.42f), DuskSky = new Color(1f, 0.62f, 0.5f); static readonly int NightId = Shader.PropertyToID("_GcNight"), LampPosId = Shader.PropertyToID("_GcLampPos"), LampDirId = Shader.PropertyToID("_GcLampDir"), SunId = Shader.PropertyToID("_SunDir"), CloudId = Shader.PropertyToID("_GcCloudShade"); const float CloudShade = 0.5f; static readonly Color NightFog = new Color(0.05f, 0.07f, 0.12f); static readonly Color DuskSun = new Color(1f, 0.5f, 0.24f); const float MoonPower = 0.55f; static readonly Color Moon = new Color(0.56f, 0.68f, 1f); static readonly Color NightSky = new Color(0.13f, 0.17f, 0.3f), NightMid = new Color(0.09f, 0.12f, 0.21f), NightLow = new Color(0.05f, 0.07f, 0.11f); Light _sun, _lamp; /// Rendering layer of things the headlamp leaves alone: whatever is held right in front of the eyes /// and machines with lights of their own, which it only blew out to white. The sun and the lampposts still light them. public const int NoLamp = 1 << 1; public static void KeepOutOfLamp(GameObject root) { foreach (var r in root.GetComponentsInChildren(true)) r.renderingLayerMask = NoLamp; // a machine's own lights still light it foreach (var l in root.GetComponentsInChildren(true)) l.renderingLayerMask |= NoLamp; } float _sunPower; Color _sunColor, _fog, _sky, _mid, _low; ColorAdjustments _grade; float _exposure; void Start() { _sun = RenderSettings.sun; if (_sun) { _sunPower = _sun.intensity; _sunColor = _sun.color; } _fog = RenderSettings.fogColor; _sky = RenderSettings.ambientSkyColor; _mid = RenderSettings.ambientEquatorColor; _low = RenderSettings.ambientGroundColor; // the scene's own post volume, not the enhanced look laid over it (LightingStyle) foreach (var volume in FindObjectsByType(FindObjectsSortMode.None)) if (!LightingStyle.IsOurs(volume) && volume.profile.TryGet(out _grade)) { _exposure = _grade.postExposure.value; break; } if (!GetComponent()) gameObject.AddComponent(); } public static void SetView(int view) => View = view; /// The trailer's work light: the lamp hangs from this instead of the camera. public static Transform Rig; void LateUpdate() { var cam = CameraFx.Cam ? CameraFx.Cam : Camera.main; if (!cam) return; Sun.Tick(); float h = Sun.Height(Planet.LocalUp); float day = Mathf.SmoothStep(0f, 1f, Mathf.InverseLerp(-0.08f, 0.16f, h)); Dark = 1f - day; float dusk = Mathf.Clamp01(1f - Mathf.Abs(h - 0.03f) / 0.2f); Dusk = dusk * dusk * (3f - 2f * dusk); float clear = 1f - WeatherFx.Storm * 0.75f - WeatherFx.Rain * 0.25f; Shader.SetGlobalFloat(DuskId, Dusk * Mathf.Clamp01(clear)); float gloom = 1f - WeatherFx.Storm * 0.4f - WeatherFx.Rain * 0.15f; if (_sun) { // after dark the same light is the moon, opposite the sun: it comes up once the sun has gone out float moon = Mathf.InverseLerp(-0.1f, -0.32f, h); var from = h < -0.09f ? -Sun.Dir : Sun.Dir; _sun.transform.rotation = Quaternion.LookRotation(-from, Mathf.Abs(from.y) > 0.99f ? Vector3.forward : Vector3.up); _sun.intensity = Mathf.Max(_sunPower * day * WeatherFx.SunMul, MoonPower * moon * WeatherFx.SunMul) + WeatherFx.Flash * 2.2f; _sun.color = moon > 0f ? Moon : Color.Lerp(DuskSun, _sunColor, Mathf.InverseLerp(0f, 0.32f, h)); } var flash = new Color(0.55f, 0.6f, 0.75f) * WeatherFx.Flash; var warm = Color.Lerp(Color.white, DuskSky, Dusk * day * 0.6f); RenderSettings.ambientSkyColor = Color.Lerp(NightSky, _sky * warm, day) * gloom + flash; RenderSettings.ambientEquatorColor = Color.Lerp(NightMid, _mid * warm, day) * gloom + flash; RenderSettings.ambientGroundColor = Color.Lerp(NightLow, _low, day) * gloom; var air = Color.Lerp(Color.Lerp(_fog, DuskFog, Dusk * day * 0.5f), WeatherFx.FogTint, WeatherFx.Murk * 0.8f); RenderSettings.fogColor = Color.Lerp(air, NightFog, Dark); Shader.SetGlobalFloat(NightId, Dark); Shader.SetGlobalFloat(CloudId, CloudShade * day * (1f - WeatherFx.Storm * 0.6f)); Fx.Motes(cam.transform.position, Dark); var sky = RenderSettings.skybox; if (sky) sky.SetVector(SunId, Quaternion.Inverse(Planet.Frame) * Sun.Dir); TickLamp(cam); LightingStyle.Tick(); } void TickLamp(Camera cam) { var me = PlayerAgent.Local; if (Ctl.LampPressed && me) { View = View == Lamp && me.HasGear(GearId.NightVision) ? NightVision : View == Off ? Lamp : Off; Sfx.Play("ui_click", 0.6f, 1.2f); Hud.Toast(View == NightVision ? Loc.T("Night vision on") : View == Lamp ? Loc.T("Headlamp on") : Loc.T("Headlamp off"), Hud.ToastKind.Info); } if (!me && View == NightVision) View = Lamp; if (!_lamp) { _lamp = new GameObject("Headlamp").AddComponent(); _lamp.type = LightType.Spot; _lamp.spotAngle = 62f; _lamp.innerSpotAngle = 34f; _lamp.range = LampRange; _lamp.color = new Color(1f, 0.95f, 0.85f); _lamp.shadows = LightShadows.None; _lamp.renderingLayerMask = ~NoLamp; // every other light keeps lighting what the headlamp leaves alone foreach (var l in FindObjectsByType(FindObjectsInactive.Include)) if (l != _lamp) l.renderingLayerMask |= NoLamp; } if (_sun && _sun.renderingLayerMask != -1) _sun.renderingLayerMask = -1; var from = Rig ? Rig : cam.transform; _lamp.transform.SetParent(from, false); // a machine's floodlights join in while you drive it float flood = me && me.Vehicle ? 1f + me.Vehicle.Level(TuneId.Lights) * 0.6f : 1f; float power = View == Lamp && me ? Dark * flood : 0f; float reach = LampRange * Mathf.Sqrt(flood); _lamp.intensity = power * 9f; _lamp.range = reach; var t = from; Shader.SetGlobalVector(LampPosId, new Vector4(t.position.x, t.position.y, t.position.z, reach)); Shader.SetGlobalVector(LampDirId, new Vector4(t.forward.x, t.forward.y, t.forward.z, power)); if (_grade) { bool green = View == NightVision; _grade.colorFilter.Override(green ? new Color(0.45f, 1f, 0.5f) : Color.white); _grade.postExposure.Override(_exposure + GameSettings.Brightness + (green ? 3.2f * Dark : 0f)); } } } }