- Bundles on a belt move to a Riding layer that ignores the belts and the static world: every belt's catch trigger called OnTriggerStay (with a GetComponent) for each bundle riding it, every physics step. - Bundles draw as instanced batches from CargoTick (their own renderers off, one in someone's hands draws itself), not as 2000 renderers to cull, sort and draw in the view and every shadow cascade. - The main camera skips front-to-back sorting: the GPU has time to spare. - BeltNet.Belted and DronePad.Belted are cached until the build changes; splitters on the host look only at their own items. On the player's Mommy save, 2560x1440: base 14-15 ms -> 7.7 ms (~130 fps), meadow 11.5-12 ms -> 6.8-7.5 ms (~135-145 fps). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
2.9 KiB
C#
70 lines
2.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>Trauma-based screen shake + FOV kicks, applied on top of whatever positions the camera.</summary>
|
|
public class CameraFx : MonoBehaviour
|
|
{
|
|
static CameraFx _i;
|
|
public static Camera Cam { get; private set; }
|
|
public static float BaseFov = 78f;
|
|
|
|
float _trauma;
|
|
float _fovKick, _fovTarget;
|
|
Vector3 _offset;
|
|
Quaternion _rotOffset = Quaternion.identity;
|
|
|
|
public static float ShakeScale = 1f;
|
|
public static float Trauma => _i ? _i._trauma : 0f;
|
|
|
|
void Awake()
|
|
{
|
|
_i = this;
|
|
Cam = GetComponent<Camera>();
|
|
// the GPU has time to spare, the CPU doesn't: sorting thousands of draws front to back cost more than the overdraw it saves
|
|
if (Cam) Cam.opaqueSortMode = UnityEngine.Rendering.OpaqueSortMode.NoDistanceSort;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds trauma, but each hit can only raise it to a ceiling set by its own size: a stream of tiny pops
|
|
/// (finger, rolling pin, road roller) settles into a light buzz instead of stacking up into an earthquake.
|
|
/// </summary>
|
|
public static void Shake(float amount)
|
|
{
|
|
if (!_i || amount <= 0f || ShakeScale <= 0f) return;
|
|
float ceiling = Mathf.Lerp(0.28f, 0.8f, Mathf.InverseLerp(0.05f, 0.6f, amount)) * Mathf.Min(1f, ShakeScale);
|
|
if (_i._trauma < ceiling) _i._trauma = Mathf.Min(ceiling, _i._trauma + amount * ShakeScale);
|
|
}
|
|
|
|
public static void FovKick(float degrees)
|
|
{
|
|
if (_i) _i._fovKick = Mathf.Clamp(_i._fovKick + degrees * Mathf.Min(1f, ShakeScale), -6f, 6f);
|
|
}
|
|
|
|
/// <summary>Extra sustained FOV (e.g. sprinting) that the camera eases toward.</summary>
|
|
public static void FovTarget(float extra)
|
|
{
|
|
if (_i) _i._fovTarget = extra;
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
// undo last frame's offsets (the rig re-positions us each frame anyway, but stay safe)
|
|
transform.localPosition -= _offset;
|
|
transform.localRotation *= Quaternion.Inverse(_rotOffset);
|
|
|
|
_trauma = Mathf.MoveTowards(_trauma, 0f, Time.deltaTime * 1.8f);
|
|
float s = _trauma * _trauma;
|
|
float t = Time.time * 24f;
|
|
_offset = new Vector3(Mathf.PerlinNoise(t, 0.3f) - 0.5f, Mathf.PerlinNoise(0.7f, t) - 0.5f, 0f) * (0.07f * s);
|
|
_rotOffset = Quaternion.Euler((Mathf.PerlinNoise(t, 5f) - 0.5f) * 3.5f * s, (Mathf.PerlinNoise(9f, t) - 0.5f) * 3.5f * s, (Mathf.PerlinNoise(t, 13f) - 0.5f) * 4.5f * s);
|
|
transform.localPosition += _offset;
|
|
transform.localRotation *= _rotOffset;
|
|
|
|
_fovKick = Mathf.Lerp(_fovKick, 0f, Time.deltaTime * 9f);
|
|
float target = BaseFov + _fovTarget + _fovKick;
|
|
if (Cam) Cam.fieldOfView = Mathf.Lerp(Cam.fieldOfView, target, Time.deltaTime * 12f);
|
|
}
|
|
}
|
|
}
|