From 179c729b62411f7a72d89d17b86d767dda7849d8 Mon Sep 17 00:00:00 2001 From: Alvin Meng Date: Tue, 23 Jul 2024 20:59:23 -0400 Subject: [PATCH] fix: late-bound texture loading So that normal map loading works properly when a shader is not set and the existing shader has a custom normal map property name. --- Source/MaterialDef.cs | 45 +++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Source/MaterialDef.cs b/Source/MaterialDef.cs index d520b6c..b5780e1 100644 --- a/Source/MaterialDef.cs +++ b/Source/MaterialDef.cs @@ -64,7 +64,9 @@ public class MaterialDef public Dictionary floats; public Dictionary colors; public Dictionary vectors; - public Dictionary textures = new Dictionary(); + public Dictionary textureNames; + + readonly Dictionary textures = new Dictionary(); public MaterialDef(ConfigNode node) { @@ -78,30 +80,10 @@ public MaterialDef(ConfigNode node) } keywords = LoadDictionary(node.GetNode("Keyword")); - floats = LoadDictionary(node.GetNode("Float")); - colors = LoadDictionary(node.GetNode("Color"), ParseColor); - vectors = LoadDictionary(node.GetNode("Vector")); - - var textureNames = LoadDictionary(node.GetNode("Texture")); - foreach (var kvp in textureNames) { - var texInfo = GameDatabase.Instance.GetTextureInfo(kvp.Value); - if (texInfo == null) - { - Debug.LogError($"[Shabby]: failed to find texture {kvp.Value}"); - continue; - } - - string nrmPropertyName = null; - if (shaderName != null) { - MaterialDefLibrary.normalMapProperties.TryGetValue(shaderName, out nrmPropertyName); - } - var isNormalMap = kvp.Key == (nrmPropertyName ?? "_BumpMap"); - - textures[kvp.Key] = isNormalMap ? texInfo.normalMap : texInfo.texture; - } + textureNames = LoadDictionary(node.GetNode("Texture")); } static readonly Func ReadValue = @@ -152,7 +134,24 @@ public void ApplyTo(Material material) foreach (var kvp in vectors) material.SetVector(kvp.Key, kvp.Value); - foreach (var kvp in textures) material.SetTexture(kvp.Key, kvp.Value); + foreach (var kvp in textureNames) { + if (!textures.TryGetValue(kvp.Value, out var texture)) { + var texInfo = GameDatabase.Instance.GetTextureInfo(kvp.Value); + if (texInfo == null) + { + Debug.LogError($"[Shabby]: failed to find texture {kvp.Value}"); + continue; + } + + MaterialDefLibrary.normalMapProperties.TryGetValue(material.shader.name, out var nrmPropertyName); + var isNormalMap = kvp.Key == (nrmPropertyName ?? "_BumpMap"); + + texture = isNormalMap ? texInfo.normalMap : texInfo.texture; + textures[kvp.Key] = texture; + } + + material.SetTexture(kvp.Key, texture); + } } }