The dump settled both. Skin: UpdateColors writes m_bodyModel.materials[0] _SkinColor, which is what the postfix already targeted, so the patch was never wrong - the colour was. _SkinColor multiplies the human skin texture rather than replacing it, and (0.72, 0.74, 0.82) is close enough to white to change nothing anyone can see. Every race that reads clearly commits to a channel, so the Dvergur now does too. Hood: the dvergr mesh reports zero triangles because it ships with Read/Write disabled, so its vertices and indices are unreachable at runtime and no submesh carving could ever have produced geometry. The rig does carry a hat1/hat2/hat3 chain that nothing but the hood is skinned to, so the hood is isolated without touching the mesh at all: instantiate the whole body, detach the hat chain from the skull, and collapse the armature root to zero scale, which pulls every vertex weighted to it into a single point and leaves only the hood standing. Alignment is computed rather than measured - the object is shifted by the gap between the hat bone and the player head bone - so there are no hand-tuned offsets, only optional nudges. Animator and LODGroup are stripped alongside the MonoBehaviours since neither is one and both would keep driving or hiding the rig, and the renderers are set to update when offscreen because the collapsed bones leave the skinned bounds meaningless.
319 lines
14 KiB
C#
319 lines
14 KiB
C#
using HarmonyLib;
|
|
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The Dvergur's looks: a short body, dvergr skin, and a hood that is always worn.
|
|
///
|
|
/// The hood is a pure cosmetic override, not an equipment lock. The helmet slot stays free and
|
|
/// whatever the player equips keeps every one of its stats - only its mesh is swapped out, so
|
|
/// armour, resistances and set bonuses all still count.
|
|
///
|
|
/// Everything here runs for local and remote players alike, which is why it takes a Player
|
|
/// rather than reading Player.m_localPlayer.
|
|
/// </summary>
|
|
public static class DvergurVisuals
|
|
{
|
|
private const string HoodName = "DvergurHood";
|
|
|
|
// The source is looked up once per session; the search walks a whole creature prefab and
|
|
// would otherwise run again for every player that comes into view. The failure is cached
|
|
// too, so a missing prefab cannot turn into a per-frame log.
|
|
private static GameObject cachedVisual;
|
|
private static bool visualResolved;
|
|
|
|
/// <summary>
|
|
/// Reads the race off the ZDO so remote players render correctly, falling back to
|
|
/// m_customData for the main-menu preview which has no ZNetView.
|
|
/// </summary>
|
|
public static string GetPlayerRace(Player player)
|
|
{
|
|
if (player == null) return "Human";
|
|
|
|
ZNetView nview = player.GetComponent<ZNetView>();
|
|
if (nview != null && nview.IsValid())
|
|
{
|
|
return nview.GetZDO().GetString("RacesOfValheim_Race", "Human");
|
|
}
|
|
|
|
if (player.m_customData != null && player.m_customData.TryGetValue("Race", out string race))
|
|
{
|
|
return race;
|
|
}
|
|
return "Human";
|
|
}
|
|
|
|
public static bool IsDvergurPlayer(Player player) => GetPlayerRace(player) == DvergurConfig.RaceId;
|
|
|
|
// ─── Scale ───────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Sizes a player, driven from a Player.Update postfix exactly the way the Trol is - which
|
|
/// is the arrangement that actually holds. Doing it from the plugin's own MonoBehaviour
|
|
/// left the scale in a fight it could not win: Trol's Player.Update postfix resets every
|
|
/// non-Trol player's visual to 1 on the same frame, and whichever ran last won.
|
|
/// </summary>
|
|
public static void ApplyScale(Player player, bool isDvergur)
|
|
{
|
|
if (player == null) return;
|
|
|
|
// The "Visual" child, never the root: the root carries the collider and the camera
|
|
// pivot, and shrinking those drops the player through the ground they stand on.
|
|
GameObject visual = Traverse.Create(player).Field("m_visual").GetValue<GameObject>();
|
|
if (visual == null) return;
|
|
|
|
if (isDvergur)
|
|
{
|
|
float target = DvergurConfig.VisualScale;
|
|
if (Mathf.Abs(visual.transform.localScale.x - target) > 0.001f)
|
|
{
|
|
visual.transform.localScale = new Vector3(target, target, target);
|
|
}
|
|
}
|
|
else if (Mathf.Abs(visual.transform.localScale.x - DvergurConfig.VisualScale) < 0.001f)
|
|
{
|
|
// Only ever undo our own scale - another race may legitimately be sizing this player.
|
|
visual.transform.localScale = Vector3.one;
|
|
}
|
|
}
|
|
|
|
// ─── Hood ────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Keeps the hood and the hidden helmet in step. Safe to call every frame: each step
|
|
/// checks the current value first and does nothing when it already matches.
|
|
/// </summary>
|
|
public static void ApplyHood(Player player)
|
|
{
|
|
VisEquipment visEq = player != null ? player.GetComponent<VisEquipment>() : null;
|
|
if (visEq == null) return;
|
|
|
|
if (IsDvergurPlayer(player) && !player.IsDead())
|
|
{
|
|
EnsureHood(visEq);
|
|
HideEquippedHelmet(visEq);
|
|
}
|
|
else
|
|
{
|
|
RemoveHood(visEq);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Blanks the mesh of whatever helmet is equipped. Scaled to zero rather than deactivated
|
|
/// because Valheim re-enables the instance whenever it refreshes visuals, and this is the
|
|
/// approach the Skeleton already proved out.
|
|
/// </summary>
|
|
private static void HideEquippedHelmet(VisEquipment visEq)
|
|
{
|
|
GameObject helmet = Traverse.Create(visEq).Field<GameObject>("m_helmetItemInstance").Value;
|
|
if (helmet != null && helmet.transform.localScale != Vector3.zero)
|
|
{
|
|
helmet.transform.localScale = Vector3.zero;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hangs the dvergr hood on the player's head.
|
|
///
|
|
/// The Dvergr ships as a single renderer whose mesh has Read/Write disabled - it reports
|
|
/// zero triangles - so the hood cannot be carved out of it as a submesh, which is what the
|
|
/// previous two attempts tried in one form or another. What the prefab does give us is a
|
|
/// hat1/hat2/hat3 bone chain that nothing but the hood is skinned to.
|
|
///
|
|
/// So the whole dvergr body is instantiated and then made to disappear: the hat chain is
|
|
/// detached from the skull, and the armature root is collapsed to zero scale, which drags
|
|
/// every vertex weighted to it into a single point. Only the hood is left standing, and it
|
|
/// is still skinned, so it keeps its own shape.
|
|
/// </summary>
|
|
private static void EnsureHood(VisEquipment visEq)
|
|
{
|
|
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
|
|
|
|
Transform playerHead = FindBoneByName(visEq.m_bodyModel.bones, DvergurConfig.PlayerHeadBoneName);
|
|
if (playerHead == null) return;
|
|
|
|
if (playerHead.Find(HoodName) != null) return; // already wearing it
|
|
|
|
GameObject sourceVisual = ResolveDvergerVisual();
|
|
if (sourceVisual == null) return; // already logged once by the resolver
|
|
|
|
bool wasActive = sourceVisual.activeSelf;
|
|
sourceVisual.SetActive(false);
|
|
GameObject hood = Object.Instantiate(sourceVisual, playerHead, false);
|
|
sourceVisual.SetActive(wasActive);
|
|
|
|
hood.name = HoodName;
|
|
StripComponents(hood);
|
|
hood.SetActive(true);
|
|
|
|
Transform hatBone = FindChildByName(hood.transform, DvergurConfig.HoodBoneName);
|
|
Transform bodyRoot = FindChildByName(hood.transform, DvergurConfig.BodyRootBoneName);
|
|
|
|
if (hatBone == null)
|
|
{
|
|
Jotunn.Logger.LogWarning(
|
|
$"[Dvergur] No '{DvergurConfig.HoodBoneName}' bone in the instantiated dvergr; hood dropped.");
|
|
Object.Destroy(hood);
|
|
return;
|
|
}
|
|
|
|
// Detached first: the hat chain hangs off the skull, so collapsing the body without
|
|
// this would collapse the hood along with it.
|
|
hatBone.SetParent(hood.transform, true);
|
|
if (bodyRoot != null) bodyRoot.localScale = Vector3.zero;
|
|
|
|
// Scale and rotation before alignment, since both move the hat bone.
|
|
hood.transform.localRotation = Quaternion.Euler(DvergurConfig.HoodOffsetRotation);
|
|
hood.transform.localScale = Vector3.one * DvergurConfig.HoodExtraScale;
|
|
|
|
// The hat now sits wherever the dvergr's own skull was. Shifting the whole object by
|
|
// the gap between that and the player's head bone lands it correctly without a single
|
|
// hand-measured offset.
|
|
hood.transform.position += playerHead.position - hatBone.position;
|
|
hood.transform.localPosition += DvergurConfig.HoodOffsetPosition;
|
|
|
|
foreach (SkinnedMeshRenderer renderer in hood.GetComponentsInChildren<SkinnedMeshRenderer>(true))
|
|
{
|
|
// The collapsed bones leave the mesh's own bounds meaningless, and a skinned
|
|
// renderer culls against those - without this the hood vanishes at some angles.
|
|
renderer.updateWhenOffscreen = true;
|
|
}
|
|
|
|
ApplyDitherShader(hood);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Strips everything that would make a decorative mesh behave like a creature: AI, hover
|
|
/// prompts and physics, plus the Animator and LODGroup, which are not MonoBehaviours and
|
|
/// would otherwise keep driving or hiding the rig.
|
|
/// </summary>
|
|
private static void StripComponents(GameObject obj)
|
|
{
|
|
foreach (MonoBehaviour comp in obj.GetComponentsInChildren<MonoBehaviour>(true))
|
|
{
|
|
Object.DestroyImmediate(comp);
|
|
}
|
|
foreach (Animator animator in obj.GetComponentsInChildren<Animator>(true))
|
|
{
|
|
Object.DestroyImmediate(animator);
|
|
}
|
|
foreach (LODGroup lod in obj.GetComponentsInChildren<LODGroup>(true))
|
|
{
|
|
Object.DestroyImmediate(lod);
|
|
}
|
|
foreach (Rigidbody body in obj.GetComponentsInChildren<Rigidbody>(true))
|
|
{
|
|
Object.DestroyImmediate(body);
|
|
}
|
|
foreach (Collider col in obj.GetComponentsInChildren<Collider>(true))
|
|
{
|
|
Object.DestroyImmediate(col);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Without the creature shader the hood ignores the camera's dither fade and fills the
|
|
/// screen in first person; its own textures are kept.
|
|
/// </summary>
|
|
private static void ApplyDitherShader(GameObject obj)
|
|
{
|
|
Shader dither = Shader.Find("Custom/Creature");
|
|
if (dither == null) return;
|
|
|
|
foreach (Renderer renderer in obj.GetComponentsInChildren<Renderer>(true))
|
|
{
|
|
if (renderer.material != null) renderer.material.shader = dither;
|
|
}
|
|
}
|
|
|
|
private static GameObject ResolveDvergerVisual()
|
|
{
|
|
if (visualResolved) return cachedVisual;
|
|
visualResolved = true;
|
|
|
|
GameObject prefab = PrefabManager.Cache.GetPrefab<GameObject>(DvergurConfig.DvergerPrefabName);
|
|
if (prefab == null)
|
|
{
|
|
Jotunn.Logger.LogWarning($"[Dvergur] Prefab '{DvergurConfig.DvergerPrefabName}' not found; no hood.");
|
|
return null;
|
|
}
|
|
|
|
Transform visual = prefab.transform.Find(DvergurConfig.DvergerVisualName);
|
|
if (visual == null)
|
|
{
|
|
// Fall back to whatever holds the renderer, in case the child is ever renamed.
|
|
SkinnedMeshRenderer renderer = prefab.GetComponentInChildren<SkinnedMeshRenderer>(true);
|
|
visual = renderer != null ? renderer.transform.parent : null;
|
|
}
|
|
|
|
if (visual == null)
|
|
{
|
|
Jotunn.Logger.LogWarning(
|
|
$"[Dvergur] '{DvergurConfig.DvergerPrefabName}' has no '{DvergurConfig.DvergerVisualName}' child and no renderer; no hood.");
|
|
return null;
|
|
}
|
|
|
|
Jotunn.Logger.LogInfo($"[Dvergur] Hood source resolved to '{DvergurConfig.DvergerPrefabName}/{visual.name}'.");
|
|
cachedVisual = visual.gameObject;
|
|
return cachedVisual;
|
|
}
|
|
|
|
private static void RemoveHood(VisEquipment visEq)
|
|
{
|
|
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
|
|
|
|
Transform playerHead = FindBoneByName(visEq.m_bodyModel.bones, DvergurConfig.PlayerHeadBoneName);
|
|
Transform hood = playerHead != null ? playerHead.Find(HoodName) : null;
|
|
if (hood == null) return; // never was a Dvergur - leave the helmet alone
|
|
|
|
// Destroy is deferred to the end of the frame, so the object is renamed first: without
|
|
// that, every call until it actually goes away finds it again and re-runs the restore.
|
|
hood.name = HoodName + "_Removed";
|
|
Object.Destroy(hood.gameObject);
|
|
|
|
GameObject helmet = Traverse.Create(visEq).Field<GameObject>("m_helmetItemInstance").Value;
|
|
if (helmet != null && helmet.transform.localScale == Vector3.zero)
|
|
{
|
|
helmet.transform.localScale = Vector3.one;
|
|
}
|
|
}
|
|
|
|
// ─── Bone lookup ─────────────────────────────────────────────────────
|
|
|
|
private static Transform FindBoneByName(Transform[] bones, string targetBoneName)
|
|
{
|
|
if (bones == null || string.IsNullOrEmpty(targetBoneName)) return null;
|
|
|
|
foreach (Transform bone in bones)
|
|
{
|
|
if (bone != null && bone.name == targetBoneName) return bone;
|
|
}
|
|
|
|
string target = targetBoneName.ToLower();
|
|
foreach (Transform bone in bones)
|
|
{
|
|
if (bone == null) continue;
|
|
string name = bone.name.ToLower();
|
|
if (name == target || name.Contains(target) || target.Contains(name)) return bone;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>Depth-first search by exact name; the dvergr rig nests bones many levels deep.</summary>
|
|
private static Transform FindChildByName(Transform root, string name)
|
|
{
|
|
if (root == null) return null;
|
|
if (root.name == name) return root;
|
|
|
|
for (int i = 0; i < root.childCount; i++)
|
|
{
|
|
Transform found = FindChildByName(root.GetChild(i), name);
|
|
if (found != null) return found;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|