using System; using System.Collections.Generic; using UnityEngine; namespace Luneraria.View { /// /// The shared material library (port of scripts/luneraria/view/lune_materials.gd). Models name /// their surfaces (LN_*, ENV_*); Dress swaps them for one shared material per name and team, /// so all buildings share a few materials (SRP batcher friendly). Team 0 Blau, 1 Rot, 2 neutral. /// public sealed class LuneMaterialSet : ScriptableObject { [Serializable] public sealed class Entry { public string Key; public Material Neutral; public Material Blau; public Material Rot; } public List Entries = new List(); Dictionary _index; public Material Get(string key, int team) { if (_index == null) { _index = new Dictionary(); foreach (Entry entry in Entries) _index[entry.Key] = entry; } if (!_index.TryGetValue(key, out Entry found)) return null; Material team_material = team == 0 ? found.Blau : team == 1 ? found.Rot : null; return team_material != null ? team_material : found.Neutral; } /// Material key of an imported slot material ("LN_Stone_Pale", "ENV_bark (Instance)" -> key). public static string KeyOf(Material material) { if (material == null) return ""; string name = material.name; int cut = name.IndexOf(" (", StringComparison.Ordinal); return cut < 0 ? name : name.Substring(0, cut); } /// Swaps every known surface below root for the shared material of the team. public void Dress(GameObject root, int team) { foreach (Renderer renderer in root.GetComponentsInChildren(true)) { Material[] materials = renderer.sharedMaterials; bool changed = false; for (int i = 0; i < materials.Length; i++) { Material shared = Get(KeyOf(materials[i]), team); if (shared != null && shared != materials[i]) { materials[i] = shared; changed = true; } } if (changed) renderer.sharedMaterials = materials; } } } }