diff --git a/JotunnModStub/Dvergar/DvergarConfig.cs b/JotunnModStub/Dvergar/DvergarConfig.cs index 15ac579..cd1319d 100644 --- a/JotunnModStub/Dvergar/DvergarConfig.cs +++ b/JotunnModStub/Dvergar/DvergarConfig.cs @@ -87,14 +87,21 @@ 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 Character.m_eye, which already sits at eye height and faces - // wherever the player is looking, so no bone axes have to be guessed at. EyeForward is - // the one to tune first: too little and the quads sink into the face, too much and they - // float in front of it. - public static readonly float EyeSeparation = 0.035f; // half the distance between them - public static readonly float EyeForward = 0.075f; - public static readonly float EyeHeight = -0.010f; - public static readonly float EyeSize = 0.022f; + // 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. + // + // 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. + // + // EyeForward and EyeHeight are the two to tune, measured from the head bone at the base + // of the skull. + 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 EyeSize = 0.020f; // Base glow, before any star. The tiers replace it with the emissive colours the game // itself gives a starred dverger - RGBA(0, 4, 3.3) at one star and RGBA(12, 0, 8.8) at diff --git a/JotunnModStub/Dvergar/DvergarRace.cs b/JotunnModStub/Dvergar/DvergarRace.cs index 9b55b9b..731dbab 100644 --- a/JotunnModStub/Dvergar/DvergarRace.cs +++ b/JotunnModStub/Dvergar/DvergarRace.cs @@ -69,6 +69,10 @@ namespace RacesOfValheim DvergarVisuals.ApplyHood(player); + // Every frame, because the eyes are placed onto the head bone in world space rather + // than parented to it - see ApplyEyes for why. + DvergarVisuals.ApplyEyes(player); + // Max health is recomputed by Player.UpdateFood, which runs on its own schedule. Left // to itself the malus would only take effect at the next food tick, so a change of // race or of tier is pushed through immediately - this is also what makes the health diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index a253124..9bcf07a 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -117,21 +117,20 @@ namespace RacesOfValheim /// /// 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. /// 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(); + 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(true)) { @@ -159,6 +174,13 @@ namespace RacesOfValheim } } + /// The scale the race is currently rendering its body at; 1 for anyone not resized. + private static float GetVisualScale(Player player) + { + GameObject visual = Traverse.Create(player).Field("m_visual").GetValue(); + return visual != null ? visual.transform.localScale.x : 1f; + } + /// /// 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; diff --git a/JotunnModStub/ZdoVisualSync.cs b/JotunnModStub/ZdoVisualSync.cs index 6a8e688..814f5ee 100644 --- a/JotunnModStub/ZdoVisualSync.cs +++ b/JotunnModStub/ZdoVisualSync.cs @@ -100,6 +100,7 @@ namespace RacesOfValheim // Scale is handled for every player by the Player.Update postfix; this only keeps // the hood and the hidden helmet asserted between equipment refreshes. DvergarVisuals.ApplyHood(m_player); + DvergarVisuals.ApplyEyes(m_player); } else {