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

@ -81,6 +81,27 @@ namespace RacesOfValheim
// B for a colder one. Keeping R below B is what holds the flesh tone down. // B for a colder one. Keeping R below B is what holds the flesh tone down.
public static readonly Color TwoStarSkinColor = new Color(0.34f, 0.37f, 0.44f, 1f); public static readonly Color TwoStarSkinColor = new Color(0.34f, 0.37f, 0.44f, 1f);
// ─── Glowing eyes ────────────────────────────────────────────────────
// A player's eyes are painted into the skin texture, and Custom/Player has no emission
// channel to light them with - so a Dvergar wears two small emissive quads over them
// instead. Nothing else in the mod does this, so there was no working example to copy;
// the Aswid's red eyes were never built at all, only cleaned up.
//
// Offsets are in metres from Character.m_eye, which already sits at eye height and faces
// wherever the player is looking, so no bone axes have to be guessed at. EyeForward is
// the one to tune first: too little and the quads sink into the face, too much and they
// float in front of it.
public static readonly float EyeSeparation = 0.035f; // half the distance between them
public static readonly float EyeForward = 0.075f;
public static readonly float EyeHeight = -0.010f;
public static readonly float EyeSize = 0.022f;
// Base glow, before any star. The tiers replace it with the emissive colours the game
// itself gives a starred dverger - RGBA(0, 4, 3.3) at one star and RGBA(12, 0, 8.8) at
// two, both far past 1, which is exactly what makes them bloom rather than just look
// bright.
public static readonly Color BaseEyeColor = new Color(0f, 2.2f, 1.8f, 1f);
// ─── Cosmetic hood ─────────────────────────────────────────────────── // ─── Cosmetic hood ───────────────────────────────────────────────────
// The helmet slot itself stays free — whatever the player equips keeps all of its stats, // The helmet slot itself stays free — whatever the player equips keeps all of its stats,
// only the mesh is swapped. // only the mesh is swapped.

View file

@ -339,7 +339,15 @@ namespace RacesOfValheim
if (__instance == null || !__instance.m_isPlayer) return; if (__instance == null || !__instance.m_isPlayer) return;
Player player = __instance.GetComponent<Player>(); Player player = __instance.GetComponent<Player>();
if (player == null || !DvergarVisuals.IsDvergarPlayer(player)) return; if (player == null) return;
// Before the race check, because it takes the eyes off anyone who has stopped
// being a Dvergar as well as putting them on anyone who is one. Here rather than
// in the UpdateVisuals postfix so that the tier refresh DvergarRace forces on a
// boss kill recolours them at the same moment it recolours the skin.
DvergarVisuals.ApplyEyes(player);
if (!DvergarVisuals.IsDvergarPlayer(player)) return;
if (__instance.m_bodyModel != null && __instance.m_bodyModel.material != null) if (__instance.m_bodyModel != null && __instance.m_bodyModel.material != null)
{ {

View file

@ -107,6 +107,136 @@ namespace RacesOfValheim
return BossProgressionManager.GetCurrentProgressionState(); 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 ─────────────────────────────────────────────────── // ─── Shader survey ───────────────────────────────────────────────────
private static bool loggedSkinShader; private static bool loggedSkinShader;