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")}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue