diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index f04221e..c36d138 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -130,8 +130,7 @@ namespace RacesOfValheim { if (player == null) return; - Animator animator = player.GetComponentInChildren(); - Transform headBone = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null; + Transform headBone = FindHeadBone(player); if (headBone == null) return; // rig not up yet; try again next frame Transform eyes = headBone.Find(EyesName); @@ -164,6 +163,30 @@ namespace RacesOfValheim } } + /// + /// The head bone the body mesh is actually skinned to. + /// + /// Not the Animator's. Asking the avatar for HumanBodyBones.Head returned a transform that + /// moves with the player but is not the one the head animation drives - which is why the + /// eyes tracked the body faithfully and ignored every idle bob of the head while being + /// parented to it, a thing a child transform cannot otherwise do. The rest of this mod has + /// always taken the head off VisEquipment.m_bodyModel.bones, the array the skinned mesh + /// deforms with, and that is the rig that moves. + /// + private static Transform FindHeadBone(Player player) + { + VisEquipment visEq = player.GetComponent(); + if (visEq != null && visEq.m_bodyModel != null && visEq.m_bodyModel.bones != null) + { + Transform bone = FindBoneByName(visEq.m_bodyModel.bones, DvergarConfig.PlayerHeadBoneName); + if (bone != null) return bone; + } + + // Last resort, and only so that something renders if the body model is not up yet. + Animator animator = player.GetComponentInChildren(); + return animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null; + } + /// The scale the race is currently rendering its body at; 1 for anyone not resized. private static float GetVisualScale(Player player) { @@ -228,19 +251,27 @@ namespace RacesOfValheim eye.localScale = Vector3.one * eyeLocalSize; } - LogEyePlacementOnce(headBone, localEye, localForward, lossy, raceScale); + LogEyePlacementOnce(player, headBone, localEye, localForward, lossy, raceScale); } private static bool loggedEyePlacement; - private static void LogEyePlacementOnce(Transform headBone, Vector3 localEye, Vector3 localForward, float lossy, float raceScale) + private static void LogEyePlacementOnce(Player player, 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}"); + $"[Dvergar] Eyes on bone '{headBone.name}' at {headBone.position}: lossyScale={lossy:F2} " + + $"raceScale={raceScale:F2} localEyePoint={localEye} localForward={localForward}"); + + // The two candidates side by side. If these are different transforms, that difference + // is the whole story of why the eyes ignored the head animation. + Animator animator = player.GetComponentInChildren(); + Transform avatarHead = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null; + Jotunn.Logger.LogInfo( + $"[Dvergar] avatar head: {(avatarHead != null ? $"'{avatarHead.name}' at {avatarHead.position}" : "none")}" + + $" | same transform = {avatarHead == headBone}"); } private static Transform BuildEyes(Player player, Transform headBone)