using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace GrassCutter
{
///
/// 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.
///
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();
}
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).
/// Off: every bundle draws itself again (a switch for tests).
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 _order = new List();
static readonly Dictionary s_instanced = new Dictionary();
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);
}
}
}
}