fix(dvergar): square the skull's axes so the eyes sit on the eye line

The trace showed the offsets arriving tilted: 0.109m of forward and 0.078m of up came out as 0.124 forward and 0.059 up, which is the jaw rather than the eyes. towardFace read 0.99, so the direction was never wrong - only leaning.

The axes are measured from whatever pose the head is in on the frame the eyes are built, and a head pitched a few degrees hands back a forward that leans down and an up that leans back. A bone's real axes are square, so the measurement is now rounded to the nearest one. The tilt was the pose at the moment of asking and nothing else.
This commit is contained in:
kanym 2026-08-05 20:59:01 +02:00
commit 7c9c1ca630

View file

@ -318,6 +318,21 @@ namespace RacesOfValheim
}
}
/// <summary>
/// Rounds a direction to the nearest of the six axis directions. Whichever component is
/// largest wins, sign included.
/// </summary>
private static Vector3 SnapToAxis(Vector3 direction)
{
float x = Mathf.Abs(direction.x);
float y = Mathf.Abs(direction.y);
float z = Mathf.Abs(direction.z);
if (x >= y && x >= z) return new Vector3(Mathf.Sign(direction.x), 0f, 0f);
if (y >= z) return new Vector3(0f, Mathf.Sign(direction.y), 0f);
return new Vector3(0f, 0f, Mathf.Sign(direction.z));
}
private static Transform BuildEyes(Player player, Transform headBone)
{
Shader glow = ResolveGlowShader();
@ -332,14 +347,16 @@ namespace RacesOfValheim
holder.transform.localRotation = Quaternion.identity;
holder.transform.localScale = Vector3.one;
// The one moment worth asking the rig which way it faces: now, before an animation has
// had a chance to turn the head away from the body. From here on the answer is kept
// and the skull carries it around.
// Ask the rig which way it faces, once - and then round the answer to the skull's own
// axes. The raw measurement is taken at whatever pose the head happens to be in on
// this frame, so it arrives tilted: a "forward" leaning 20 degrees down put the eyes
// at the jaw instead of the eye line. A bone's real axes are square, and the tilt is
// nothing but the pose at the moment of asking, so it is rounded away.
Transform root = player.transform;
EyeFrame frame = holder.AddComponent<EyeFrame>();
frame.LocalForward = headBone.InverseTransformDirection(root.forward).normalized;
frame.LocalUp = headBone.InverseTransformDirection(root.up).normalized;
frame.LocalRight = headBone.InverseTransformDirection(root.right).normalized;
frame.LocalForward = SnapToAxis(headBone.InverseTransformDirection(root.forward));
frame.LocalUp = SnapToAxis(headBone.InverseTransformDirection(root.up));
frame.LocalRight = SnapToAxis(headBone.InverseTransformDirection(root.right));
for (int i = 0; i < 2; i++)
{