using UnityEngine;
namespace GrassCutter
{
///
/// The face of a crew member: eyeballs that look about (and at you, when you come close and stand in front of them), upper
/// lids that blink and follow the eyes down, and brows that go up when they're in the air or surprised and knit together
/// while they're working. The parts are rigid children of the Head bone (EyeBall_L/R, Lid_L/R, Brow_L/R, from
/// gc_tripo_crew.py); every turn is made about the character's own axes, worked out once from the bind pose, so it doesn't
/// matter how the FBX importer turned them. A body without those parts simply has no face to move.
///
public class CrewFace
{
/// How far the lids turn down over the eye to close it, from where they rest.
public const float LidClose = 62f;
const float BlinkTime = 0.17f, EyeYaw = 26f, EyePitch = 16f;
struct Part
{
public Transform t;
public Quaternion rest;
public Vector3 pos;
/// The character's right, up and forward in the part's parent space, and one millimetre of up.
public Vector3 right, up, fwd, mm;
public bool ok;
public static Part Of(Transform t, Transform frame)
{
if (!t || !t.parent) return default;
var inv = Quaternion.Inverse(t.parent.rotation);
return new Part
{
t = t, rest = t.localRotation, pos = t.localPosition, ok = true,
right = inv * frame.right, up = inv * frame.up, fwd = inv * frame.forward,
mm = t.parent.InverseTransformVector(frame.up * 0.001f),
};
}
}
readonly Transform _frame;
Part _eyeL, _eyeR, _lidL, _lidR, _browL, _browR;
float _nextBlink, _blinkAt = -10f, _nextGlance, _nextFlash, _flashUntil;
Vector2 _glance, _look;
float _lift, _frown, _blinkHold;
CrewFace(Transform frame) { _frame = frame; }
/// The face on this body, or null if it has none.
public static CrewFace Find(Transform model, Transform frame)
{
var f = new CrewFace(frame);
foreach (var t in model.GetComponentsInChildren(true))
{
switch (t.name)
{
case "EyeBall_L": f._eyeL = Part.Of(t, frame); break;
case "EyeBall_R": f._eyeR = Part.Of(t, frame); break;
case "Lid_L": f._lidL = Part.Of(t, frame); break;
case "Lid_R": f._lidR = Part.Of(t, frame); break;
case "Brow_L": f._browL = Part.Of(t, frame); break;
case "Brow_R": f._browR = Part.Of(t, frame); break;
}
}
if (!f._eyeL.ok && !f._eyeR.ok && !f._lidL.ok && !f._browL.ok) return null;
f._nextBlink = Time.time + Random.Range(0.5f, 3f);
f._nextFlash = Time.time + Random.Range(5f, 14f);
return f;
}
/// Where the crew member looks up or down, degrees (up positive); the head already took some of it.
/// Cutting or otherwise busy: the brows knit.
/// In the air, thrown, splashing about: the brows shoot up.
public void Tick(float dt, float pitch, bool working, bool startled, bool driving)
{
float now = Time.time;
// where the eyes go: whoever's camera is close in front of them, or ahead with little glances about
if (now >= _nextGlance)
{
_nextGlance = now + Random.Range(0.7f, 2.8f);
var was = _glance;
_glance = driving ? new Vector2(Random.Range(-10f, 10f), Random.Range(-4f, 3f)) : new Vector2(Random.Range(-18f, 18f), Random.Range(-8f, 6f));
if (Random.value < 0.35f) _glance = Vector2.zero;
// a big jump of the eyes often comes with a blink
if ((_glance - was).magnitude > 16f && Random.value < 0.5f) Blink(now);
}
var want = _glance + new Vector2(0f, pitch * 0.55f);
var cam = CameraFx.Cam ? CameraFx.Cam : Camera.main;
if (cam)
{
var head = _eyeL.ok ? _eyeL.t.position : _frame.position + _frame.up * 1.1f;
var to = cam.transform.position - head;
float d = to.magnitude;
if (d > 0.3f && d < 7f && Vector3.Dot(to / d, _frame.forward) > 0.35f)
{
var local = _frame.InverseTransformDirection(to / d);
want = new Vector2(Mathf.Atan2(local.x, local.z) * Mathf.Rad2Deg, Mathf.Asin(Mathf.Clamp(local.y, -1f, 1f)) * Mathf.Rad2Deg - pitch * 0.45f);
}
}
want.x = Mathf.Clamp(want.x, -EyeYaw, EyeYaw);
want.y = Mathf.Clamp(want.y, -EyePitch, EyePitch);
// eyes jump, they don't drift
_look = Vector2.Lerp(_look, want, 1f - Mathf.Exp(-dt * 28f));
// blinking
if (now >= _nextBlink) Blink(now);
float b = (now - _blinkAt) / BlinkTime;
float shut = b < 0f || b > 1f ? 0f : b < 0.4f ? b / 0.4f : 1f - (b - 0.4f) / 0.6f;
if (_blinkHold > 0f) { _blinkHold -= dt; shut = 1f; }
// the lids ride down with the eyes when they look down
float droop = Mathf.Clamp01(-_look.y / EyePitch) * 0.3f;
float lid = Mathf.Max(shut, droop) * LidClose;
// brows: up when startled or now and then for nothing, knitted while working
if (now >= _nextFlash) { _nextFlash = now + Random.Range(6f, 16f); _flashUntil = now + 0.45f; }
float lift = startled ? 4.5f : now < _flashUntil ? 3.5f : working ? -1.2f : 0f;
float frown = working ? 11f : driving ? 4f : 0f;
_lift = Mathf.MoveTowards(_lift, lift, dt * 30f);
_frown = Mathf.MoveTowards(_frown, frown, dt * 60f);
Eye(ref _eyeL);
Eye(ref _eyeR);
Lid(ref _lidL, lid);
Lid(ref _lidR, lid);
Brow(ref _browL, -_frown);
Brow(ref _browR, _frown);
}
void Blink(float now)
{
_blinkAt = now;
_nextBlink = now + (Random.value < 0.18f ? 0.25f : Random.Range(1.8f, 5.2f));
}
void Eye(ref Part p)
{
if (!p.ok || !p.t) return;
p.t.localRotation = Quaternion.AngleAxis(_look.x, p.up) * Quaternion.AngleAxis(-_look.y, p.right) * p.rest;
}
void Lid(ref Part p, float down)
{
if (!p.ok || !p.t) return;
// the lid follows the eye left and right a little, so its edge stays over the iris
p.t.localRotation = Quaternion.AngleAxis(_look.x * 0.25f, p.up) * Quaternion.AngleAxis(down, p.right) * p.rest;
}
void Brow(ref Part p, float tilt)
{
if (!p.ok || !p.t) return;
p.t.localPosition = p.pos + p.mm * _lift;
p.t.localRotation = Quaternion.AngleAxis(tilt, p.fwd) * p.rest;
}
}
}