Add manual material property constructors

feature-multiSDF
Andrew Cassidy 4 years ago
parent 53538a7b09
commit fccf52ae6e

@ -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);
}

@ -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);
}

@ -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);
}

@ -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) {

Loading…
Cancel
Save