Unify material modifiers

Allows float and color properties to be set on actual parts now, not just
fairings
pull/477/head
blowfish 5 years ago
parent a5944d8193
commit 8d8795c57d

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

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

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

@ -1,6 +1,7 @@
using System;
using System.Collections;
using UnityEngine;
using Restock.MaterialModifiers;
namespace Restock
{
@ -44,35 +45,30 @@ namespace Restock
yield break;
}
UpdateMaterial(fairingModule.FairingMaterial, node);
UpdateMaterial(fairingModule.FairingConeMaterial, node);
UpdateMaterial(fairingModule.FairingFlightMaterial, node);
UpdateMaterial(fairingModule.FairingFlightConeMaterial, node);
MaterialModifierParser parser = new MaterialModifierParser();
foreach (ProceduralFairings.FairingPanel fairingPanel in fairingModule.Panels)
{
MeshRenderer renderer = fairingPanel.go.GetComponent<MeshRenderer>();
UpdateMaterial(renderer.material, node);
}
}
private void UpdateMaterial(Material material, ConfigNode node)
{
foreach (ConfigNode node2 in node.nodes)
{
if (node2.name == "COLOR_PROPERTY")
IMaterialModifier modifier;
try
{
string name = node2.GetValue("name");
Color color = ConfigNode.ParseColor(node2.GetValue("color"));
material.SetColor(name, color);
modifier = parser.Parse(node2);
}
else if (node2.name == "FLOAT_PROPERTY")
catch (Exception ex)
{
string name = node2.GetValue("name");
float value = float.Parse(node2.GetValue("value"));
Debug.LogException(new Exception($"[{nameof(ModuleRestockModifyFairingMaterials)}] cannot parse node as material modifier: \n{node2.ToString()}\n", ex));
continue;
}
material.SetFloat(name, value);
modifier.Modify(fairingModule.FairingMaterial);
modifier.Modify(fairingModule.FairingConeMaterial);
modifier.Modify(fairingModule.FairingFlightMaterial);
modifier.Modify(fairingModule.FairingFlightConeMaterial);
foreach (ProceduralFairings.FairingPanel fairingPanel in fairingModule.Panels)
{
MeshRenderer renderer = fairingPanel.go.GetComponent<MeshRenderer>();
modifier.Modify(renderer.material);
}
}
}

@ -1,5 +1,6 @@
using System;
using UnityEngine;
using Restock.MaterialModifiers;
namespace Restock
{
@ -40,28 +41,24 @@ namespace Restock
}
foreach (ConfigNode node3 in node2.GetNodes("TEXTURE_PROPERTY"))
{
string name = node3.GetValue("name");
string textureUrl = node3.GetValue("textureUrl");
bool normalMapToggle = false;
MaterialModifierParser parser = new MaterialModifierParser();
if (node3.GetValue("isNormalMap") is string normalMapToggleString)
foreach (ConfigNode node3 in node2.nodes)
{
IMaterialModifier modifier;
try
{
normalMapToggle = bool.Parse(normalMapToggleString);
modifier = parser.Parse(node3);
}
GameDatabase.TextureInfo textureInfo = GameDatabase.Instance.GetTextureInfo(textureUrl);
if (textureInfo == null)
catch (Exception ex)
{
Debug.LogError($"Cannot find texture: {textureUrl}");
Debug.LogException(new Exception($"[{nameof(ModuleRestockModifyMaterials)}] cannot parse node as material modifier: \n{node3.ToString()}\n", ex));
continue;
}
foreach (Renderer renderer in renderers)
{
renderer.material.SetTexture(name, normalMapToggle ? textureInfo.normalMap : textureInfo.texture);
modifier.Modify(renderer.material);
}
}
}

@ -41,6 +41,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MaterialModifiers\ColorPropertyMaterialModifier.cs" />
<Compile Include="MaterialModifiers\FloatPropertyMaterialModifier.cs" />
<Compile Include="MaterialModifiers\IMaterialModifier.cs" />
<Compile Include="MaterialModifiers\MaterialModifierParser.cs" />
<Compile Include="MaterialModifiers\TexturePropertyMaterialModifier.cs" />
<Compile Include="ModuleRestockModifyFairingMaterials.cs" />
<Compile Include="ModuleRestockModifyMaterials.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

Loading…
Cancel
Save