diff --git a/JotunnModStub/Dvergar/DvergarConfig.cs b/JotunnModStub/Dvergar/DvergarConfig.cs index cd1319d..3fa0c72 100644 --- a/JotunnModStub/Dvergar/DvergarConfig.cs +++ b/JotunnModStub/Dvergar/DvergarConfig.cs @@ -87,20 +87,22 @@ namespace RacesOfValheim // instead. Nothing else in the mod does this, so there was no working example to copy; // the Aswid's red eyes were never built at all, only cleaned up. // - // Offsets are in metres from the head bone, along the player's own facing, and every one - // of them is multiplied by the race's visual scale so they shrink with a Dvergar rather - // than hanging where a full-height human's face would have been. + // These are adjustments in metres on top of a measured starting point, not absolute + // offsets from a bone. The eyes are parented to the head bone and placed at whatever + // Character.m_eye works out to in that bone's local space - the game's own answer to + // where this player's eyes are - so the numbers below only have to nudge, and everything + // rides the head animation because it is expressed in the head's own coordinates. // - // The first attempt hung them off Character.m_eye instead. That is the camera's eye - // point, not the model's: it sits outside the scaled rig, so the quads floated above and - // in front of a shrunken Dvergar, and it turns with the look direction rather than the - // head, so they never sat in the face at all. + // Two earlier attempts guessed instead, and both failed in an instructive way: parented + // to m_eye the eyes floated a foot above a shrunken Dvergar, because m_eye is the + // camera's anchor and lives outside the scaled rig; placed from the player root's axes + // they ignored the head entirely and read as stuck to the camera. // - // EyeForward and EyeHeight are the two to tune, measured from the head bone at the base - // of the skull. + // EyeForward pushes them out of the skull to the surface of the face - it is the one that + // matters. All of them scale with the race. public static readonly float EyeSeparation = 0.032f; // half the distance between them - public static readonly float EyeForward = 0.090f; - public static readonly float EyeHeight = 0.080f; + public static readonly float EyeForward = 0.070f; + public static readonly float EyeHeight = 0.000f; public static readonly float EyeSize = 0.020f; // Base glow, before any star. The tiers replace it with the emissive colours the game diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index bc83191..f04221e 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -151,20 +151,7 @@ namespace RacesOfValheim if (eyes == null) eyes = BuildEyes(player, headBone); if (eyes == null) return; // already logged once by the resolver - // 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); - } + PlaceEyes(player, headBone, eyes); Color glow = GetEyeColor(player); foreach (Renderer renderer in eyes.GetComponentsInChildren(true)) @@ -196,28 +183,78 @@ namespace RacesOfValheim return DvergarConfig.BaseEyeColor; } + /// + /// Puts the two eyes where the face is, in the head bone's own coordinates, every frame. + /// + /// The placement is measured rather than guessed. Character.m_eye is the game's own answer + /// to "where are this player's eyes", and asking the head bone to express that point in + /// its local space gives an offset that is correct by construction - no working out which + /// way the bone points, no hand-picked distance from the base of the skull. Being local, + /// it then rides every head animation for free. + /// + /// The config offsets stay, as small adjustments in metres on top of that measurement. + /// + private static void PlaceEyes(Player player, Transform headBone, Transform holder) + { + float lossy = headBone.lossyScale.x; + if (Mathf.Abs(lossy) < 0.0001f) return; + + Transform root = player.transform; + + // Metres -> the head bone's local units, which the armature authors at roughly 100x. + float toLocal = 1f / lossy; + + Vector3 localEye = player.m_eye != null + ? headBone.InverseTransformPoint(player.m_eye.position) + : Vector3.zero; + + Vector3 localForward = headBone.InverseTransformDirection(root.forward).normalized; + Vector3 localUp = headBone.InverseTransformDirection(root.up).normalized; + Vector3 localRight = headBone.InverseTransformDirection(root.right).normalized; + + float raceScale = GetVisualScale(player); + Vector3 baseLocal = localEye + + localForward * (DvergarConfig.EyeForward * raceScale * toLocal) + + localUp * (DvergarConfig.EyeHeight * raceScale * toLocal); + + float eyeLocalSize = DvergarConfig.EyeSize * raceScale * toLocal; + + for (int i = 0; i < holder.childCount; i++) + { + Transform eye = holder.GetChild(i); + float side = i == 0 ? -1f : 1f; + + eye.localPosition = baseLocal + localRight * (side * DvergarConfig.EyeSeparation * raceScale * toLocal); + eye.localScale = Vector3.one * eyeLocalSize; + } + + LogEyePlacementOnce(headBone, localEye, localForward, lossy, raceScale); + } + + private static bool loggedEyePlacement; + + private static void LogEyePlacementOnce(Transform headBone, Vector3 localEye, Vector3 localForward, float lossy, float raceScale) + { + if (loggedEyePlacement) return; + loggedEyePlacement = true; + + Jotunn.Logger.LogInfo( + $"[Dvergar] Eyes on bone '{headBone.name}': lossyScale={lossy:F2} raceScale={raceScale:F2} " + + $"localEyePoint={localEye} localForward={localForward}"); + } + private static Transform BuildEyes(Player player, Transform headBone) { Shader glow = ResolveGlowShader(); if (glow == null) return null; + // Straight onto the head bone, with no rotation of its own: everything underneath is + // expressed in the bone's own local space by PlaceEyes, so position and rotation are + // inherited from the skull rather than recomputed against the body. GameObject holder = new GameObject(EyesName); holder.transform.SetParent(headBone, false); holder.transform.localPosition = Vector3.zero; - - // 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.localRotation = Quaternion.identity; holder.transform.localScale = Vector3.one; for (int i = 0; i < 2; i++) @@ -238,11 +275,11 @@ namespace RacesOfValheim Object.Destroy(collider); } + // Position and size are PlaceEyes' business, and it runs immediately after this. eye.transform.SetParent(holder.transform, false); - eye.transform.localPosition = new Vector3( - side * DvergarConfig.EyeSeparation, DvergarConfig.EyeHeight, DvergarConfig.EyeForward); + eye.transform.localPosition = Vector3.zero; eye.transform.localRotation = Quaternion.identity; - eye.transform.localScale = Vector3.one * DvergarConfig.EyeSize; + eye.transform.localScale = Vector3.zero; Renderer renderer = eye.GetComponent(); renderer.material = new Material(glow);