mirror of
https://github.com/PorktoberRevolution/ReStocked
synced 2024-09-01 17:34:42 +00:00
Unify material modifiers
Allows float and color properties to be set on actual parts now, not just fairings
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
10
Source/Restock/MaterialModifiers/IMaterialModifier.cs
Normal file
10
Source/Restock/MaterialModifiers/IMaterialModifier.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Restock.MaterialModifiers
|
||||
{
|
||||
public interface IMaterialModifier
|
||||
{
|
||||
void Modify(Material material);
|
||||
}
|
||||
}
|
62
Source/Restock/MaterialModifiers/MaterialModifierParser.cs
Normal file
62
Source/Restock/MaterialModifiers/MaterialModifierParser.cs
Normal 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}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user