diff --git a/JotunnModStub/Dvergar/DvergarConfig.cs b/JotunnModStub/Dvergar/DvergarConfig.cs index 7fbeb82..15ac579 100644 --- a/JotunnModStub/Dvergar/DvergarConfig.cs +++ b/JotunnModStub/Dvergar/DvergarConfig.cs @@ -81,6 +81,27 @@ namespace RacesOfValheim // 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); + // ─── 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 ─────────────────────────────────────────────────── // The helmet slot itself stays free — whatever the player equips keeps all of its stats, // only the mesh is swapped. diff --git a/JotunnModStub/Dvergar/DvergarPatches.cs b/JotunnModStub/Dvergar/DvergarPatches.cs index 0375054..a744e15 100644 --- a/JotunnModStub/Dvergar/DvergarPatches.cs +++ b/JotunnModStub/Dvergar/DvergarPatches.cs @@ -339,7 +339,15 @@ namespace RacesOfValheim if (__instance == null || !__instance.m_isPlayer) return; Player player = __instance.GetComponent(); - 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) { diff --git a/JotunnModStub/Dvergar/DvergarVisuals.cs b/JotunnModStub/Dvergar/DvergarVisuals.cs index f27c198..a253124 100644 --- a/JotunnModStub/Dvergar/DvergarVisuals.cs +++ b/JotunnModStub/Dvergar/DvergarVisuals.cs @@ -107,6 +107,136 @@ namespace RacesOfValheim return BossProgressionManager.GetCurrentProgressionState(); } + // ─── Glowing eyes ──────────────────────────────────────────────────── + + private const string EyesName = "DvergarEyes"; + + private static Shader cachedGlowShader; + private static bool glowShaderResolved; + + /// + /// 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. + /// + 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(true)) + { + Material material = renderer.material; + if (material == null) continue; + + material.color = glow; + if (material.HasProperty("_EmissionColor")) material.SetColor("_EmissionColor", glow); + } + } + + /// + /// 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. + /// + 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()) + { + 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.material = new Material(glow); + renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; + renderer.receiveShadows = false; + } + + return holder.transform; + } + + /// + /// 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. + /// + 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;