RacesOfValheim/JotunnModStub/Skeleton/SkeletonJawFix.cs
2026-08-11 19:29:58 +02:00

114 lines
5.1 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Poses the Skeleton's mandible, which the player rig alone puts in the wrong place.
///
/// The three skeleton models are skinned to the player rig by bone name, and the rig owns a
/// real 'Jaw' bone, so the mandible lands on the right bone - just at the rest pose that bone
/// happens to hold, which sits a little low and a little far back for a skull. Nothing here
/// remaps anything: it moves that one bone, and only that one.
///
/// THE POSE IS WRITTEN ABSOLUTELY, NEVER ACCUMULATED. That distinction is the whole reason
/// this file reads the way it does. No Valheim animation touches the Jaw bone, so nothing
/// resets it between frames: an earlier version added its offset to whatever the bone already
/// held, on the usual assumption that the Animator rewrites the rig every frame. It does not,
/// so the jaw drifted a few centimetres further away every frame - it left the skull entirely
/// within a fifth of a second, snapped back when a visual refresh happened to rebind the rig,
/// and started over, which is what showed up in game as a head flickering five times a second.
/// With the game paused nothing rebound anything and the mandible simply kept going, dragging
/// the vertices weighted to it into a metres-long spike. Writing the base pose back before
/// applying the correction makes the whole thing idempotent: applied once, five times or not
/// at all in a given frame, the jaw ends up in exactly the same place.
///
/// Runs in LateUpdate, after the animation phase, so the correction cannot be undone by an
/// animation that does move the head around the jaw.
///
/// Attached to every player VisEquipment rebuilds visuals for - local, remote and the
/// main-menu preview - so the jaw is right for everyone looking, not just for the client
/// wearing it.
/// </summary>
public class SkeletonJawFix : MonoBehaviour
{
// Found in game with a temporary set of Configuration Manager sliders, on the tier-3
// Skeleton_Poison model; the plain and the fire models share the rig and the same jaw rest
// pose bone for bone (confirmed in the bone-mapping dump), so one correction covers all
// three. Metres along the skull's own axes, so the mandible keeps following the head
// however it turns.
private static readonly Vector3 Offset = new Vector3(0.0338f, 0.0394f, 0f);
// The rig's jaw is a touch large for these skulls.
private const float Scale = 0.9437f;
private Player m_player;
private VisEquipment m_vis;
private Transform m_jaw;
private Vector3 m_baseLocalPosition;
private Vector3 m_baseLocalScale;
private bool m_applied;
private void Awake()
{
m_player = GetComponent<Player>();
m_vis = GetComponent<VisEquipment>();
}
private void LateUpdate()
{
if (m_player == null || m_vis == null) return;
Transform jaw = ResolveJaw();
if (jaw == null || jaw.parent == null) return;
if (!SkeletonVisuals.IsSkeletonPlayer(m_player))
{
// Nothing rewrites this bone on its own, so a player who stops being a Skeleton
// would otherwise keep wearing the correction on their human face.
if (m_applied)
{
jaw.localPosition = m_baseLocalPosition;
jaw.localScale = m_baseLocalScale;
m_applied = false;
}
return;
}
// Back to the rest pose first: this is what makes the block below absolute rather than
// a nudge on top of last frame's nudge. See the note in the class summary.
jaw.localPosition = m_baseLocalPosition;
jaw.localScale = m_baseLocalScale * Scale;
// In world space rather than localPosition, so the offset stays in metres whatever the
// rig's authored scale is, and rotated by the skull so it tracks the head.
jaw.position += jaw.parent.rotation * Offset;
m_applied = true;
}
/// <summary>
/// The player rig's own jaw bone, with the rest pose it holds before anything here has
/// touched it. Captured once per bone: re-reading it later would sample a pose this
/// component had already moved and bake the correction in twice.
/// </summary>
private Transform ResolveJaw()
{
if (m_jaw != null) return m_jaw;
if (m_vis.m_bodyModel == null || m_vis.m_bodyModel.bones == null) return null;
foreach (Transform bone in m_vis.m_bodyModel.bones)
{
if (bone == null || !bone.name.ToLower().Contains("jaw")) continue;
m_jaw = bone;
m_baseLocalPosition = bone.localPosition;
m_baseLocalScale = bone.localScale;
m_applied = false;
break;
}
return m_jaw;
}
}
}