fix(dvergar): measure where the eyes go instead of guessing at it
Three attempts have now placed these by picking a distance from a bone and hoping, and all three landed somewhere wrong - above the head, on the camera, and inside the skull. The distance was never the thing to fix. Character.m_eye is the game's own answer to where a player's eyes are. Asking the head bone to express that point in its local space gives an offset that is correct by construction: no working out which way the bone points, no hand-picked distance from the base of the skull, and because the result is local it rides every head animation without being recomputed. The config offsets stay as small nudges in metres on top of that, with EyeForward pushing out of the skull to the surface of the face. Also logs the bone name, its scale and the measured eye point once per session, so the next round starts from numbers rather than from another guess.
This commit is contained in:
parent
8014986f0a
commit
e7d60f61ba
2 changed files with 81 additions and 42 deletions
|
|
@ -151,20 +151,7 @@ namespace RacesOfValheim
|
|||
if (eyes == null) eyes = BuildEyes(player, headBone);
|
||||
if (eyes == null) return; // already logged once by the resolver
|
||||
|
||||
// The holder is a child of the head bone, so position and rotation are inherited and
|
||||
// the eyes are socketed rather than followed - the previous version drove them from
|
||||
// the player root's axes each frame, which meant an idle animation could turn the head
|
||||
// while the eyes stayed pointing wherever the body did.
|
||||
//
|
||||
// Only the scale still needs saying, because it can change under us: the bone carries
|
||||
// the armature's own hundredfold scale multiplied by whatever the race is rendering
|
||||
// at, and dividing it out leaves the holder standing at exactly the race's scale, so
|
||||
// the offsets underneath stay honest metres.
|
||||
float lossy = headBone.lossyScale.x;
|
||||
if (Mathf.Abs(lossy) > 0.0001f)
|
||||
{
|
||||
eyes.localScale = Vector3.one * (GetVisualScale(player) / lossy);
|
||||
}
|
||||
PlaceEyes(player, headBone, eyes);
|
||||
|
||||
Color glow = GetEyeColor(player);
|
||||
foreach (Renderer renderer in eyes.GetComponentsInChildren<Renderer>(true))
|
||||
|
|
@ -196,28 +183,78 @@ namespace RacesOfValheim
|
|||
return DvergarConfig.BaseEyeColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts the two eyes where the face is, in the head bone's own coordinates, every frame.
|
||||
///
|
||||
/// The placement is measured rather than guessed. Character.m_eye is the game's own answer
|
||||
/// to "where are this player's eyes", and asking the head bone to express that point in
|
||||
/// its local space gives an offset that is correct by construction - no working out which
|
||||
/// way the bone points, no hand-picked distance from the base of the skull. Being local,
|
||||
/// it then rides every head animation for free.
|
||||
///
|
||||
/// The config offsets stay, as small adjustments in metres on top of that measurement.
|
||||
/// </summary>
|
||||
private static void PlaceEyes(Player player, Transform headBone, Transform holder)
|
||||
{
|
||||
float lossy = headBone.lossyScale.x;
|
||||
if (Mathf.Abs(lossy) < 0.0001f) return;
|
||||
|
||||
Transform root = player.transform;
|
||||
|
||||
// Metres -> the head bone's local units, which the armature authors at roughly 100x.
|
||||
float toLocal = 1f / lossy;
|
||||
|
||||
Vector3 localEye = player.m_eye != null
|
||||
? headBone.InverseTransformPoint(player.m_eye.position)
|
||||
: Vector3.zero;
|
||||
|
||||
Vector3 localForward = headBone.InverseTransformDirection(root.forward).normalized;
|
||||
Vector3 localUp = headBone.InverseTransformDirection(root.up).normalized;
|
||||
Vector3 localRight = headBone.InverseTransformDirection(root.right).normalized;
|
||||
|
||||
float raceScale = GetVisualScale(player);
|
||||
Vector3 baseLocal = localEye
|
||||
+ localForward * (DvergarConfig.EyeForward * raceScale * toLocal)
|
||||
+ localUp * (DvergarConfig.EyeHeight * raceScale * toLocal);
|
||||
|
||||
float eyeLocalSize = DvergarConfig.EyeSize * raceScale * toLocal;
|
||||
|
||||
for (int i = 0; i < holder.childCount; i++)
|
||||
{
|
||||
Transform eye = holder.GetChild(i);
|
||||
float side = i == 0 ? -1f : 1f;
|
||||
|
||||
eye.localPosition = baseLocal + localRight * (side * DvergarConfig.EyeSeparation * raceScale * toLocal);
|
||||
eye.localScale = Vector3.one * eyeLocalSize;
|
||||
}
|
||||
|
||||
LogEyePlacementOnce(headBone, localEye, localForward, lossy, raceScale);
|
||||
}
|
||||
|
||||
private static bool loggedEyePlacement;
|
||||
|
||||
private static void LogEyePlacementOnce(Transform headBone, Vector3 localEye, Vector3 localForward, float lossy, float raceScale)
|
||||
{
|
||||
if (loggedEyePlacement) return;
|
||||
loggedEyePlacement = true;
|
||||
|
||||
Jotunn.Logger.LogInfo(
|
||||
$"[Dvergar] Eyes on bone '{headBone.name}': lossyScale={lossy:F2} raceScale={raceScale:F2} " +
|
||||
$"localEyePoint={localEye} localForward={localForward}");
|
||||
}
|
||||
|
||||
private static Transform BuildEyes(Player player, Transform headBone)
|
||||
{
|
||||
Shader glow = ResolveGlowShader();
|
||||
if (glow == null) return null;
|
||||
|
||||
// Straight onto the head bone, with no rotation of its own: everything underneath is
|
||||
// expressed in the bone's own local space by PlaceEyes, so position and rotation are
|
||||
// inherited from the skull rather than recomputed against the body.
|
||||
GameObject holder = new GameObject(EyesName);
|
||||
holder.transform.SetParent(headBone, false);
|
||||
holder.transform.localPosition = Vector3.zero;
|
||||
|
||||
// Which way a rig bone points is nobody's business but the artist's, and the head
|
||||
// bone's axes are not the model's. Rather than guess, ask: the body's own forward and
|
||||
// up, expressed in the head bone's local space, are exactly the directions wanted -
|
||||
// and because they are local they keep meaning the same thing as the head turns.
|
||||
// Measured once, here, while the head is still looking where the body is.
|
||||
Transform root = player.transform;
|
||||
Vector3 localForward = headBone.InverseTransformDirection(root.forward).normalized;
|
||||
Vector3 localUp = headBone.InverseTransformDirection(root.up).normalized;
|
||||
|
||||
holder.transform.localRotation = localForward.sqrMagnitude > 0.0001f
|
||||
? Quaternion.LookRotation(localForward, localUp)
|
||||
: Quaternion.identity;
|
||||
|
||||
holder.transform.localRotation = Quaternion.identity;
|
||||
holder.transform.localScale = Vector3.one;
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
|
|
@ -238,11 +275,11 @@ namespace RacesOfValheim
|
|||
Object.Destroy(collider);
|
||||
}
|
||||
|
||||
// Position and size are PlaceEyes' business, and it runs immediately after this.
|
||||
eye.transform.SetParent(holder.transform, false);
|
||||
eye.transform.localPosition = new Vector3(
|
||||
side * DvergarConfig.EyeSeparation, DvergarConfig.EyeHeight, DvergarConfig.EyeForward);
|
||||
eye.transform.localPosition = Vector3.zero;
|
||||
eye.transform.localRotation = Quaternion.identity;
|
||||
eye.transform.localScale = Vector3.one * DvergarConfig.EyeSize;
|
||||
eye.transform.localScale = Vector3.zero;
|
||||
|
||||
Renderer renderer = eye.GetComponent<Renderer>();
|
||||
renderer.material = new Material(glow);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue