Files
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
4.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace GrassCutter
{
/// <summary>
/// Steps every bundle and every grabbable thing from one FixedUpdate. A big farm has two thousand bundles, and a Unity
/// message per object per physics step (two scripts each) cost more than what they do, which is mostly nothing.
/// </summary>
public class CargoTick : MonoBehaviour
{
static CargoTick s_on;
public static void Ensure()
{
if (s_on) return;
var go = new GameObject("CargoTick");
DontDestroyOnLoad(go);
s_on = go.AddComponent<CargoTick>();
}
void FixedUpdate()
{
// backwards: a step can hand a bundle to a depot, which takes it off the list
var g = Grabbable.All;
for (int i = g.Count - 1; i >= 0; i--)
if (i < g.Count && g[i]) g[i].Step();
var c = Cargo.All;
for (int i = c.Count - 1; i >= 0; i--)
if (i < c.Count && c[i]) c[i].Step();
}
// ───────────────────────── drawing ─────────────────────────
//
// Two thousand bundles were two thousand renderers: culled, sorted and drawn one by one, in the main view and
// every shadow cascade. Their own renderers are off and they go down here as a few instanced draws, one per
// model. One in someone's hands draws itself as before (it may be on another layer, in first person).
/// <summary>Off: every bundle draws itself again (a switch for tests).</summary>
public static bool Instanced = true;
sealed class Group
{
public Mesh mesh;
public int sub;
public Material mat;
public Matrix4x4[] m = new Matrix4x4[256];
public int n;
public Bounds bounds;
}
readonly Dictionary<(Mesh, int, Material), Group> _groups = new Dictionary<(Mesh, int, Material), Group>();
readonly List<Group> _order = new List<Group>();
static readonly Dictionary<Material, Material> s_instanced = new Dictionary<Material, Material>();
const int Chunk = 1023;
static Material InstancedCopy(Material m)
{
if (!s_instanced.TryGetValue(m, out var copy))
{
copy = new Material(m) { name = m.name + " (instanced)", enableInstancing = true };
s_instanced[m] = copy;
}
return copy;
}
void LateUpdate()
{
foreach (var g in _order) g.n = 0;
var all = Cargo.All;
for (int i = 0; i < all.Count; i++)
{
var c = all[i];
if (!c) continue;
var r = c.Look;
if (!r) continue;
bool mine = Instanced && !c.Grab.IsHeld && c.gameObject.activeInHierarchy;
if (r.enabled == mine) r.enabled = !mine;
if (!mine) continue;
var mesh = c.LookMesh;
var mats = c.LookMats;
if (!mesh || mats == null) { r.enabled = true; continue; }
var m = r.localToWorldMatrix;
var at = new Vector3(m.m03, m.m13, m.m23);
for (int sub = 0; sub < mesh.subMeshCount && sub < mats.Length; sub++)
{
var mat = mats[sub];
if (!mat) continue;
var key = (mesh, sub, mat);
if (!_groups.TryGetValue(key, out var g))
{
g = new Group { mesh = mesh, sub = sub, mat = InstancedCopy(mat) };
_groups[key] = g;
_order.Add(g);
}
if (g.n == g.m.Length) System.Array.Resize(ref g.m, g.m.Length * 2);
if (g.n == 0) g.bounds = new Bounds(at, Vector3.one); else g.bounds.Encapsulate(at);
g.m[g.n++] = m;
}
}
foreach (var g in _order)
{
if (g.n == 0) continue;
g.bounds.Expand(2f);
var rp = new RenderParams(g.mat)
{
worldBounds = g.bounds,
layer = Layers.Prop,
shadowCastingMode = ShadowCastingMode.On,
receiveShadows = true,
lightProbeUsage = LightProbeUsage.Off,
};
for (int start = 0; start < g.n; start += Chunk)
Graphics.RenderMeshInstanced(rp, g.mesh, g.sub, g.m, Mathf.Min(Chunk, g.n - start), start);
}
}
}
}