Skip to content

Commit

Permalink
fix: late-bound texture loading
Browse files Browse the repository at this point in the history
So that normal map loading works properly when a shader is not set and the existing shader has a custom normal map property name.
  • Loading branch information
al2me6 committed Jul 24, 2024
1 parent 592d0f4 commit 179c729
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions Source/MaterialDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public class MaterialDef
public Dictionary<string, float> floats;
public Dictionary<string, Color> colors;
public Dictionary<string, Vector4> vectors;
public Dictionary<string, Texture> textures = new Dictionary<string, Texture>();
public Dictionary<string, string> textureNames;

readonly Dictionary<string, Texture> textures = new Dictionary<string, Texture>();

public MaterialDef(ConfigNode node)
{
Expand All @@ -78,30 +80,10 @@ public MaterialDef(ConfigNode node)
}

keywords = LoadDictionary<bool>(node.GetNode("Keyword"));

floats = LoadDictionary<float>(node.GetNode("Float"));

colors = LoadDictionary<Color>(node.GetNode("Color"), ParseColor);

vectors = LoadDictionary<Vector4>(node.GetNode("Vector"));

var textureNames = LoadDictionary<string>(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<string>(node.GetNode("Texture"));
}

static readonly Func<Type, string, object> ReadValue =
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 179c729

Please sign in to comment.