- Held tools sit lower and further right; the hammer faces forward and strikes about the camera's right - Headlamp leaves held items, treats and vending machines alone (rendering layer NoLamp); other lights keep lighting them - Hay grounded on the floor in world space (it floated) - Deck fence opens at each ladder, rail colliders split around it; ladders moved to 2 m off the edge middle, clear of the intakes; climbing goes over a rail only if one is in the way - Crew stand to the soil's up (Planet.Level): flat round the base, the ball's up past it, so the horizon stays level on a small planet - Convex roof hulls for the crew cabin, garage, pitched roof and windmill tower; cabin box lowered to the eaves - Rain and acid rain hits differ by surface: soil, grass, wood, metal - Idle motion: dishes pan, sways sway, sensors turn, eyes blink, windmill turns in the breeze, reels and parked rotors tick over - Belt: take tools off at the supply wall; hold E at a vending machine for a pack of 3 on the belt, LMB has one - New meadow pieces: boulders, slab stones, crystals, mushrooms, stump, bulb plants - -demofix checks for all of the above; loc rows in all 11 tables Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
10 KiB
C#
193 lines
10 KiB
C#
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;
|
|
|
|
/// <summary>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.</summary>
|
|
public const int NoLamp = 1 << 1;
|
|
|
|
public static void KeepOutOfLamp(GameObject root)
|
|
{
|
|
foreach (var r in root.GetComponentsInChildren<Renderer>(true)) r.renderingLayerMask = NoLamp;
|
|
// a machine's own lights still light it
|
|
foreach (var l in root.GetComponentsInChildren<Light>(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<Volume>(FindObjectsSortMode.None))
|
|
if (!LightingStyle.IsOurs(volume) && volume.profile.TryGet(out _grade)) { _exposure = _grade.postExposure.value; break; }
|
|
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);
|
|
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<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;
|
|
_lamp.renderingLayerMask = ~NoLamp;
|
|
// every other light keeps lighting what the headlamp leaves alone
|
|
foreach (var l in FindObjectsByType<Light>(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));
|
|
}
|
|
}
|
|
}
|
|
}
|