feat(dvergar): give the eyes a glow, and take the colour from the stars

A player's eyes are painted into the skin texture, and Custom/Player has no
emission channel to light them with - the shader survey in the last commit is
what established that. So a Dvergar wears two small emissive quads over them
instead. There was no working example to copy: the Aswid's red eyes were never
built, only cleaned up, which is why they never glowed.

Colour comes from the same LevelSetup list the skin tint does. One star is
RGBA(0, 4, 3.3), two stars RGBA(12, 0, 8.8) - both far past 1, which is what
makes them bloom rather than merely look bright. Only the base tier's colour is
ours to choose.

The quads hang off Character.m_eye, which already sits at eye height and already
faces the look direction, so the offsets are plain metres in an obvious frame:
no working out which way a rig bone points, and no fighting the 100x scale the
player armature is authored at. Everything about the placement is a named
constant, since none of it can be judged without seeing it.

Applied from the UpdateColors postfix rather than UpdateVisuals so that the
refresh DvergarRace already forces when a boss falls recolours the eyes in the
same frame it recolours the skin. It runs before the race check so it also takes
them off again.

The glow shader is resolved from a list and the winner is logged, because Unity
strips shaders nothing references and assuming one would be a silent failure.
This commit is contained in:
kanym 2026-08-05 20:05:52 +02:00
commit 9dc072f0a5
3 changed files with 160 additions and 1 deletions

View file

@ -107,6 +107,136 @@ namespace RacesOfValheim
return BossProgressionManager.GetCurrentProgressionState();
}
// ─── Glowing eyes ────────────────────────────────────────────────────
private const string EyesName = "DvergarEyes";
private static Shader cachedGlowShader;
private static bool glowShaderResolved;
/// <summary>
/// Keeps two small emissive quads sitting over the player's painted-on eyes, coloured by
/// star tier. Safe to call for anyone: it removes them again from whoever is not a living
/// Dvergar.
///
/// They hang off Character.m_eye rather than the head bone. m_eye is already at eye
/// height and already faces the look direction, so the offsets below are plain metres in
/// an obvious frame - no working out which way a rig bone happens to point, and no
/// fighting the 100x scale the player armature is authored at.
/// </summary>
public static void ApplyEyes(Player player)
{
if (player == null) return;
Transform anchor = player.m_eye;
if (anchor == null) return;
Transform eyes = anchor.Find(EyesName);
if (!IsDvergarPlayer(player) || player.IsDead())
{
if (eyes != null)
{
// Renamed first: Destroy is deferred to the end of the frame, and every call
// until then would otherwise find it again.
eyes.name = EyesName + "_Removed";
Object.Destroy(eyes.gameObject);
}
return;
}
if (eyes == null) eyes = BuildEyes(anchor);
if (eyes == null) return; // already logged once by the resolver
Color glow = GetEyeColor(player);
foreach (Renderer renderer in eyes.GetComponentsInChildren<Renderer>(true))
{
Material material = renderer.material;
if (material == null) continue;
material.color = glow;
if (material.HasProperty("_EmissionColor")) material.SetColor("_EmissionColor", glow);
}
}
/// <summary>
/// One star and two stars use the emissive colours the game gives a starred dverger, read
/// off the same LevelSetup list the skin tint comes from. Only the base tier is ours.
/// </summary>
private static Color GetEyeColor(Player player)
{
LevelEffects.LevelSetup setup = GetStarSetup(GetStarTier(GetProgressionState(player)));
if (setup != null && setup.m_setEmissiveColor) return setup.m_emissiveColor;
return DvergarConfig.BaseEyeColor;
}
private static Transform BuildEyes(Transform anchor)
{
Shader glow = ResolveGlowShader();
if (glow == null) return null;
GameObject holder = new GameObject(EyesName);
holder.transform.SetParent(anchor, false);
holder.transform.localPosition = Vector3.zero;
holder.transform.localRotation = Quaternion.identity;
holder.transform.localScale = Vector3.one;
for (int i = 0; i < 2; i++)
{
float side = i == 0 ? -1f : 1f;
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.name = "Eye" + i;
// A primitive comes with a collider, which on a player's face would be its own
// kind of bug.
foreach (Collider collider in quad.GetComponents<Collider>())
{
Object.Destroy(collider);
}
quad.transform.SetParent(holder.transform, false);
quad.transform.localPosition = new Vector3(
side * DvergarConfig.EyeSeparation, DvergarConfig.EyeHeight, DvergarConfig.EyeForward);
quad.transform.localRotation = Quaternion.identity;
quad.transform.localScale = Vector3.one * DvergarConfig.EyeSize;
Renderer renderer = quad.GetComponent<Renderer>();
renderer.material = new Material(glow);
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.receiveShadows = false;
}
return holder.transform;
}
/// <summary>
/// Finds a shader that will render a flat colour past 1 so the bloom pass picks it up.
/// Unity strips shaders nothing references, so this asks for several and reports which one
/// the game actually shipped rather than assuming.
/// </summary>
private static Shader ResolveGlowShader()
{
if (glowShaderResolved) return cachedGlowShader;
glowShaderResolved = true;
string[] candidates = { "Sprites/Default", "Unlit/Color", "UI/Default", "Particles/Standard Unlit", "Standard" };
foreach (string name in candidates)
{
Shader shader = Shader.Find(name);
if (shader == null) continue;
Jotunn.Logger.LogInfo($"[Dvergar] Eye glow shader resolved to '{name}'.");
cachedGlowShader = shader;
return cachedGlowShader;
}
Jotunn.Logger.LogWarning(
"[Dvergar] No glow shader resolved (" + string.Join(", ", candidates) + "); eyes stay unlit.");
return null;
}
// ─── Shader survey ───────────────────────────────────────────────────
private static bool loggedSkinShader;