fix(dvergar): put the eyes on the head instead of on the camera
Both faults reported came from the same wrong anchor. Character.m_eye is the camera's eye point, not the model's: it lives outside the scaled rig, so on a Dvergar - who renders at 78% - the quads hung where a full-height human's face would have been, a foot above the head. And it turns with the look direction rather than with the face, so they pointed wherever the player was looking rather than wherever the head was. They now hang off the player root, unscaled, and are placed onto the head bone in world space every frame: position from the bone, orientation from the player's own facing, and every offset multiplied by the race's visual scale so they shrink with the body. That also means the placement constants are honest metres measured from the base of the skull, which is what makes them tunable at all. Driven per frame from the two places that already tick a Dvergar - the race script for the local player, the visual controller for remote ones - because world-space placement has to be re-applied rather than inherited.
This commit is contained in:
parent
9dc072f0a5
commit
76b89a8e5a
4 changed files with 56 additions and 20 deletions
|
|
@ -117,21 +117,20 @@ namespace RacesOfValheim
|
|||
/// <summary>
|
||||
/// Keeps two small emissive quads sitting over the player's painted-on eyes, coloured by
|
||||
/// star tier. Safe to call for anyone: it removes them again from whoever is not a living
|
||||
/// Dvergar.
|
||||
/// Dvergar. Call it every frame - it builds once and then only follows the head.
|
||||
///
|
||||
/// They hang off Character.m_eye rather than the head bone. m_eye is already at eye
|
||||
/// height and already faces the look direction, so the offsets below are plain metres in
|
||||
/// an obvious frame - no working out which way a rig bone happens to point, and no
|
||||
/// fighting the 100x scale the player armature is authored at.
|
||||
/// The holder hangs off the player root, which is unscaled, and is placed onto the head
|
||||
/// bone in world space each frame. That is deliberate. Parenting straight to the head
|
||||
/// bone would drag in the 100x scale the armature is authored at, and parenting to
|
||||
/// Character.m_eye - the first attempt - put them outside the scaled rig entirely, which
|
||||
/// is why they floated above a shrunken Dvergar and turned with the camera instead of the
|
||||
/// face. Reading the bone's position and the player's facing sidesteps both.
|
||||
/// </summary>
|
||||
public static void ApplyEyes(Player player)
|
||||
{
|
||||
if (player == null) return;
|
||||
|
||||
Transform anchor = player.m_eye;
|
||||
if (anchor == null) return;
|
||||
|
||||
Transform eyes = anchor.Find(EyesName);
|
||||
Transform eyes = player.transform.Find(EyesName);
|
||||
|
||||
if (!IsDvergarPlayer(player) || player.IsDead())
|
||||
{
|
||||
|
|
@ -145,9 +144,25 @@ namespace RacesOfValheim
|
|||
return;
|
||||
}
|
||||
|
||||
if (eyes == null) eyes = BuildEyes(anchor);
|
||||
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
|
||||
|
||||
if (eyes == null) eyes = BuildEyes(player);
|
||||
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
|
||||
// visual scale, so the eyes sit in a Dvergar's face at Dvergar size rather than where
|
||||
// a full-height human's would have been.
|
||||
float raceScale = GetVisualScale(player);
|
||||
Transform root = player.transform;
|
||||
|
||||
eyes.position = headBone.position
|
||||
+ root.forward * (DvergarConfig.EyeForward * raceScale)
|
||||
+ root.up * (DvergarConfig.EyeHeight * raceScale);
|
||||
eyes.rotation = Quaternion.LookRotation(root.forward, root.up);
|
||||
eyes.localScale = Vector3.one * raceScale;
|
||||
|
||||
Color glow = GetEyeColor(player);
|
||||
foreach (Renderer renderer in eyes.GetComponentsInChildren<Renderer>(true))
|
||||
{
|
||||
|
|
@ -159,6 +174,13 @@ namespace RacesOfValheim
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>The scale the race is currently rendering its body at; 1 for anyone not resized.</summary>
|
||||
private static float GetVisualScale(Player player)
|
||||
{
|
||||
GameObject visual = Traverse.Create(player).Field("m_visual").GetValue<GameObject>();
|
||||
return visual != null ? visual.transform.localScale.x : 1f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One star and two stars use the emissive colours the game gives a starred dverger, read
|
||||
/// off the same LevelSetup list the skin tint comes from. Only the base tier is ours.
|
||||
|
|
@ -171,13 +193,15 @@ namespace RacesOfValheim
|
|||
return DvergarConfig.BaseEyeColor;
|
||||
}
|
||||
|
||||
private static Transform BuildEyes(Transform anchor)
|
||||
private static Transform BuildEyes(Player player)
|
||||
{
|
||||
Shader glow = ResolveGlowShader();
|
||||
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);
|
||||
holder.transform.SetParent(anchor, false);
|
||||
holder.transform.SetParent(player.transform, false);
|
||||
holder.transform.localPosition = Vector3.zero;
|
||||
holder.transform.localRotation = Quaternion.identity;
|
||||
holder.transform.localScale = Vector3.one;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue