diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index befdaef..5357b6a 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -111,6 +111,18 @@ namespace RacesOfValheim private const string EyesName = "DvergarEyes"; + /// + /// 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. + /// + 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(); + 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(); + 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;