diff --git a/Source/ConformalDecals/MaterialModifiers/MaterialColorProperty.cs b/Source/ConformalDecals/MaterialModifiers/MaterialColorProperty.cs index ab0ff88..4aba7f0 100644 --- a/Source/ConformalDecals/MaterialModifiers/MaterialColorProperty.cs +++ b/Source/ConformalDecals/MaterialModifiers/MaterialColorProperty.cs @@ -9,6 +9,10 @@ namespace ConformalDecals.MaterialModifiers { _color = ParsePropertyColor(node, "color", false); } + public MaterialColorProperty(string name, Color value) : base(name) { + _color = value; + } + public override void Modify(Material material) { material.SetColor(_propertyID, _color); } diff --git a/Source/ConformalDecals/MaterialModifiers/MaterialFloatProperty.cs b/Source/ConformalDecals/MaterialModifiers/MaterialFloatProperty.cs index 6efb83b..a92ab95 100644 --- a/Source/ConformalDecals/MaterialModifiers/MaterialFloatProperty.cs +++ b/Source/ConformalDecals/MaterialModifiers/MaterialFloatProperty.cs @@ -9,6 +9,10 @@ namespace ConformalDecals.MaterialModifiers { _value = ParsePropertyFloat(node, "value", false); } + public MaterialFloatProperty(string name, float value) : base(name) { + _value = value; + } + public override void Modify(Material material) { material.SetFloat(_propertyID, _value); } diff --git a/Source/ConformalDecals/MaterialModifiers/MaterialProperty.cs b/Source/ConformalDecals/MaterialModifiers/MaterialProperty.cs index 2751b3e..a8ea801 100644 --- a/Source/ConformalDecals/MaterialModifiers/MaterialProperty.cs +++ b/Source/ConformalDecals/MaterialModifiers/MaterialProperty.cs @@ -8,15 +8,16 @@ namespace ConformalDecals.MaterialModifiers { protected readonly int _propertyID; - protected MaterialProperty(ConfigNode node) { - PropertyName = node.GetValue("name"); + protected MaterialProperty(ConfigNode node) : this(node.GetValue("name")) { } - if (PropertyName == null) + protected MaterialProperty(string name) { + if (name == null) throw new FormatException("name not found, cannot create material modifier"); - if (PropertyName == string.Empty) + if (name == string.Empty) throw new FormatException("name is empty, cannot create material modifier"); + PropertyName = name; _propertyID = Shader.PropertyToID(PropertyName); } diff --git a/Source/ConformalDecals/MaterialModifiers/MaterialTextureProperty.cs b/Source/ConformalDecals/MaterialModifiers/MaterialTextureProperty.cs index d327b65..c14ab06 100644 --- a/Source/ConformalDecals/MaterialModifiers/MaterialTextureProperty.cs +++ b/Source/ConformalDecals/MaterialModifiers/MaterialTextureProperty.cs @@ -3,43 +3,69 @@ using UnityEngine; namespace ConformalDecals.MaterialModifiers { public class MaterialTextureProperty : MaterialProperty { - public string TextureUrl { get; } public Texture2D TextureRef { get; } public bool IsNormal { get; } public bool IsMain { get; } public bool AutoScale { get; } - public Rect TileRect { get; } + private readonly Rect _tileRect; - public float AspectRatio => TileRect.height / TileRect.width; + public float AspectRatio => _tileRect.height / _tileRect.width; private readonly Vector2 _textureOffset; private readonly Vector2 _textureScale; public MaterialTextureProperty(ConfigNode node) : base(node) { - TextureUrl = node.GetValue("textureURL"); + IsNormal = ParsePropertyBool(node, "isNormalMap", true, PropertyName == "_BumpMap"); + IsMain = ParsePropertyBool(node, "isMain", true); + AutoScale = ParsePropertyBool(node, "autoScale", true); + var textureUrl = node.GetValue("textureURL"); - var textureInfo = GameDatabase.Instance.GetTextureInfo(TextureUrl); + if ((textureUrl == null && IsNormal) || textureUrl == "Bump") { + TextureRef = Texture2D.normalTexture; + } + else if ((textureUrl == null && !IsNormal) || textureUrl == "White") { + TextureRef = Texture2D.whiteTexture; + } + else if (textureUrl == "Black") { + TextureRef = Texture2D.blackTexture; + } + else { + var textureInfo = GameDatabase.Instance.GetTextureInfo(textureUrl); - if (textureInfo == null) - throw new Exception($"Cannot find texture: '{TextureUrl}'"); + if (textureInfo == null) throw new Exception($"Cannot find texture: '{textureUrl}'"); - TextureRef = IsNormal ? textureInfo.normalMap : textureInfo.texture; + TextureRef = IsNormal ? textureInfo.normalMap : textureInfo.texture; + } - if (TextureRef == null) - throw new Exception($"Cannot get texture from texture info '{TextureUrl}' isNormalMap = {IsNormal}"); + if (TextureRef == null) throw new Exception($"Cannot get texture from texture info '{textureUrl}' isNormalMap = {IsNormal}"); - IsNormal = ParsePropertyBool(node, "isNormalMap", true); - IsMain = ParsePropertyBool(node, "isMain", true); - AutoScale = ParsePropertyBool(node, "autoScale", true); - TileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, TextureRef.width, TextureRef.height)); + _tileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, TextureRef.width, TextureRef.height)); + + _textureScale.x = _tileRect.width / TextureRef.width; + _textureScale.y = _tileRect.height / TextureRef.height; + + _textureOffset.x = _tileRect.x / TextureRef.width; + _textureOffset.y = _tileRect.y / TextureRef.height; + } + + public MaterialTextureProperty(string name, Texture2D texture, Rect tileRect = default, + bool isNormal = false, bool isMain = false, bool autoScale = false) : base(name) { + + TextureRef = texture; + + _tileRect = tileRect == default ? new Rect(0, 0, TextureRef.width, TextureRef.height) : tileRect; + + IsNormal = isNormal; + IsMain = isMain; + AutoScale = autoScale; - _textureScale.x = TileRect.width / TextureRef.width; - _textureScale.y = TileRect.height / TextureRef.height; + _textureScale.x = _tileRect.width / TextureRef.width; + _textureScale.y = _tileRect.height / TextureRef.height; - _textureOffset.x = TileRect.x / TextureRef.width; - _textureOffset.y = TileRect.y / TextureRef.height; + _textureOffset.x = _tileRect.x / TextureRef.width; + _textureOffset.y = _tileRect.y / TextureRef.height; } public override void Modify(Material material) {