Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
174
Assets/Scripts/World/DayNight.cs
Normal file
174
Assets/Scripts/World/DayNight.cs
Normal file
@ -0,0 +1,174 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public static class Sun
|
||||
{
|
||||
const float Lean = 25f * Mathf.Deg2Rad;
|
||||
/// <summary>The time of day before the crew has one: the morning light of the main menu.</summary>
|
||||
const float MenuPhase = 0.9f;
|
||||
|
||||
public static float Phase { get; private set; } = MenuPhase;
|
||||
/// <summary>Which way the sun is, from anywhere.</summary>
|
||||
public static Vector3 Dir { get; private set; } = Vector3.up;
|
||||
/// <summary>Test bots hold the sun still so their pictures compare. Below 0: it moves.</summary>
|
||||
public static float Pinned = -1f;
|
||||
|
||||
/// <summary>1 with the sun overhead, 0 on the horizon, below that it's night.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>What the time of day has to be for the given offset of the clock to read this phase.</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
/// <summary>0 in daylight, 1 in the dark, where the camera is.</summary>
|
||||
public static float Dark { get; private set; }
|
||||
/// <summary>1 at sunrise and sunset, when the sun sits on the horizon; 0 the rest of the time.</summary>
|
||||
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;
|
||||
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;
|
||||
var volume = FindAnyObjectByType<Volume>();
|
||||
if (volume && volume.profile.TryGet(out _grade)) _exposure = _grade.postExposure.value;
|
||||
if (!GetComponent<WeatherFx>()) gameObject.AddComponent<WeatherFx>();
|
||||
}
|
||||
|
||||
public static void SetView(int view) => View = view;
|
||||
|
||||
/// <summary>The trailer's work light: the lamp hangs from this instead of the camera.</summary>
|
||||
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);
|
||||
}
|
||||
|
||||
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<Light>();
|
||||
_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;
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user