From 2f890f95f73f8f4dda877ff4bf070c9e57d384c0 Mon Sep 17 00:00:00 2001 From: kanym Date: Thu, 6 Aug 2026 12:12:30 +0200 Subject: [PATCH] fix(skeleton): hide beard and skinned helmet pieces correctly The hair/beard hide called into m_hairItemInstances/m_beardItemInstances (plural), fields that don't exist on VisEquipment - every other race uses the real singular m_hairItemInstance/m_beardItemInstance, so the beard was never actually hidden. Fixed the field names and made the helper handle both a GameObject and a List defensively. Helmet hiding only scaled the wrapper to zero, which does nothing for a skinned-mesh piece bound to the player's own bones - it ignores its wrapper's transform. Added SkeletonVisuals.SetEquipmentPieceHidden, which scales to zero and disables every child renderer, and used it everywhere the helmet is hidden (local player, remote players, main-menu preview). Also added an F8 debug dump of the hair/beard/helmet instance state (active, renderer enabled, scale) for diagnosing any future visibility regressions in-game. Co-Authored-By: Claude Sonnet 5 --- JotunnModStub/Skeleton/SkeletonPatches.cs | 85 ++++++++++------------ JotunnModStub/Skeleton/SkeletonRace.cs | 89 ++++++++++++++++++++++- JotunnModStub/Skeleton/SkeletonVisuals.cs | 31 ++++++++ JotunnModStub/ZdoVisualSync.cs | 5 +- 4 files changed, 155 insertions(+), 55 deletions(-) diff --git a/JotunnModStub/Skeleton/SkeletonPatches.cs b/JotunnModStub/Skeleton/SkeletonPatches.cs index 1fee96c..df22e9d 100644 --- a/JotunnModStub/Skeleton/SkeletonPatches.cs +++ b/JotunnModStub/Skeleton/SkeletonPatches.cs @@ -333,8 +333,8 @@ namespace RacesOfValheim anim.cullingMode = AnimatorCullingMode.AlwaysAnimate; } - HideItemInstances(__instance, "m_hairItemInstances"); - HideItemInstances(__instance, "m_beardItemInstances"); + HideItemInstances(__instance, "m_hairItemInstance"); + HideItemInstances(__instance, "m_beardItemInstance"); SetHelmetVisible(__instance, false); } } @@ -357,8 +357,8 @@ namespace RacesOfValheim } } - ShowItemInstances(__instance, "m_hairItemInstances"); - ShowItemInstances(__instance, "m_beardItemInstances"); + ShowItemInstances(__instance, "m_hairItemInstance"); + ShowItemInstances(__instance, "m_beardItemInstance"); SetHelmetVisible(__instance, true); } } @@ -373,70 +373,61 @@ namespace RacesOfValheim /// first frame after a race change, all of which reach this postfix and none of which /// reach SkeletonRace. /// - /// Scaled rather than deactivated, because Valheim re-enables the instance whenever it - /// refreshes visuals - the approach the Dvergar's hood already proved out. + /// Delegates to SkeletonVisuals.SetEquipmentPieceHidden, which scales the wrapper to + /// zero and disables its renderers - scale alone leaves a skinned piece (bound to the + /// player's own bones rather than its own transform) still drawing, which is what a + /// helmet with a skinned cloth or chain part looked like. /// private static void SetHelmetVisible(VisEquipment visEq, bool visible) { GameObject helmet = Traverse.Create(visEq).Field("m_helmetItemInstance").Value; - if (helmet == null) return; - - Vector3 wanted = visible ? Vector3.one : Vector3.zero; - - // Only ever undo our own hiding: another race may legitimately be scaling this. - if (!visible && helmet.transform.localScale != Vector3.zero) - { - helmet.transform.localScale = wanted; - } - else if (visible && helmet.transform.localScale == Vector3.zero) - { - helmet.transform.localScale = wanted; - } + SkeletonVisuals.SetEquipmentPieceHidden(helmet, !visible); } private static void HideItemInstances(VisEquipment visEq, string fieldName) + { + SetItemInstanceActive(visEq, fieldName, false); + } + + private static void ShowItemInstances(VisEquipment visEq, string fieldName) + { + SetItemInstanceActive(visEq, fieldName, true); + } + + /// + /// Hair and beard live in VisEquipment as a single GameObject (m_hairItemInstance / + /// m_beardItemInstance), the same as the helmet - not a list. A previous version of + /// this looked for "m_hairItemInstances" / "m_beardItemInstances" (plural), which do + /// not exist on VisEquipment, so the reflection lookup silently found nothing and the + /// beard never got the SetActive(false) this was supposed to give it. Handling both + /// shapes here matches the defensive pattern the Furling race already uses. + /// + private static void SetItemInstanceActive(VisEquipment visEq, string fieldName, bool active) { try { var field = typeof(VisEquipment).GetField(fieldName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); - if (field != null) + if (field == null) return; + + object value = field.GetValue(visEq); + if (value is GameObject go) { - var list = field.GetValue(visEq) as System.Collections.Generic.List; - if (list != null) + if (go != null) go.SetActive(active); + } + else if (value is System.Collections.Generic.List list) + { + foreach (GameObject item in list) { - foreach (GameObject go in list) - { - if (go != null) go.SetActive(false); - } + if (item != null) item.SetActive(active); } } } catch (System.Exception e) { - Jotunn.Logger.LogWarning($"Failed to hide {fieldName}: {e.Message}"); + Jotunn.Logger.LogWarning($"Failed to set {fieldName} active={active}: {e.Message}"); } } - private static void ShowItemInstances(VisEquipment visEq, string fieldName) - { - try - { - var field = typeof(VisEquipment).GetField(fieldName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); - if (field != null) - { - var list = field.GetValue(visEq) as System.Collections.Generic.List; - if (list != null) - { - foreach (GameObject go in list) - { - if (go != null) go.SetActive(true); - } - } - } - } - catch (System.Exception e) { } - } - // Bones worth reporting on. Not remapped - the player rig has a real 'Jaw' bone and the // name match lands on it correctly, which is exactly why the plain and poison skeletons // always looked right. An earlier attempt pinned this group to the head instead and diff --git a/JotunnModStub/Skeleton/SkeletonRace.cs b/JotunnModStub/Skeleton/SkeletonRace.cs index fa1b74f..b6f7e1f 100644 --- a/JotunnModStub/Skeleton/SkeletonRace.cs +++ b/JotunnModStub/Skeleton/SkeletonRace.cs @@ -170,6 +170,87 @@ namespace RacesOfValheim } } + /// + /// F8 debug: fires a ray from the camera and logs whatever it hits. Cosmetic pieces + /// (hair, beard, helmet) carry no collider, so this mostly confirms terrain/creature hits + /// - LogHeadCosmeticsState below is what actually answers "what is that floating chunk." + /// Kept anyway because it is the general-purpose "what am I looking at" tool for any other + /// bone or prefab placement question that comes up later. + /// + private void LogRaycastHit() + { + Camera cam = Camera.main; + if (cam == null) + { + Jotunn.Logger.LogInfo("[F8] No camera found for raycast."); + return; + } + + Ray ray = new Ray(cam.transform.position, cam.transform.forward); + if (Physics.Raycast(ray, out RaycastHit hit, 50f)) + { + Jotunn.Logger.LogInfo("--- F8 RAYCAST HIT ---"); + Jotunn.Logger.LogInfo($"Collider: '{hit.collider.gameObject.name}' at {hit.point:F2}, distance={hit.distance:F2}"); + Jotunn.Logger.LogInfo($"Full path: {GetFullPath(hit.collider.transform)}"); + Renderer renderer = hit.collider.GetComponentInParent(); + if (renderer != null) + { + Jotunn.Logger.LogInfo($"Nearest renderer: '{renderer.gameObject.name}' ({renderer.GetType().Name}), enabled={renderer.enabled}, scale={renderer.transform.lossyScale:F3}"); + } + Jotunn.Logger.LogInfo("----------------------"); + } + else + { + Jotunn.Logger.LogInfo("[F8] Raycast hit nothing within 50m (cosmetic pieces usually have no collider anyway)."); + } + } + + private static string GetFullPath(Transform t) + { + string path = t.name; + for (Transform parent = t.parent; parent != null; parent = parent.parent) + { + path = parent.name + "/" + path; + } + return path; + } + + /// + /// F8 debug: dumps the exact state of the three item slots this race hides (hair, beard, + /// helmet) - active flag, renderer enabled flag and scale for every renderer underneath. + /// This is the direct answer to "what is still visible and why": a chunk that survives + /// hiding will show up here as active=true, or as a renderer with enabled=true even while + /// its wrapper is scaled to zero (the skinned-mesh case scale alone cannot hide). + /// + private void LogHeadCosmeticsState() + { + if (Player.m_localPlayer == null) return; + VisEquipment vis = Player.m_localPlayer.GetComponent(); + if (vis == null) return; + + Jotunn.Logger.LogInfo("--- F8 SKELETON HEAD COSMETICS ---"); + DumpItemInstanceField(vis, "m_hairItemInstance"); + DumpItemInstanceField(vis, "m_beardItemInstance"); + DumpItemInstanceField(vis, "m_helmetItemInstance"); + Jotunn.Logger.LogInfo("----------------------------------"); + } + + private static void DumpItemInstanceField(VisEquipment vis, string fieldName) + { + GameObject go = HarmonyLib.Traverse.Create(vis).Field(fieldName).Value; + if (go == null) + { + Jotunn.Logger.LogInfo($"[F8] {fieldName}: null (nothing equipped/instantiated)."); + return; + } + + Jotunn.Logger.LogInfo($"[F8] {fieldName}: '{go.name}' active={go.activeSelf} scale={go.transform.localScale:F3}"); + foreach (Renderer r in go.GetComponentsInChildren(true)) + { + Jotunn.Logger.LogInfo($"[F8] renderer '{r.gameObject.name}' ({r.GetType().Name}) enabled={r.enabled}"); + } + } + private void Update() { if (Input.GetKeyDown(KeyCode.F8)) @@ -180,6 +261,9 @@ namespace RacesOfValheim DumpTransformHierarchy(Hud.instance.transform, ""); Jotunn.Logger.LogInfo("--------------------------"); } + + LogRaycastHit(); + LogHeadCosmeticsState(); } if (Player.m_localPlayer == null) return; @@ -245,10 +329,7 @@ namespace RacesOfValheim { // Hide Helmets (Valheim uses singular GameObject for helmet) var helmetObj = HarmonyLib.Traverse.Create(currentVis).Field("m_helmetItemInstance").Value; - if (helmetObj != null && helmetObj.transform.localScale != Vector3.zero) - { - helmetObj.transform.localScale = Vector3.zero; - } + SkeletonVisuals.SetEquipmentPieceHidden(helmetObj, true); // Scale Body Armor (Chest, Legs, Shoulder) by 5% string[] armorFields = { "m_chestItemInstances", "m_legItemInstances", "m_shoulderItemInstances" }; diff --git a/JotunnModStub/Skeleton/SkeletonVisuals.cs b/JotunnModStub/Skeleton/SkeletonVisuals.cs index cc46787..1c5f41e 100644 --- a/JotunnModStub/Skeleton/SkeletonVisuals.cs +++ b/JotunnModStub/Skeleton/SkeletonVisuals.cs @@ -48,6 +48,37 @@ namespace RacesOfValheim public static bool IsSkeletonPlayer(Player player) => GetPlayerRace(player) == RaceId; + /// + /// Fully hides (or restores) one equipped item instance - a helmet, typically. + /// + /// Scale-to-zero alone leaves skinned pieces behind: a SkinnedMeshRenderer bound to the + /// player's own bones (some helmets carry a cloth or chain piece skinned that way) is + /// deformed by those bone matrices, not by its GameObject's transform, so zeroing the + /// wrapper's localScale does nothing to it - exactly the "part of the helmet is still + /// there" artifact reported on the Skeleton. Disabling every Renderer underneath has no + /// such exception; combined with the scale it hides both plain and skinned pieces, and + /// is reasserted the same way (Valheim's own visual refresh re-enables the instance and + /// resets its scale, so both must be redone every time visuals rebuild). + /// + public static void SetEquipmentPieceHidden(GameObject instance, bool hidden) + { + if (instance == null) return; + + Vector3 wantedScale = hidden ? Vector3.zero : Vector3.one; + if (instance.transform.localScale != wantedScale) + { + instance.transform.localScale = wantedScale; + } + + foreach (Renderer renderer in instance.GetComponentsInChildren(true)) + { + if (renderer.enabled == hidden) + { + renderer.enabled = !hidden; + } + } + } + /// /// The tier a given player's body should be built for. The ZDO carries it so remote /// players render at their own standing; without one - the menu preview - fall back to diff --git a/JotunnModStub/ZdoVisualSync.cs b/JotunnModStub/ZdoVisualSync.cs index 814f5ee..1c698dc 100644 --- a/JotunnModStub/ZdoVisualSync.cs +++ b/JotunnModStub/ZdoVisualSync.cs @@ -128,10 +128,7 @@ namespace RacesOfValheim // Hide Helmet var helmetObj = HarmonyLib.Traverse.Create(vis).Field("m_helmetItemInstance").Value; - if (helmetObj != null && helmetObj.transform.localScale != Vector3.zero) - { - helmetObj.transform.localScale = Vector3.zero; - } + SkeletonVisuals.SetEquipmentPieceHidden(helmetObj, true); // Scale Body Armor string[] armorFields = { "m_chestItemInstances", "m_legItemInstances", "m_shoulderItemInstances" };