KSP-Conformal-Decals/Source/ConformalDecals/MaterialModifiers/MaterialProperty.cs
drewcassidy e9c8f3dafb Big refactor to enable preview materials
• add shader variants for decal previewing
• start to add code for part icon
• refactor material properties to be serializable
todo:
• fix decal preview scale (need to call UpdateScale on detached state)
• fix texture preview in part icon
• adjust culling per-object when rendering (turns out cull and ztest values are used by unity at the render time, not by the shader itself, so they can be adjusted in material property blocks!)
2020-06-04 00:12:09 -07:00

79 lines
3.3 KiB
C#

using System;
using UnityEngine;
using Object = UnityEngine.Object;
namespace ConformalDecals.MaterialModifiers {
public abstract class MaterialProperty : ScriptableObject {
public string Name {
get => _propertyName;
set {
_propertyName = value;
_propertyID = Shader.PropertyToID(_propertyName);
}
}
[SerializeField] protected int _propertyID;
[SerializeField] protected string _propertyName;
public virtual void ParseNode(ConfigNode node) {
if (node == null) throw new ArgumentNullException("node cannot be null");
Name = node.GetValue("name");
}
public abstract void Modify(Material material);
private delegate bool TryParseDelegate<T>(string valueString, out T value);
protected bool ParsePropertyBool(ConfigNode node, string valueName, bool isOptional = false, bool defaultValue = false) {
return ParsePropertyValue(node, valueName, bool.TryParse, isOptional, defaultValue);
}
protected float ParsePropertyFloat(ConfigNode node, string valueName, bool isOptional = false, float defaultValue = 0.0f) {
return ParsePropertyValue(node, valueName, float.TryParse, isOptional, defaultValue);
}
protected int ParsePropertyInt(ConfigNode node, string valueName, bool isOptional = false, int defaultValue = 0) {
return ParsePropertyValue(node, valueName, int.TryParse, isOptional, defaultValue);
}
protected Color ParsePropertyColor(ConfigNode node, string valueName, bool isOptional = false, Color defaultValue = default) {
return ParsePropertyValue(node, valueName, ParseExtensions.TryParseColor, isOptional, defaultValue);
}
protected Rect ParsePropertyRect(ConfigNode node, string valueName, bool isOptional = false, Rect defaultValue = default) {
return ParsePropertyValue(node, valueName, ParseExtensions.TryParseRect, isOptional, defaultValue);
}
protected Vector2 ParsePropertyVector2(ConfigNode node, string valueName, bool isOptional = false, Vector2 defaultValue = default) {
return ParsePropertyValue(node, valueName, ParseExtensions.TryParseVector2, isOptional, defaultValue);
}
private T ParsePropertyValue<T>(ConfigNode node, string valueName, TryParseDelegate<T> tryParse, bool isOptional = false, T defaultValue = default) {
string valueString = node.GetValue(valueName);
if (isOptional) {
if (string.IsNullOrEmpty(valueString)) return defaultValue;
}
else {
if (valueString == null)
throw new FormatException($"Missing {typeof(T)} value for {valueName} in property '{Name}'");
if (valueString == string.Empty)
throw new FormatException($"Empty {typeof(T)} value for {valueName} in property '{Name}'");
}
if (tryParse(valueString, out var value)) {
return value;
}
if (isOptional) {
return defaultValue;
}
else {
throw new FormatException($"Improperly formatted {typeof(T)} value for {valueName} in property '{Name}' : '{valueString}");
}
}
}
}