Files
grass-cutters/Assets/Scripts/Player/CrewBody.cs
NiemandKG bbf73a6b96 Frame time on a big farm, round two: riding layer, instanced bundles
- 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>
2026-11-09 20:35:35 +06:00

120 lines
5.3 KiB
C#

using System;
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// The crew's collision capsule. Unity's CharacterController is welded to the world's Y axis, which is no good on a
/// ball, so this one stands along the local up: it sweeps through solid things (deck, machines, rocks), climbs low
/// steps, and takes the soil straight from <see cref="Planet.Altitude"/>.
/// </summary>
[RequireComponent(typeof(CapsuleCollider))]
public class CrewBody : MonoBehaviour
{
const float Skin = 0.03f, StepUp = 0.4f, FloorDot = 0.6f, StickDown = 0.3f;
const int Solid = (1 << Layers.Default) | (1 << Physics.IgnoreRaycastLayer) | (1 << Layers.Prop) | (1 << Layers.Riding) | (1 << Layers.Vehicle) | (1 << Layers.Player) | (1 << Layers.Build);
public bool isGrounded { get; private set; }
/// <summary>Walked into something: what was hit and which way we were going.</summary>
public event Action<RaycastHit, Vector3> Bumped;
CapsuleCollider _cap;
readonly Collider[] _near = new Collider[16];
void Awake() { _cap = GetComponent<CapsuleCollider>(); }
void OnEnable() { if (_cap) _cap.enabled = true; }
void OnDisable() { if (_cap) _cap.enabled = false; isGrounded = false; }
void Ends(Vector3 pos, Vector3 up, out Vector3 low, out Vector3 high)
{
low = pos + up * _cap.radius;
high = pos + up * (_cap.height - _cap.radius);
}
public void Move(Vector3 delta)
{
var pos = transform.position;
var up = Planet.Up(pos);
float rise = Vector3.Dot(delta, up);
var flat = delta - up * rise;
bool was = isGrounded;
isGrounded = false;
Unstick(ref pos, up);
float lifted = was ? Sweep(ref pos, up, StepUp, up) : 0f;
Slide(ref pos, flat, up);
if (rise > 0f && !was) Sweep(ref pos, up, rise, up);
else if (rise > 0f) Sweep(ref pos, up, rise - Mathf.Min(rise, lifted), up);
if (rise <= 0f || lifted > rise)
{
float down = Mathf.Max(0f, lifted - Mathf.Max(0f, rise)) - Mathf.Min(0f, rise);
if (was && rise <= 0f) down += 0.04f;
if (Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, -up, out var hit, down + Skin, Solid, QueryTriggerInteraction.Ignore))
{
if (hit.distance > 0f) pos -= up * Mathf.Max(0f, hit.distance - Skin);
if (Vector3.Dot(hit.normal, up) >= FloorDot) isGrounded = true;
}
else pos -= up * down;
}
// the soil itself
float alt = Planet.Altitude(pos);
if (alt <= 0f || (was && rise <= 0f && alt < StickDown && !isGrounded))
{
pos -= Planet.Up(pos) * alt;
isGrounded = true;
}
transform.position = pos;
}
Vector3 Low(Vector3 pos, Vector3 up) { Ends(pos, up, out var l, out _); return l; }
Vector3 High(Vector3 pos, Vector3 up) { Ends(pos, up, out _, out var h); return h; }
/// <summary>Move straight along dir until something stops us. Returns how far we got.</summary>
float Sweep(ref Vector3 pos, Vector3 dir, float dist, Vector3 up)
{
if (dist <= 0f) return 0f;
float go = dist;
if (Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, dir, out var hit, dist + Skin, Solid, QueryTriggerInteraction.Ignore))
go = hit.distance > 0f ? Mathf.Max(0f, hit.distance - Skin) : 0f;
pos += dir * go;
return go;
}
void Slide(ref Vector3 pos, Vector3 move, Vector3 up)
{
for (int i = 0; i < 4 && move.sqrMagnitude > 1e-8f; i++)
{
float dist = move.magnitude;
var dir = move / dist;
if (!Physics.CapsuleCast(Low(pos, up), High(pos, up), _cap.radius - Skin, dir, out var hit, dist + Skin, Solid, QueryTriggerInteraction.Ignore))
{
pos += move;
return;
}
if (hit.distance <= 0f) { Unstick(ref pos, up); continue; }
float go = Mathf.Max(0f, hit.distance - Skin);
pos += dir * go;
Bumped?.Invoke(hit, dir);
move = Vector3.ProjectOnPlane(dir * (dist - go), hit.normal);
// a wall never lifts you: only a walkable slope may
if (Vector3.Dot(hit.normal, up) < FloorDot) move -= up * Mathf.Max(0f, Vector3.Dot(move, up));
}
}
/// <summary>Spawned, teleported or shoved into something: step out of it.</summary>
void Unstick(ref Vector3 pos, Vector3 up)
{
int n = Physics.OverlapCapsuleNonAlloc(Low(pos, up), High(pos, up), _cap.radius, _near, Solid, QueryTriggerInteraction.Ignore);
var turn = Quaternion.FromToRotation(Vector3.up, up);
for (int i = 0; i < n; i++)
{
var c = _near[i];
if (c == _cap || c.isTrigger) continue;
if (Physics.ComputePenetration(_cap, pos, turn, c, c.transform.position, c.transform.rotation, out var dir, out var dist))
pos += dir * dist;
}
}
}
}