fix(dvergar): hang the eyes on the bone the mesh is actually skinned to

A child transform cannot ignore its parent's rotation, so eyes that followed the
body but sat out every idle bob of the head were never parented to the head that
was moving. They were parented to the Animator avatar's HumanBodyBones.Head,
which on this rig is a transform that travels with the player and is not the one
the animation drives.

The rest of the mod has never used the avatar for this. The hood, the Skeleton's
body swap and the Furling's head all take the bone off
VisEquipment.m_bodyModel.bones - the array the skinned mesh deforms with, which
is by definition the rig that moves. The eyes do too now, with the avatar left as
a fallback only for the case where the body model is not up yet.

The one-shot log prints both candidates side by side and says whether they are
the same transform, so the next report confirms or kills this outright rather
than leaving it to inference.
This commit is contained in:
kanym 2026-08-05 20:37:45 +02:00
commit c974977f64

View file

@ -130,8 +130,7 @@ namespace RacesOfValheim
{
if (player == null) return;
Animator animator = player.GetComponentInChildren<Animator>();
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
}
}
/// <summary>
/// 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.
/// </summary>
private static Transform FindHeadBone(Player player)
{
VisEquipment visEq = player.GetComponent<VisEquipment>();
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<Animator>();
return animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null;
}
/// <summary>The scale the race is currently rendering its body at; 1 for anyone not resized.</summary>
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<Animator>();
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)