RacesOfValheim/JotunnModStub/PieceLook.cs
kanym f3d3c0ebb5 feat: new looks for the Race Menu Ward, Contract Bank and Bone Servant tables
Recolour and kitbash from vanilla parts, no new assets. PieceLook copies
materials before tinting so vanilla pieces keep their colours. ModelBorrow
gains activeOnly, which drops hidden snow, fire and broken meshes before
a borrowed piece is measured.
2026-10-28 12:40:33 +01:00

119 lines
5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// Recolouring a cloned vanilla piece without touching the original.
///
/// A clone made by CreateClonedPrefab shares its MATERIALS with the vanilla prefab, so writing a
/// colour onto renderer.sharedMaterial would repaint every Ward or cartography table in the world.
/// Each material is copied once and the copy is written; renderers that shared one material before
/// share the one copy after, so the LOD, worn and broken meshes stay matched.
/// </summary>
public static class PieceLook
{
/// <summary>
/// Multiplies the base colour of every mesh under <paramref name="root"/>, inactive ones included
/// (a piece's worn and broken meshes are switched off until it takes damage). Particles are left
/// alone: that is what <see cref="Rehue"/> is for.
/// </summary>
public static void Tint(Transform root, Color tint)
{
if (root == null) return;
var copies = new Dictionary<Material, Material>();
foreach (Renderer renderer in root.GetComponentsInChildren<Renderer>(true))
{
if (renderer == null || renderer is ParticleSystemRenderer) continue;
Material[] materials = renderer.sharedMaterials;
for (int i = 0; i < materials.Length; i++)
{
Material copy = CopyOf(materials[i], copies);
if (copy == null) continue;
if (copy.HasProperty("_Color")) copy.SetColor("_Color", copy.GetColor("_Color") * tint);
materials[i] = copy;
}
renderer.sharedMaterials = materials;
}
}
/// <summary>
/// Swaps the hue of everything under <paramref name="root"/> that GLOWS - lights, particles and
/// any material with emission - for <paramref name="glow"/>, keeping each one's brightness.
///
/// Brightness is kept rather than overwritten because these values are not 0 to 1: the Ward's
/// sparks are authored at a colour of 2.0, and painting them a flat 1.0 blue would dim them by
/// half. A material whose emission is black does not glow and is skipped, so the Ward's stone
/// stays stone.
/// </summary>
public static void Rehue(Transform root, Color glow)
{
if (root == null) return;
foreach (Light light in root.GetComponentsInChildren<Light>(true))
{
if (light != null) light.color = glow;
}
foreach (ParticleSystem ps in root.GetComponentsInChildren<ParticleSystem>(true))
{
if (ps == null) continue;
ParticleSystem.MainModule main = ps.main;
main.startColor = new ParticleSystem.MinMaxGradient(glow);
}
var copies = new Dictionary<Material, Material>();
foreach (Renderer renderer in root.GetComponentsInChildren<Renderer>(true))
{
if (renderer == null) continue;
bool particles = renderer is ParticleSystemRenderer;
Material[] materials = renderer.sharedMaterials;
bool changed = false;
for (int i = 0; i < materials.Length; i++)
{
Material source = materials[i];
if (source == null) continue;
bool emits = source.HasProperty("_EmissionColor") && Brightness(source.GetColor("_EmissionColor")) > 0.01f;
if (!emits && !particles) continue;
Material copy = CopyOf(source, copies);
if (emits) Recolour(copy, "_EmissionColor", glow);
if (particles)
{
Recolour(copy, "_Color", glow);
Recolour(copy, "_TintColor", glow);
}
materials[i] = copy;
changed = true;
}
if (changed) renderer.sharedMaterials = materials;
}
}
private static void Recolour(Material material, string property, Color glow)
{
if (!material.HasProperty(property)) return;
Color old = material.GetColor(property);
Color hue = glow * Brightness(old);
hue.a = old.a;
material.SetColor(property, hue);
}
private static float Brightness(Color colour) => Mathf.Max(colour.r, Mathf.Max(colour.g, colour.b));
private static Material CopyOf(Material source, Dictionary<Material, Material> copies)
{
if (source == null) return null;
if (!copies.TryGetValue(source, out Material copy))
{
copy = new Material(source) { name = source.name + "_rov" };
copies[source] = copy;
}
return copy;
}
}
}