fix(visuals): sheathed items follow the model's scale, not the back joint's

The back joints sit inside the armature, authored at 100x, so scaling a
sheathed item by its joint's world scale put a shield on a Trol's back
at 250 times its size. Items are now scaled by the Visual's world scale.
This commit is contained in:
kanym 2026-09-29 02:48:58 +02:00
commit e8ca631dee

View file

@ -18,11 +18,16 @@ namespace RacesOfValheim
/// Items already in hand grow along with the model afterwards, being its children, which is why /// Items already in hand grow along with the model afterwards, being its children, which is why
/// the report only showed up after R. Measured in game with an F8 dump: joint 2.5, items 1.92. /// the report only showed up after R. Measured in game with an F8 dump: joint 2.5, items 1.92.
/// ///
/// Multiplying back by the joint's scale gives exactly what SetParent(joint, false) would have /// The item is multiplied by the model's own size, the world scale of VisEquipment's "Visual"
/// given. From there the item is an ordinary child of the hand and follows every later change /// (1 on a human, 2.5 in True Form, and it includes a resized player root too). From there the
/// of size on its own: progression tiers, True Form, the grow and shrink animation, the config. /// item is an ordinary child of the joint and follows every later change of size on its own:
/// It covers every race that resizes its model or its root the same way, and remote players /// progression tiers, True Form, the grow and shrink animation, the config. It covers every race
/// too, since each client attaches its own copy. /// that resizes its model or its root, and remote players too, since each client attaches its
/// own copy.
///
/// NOT the joint's own scale. The hand attach points happen to sit at the model's scale, but
/// the back joints sit inside the armature, which is authored at 100x: multiplying by theirs
/// put a sheathed shield on a Trol's back at 250 times its size.
/// ///
/// Armor (attach_skin) is skinned to the body's bones and follows them whatever its own scale, /// Armor (attach_skin) is skinned to the body's bones and follows them whatever its own scale,
/// so it is left alone. So are creatures: this is for players only. /// so it is left alone. So are creatures: this is for players only.
@ -30,6 +35,9 @@ namespace RacesOfValheim
[HarmonyPatch(typeof(VisEquipment), "AttachItem")] [HarmonyPatch(typeof(VisEquipment), "AttachItem")]
internal static class HeldItemScaleFix internal static class HeldItemScaleFix
{ {
private static readonly AccessTools.FieldRef<VisEquipment, GameObject> Visual =
AccessTools.FieldRefAccess<VisEquipment, GameObject>("m_visual");
private static void Postfix(VisEquipment __instance, GameObject __result) private static void Postfix(VisEquipment __instance, GameObject __result)
{ {
if (__result == null || !__instance.m_isPlayer) return; if (__result == null || !__instance.m_isPlayer) return;
@ -38,11 +46,14 @@ namespace RacesOfValheim
if (joint == null) return; if (joint == null) return;
if (__instance.m_bodyModel != null && joint == __instance.m_bodyModel.transform.parent) return; if (__instance.m_bodyModel != null && joint == __instance.m_bodyModel.transform.parent) return;
// Every scale in the chain is uniform, so one axis tells the whole story. GameObject visual = Visual(__instance);
float jointScale = joint.lossyScale.x; if (visual == null) return;
if (jointScale <= 0f || Mathf.Abs(jointScale - 1f) < 0.001f) return;
__result.transform.localScale *= jointScale; // Every scale in the chain is uniform, so one axis tells the whole story.
float modelScale = visual.transform.lossyScale.x;
if (modelScale <= 0f || Mathf.Abs(modelScale - 1f) < 0.001f) return;
__result.transform.localScale *= modelScale;
} }
} }
} }