Unify material modifiers

Allows float and color properties to be set on actual parts now, not just
fairings
This commit is contained in:
blowfish
2019-02-27 22:46:39 -08:00
parent a5944d8193
commit 8d8795c57d
9 changed files with 171 additions and 35 deletions

View File

@ -0,0 +1,22 @@
using System;
using UnityEngine;
namespace Restock.MaterialModifiers
{
public class ColorPropertyMaterialModifier : IMaterialModifier
{
private readonly string propertyName;
private readonly Color color;
public ColorPropertyMaterialModifier(string propertyName, Color color)
{
this.propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
this.color = color;
}
public void Modify(Material material)
{
material.SetColor(propertyName, color);
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using UnityEngine;
namespace Restock.MaterialModifiers
{
public class FloatPropertyMaterialModifier : IMaterialModifier
{
private readonly string propertyName;
private readonly float value;
public FloatPropertyMaterialModifier(string propertyName, float value)
{
this.propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
this.value = value;
}
public void Modify(Material material)
{
material.SetFloat(propertyName, value);
}
}
}

View File

@ -0,0 +1,10 @@
using System;
using UnityEngine;
namespace Restock.MaterialModifiers
{
public interface IMaterialModifier
{
void Modify(Material material);
}
}

View File

@ -0,0 +1,62 @@
using System;
using UnityEngine;
namespace Restock.MaterialModifiers
{
public class MaterialModifierParser
{
private readonly string logContext;
public MaterialModifierParser(string logContext = nameof(MaterialModifierParser))
{
this.logContext = logContext ?? throw new ArgumentNullException(nameof(logContext));
}
public IMaterialModifier Parse(ConfigNode node)
{
string propertyName = node.GetValue("name");
if (propertyName == null)
throw new FormatException("name not found, cannot create material modifier");
else if (propertyName == string.Empty)
throw new FormatException("name is empty, cannot create material modifier");
if (node.name == "FLOAT_PROPERTY")
{
float value = float.Parse(node.GetValue("value"));
return new FloatPropertyMaterialModifier(propertyName, value);
}
else if (node.name == "COLOR_PROPERTY")
{
Color color = ConfigNode.ParseColor(node.GetValue("color"));
return new ColorPropertyMaterialModifier(propertyName, color);
}
else if (node.name == "TEXTURE_PROPERTY")
{
string textureUrl = node.GetValue("textureUrl");
bool normalMapToggle = false;
if (node.GetValue("isNormalMap") is string normalMapToggleString)
normalMapToggle = bool.Parse(normalMapToggleString);
GameDatabase.TextureInfo textureInfo = GameDatabase.Instance.GetTextureInfo(textureUrl);
if (textureInfo == null)
throw new Exception($"Cannot find texture: '{textureUrl}'");
Texture2D texture = normalMapToggle ? textureInfo.normalMap : textureInfo.texture;
if (texture == null)
throw new Exception($"Cannot get texture from texture info '{textureUrl}' isNormalMap = {normalMapToggle}");
return new TexturePropertyMaterialModifier(propertyName, texture);
}
else
{
throw new FormatException($"Can't create material modifier from node: '{node.name}'");
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using UnityEngine;
namespace Restock.MaterialModifiers
{
public class TexturePropertyMaterialModifier : IMaterialModifier
{
private readonly string propertyName;
private readonly Texture2D texture;
public TexturePropertyMaterialModifier(string propertyName, Texture2D texture)
{
this.propertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
this.texture = texture ?? throw new ArgumentNullException(nameof(texture));
}
public void Modify(Material material)
{
material.SetTexture(propertyName, texture);
}
}
}