diff --git a/JotunnModStub/Aswid/AswidConfig.cs b/JotunnModStub/Aswid/AswidConfig.cs index 34f9741..8d84759 100644 --- a/JotunnModStub/Aswid/AswidConfig.cs +++ b/JotunnModStub/Aswid/AswidConfig.cs @@ -267,10 +267,10 @@ namespace RacesOfValheim new ConfigDescription("Scales both sockets. 1 is the size the source creature wears them at.", new AcceptableValueRange(0.01f, 50f), new ConfigurationManagerAttributes { Order = 80 })); - EyeForwardOffset = config.Bind(eyes, "Eye Forward Offset", 0.01f, + EyeForwardOffset = config.Bind(eyes, "Eye Forward Offset", 0.013f, new ConfigDescription("Moves both eyes out of (or into) the face, in metres. Positive is forward, out of the skull.", new AcceptableValueRange(-0.5f, 0.5f), new ConfigurationManagerAttributes { Order = 70 })); - EyeHeightOffset = config.Bind(eyes, "Eye Height Offset", 0f, + EyeHeightOffset = config.Bind(eyes, "Eye Height Offset", 0.004f, new ConfigDescription("Moves both eyes up or down the face, in metres. Positive is up.", new AcceptableValueRange(-0.5f, 0.5f), new ConfigurationManagerAttributes { Order = 69 })); EyeSeparationOffset = config.Bind(eyes, "Eye Separation Offset", 0.007f, @@ -280,7 +280,7 @@ namespace RacesOfValheim EyeRotationX = config.Bind(eyes, "Eye Rotation X", 0f, new ConfigDescription("Turns both eyes on the spot. Only matters if the borrowed socket is flat enough to be seen edge-on.", new AcceptableValueRange(-180f, 180f), new ConfigurationManagerAttributes { Order = 60 })); - EyeRotationY = config.Bind(eyes, "Eye Rotation Y", 24.664f, + EyeRotationY = config.Bind(eyes, "Eye Rotation Y", 22.068f, new ConfigDescription("As above, around the second axis - but MIRRORED: the left eye turns the opposite way, so the pair toes in or out together instead of both pointing off the same shoulder.", new AcceptableValueRange(-180f, 180f), new ConfigurationManagerAttributes { Order = 59 })); EyeRotationZ = config.Bind(eyes, "Eye Rotation Z", -180f, diff --git a/JotunnModStub/Aswid/AswidVisuals.cs b/JotunnModStub/Aswid/AswidVisuals.cs index 47f9453..b118424 100644 --- a/JotunnModStub/Aswid/AswidVisuals.cs +++ b/JotunnModStub/Aswid/AswidVisuals.cs @@ -382,20 +382,17 @@ namespace RacesOfValheim } /// - /// Rounds a direction to the nearest of the six axis directions. Whichever component is - /// largest wins, sign included. + /// Which way the skull faces, from the bind pose rather than from this frame's animation. + /// See RigBindPose for why that distinction is the whole bug - shared with the Dvergar and + /// the Sverm, which hit it too. /// - private static Vector3 SnapToAxis(Vector3 direction) + private static void MeasureFrame(Player player, Transform headBone, EyeFrame frame) { - 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)); + RigBindPose.MeasureHeadFrame(player, headBone, "Aswid", + out frame.LocalForward, out frame.LocalUp, out frame.LocalRight); } + private static Transform BuildEyes(Player player, Transform headBone) { string modelName = AswidConfig.EyeSourceModel.Value; @@ -420,22 +417,11 @@ namespace RacesOfValheim holder.transform.localRotation = Quaternion.identity; holder.transform.localScale = Vector3.one; - // Ask the rig which way it faces, once, and round the answer to the skull's own axes - - // see DvergarVisuals.BuildEyes for why the raw, unrounded measurement puts the eyes at - // the jaw instead of the eye line. - // - // player.transform, NOT headBone.root. headBone comes off VisEquipment.m_bodyModel's - // own bone array, and walking .root from there does not reliably land back on the - // player - the eyes came out wrong on every single build, not just an unlucky one, which - // is what a bad-timing read (the guard above already handles that) would look like. - // A CONSISTENTLY wrong answer means a consistently wrong reference, not a bad moment to - // ask it in. DvergarVisuals.BuildEyes already reads player.transform directly for - // exactly this reason; this now matches it. - Transform root = player.transform; + // Which way the skull faces, taken from the BIND POSE rather than from the pose the + // head happens to hold on this frame. See MeasureFrame - this is the whole reason a + // rebuild is no longer a lottery. EyeFrame frame = holder.AddComponent(); - frame.LocalForward = SnapToAxis(headBone.InverseTransformDirection(root.forward)); - frame.LocalUp = SnapToAxis(headBone.InverseTransformDirection(root.up)); - frame.LocalRight = Vector3.Cross(frame.LocalUp, frame.LocalForward).normalized; + MeasureFrame(player, headBone, frame); frame.SourceModel = modelName; frame.FlatShader = AswidConfig.EyeFlatColorShader.Value; diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index 51e00a5..a6a694a 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -298,20 +298,6 @@ namespace RacesOfValheim } } - /// - /// Rounds a direction to the nearest of the six axis directions. Whichever component is - /// largest wins, sign included. - /// - 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) { @@ -327,22 +313,15 @@ namespace RacesOfValheim holder.transform.localRotation = Quaternion.identity; holder.transform.localScale = Vector3.one; - // 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; + // Which way the rig faces, rounded to the skull's own square axes - and taken from the + // BIND POSE rather than from whatever pose the head holds on this frame. See + // RigBindPose: read live, the rounding lands on the neighbouring axis whenever the head + // is caught mid-turn, and a race change rebuilds at exactly such a moment. That is why + // a Dvergar could come out of a reset with its eyes misplaced and be right again after + // a logout - the logout was simply a rebuild at a calmer moment, not a fix. EyeFrame frame = holder.AddComponent(); - frame.LocalForward = SnapToAxis(headBone.InverseTransformDirection(root.forward)); - frame.LocalUp = SnapToAxis(headBone.InverseTransformDirection(root.up)); - - // Right is derived, never snapped. Rounding all three independently guarantees - // nothing about the angles between them, and a "right" that shares an axis with - // forward stops being sideways: the separation then pushes one eye out of the face - // and pulls the other into it, which is one eye sitting proud of a forehead while its - // twin sits correctly. A cross product cannot be anything but perpendicular. - frame.LocalRight = Vector3.Cross(frame.LocalUp, frame.LocalForward).normalized; + RigBindPose.MeasureHeadFrame(player, headBone, "Dvergar", + out frame.LocalForward, out frame.LocalUp, out frame.LocalRight); for (int i = 0; i < 2; i++) { diff --git a/JotunnModStub/RigBindPose.cs b/JotunnModStub/RigBindPose.cs new file mode 100644 index 0000000..7838498 --- /dev/null +++ b/JotunnModStub/RigBindPose.cs @@ -0,0 +1,113 @@ +using UnityEngine; + +namespace RacesOfValheim +{ + /// + /// Which way a player's skull faces, in the head bone's own coordinates, taken from the rig as + /// it was AUTHORED rather than from the pose it happens to hold this frame. + /// + /// THE BUG THIS EXISTS TO KILL. Three races hang something on the head bone - the Aswid's + /// borrowed sockets, the Dvergar's and the Sverm's spheres - and all three need to know which + /// local axis points forward and which points up before they can place it. The obvious way to + /// ask is headBone.InverseTransformDirection(player.transform.forward), and it works right up + /// until it does not: that reads the head WHERE IT IS STANDING THIS FRAME, the answer is then + /// snapped to the nearest axis, and the result is cached for as long as the eyes exist. Catch + /// the head a few degrees into a turn and the snap picks the neighbouring axis instead, and the + /// eyes stay wrong until something happens to rebuild them. + /// + /// Each race met this separately and each added the same guard - do not build while attached, + /// mid-attack or asleep - which caught the loud cases and left the quiet one. A race change + /// rebuilds while the rig is still settling, which is none of those three, so resetting into a + /// race could break the placement while logging out and back in - a rebuild at a calmer moment - + /// silently fixed it. That is not a bug that a fourth condition fixes. The read itself has to + /// stop depending on when it happens. + /// + /// THE BIND POSE DOES NOT MOVE. SkinnedMeshRenderer.sharedMesh.bindposes[i] is the matrix that + /// takes a direction out of the renderer's own space and into bone i's local space, as the + /// model was rigged - no animation in it, no timing, the same answer on frame one and on frame + /// ten thousand. Measured from there, a rebuild is deterministic and a bad moment cannot exist. + /// + /// ONE COPY, THREE CALLERS, deliberately. The three eye implementations are otherwise each + /// their own file by design - they differ in what they hang and how it is coloured - but this + /// is the one piece of maths where a divergence would be a bug in whichever copy got fixed + /// second, and it has already been fixed twice. + /// + public static class RigBindPose + { + /// + /// Fills in the skull's own forward, up and right. Always produces a usable answer: where + /// the rig carries no bind pose it falls back to reading the live pose, which is the old + /// behaviour and its old caveat, said out loud once rather than silently. + /// + public static void MeasureHeadFrame(Player player, Transform headBone, string raceTag, + out Vector3 localForward, out Vector3 localUp, out Vector3 localRight) + { + VisEquipment visEq = player != null ? player.GetComponent() : null; + SkinnedMeshRenderer body = visEq != null ? visEq.m_bodyModel : null; + Mesh mesh = body != null ? body.sharedMesh : null; + + if (mesh != null && body.bones != null && mesh.bindposes != null && mesh.bindposes.Length > 0) + { + int index = System.Array.IndexOf(body.bones, headBone); + if (index >= 0 && index < mesh.bindposes.Length) + { + // The player's own forward and up, put into the renderer's space first - that is + // where a bindpose starts from. The renderer's own transform is not animated + // (the bones under it are), so this half is stable too. + Vector3 forwardInRenderer = body.transform.InverseTransformDirection(player.transform.forward); + Vector3 upInRenderer = body.transform.InverseTransformDirection(player.transform.up); + + Matrix4x4 toBone = mesh.bindposes[index]; + localForward = SnapToAxis(toBone.MultiplyVector(forwardInRenderer).normalized); + localUp = SnapToAxis(toBone.MultiplyVector(upInRenderer).normalized); + localRight = Vector3.Cross(localUp, localForward).normalized; + return; + } + } + + WarnOnce(raceTag); + + Transform root = player.transform; + localForward = SnapToAxis(headBone.InverseTransformDirection(root.forward)); + localUp = SnapToAxis(headBone.InverseTransformDirection(root.up)); + localRight = Vector3.Cross(localUp, localForward).normalized; + } + + private static readonly System.Collections.Generic.HashSet warned = + new System.Collections.Generic.HashSet(); + + private static void WarnOnce(string raceTag) + { + if (!warned.Add(raceTag ?? "?")) return; + + Jotunn.Logger.LogWarning( + $"[{raceTag}] No bind pose for the head bone on this rig, so the facing is being read " + + "from the live pose instead. That read can land while the head is mid-turn and leave " + + "the eyes off the face until they are rebuilt."); + } + + /// + /// Rounds a direction to the nearest of the six axis directions. Whichever component is + /// largest wins, sign included. + /// + /// The raw measurement arrives tilted - a "forward" leaning 20 degrees down puts the eyes on + /// the jaw instead of the eye line - and a bone's real axes are square, so the tilt is + /// nothing but noise and is rounded away. + /// + /// Right is never snapped by callers, only derived: rounding all three independently + /// guarantees nothing about the angles between them, and a "right" that shares an axis with + /// forward stops being sideways - the separation then pushes one eye out of the face and + /// pulls the other into it. A cross product cannot be anything but perpendicular. + /// + public 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)); + } + } +} diff --git a/JotunnModStub/Sverm/SvermVisuals.cs b/JotunnModStub/Sverm/SvermVisuals.cs index c912e5e..2c51174 100644 --- a/JotunnModStub/Sverm/SvermVisuals.cs +++ b/JotunnModStub/Sverm/SvermVisuals.cs @@ -288,11 +288,13 @@ namespace RacesOfValheim // VisEquipment.m_bodyModel's own array does not reliably land back on the player, and // gave a wrong answer on every single build rather than an occasional bad-timing one. // DvergarVisuals.BuildEyes already reads player.transform directly; this now matches it. - Transform root = player.transform; + // From the BIND POSE, not from this frame's animation - see RigBindPose. Read live, the + // snap-to-axis lands on the neighbouring axis whenever the head is caught mid-turn, and + // a race change rebuilds at exactly such a moment: that is why a freshly spawned Sverm + // could come up with its eyes off the face and be fine again after a relog. EyeFrame frame = holder.AddComponent(); - frame.LocalForward = SnapToAxis(headBone.InverseTransformDirection(root.forward)); - frame.LocalUp = SnapToAxis(headBone.InverseTransformDirection(root.up)); - frame.LocalRight = Vector3.Cross(frame.LocalUp, frame.LocalForward).normalized; + RigBindPose.MeasureHeadFrame(player, headBone, "Sverm", + out frame.LocalForward, out frame.LocalUp, out frame.LocalRight); for (int i = 0; i < 2; i++) { @@ -320,17 +322,6 @@ namespace RacesOfValheim return holder.transform; } - /// Rounds a direction to the nearest of the six axis directions, sign included. - 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)); - } /// /// A shader that will render a colour past 1 so the bloom pass picks it up, AND that fades