fix(dvergar): socket the eyes into the head bone instead of following it

They were placed from the player root's axes every frame, which follows where the
body faces and knows nothing about where the head is looking - so an idle
animation turned the head and left the eyes behind, reading as attached to the
camera rather than to the face.

They are children of the head bone now, so position and rotation are inherited
and no longer computed. The reason that was avoided in the first place was not
knowing which way a rig bone points, and the answer turned out to be to stop
guessing: the body's forward and up, expressed in the head bone's local space,
are exactly the directions wanted, and because they are local they go on meaning
the same thing as the head turns. Measured once at build time, while the head is
still looking where the body is.

Scale is the only thing still asserted each frame, because it can change under
us. The bone carries the armature's hundredfold scale multiplied by whatever the
race renders at; dividing it out leaves the holder standing at exactly the race's
scale, which keeps the offsets underneath honest metres and keeps the eyes
shrinking with a Dvergar.
This commit is contained in:
kanym 2026-08-05 20:28:01 +02:00
commit 8014986f0a

View file

@ -130,7 +130,11 @@ namespace RacesOfValheim
{
if (player == null) return;
Transform eyes = player.transform.Find(EyesName);
Animator animator = player.GetComponentInChildren<Animator>();
Transform headBone = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null;
if (headBone == null) return; // rig not up yet; try again next frame
Transform eyes = headBone.Find(EyesName);
if (!IsDvergarPlayer(player) || player.IsDead())
{
@ -144,24 +148,23 @@ namespace RacesOfValheim
return;
}
Animator animator = player.GetComponentInChildren<Animator>();
Transform headBone = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null;
if (headBone == null) return; // rig not up yet; try again next frame
if (eyes == null) eyes = BuildEyes(player);
if (eyes == null) eyes = BuildEyes(player, headBone);
if (eyes == null) return; // already logged once by the resolver
// Everything is expressed in the player's own frame and multiplied by the race's
// visual scale, so the eyes sit in a Dvergar's face at Dvergar size rather than where
// a full-height human's would have been.
float raceScale = GetVisualScale(player);
Transform root = player.transform;
eyes.position = headBone.position
+ root.forward * (DvergarConfig.EyeForward * raceScale)
+ root.up * (DvergarConfig.EyeHeight * raceScale);
eyes.rotation = Quaternion.LookRotation(root.forward, root.up);
eyes.localScale = Vector3.one * raceScale;
// 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);
}
Color glow = GetEyeColor(player);
foreach (Renderer renderer in eyes.GetComponentsInChildren<Renderer>(true))
@ -193,17 +196,28 @@ namespace RacesOfValheim
return DvergarConfig.BaseEyeColor;
}
private static Transform BuildEyes(Player player)
private static Transform BuildEyes(Player player, Transform headBone)
{
Shader glow = ResolveGlowShader();
if (glow == null) return null;
// Under the player root rather than the rig: ApplyEyes drives the transform in world
// space every frame, and an unscaled parent keeps the offsets honest metres.
GameObject holder = new GameObject(EyesName);
holder.transform.SetParent(player.transform, false);
holder.transform.SetParent(headBone, false);
holder.transform.localPosition = Vector3.zero;
holder.transform.localRotation = Quaternion.identity;
// 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.localScale = Vector3.one;
for (int i = 0; i < 2; i++)