add-dvergar-color-and-skeleton-menu #14

Merged
kanym merged 30 commits from add-dvergar-color-and-skeleton-menu into main 2026-08-05 22:08:39 +02:00
Showing only changes of commit 8014986f0a - Show all commits

fix(dvergar): socket the eyes into the head bone instead of following it

They were placed from the player root's axes every frame, which follows where the
body faces and knows nothing about where the head is looking - so an idle
animation turned the head and left the eyes behind, reading as attached to the
camera rather than to the face.

They are children of the head bone now, so position and rotation are inherited
and no longer computed. The reason that was avoided in the first place was not
knowing which way a rig bone points, and the answer turned out to be to stop
guessing: the body's forward and up, expressed in the head bone's local space,
are exactly the directions wanted, and because they are local they go on meaning
the same thing as the head turns. Measured once at build time, while the head is
still looking where the body is.

Scale is the only thing still asserted each frame, because it can change under
us. The bone carries the armature's hundredfold scale multiplied by whatever the
race renders at; dividing it out leaves the holder standing at exactly the race's
scale, which keeps the offsets underneath honest metres and keeps the eyes
shrinking with a Dvergar.
kanym 2026-08-05 20:28:01 +02:00

View file

@ -130,7 +130,11 @@ namespace RacesOfValheim
{ {
if (player == null) return; if (player == null) return;
Transform eyes = player.transform.Find(EyesName); Animator animator = player.GetComponentInChildren<Animator>();
Transform headBone = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null;
if (headBone == null) return; // rig not up yet; try again next frame
Transform eyes = headBone.Find(EyesName);
if (!IsDvergarPlayer(player) || player.IsDead()) if (!IsDvergarPlayer(player) || player.IsDead())
{ {
@ -144,24 +148,23 @@ namespace RacesOfValheim
return; return;
} }
Animator animator = player.GetComponentInChildren<Animator>(); if (eyes == null) eyes = BuildEyes(player, headBone);
Transform headBone = animator != null ? animator.GetBoneTransform(HumanBodyBones.Head) : null;
if (headBone == null) return; // rig not up yet; try again next frame
if (eyes == null) eyes = BuildEyes(player);
if (eyes == null) return; // already logged once by the resolver if (eyes == null) return; // already logged once by the resolver
// Everything is expressed in the player's own frame and multiplied by the race's // The holder is a child of the head bone, so position and rotation are inherited and
// visual scale, so the eyes sit in a Dvergar's face at Dvergar size rather than where // the eyes are socketed rather than followed - the previous version drove them from
// a full-height human's would have been. // the player root's axes each frame, which meant an idle animation could turn the head
float raceScale = GetVisualScale(player); // while the eyes stayed pointing wherever the body did.
Transform root = player.transform; //
// Only the scale still needs saying, because it can change under us: the bone carries
eyes.position = headBone.position // the armature's own hundredfold scale multiplied by whatever the race is rendering
+ root.forward * (DvergarConfig.EyeForward * raceScale) // at, and dividing it out leaves the holder standing at exactly the race's scale, so
+ root.up * (DvergarConfig.EyeHeight * raceScale); // the offsets underneath stay honest metres.
eyes.rotation = Quaternion.LookRotation(root.forward, root.up); float lossy = headBone.lossyScale.x;
eyes.localScale = Vector3.one * raceScale; if (Mathf.Abs(lossy) > 0.0001f)
{
eyes.localScale = Vector3.one * (GetVisualScale(player) / lossy);
}
Color glow = GetEyeColor(player); Color glow = GetEyeColor(player);
foreach (Renderer renderer in eyes.GetComponentsInChildren<Renderer>(true)) foreach (Renderer renderer in eyes.GetComponentsInChildren<Renderer>(true))
@ -193,17 +196,28 @@ namespace RacesOfValheim
return DvergarConfig.BaseEyeColor; return DvergarConfig.BaseEyeColor;
} }
private static Transform BuildEyes(Player player) private static Transform BuildEyes(Player player, Transform headBone)
{ {
Shader glow = ResolveGlowShader(); Shader glow = ResolveGlowShader();
if (glow == null) return null; if (glow == null) return null;
// Under the player root rather than the rig: ApplyEyes drives the transform in world
// space every frame, and an unscaled parent keeps the offsets honest metres.
GameObject holder = new GameObject(EyesName); GameObject holder = new GameObject(EyesName);
holder.transform.SetParent(player.transform, false); holder.transform.SetParent(headBone, false);
holder.transform.localPosition = Vector3.zero; holder.transform.localPosition = Vector3.zero;
holder.transform.localRotation = Quaternion.identity;
// 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.localScale = Vector3.one; holder.transform.localScale = Vector3.one;
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)