using System.Collections.Generic;
using UnityEngine;
namespace GrassCutter
{
///
/// Remembers the imported local rotation of model parts (Blender FBX nodes carry a 90° X rotation) so procedural
/// animation can rotate *on top of* it instead of overwriting it.
///
public static class BaseRot
{
static readonly Dictionary _base = new Dictionary();
public static Quaternion Of(Transform t)
{
if (!t) return Quaternion.identity;
if (!_base.TryGetValue(t, out var q))
{
q = t.localRotation;
_base[t] = q;
if (_base.Count > 4096) Prune();
}
return q;
}
/// Which local axis (0=x,1=y,2=z) of t points along the given world direction.
public static int LocalAxisFor(Transform t, Vector3 worldDir)
{
var l = t.InverseTransformDirection(worldDir);
l = new Vector3(Mathf.Abs(l.x), Mathf.Abs(l.y), Mathf.Abs(l.z));
return l.x >= l.y && l.x >= l.z ? 0 : (l.y >= l.z ? 1 : 2);
}
static void Prune()
{
var dead = new List();
foreach (var k in _base.Keys) if (!k) dead.Add(k);
foreach (var d in dead) _base.Remove(d);
}
}
}