fix(dvergar): measure the skull's axes once, so the eyes turn with the head

The eyes were parented to the head bone and still ignored every head animation,
which should have been impossible - a child cannot refuse its parent's rotation.
It was not refusing it. It was being handed the exact opposite every frame.

The offsets were built from directions computed as
headBone.InverseTransformDirection(playerRoot.forward), and that was recomputed
on every placement. As the head turns, the same world direction expressed in the
head's local space rotates by exactly the negative of the head's rotation - so
the local offset counter-rotated and cancelled the animation out. The eyes rode
the head's position perfectly and its rotation not at all, which is exactly what
it looked like from the first screenshot onwards.

The axes are now measured once, at build time, while the head is still looking
where the body is, and kept on the eye holder. Fixed in the skull's frame, they
ride every animation for free - which was the whole point of parenting to the
bone in the first place.
This commit is contained in:
kanym 2026-08-05 20:54:30 +02:00
commit 196e413573

View file

@ -111,6 +111,18 @@ namespace RacesOfValheim
private const string EyesName = "DvergarEyes";
/// <summary>
/// The skull's own idea of forward, up and right, measured once while the head is still
/// looking where the body is. Kept on the eye holder so it survives as long as the eyes
/// do, and so nothing is tempted to work it out again from a rig that has since moved.
/// </summary>
private class EyeFrame : MonoBehaviour
{
public Vector3 LocalForward = Vector3.forward;
public Vector3 LocalUp = Vector3.up;
public Vector3 LocalRight = Vector3.right;
}
private static Shader cachedGlowShader;
private static bool glowShaderResolved;
@ -227,9 +239,19 @@ namespace RacesOfValheim
// Metres -> the head bone's local units, which the armature authors at roughly 100x.
float toLocal = 1f / lossy;
Vector3 localForward = headBone.InverseTransformDirection(root.forward).normalized;
Vector3 localUp = headBone.InverseTransformDirection(root.up).normalized;
Vector3 localRight = headBone.InverseTransformDirection(root.right).normalized;
// Read back, never recomputed. Deriving these from the player root every frame is
// what kept the eyes from turning with the head: as the head rotates,
// InverseTransformDirection returns a direction rotated by exactly the opposite
// amount, so the offset counter-rotated and cancelled the head's own movement out.
// The eyes moved with the head and never turned with it, which is precisely what it
// looked like. Measured once at build time, they are fixed in the skull's frame and
// ride every animation for free.
EyeFrame frame = holder.GetComponent<EyeFrame>();
if (frame == null) return;
Vector3 localForward = frame.LocalForward;
Vector3 localUp = frame.LocalUp;
Vector3 localRight = frame.LocalRight;
// Measured from the head bone itself, and from nothing else.
//
@ -310,6 +332,15 @@ 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.
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;
for (int i = 0; i < 2; i++)
{
float side = i == 0 ? -1f : 1f;