diff --git a/JotunnModStub/Aswid/AswidPatches.cs b/JotunnModStub/Aswid/AswidPatches.cs index fe5a45e..c4bd030 100644 --- a/JotunnModStub/Aswid/AswidPatches.cs +++ b/JotunnModStub/Aswid/AswidPatches.cs @@ -607,9 +607,9 @@ namespace RacesOfValheim // Toggle visibility if (isBat || isMist) { - if (playerVisual != null) playerVisual.gameObject.SetActive(false); + PlayerVisualHider.Hide(playerVisual, "aswid-remote-form"); } else { - if (playerVisual != null) playerVisual.gameObject.SetActive(true); + PlayerVisualHider.Show(playerVisual, "aswid-remote-revert"); } } } diff --git a/JotunnModStub/Aswid/AswidRace.cs b/JotunnModStub/Aswid/AswidRace.cs index ca3947d..6d3ba78 100644 --- a/JotunnModStub/Aswid/AswidRace.cs +++ b/JotunnModStub/Aswid/AswidRace.cs @@ -545,10 +545,7 @@ namespace RacesOfValheim } Transform playerVisual = Player.m_localPlayer.transform.Find("Visual"); - if (playerVisual != null) - { - playerVisual.gameObject.SetActive(false); - } + PlayerVisualHider.Hide(playerVisual, "aswid-local-bat"); GameObject batPrefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab("Bat"); if (batPrefab != null) @@ -583,15 +580,7 @@ namespace RacesOfValheim } Transform playerVisual = Player.m_localPlayer.transform.Find("Visual"); - if (playerVisual != null) - { - playerVisual.gameObject.SetActive(true); - - ZSyncAnimation zanim = Player.m_localPlayer.GetComponent(); - if (zanim != null) zanim.SetBool("sit", false); - Animator anim = playerVisual.GetComponent(); - if (anim != null) anim.SetBool("sit", false); - } + PlayerVisualHider.Show(playerVisual, "aswid-local-bat-revert"); Player.m_localPlayer.GetSEMan().RemoveStatusEffect("SE_BatForm".GetStableHashCode(), true); if (Player.m_localPlayer.GetComponent() != null) { @@ -680,11 +669,7 @@ namespace RacesOfValheim yield return null; } - if (playerVisual != null) - { - playerVisual.localScale = Vector3.one * 0.01f; - playerVisual.gameObject.SetActive(false); - } + PlayerVisualHider.Hide(playerVisual, "aswid-local-mist"); if (mistTransformation != null) mistTransformation.transform.localScale = Vector3.one; isTransformingSequence = false; @@ -699,15 +684,8 @@ namespace RacesOfValheim if (poofPrefab != null) Instantiate(poofPrefab, Player.m_localPlayer.transform.position, Quaternion.identity); Transform playerVisual = Player.m_localPlayer.transform.Find("Visual"); - if (playerVisual != null) - { - playerVisual.gameObject.SetActive(true); - - ZSyncAnimation zanim = Player.m_localPlayer.GetComponent(); - if (zanim != null) zanim.SetBool("sit", false); - Animator anim = playerVisual.GetComponent(); - if (anim != null) anim.SetBool("sit", false); - } + // Renderers come back on here; the loop below still grows the body out of the mist. + PlayerVisualHider.Show(playerVisual, "aswid-local-mist-revert"); float elapsed = 0f; float duration = 1.0f; diff --git a/JotunnModStub/PlayerVisualHider.cs b/JotunnModStub/PlayerVisualHider.cs new file mode 100644 index 0000000..9ba279e --- /dev/null +++ b/JotunnModStub/PlayerVisualHider.cs @@ -0,0 +1,162 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace RacesOfValheim +{ + /// + /// Hides and restores a player's "Visual" child without ever deactivating it. + /// + /// The player's Animator sits on that same GameObject, and Unity rebinds an Animator whenever it + /// is re-enabled: the controller restarts from its entry state, which for the player is the same + /// get-up clip that plays when you spawn into a world. Deactivating the Visual to hide the human + /// body during a monster form is therefore why reverting looked exactly like respawning. + /// + /// Hiding is done with Renderer.forceRenderingOff on top of Renderer.enabled. Nothing in the game + /// touches forceRenderingOff on a character (only EnvZone uses it, on its own props), so the body + /// stays invisible even if some other code re-enables the renderers behind our back. + /// + public class PlayerVisualHider : MonoBehaviour + { + // TEMPORARY: tracing for the transform-visibility work. Flip to false once multiplayer + // transforms are confirmed good, along with the rest of the debug logging in 1.3.9. + public const bool Diagnostics = true; + + // VisEquipment rebuilds item visuals whenever gear changes and they come back visible, so + // the hidden set has to be re-swept rather than applied once. + private const float RescanInterval = 0.25f; + private const float LogInterval = 2f; + + private struct HiddenRenderer + { + public Renderer Renderer; + public bool WasEnabled; + } + + private readonly List m_hidden = new List(); + private float m_nextRescan; + private float m_nextLog; + private string m_context = ""; + + public static void Hide(Transform playerVisual, string context) + { + if (playerVisual == null) return; + + // Undo the two older ways this body could have been hidden, so a form entered right + // after one of those code paths still starts from a sane state. + if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true); + if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one; + + // Belt and braces: if anything else ever does deactivate the Visual, at least keep the + // animator from snapping back to its spawn state. + Animator anim = playerVisual.GetComponent(); + if (anim != null) anim.keepAnimatorStateOnDisable = true; + + PlayerVisualHider hider = playerVisual.GetComponent(); + if (hider == null) hider = playerVisual.gameObject.AddComponent(); + + bool alreadyHiding = hider.enabled; + hider.m_context = context; + hider.enabled = true; + + int added = hider.HideRenderers(); + + if (Diagnostics && (!alreadyHiding || added > 0)) + { + Jotunn.Logger.LogInfo( + $"[VisualHider] hide '{context}' newlyHidden={added} tracked={hider.m_hidden.Count} " + + $"visualActive={playerVisual.gameObject.activeInHierarchy}"); + } + } + + public static void Show(Transform playerVisual, string context) + { + if (playerVisual == null) return; + + if (!playerVisual.gameObject.activeSelf) playerVisual.gameObject.SetActive(true); + if (playerVisual.localScale != Vector3.one) playerVisual.localScale = Vector3.one; + + PlayerVisualHider hider = playerVisual.GetComponent(); + if (hider == null || !hider.enabled) return; + + int restored = hider.RestoreRenderers(); + + if (Diagnostics) + { + Jotunn.Logger.LogInfo( + $"[VisualHider] show '{context}' restored={restored} (hidden by '{hider.m_context}')"); + } + } + + public static bool IsHidden(Transform playerVisual) + { + if (playerVisual == null) return false; + PlayerVisualHider hider = playerVisual.GetComponent(); + return hider != null && hider.enabled; + } + + /// Hides every renderer not already hidden by us. Returns how many were added. + private int HideRenderers() + { + int added = 0; + + // Equipment is parented to the body's bones, so this covers weapons, armour, hair and + // the cape along with the body mesh itself. + foreach (Renderer r in GetComponentsInChildren(true)) + { + if (r == null || r.forceRenderingOff) continue; // already ours + + m_hidden.Add(new HiddenRenderer { Renderer = r, WasEnabled = r.enabled }); + r.enabled = false; + r.forceRenderingOff = true; + added++; + } + + m_nextRescan = Time.time + RescanInterval; + return added; + } + + private int RestoreRenderers() + { + int restored = 0; + + foreach (HiddenRenderer h in m_hidden) + { + if (h.Renderer == null) continue; + h.Renderer.forceRenderingOff = false; + h.Renderer.enabled = h.WasEnabled; + restored++; + } + + m_hidden.Clear(); + enabled = false; + return restored; + } + + private void LateUpdate() + { + if (Time.time < m_nextRescan) return; + + int added = HideRenderers(); + + if (!Diagnostics || Time.time < m_nextLog) return; + + // If anything re-enables a renderer we own, forceRenderingOff still keeps it invisible - + // but we want to know who, so report it rather than silently papering over it. + int reEnabled = 0; + string firstName = ""; + foreach (HiddenRenderer h in m_hidden) + { + if (h.Renderer == null || !h.Renderer.enabled) continue; + if (reEnabled == 0) firstName = h.Renderer.name; + reEnabled++; + } + + if (added == 0 && reEnabled == 0) return; + + m_nextLog = Time.time + LogInterval; + Jotunn.Logger.LogInfo( + $"[VisualHider] sweep '{m_context}' newlyHidden={added} reEnabledBehindUs={reEnabled} " + + $"first='{firstName}' tracked={m_hidden.Count}"); + } + } +} diff --git a/JotunnModStub/Skadi/SkadiPatches.cs b/JotunnModStub/Skadi/SkadiPatches.cs index 5b6bfec..5652033 100644 --- a/JotunnModStub/Skadi/SkadiPatches.cs +++ b/JotunnModStub/Skadi/SkadiPatches.cs @@ -62,7 +62,7 @@ namespace RacesOfValheim if (string.IsNullOrEmpty(formName)) { - if (playerVisual != null) playerVisual.gameObject.SetActive(true); + PlayerVisualHider.Show(playerVisual, "skadi-remote-revert"); return; } @@ -84,7 +84,7 @@ namespace RacesOfValheim } } - if (playerVisual != null) playerVisual.gameObject.SetActive(false); + PlayerVisualHider.Hide(playerVisual, "skadi-remote-form"); } } diff --git a/JotunnModStub/Skadi/SkadiRace.cs b/JotunnModStub/Skadi/SkadiRace.cs index f8290bf..f85f1e9 100644 --- a/JotunnModStub/Skadi/SkadiRace.cs +++ b/JotunnModStub/Skadi/SkadiRace.cs @@ -637,7 +637,7 @@ namespace RacesOfValheim if (prefab != null && playerVisual != null) { - playerVisual.gameObject.SetActive(false); + PlayerVisualHider.Hide(playerVisual, "skadi-local-form"); Transform visual = prefab.transform.Find("Visual"); if (visual != null) @@ -722,16 +722,7 @@ namespace RacesOfValheim var forms1 = Player.m_localPlayer.GetComponentsInChildren(true); foreach (var f in forms1) Destroy(f.gameObject); - if (playerVisual != null) - { - playerVisual.localScale = Vector3.one; - playerVisual.gameObject.SetActive(true); - - ZSyncAnimation zanim = Player.m_localPlayer.GetComponent(); - if (zanim != null) zanim.SetBool("sit", false); - Animator anim = playerVisual.GetComponent(); - if (anim != null) anim.SetBool("sit", false); - } + PlayerVisualHider.Show(playerVisual, "skadi-local-revert"); isTransformed = false; formExpireTime = 0f; diff --git a/JotunnModStub/ZdoVisualSync.cs b/JotunnModStub/ZdoVisualSync.cs index ad21bcf..b2bb17b 100644 --- a/JotunnModStub/ZdoVisualSync.cs +++ b/JotunnModStub/ZdoVisualSync.cs @@ -241,10 +241,8 @@ namespace RacesOfValheim if (m_player.transform.localScale != Vector3.one) m_player.transform.localScale = Vector3.one; Transform playerVisual = m_player.transform.Find("Visual"); - if (playerVisual != null && playerVisual.localScale != Vector3.one) - { - playerVisual.localScale = Vector3.one; - } + // Also clears a hidden body left behind if a remote player died mid-transformation. + PlayerVisualHider.Show(playerVisual, "zdosync-revert"); if (m_currentFormInstance != null) Destroy(m_currentFormInstance); EnsureFireAuraVFX(false);