warvulture
This commit is contained in:
parent
1d66b00374
commit
1c4b20bb06
13 changed files with 1671 additions and 142 deletions
264
JotunnModStub/Skeleton/SkeletonEyeGlow.cs
Normal file
264
JotunnModStub/Skeleton/SkeletonEyeGlow.cs
Normal file
|
|
@ -0,0 +1,264 @@
|
|||
using System.Collections.Generic;
|
||||
using Jotunn.Managers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// The lights in the eye sockets, taken from the creature the tier's body is borrowed from.
|
||||
///
|
||||
/// The body swap copies SkinnedMeshRenderers and nothing else, which is the whole of the mesh
|
||||
/// but none of the effects hung off the creature's rig - so the race wore a skull with two
|
||||
/// empty holes where the mob it is wearing has a lit pair. Every part of that lighting is
|
||||
/// authored per creature, and the tiers wear three different creatures (Skeleton,
|
||||
/// Skeleton_Poison, Skeleton_Hildir) with three different colours, so nothing here names a
|
||||
/// colour: each tier's glow is cloned out of that tier's own prefab and arrives already the
|
||||
/// right one.
|
||||
///
|
||||
/// What counts as "the eye lights" is deliberately broad - any Light, any ParticleSystem, and
|
||||
/// any mesh whose object is named for an eye - because which of the three a given creature
|
||||
/// uses is the creature's business, not ours. Everything cloned is logged once per model, so
|
||||
/// if a tier turns out to carry something this misses, the log says exactly what it carries.
|
||||
///
|
||||
/// Each clone is parented to the player bone its own parent maps onto - the same by-name
|
||||
/// mapping the body itself is skinned through - and keeps the local pose it had on the
|
||||
/// creature, so it sits where the creature wore it and rides the head animation for free.
|
||||
/// </summary>
|
||||
public static class SkeletonEyeGlow
|
||||
{
|
||||
// Every clone carries this, followed by the model it came from: the sweep below uses the
|
||||
// name alone to tell its own work from a previous tier's and from everyone else's.
|
||||
private const string Prefix = "SkeletonEyeFX_";
|
||||
|
||||
private static readonly HashSet<string> loggedModels = new HashSet<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Makes sure the player wears the eye lights of <paramref name="modelName"/> and of no
|
||||
/// other tier. Cheap to call on every visual refresh: once the right ones are on, it does
|
||||
/// nothing but the sweep.
|
||||
/// </summary>
|
||||
public static void Apply(VisEquipment vis, string modelName)
|
||||
{
|
||||
Transform[] bones = GetBones(vis);
|
||||
if (bones == null || string.IsNullOrEmpty(modelName)) return;
|
||||
|
||||
string wanted = Prefix + modelName + "_";
|
||||
if (Sweep(bones, wanted)) return; // already wearing this tier's
|
||||
|
||||
GameObject source = PrefabManager.Cache.GetPrefab<GameObject>(modelName);
|
||||
if (source == null) return; // the body swap already warns about a missing model
|
||||
|
||||
LogSourceEffectsOnce(modelName, source);
|
||||
|
||||
foreach (GameObject effect in CollectEffects(source))
|
||||
{
|
||||
Transform parentBone = ResolveParentBone(bones, effect.transform);
|
||||
if (parentBone == null) continue;
|
||||
|
||||
Clone(effect, parentBone, wanted);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Takes every cloned light back off, for a player who is no longer a Skeleton.</summary>
|
||||
public static void Clear(VisEquipment vis)
|
||||
{
|
||||
Transform[] bones = GetBones(vis);
|
||||
if (bones == null) return;
|
||||
|
||||
Sweep(bones, null);
|
||||
}
|
||||
|
||||
private static Transform[] GetBones(VisEquipment vis)
|
||||
{
|
||||
if (vis == null || vis.m_bodyModel == null) return null;
|
||||
return vis.m_bodyModel.bones;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destroys clones that do not belong to <paramref name="keepPrefix"/> and reports whether
|
||||
/// any that do are already in place. Pass null to clear the lot.
|
||||
/// </summary>
|
||||
private static bool Sweep(Transform[] bones, string keepPrefix)
|
||||
{
|
||||
bool present = false;
|
||||
|
||||
foreach (Transform bone in bones)
|
||||
{
|
||||
if (bone == null) continue;
|
||||
|
||||
for (int i = bone.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
Transform child = bone.GetChild(i);
|
||||
if (!child.name.StartsWith(Prefix)) continue;
|
||||
|
||||
if (keepPrefix != null && child.name.StartsWith(keepPrefix))
|
||||
{
|
||||
present = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Renamed before it goes: Destroy only takes effect at the end of the frame,
|
||||
// and until then this sweep would keep finding it and counting it as present.
|
||||
child.name = child.name + "_Removed";
|
||||
Object.Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
return present;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The objects on a creature worth bringing across. Depth first, and a match swallows its
|
||||
/// own subtree - a light with a particle system under it is one clone, not two.
|
||||
/// </summary>
|
||||
private static List<GameObject> CollectEffects(GameObject source)
|
||||
{
|
||||
List<GameObject> found = new List<GameObject>();
|
||||
HashSet<Transform> taken = new HashSet<Transform>();
|
||||
|
||||
foreach (Transform t in source.GetComponentsInChildren<Transform>(true))
|
||||
{
|
||||
if (!IsEffect(t)) continue;
|
||||
|
||||
bool insideOne = false;
|
||||
for (Transform parent = t.parent; parent != null; parent = parent.parent)
|
||||
{
|
||||
if (taken.Contains(parent)) { insideOne = true; break; }
|
||||
}
|
||||
if (insideOne) continue;
|
||||
|
||||
taken.Add(t);
|
||||
found.Add(t.gameObject);
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
private static bool IsEffect(Transform t)
|
||||
{
|
||||
if (t.GetComponent<Light>() != null) return true;
|
||||
if (t.GetComponent<ParticleSystem>() != null) return true;
|
||||
|
||||
// Meshes are taken by name only. A creature's non-skinned meshes are mostly the
|
||||
// weapons in its hands, and a player who sprouts a second sword out of their wrist is
|
||||
// a worse bug than a missing glow.
|
||||
return t.name.ToLower().Contains("eye") && t.GetComponent<MeshRenderer>() != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The player bone a creature's effect should hang from: whatever its own parent chain
|
||||
/// first matches by name. No root fallback - an effect nobody can place is better left
|
||||
/// off than dropped on the pelvis.
|
||||
/// </summary>
|
||||
private static Transform ResolveParentBone(Transform[] bones, Transform effect)
|
||||
{
|
||||
for (Transform parent = effect.parent; parent != null; parent = parent.parent)
|
||||
{
|
||||
Transform match = SkeletonPatches.VisEquipment_UpdateVisuals_Patch.FindBoneByName(bones, parent.name);
|
||||
if (match != null) return match;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void Clone(GameObject source, Transform parentBone, string namePrefix)
|
||||
{
|
||||
GameObject copy = Object.Instantiate(source, parentBone);
|
||||
copy.name = namePrefix + source.name;
|
||||
|
||||
// The pose it was authored at on the creature. Instantiate keeps the world transform,
|
||||
// which for a prefab means nothing useful.
|
||||
copy.transform.localPosition = source.transform.localPosition;
|
||||
copy.transform.localRotation = source.transform.localRotation;
|
||||
copy.transform.localScale = source.transform.localScale;
|
||||
|
||||
// Allowlist, not a blocklist - the same rule the head flame is built under. Whatever
|
||||
// else rode along (an AnimEvent, a collider, a script that expects a Character above
|
||||
// it) goes, whatever type it turns out to be; what is left is a light and the things
|
||||
// that draw it.
|
||||
foreach (Component comp in copy.GetComponentsInChildren<Component>(true))
|
||||
{
|
||||
if (comp is Transform || comp is Light || comp is ParticleSystem ||
|
||||
comp is ParticleSystemRenderer || comp is MeshRenderer || comp is MeshFilter)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Object.Destroy(comp);
|
||||
}
|
||||
|
||||
// A creature can leave its own effects switched off for a script to turn on later, and
|
||||
// that script is exactly what was just destroyed.
|
||||
copy.SetActive(true);
|
||||
foreach (Light light in copy.GetComponentsInChildren<Light>(true)) light.enabled = true;
|
||||
foreach (Renderer renderer in copy.GetComponentsInChildren<Renderer>(true)) renderer.enabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Says what a creature actually carries, once per model per session.
|
||||
///
|
||||
/// Which of the three shapes a glow is authored as is not something to guess at, and if a
|
||||
/// tier arrives here with none of them, the emission on its skinned materials - printed
|
||||
/// below - is the remaining answer to where its eyes get their colour.
|
||||
/// </summary>
|
||||
private static void LogSourceEffectsOnce(string modelName, GameObject source)
|
||||
{
|
||||
if (!loggedModels.Add(modelName)) return;
|
||||
|
||||
Jotunn.Logger.LogInfo($"[Skeleton] Eye glow: reading '{modelName}'.");
|
||||
|
||||
int count = 0;
|
||||
foreach (Transform t in source.GetComponentsInChildren<Transform>(true))
|
||||
{
|
||||
Light light = t.GetComponent<Light>();
|
||||
if (light != null)
|
||||
{
|
||||
count++;
|
||||
Jotunn.Logger.LogInfo(
|
||||
$"[Skeleton] Light '{t.name}' under '{(t.parent != null ? t.parent.name : "-")}' " +
|
||||
$"color={light.color} intensity={light.intensity:F2} range={light.range:F2} " +
|
||||
$"active={t.gameObject.activeSelf}");
|
||||
}
|
||||
|
||||
ParticleSystem ps = t.GetComponent<ParticleSystem>();
|
||||
if (ps != null)
|
||||
{
|
||||
count++;
|
||||
Jotunn.Logger.LogInfo(
|
||||
$"[Skeleton] ParticleSystem '{t.name}' under '{(t.parent != null ? t.parent.name : "-")}' " +
|
||||
$"startColor={ps.main.startColor.color} active={t.gameObject.activeSelf}");
|
||||
}
|
||||
|
||||
MeshRenderer mesh = t.GetComponent<MeshRenderer>();
|
||||
if (mesh != null)
|
||||
{
|
||||
count++;
|
||||
Jotunn.Logger.LogInfo(
|
||||
$"[Skeleton] MeshRenderer '{t.name}' under '{(t.parent != null ? t.parent.name : "-")}' " +
|
||||
$"material='{(mesh.sharedMaterial != null ? mesh.sharedMaterial.name : "none")}' " +
|
||||
$"active={t.gameObject.activeSelf}");
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
Jotunn.Logger.LogInfo("[Skeleton] no lights, particles or loose meshes at all.");
|
||||
}
|
||||
|
||||
foreach (SkinnedMeshRenderer smr in source.GetComponentsInChildren<SkinnedMeshRenderer>(true))
|
||||
{
|
||||
foreach (Material material in smr.sharedMaterials)
|
||||
{
|
||||
if (material == null) continue;
|
||||
|
||||
string emission = material.HasProperty("_EmissionColor")
|
||||
? material.GetColor("_EmissionColor").ToString()
|
||||
: "no _EmissionColor";
|
||||
Jotunn.Logger.LogInfo(
|
||||
$"[Skeleton] skin '{smr.name}' material '{material.name}' shader '{material.shader?.name}' " +
|
||||
$"emission={emission} keyword={material.IsKeywordEnabled("_EMISSION")}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
114
JotunnModStub/Skeleton/SkeletonJawFix.cs
Normal file
114
JotunnModStub/Skeleton/SkeletonJawFix.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// Poses the Skeleton's mandible, which the player rig alone puts in the wrong place.
|
||||
///
|
||||
/// The three skeleton models are skinned to the player rig by bone name, and the rig owns a
|
||||
/// real 'Jaw' bone, so the mandible lands on the right bone - just at the rest pose that bone
|
||||
/// happens to hold, which sits a little low and a little far back for a skull. Nothing here
|
||||
/// remaps anything: it moves that one bone, and only that one.
|
||||
///
|
||||
/// THE POSE IS WRITTEN ABSOLUTELY, NEVER ACCUMULATED. That distinction is the whole reason
|
||||
/// this file reads the way it does. No Valheim animation touches the Jaw bone, so nothing
|
||||
/// resets it between frames: an earlier version added its offset to whatever the bone already
|
||||
/// held, on the usual assumption that the Animator rewrites the rig every frame. It does not,
|
||||
/// so the jaw drifted a few centimetres further away every frame - it left the skull entirely
|
||||
/// within a fifth of a second, snapped back when a visual refresh happened to rebind the rig,
|
||||
/// and started over, which is what showed up in game as a head flickering five times a second.
|
||||
/// With the game paused nothing rebound anything and the mandible simply kept going, dragging
|
||||
/// the vertices weighted to it into a metres-long spike. Writing the base pose back before
|
||||
/// applying the correction makes the whole thing idempotent: applied once, five times or not
|
||||
/// at all in a given frame, the jaw ends up in exactly the same place.
|
||||
///
|
||||
/// Runs in LateUpdate, after the animation phase, so the correction cannot be undone by an
|
||||
/// animation that does move the head around the jaw.
|
||||
///
|
||||
/// Attached to every player VisEquipment rebuilds visuals for - local, remote and the
|
||||
/// main-menu preview - so the jaw is right for everyone looking, not just for the client
|
||||
/// wearing it.
|
||||
/// </summary>
|
||||
public class SkeletonJawFix : MonoBehaviour
|
||||
{
|
||||
// Found in game with a temporary set of Configuration Manager sliders, on the tier-3
|
||||
// Skeleton_Poison model; the plain and the fire models share the rig and the same jaw rest
|
||||
// pose bone for bone (confirmed in the bone-mapping dump), so one correction covers all
|
||||
// three. Metres along the skull's own axes, so the mandible keeps following the head
|
||||
// however it turns.
|
||||
private static readonly Vector3 Offset = new Vector3(0.0338f, 0.0394f, 0f);
|
||||
|
||||
// The rig's jaw is a touch large for these skulls.
|
||||
private const float Scale = 0.9437f;
|
||||
|
||||
private Player m_player;
|
||||
private VisEquipment m_vis;
|
||||
|
||||
private Transform m_jaw;
|
||||
private Vector3 m_baseLocalPosition;
|
||||
private Vector3 m_baseLocalScale;
|
||||
private bool m_applied;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
m_player = GetComponent<Player>();
|
||||
m_vis = GetComponent<VisEquipment>();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (m_player == null || m_vis == null) return;
|
||||
|
||||
Transform jaw = ResolveJaw();
|
||||
if (jaw == null || jaw.parent == null) return;
|
||||
|
||||
if (!SkeletonVisuals.IsSkeletonPlayer(m_player))
|
||||
{
|
||||
// Nothing rewrites this bone on its own, so a player who stops being a Skeleton
|
||||
// would otherwise keep wearing the correction on their human face.
|
||||
if (m_applied)
|
||||
{
|
||||
jaw.localPosition = m_baseLocalPosition;
|
||||
jaw.localScale = m_baseLocalScale;
|
||||
m_applied = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Back to the rest pose first: this is what makes the block below absolute rather than
|
||||
// a nudge on top of last frame's nudge. See the note in the class summary.
|
||||
jaw.localPosition = m_baseLocalPosition;
|
||||
jaw.localScale = m_baseLocalScale * Scale;
|
||||
|
||||
// In world space rather than localPosition, so the offset stays in metres whatever the
|
||||
// rig's authored scale is, and rotated by the skull so it tracks the head.
|
||||
jaw.position += jaw.parent.rotation * Offset;
|
||||
|
||||
m_applied = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The player rig's own jaw bone, with the rest pose it holds before anything here has
|
||||
/// touched it. Captured once per bone: re-reading it later would sample a pose this
|
||||
/// component had already moved and bake the correction in twice.
|
||||
/// </summary>
|
||||
private Transform ResolveJaw()
|
||||
{
|
||||
if (m_jaw != null) return m_jaw;
|
||||
if (m_vis.m_bodyModel == null || m_vis.m_bodyModel.bones == null) return null;
|
||||
|
||||
foreach (Transform bone in m_vis.m_bodyModel.bones)
|
||||
{
|
||||
if (bone == null || !bone.name.ToLower().Contains("jaw")) continue;
|
||||
|
||||
m_jaw = bone;
|
||||
m_baseLocalPosition = bone.localPosition;
|
||||
m_baseLocalScale = bone.localScale;
|
||||
m_applied = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return m_jaw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -338,6 +338,21 @@ namespace RacesOfValheim
|
|||
HideItemInstances(__instance, "m_hairItemInstance");
|
||||
HideItemInstances(__instance, "m_beardItemInstance");
|
||||
SetHelmetVisible(__instance, false);
|
||||
|
||||
// Poses the mandible, once per frame, after the animation. Attached from
|
||||
// here rather than from SkeletonRace because this postfix is the one path
|
||||
// every skeleton reaches - local, remote and the menu preview - and the
|
||||
// jaw is a rig bone, so whoever is looking sees the same face.
|
||||
if (p.GetComponent<SkeletonJawFix>() == null)
|
||||
{
|
||||
p.gameObject.AddComponent<SkeletonJawFix>();
|
||||
}
|
||||
|
||||
// The lights in the eye sockets, cloned out of this tier's own creature so
|
||||
// that each one arrives its own colour. Outside the build block above on
|
||||
// purpose: they hang off the player's rig bones rather than off the body
|
||||
// container, so they outlive it and have to be checked every refresh.
|
||||
SkeletonEyeGlow.Apply(__instance, rules.ModelPrefab);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -362,6 +377,10 @@ namespace RacesOfValheim
|
|||
ShowItemInstances(__instance, "m_hairItemInstance");
|
||||
ShowItemInstances(__instance, "m_beardItemInstance");
|
||||
SetHelmetVisible(__instance, true);
|
||||
|
||||
// Parented to rig bones, not to the body container that was just switched
|
||||
// off above - so without this a human keeps the skeleton's lit sockets.
|
||||
SkeletonEyeGlow.Clear(__instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -514,7 +533,12 @@ namespace RacesOfValheim
|
|||
}
|
||||
}
|
||||
|
||||
private static Transform FindBoneByName(Transform[] bones, string skelBoneName)
|
||||
/// <summary>
|
||||
/// The player bone a creature's bone maps onto. Internal because the eye glow hangs
|
||||
/// its clones off the same mapping the body is skinned through - one answer to
|
||||
/// "which bone is this", not two that can drift apart.
|
||||
/// </summary>
|
||||
internal static Transform FindBoneByName(Transform[] bones, string skelBoneName)
|
||||
{
|
||||
// Exact match first
|
||||
foreach (Transform b in bones)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue