From 598d709af1d8fcef1396ba85280c4454a0f4a895 Mon Sep 17 00:00:00 2001 From: kanym Date: Tue, 29 Sep 2026 02:33:54 +0200 Subject: [PATCH 1/2] fix(visuals): weapons and shields drawn by a resized player match their size VisEquipment.AttachItem parents the item with SetParent(joint), which keeps its world size, so on a scaled model the item's local scale was divided by the model's. A Trol drew items 23% too small at base size, and after sheathing in True Form they came back at human size on a 2.5x body. The item is now scaled back by the joint's scale, as if parented without keeping world size, and follows the model from there. --- JotunnModStub/HeldItemScaleFix.cs | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 JotunnModStub/HeldItemScaleFix.cs diff --git a/JotunnModStub/HeldItemScaleFix.cs b/JotunnModStub/HeldItemScaleFix.cs new file mode 100644 index 0000000..fb7ab1e --- /dev/null +++ b/JotunnModStub/HeldItemScaleFix.cs @@ -0,0 +1,48 @@ +using HarmonyLib; +using UnityEngine; + +namespace RacesOfValheim +{ + /// + /// Makes a weapon or shield drawn by a resized player come out at that player's size. + /// + /// VisEquipment.AttachItem instantiates the item and then calls transform.SetParent(joint), + /// and SetParent keeps the object's WORLD size by default. On a normal player the hand joint + /// sits at world scale 1, so nothing changes. On a player whose model we have resized, Unity + /// divides the item's local scale by the joint's scale to keep it at a human's size: + /// + /// Trol at 1.3, draws a mace -> mace local 0.77, world 1.0 (should be 1.3) + /// then True Form grows to 2.5 -> mace world 1.92 (should be 2.5) + /// sheathe and draw again (R) -> mace world 1.0 on a 2.5 body + /// + /// 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. + /// + /// Multiplying back by the joint's scale gives exactly what SetParent(joint, false) would have + /// given. From there the item is an ordinary child of the hand and follows every later change + /// of size on its own: progression tiers, True Form, the grow and shrink animation, the config. + /// It covers every race that resizes its model or its root the same way, and remote players + /// too, since each client attaches its own copy. + /// + /// 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. + /// + [HarmonyPatch(typeof(VisEquipment), "AttachItem")] + internal static class HeldItemScaleFix + { + private static void Postfix(VisEquipment __instance, GameObject __result) + { + if (__result == null || !__instance.m_isPlayer) return; + + Transform joint = __result.transform.parent; + if (joint == null) 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. + float jointScale = joint.lossyScale.x; + if (jointScale <= 0f || Mathf.Abs(jointScale - 1f) < 0.001f) return; + + __result.transform.localScale *= jointScale; + } + } +} From e8ca631dee156bc5ae2f14bc58da3b14b33e987c Mon Sep 17 00:00:00 2001 From: kanym Date: Tue, 29 Sep 2026 02:48:58 +0200 Subject: [PATCH 2/2] 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. --- JotunnModStub/HeldItemScaleFix.cs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/JotunnModStub/HeldItemScaleFix.cs b/JotunnModStub/HeldItemScaleFix.cs index fb7ab1e..736c706 100644 --- a/JotunnModStub/HeldItemScaleFix.cs +++ b/JotunnModStub/HeldItemScaleFix.cs @@ -18,11 +18,16 @@ namespace RacesOfValheim /// 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. /// - /// Multiplying back by the joint's scale gives exactly what SetParent(joint, false) would have - /// given. From there the item is an ordinary child of the hand and follows every later change - /// of size on its own: progression tiers, True Form, the grow and shrink animation, the config. - /// It covers every race that resizes its model or its root the same way, and remote players - /// too, since each client attaches its own copy. + /// The item is multiplied by the model's own size, the world scale of VisEquipment's "Visual" + /// (1 on a human, 2.5 in True Form, and it includes a resized player root too). From there the + /// item is an ordinary child of the joint and follows every later change of size on its own: + /// progression tiers, True Form, the grow and shrink animation, the config. It covers every race + /// 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, /// so it is left alone. So are creatures: this is for players only. @@ -30,6 +35,9 @@ namespace RacesOfValheim [HarmonyPatch(typeof(VisEquipment), "AttachItem")] internal static class HeldItemScaleFix { + private static readonly AccessTools.FieldRef Visual = + AccessTools.FieldRefAccess("m_visual"); + private static void Postfix(VisEquipment __instance, GameObject __result) { if (__result == null || !__instance.m_isPlayer) return; @@ -38,11 +46,14 @@ namespace RacesOfValheim if (joint == null) 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. - float jointScale = joint.lossyScale.x; - if (jointScale <= 0f || Mathf.Abs(jointScale - 1f) < 0.001f) return; + GameObject visual = Visual(__instance); + if (visual == null) 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; } } }