KSP-Conformal-Decals/Source/ConformalDecals/MaterialModifiers/MaterialPropertyCollection.cs

72 lines
2.9 KiB
C#
Raw Normal View History

2020-05-27 22:19:46 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ConformalDecals.MaterialModifiers {
public class MaterialPropertyCollection {
2020-05-27 22:19:46 +00:00
public Shader ShaderRef { get; }
public TextureMaterialProperty MainTextureMaterial { get; }
2020-05-27 22:19:46 +00:00
private List<MaterialProperty> _materialModifiers;
private List<TextureMaterialProperty> _texturePropertyMaterialModifiers;
2020-05-27 22:19:46 +00:00
public MaterialPropertyCollection(ConfigNode node) {
2020-05-27 22:19:46 +00:00
var shaderString = node.GetValue("shader");
if (shaderString == null)
throw new FormatException("Missing shader name in material");
2020-05-27 22:19:46 +00:00
if (shaderString == string.Empty)
throw new FormatException("Empty shader name in material");
2020-05-27 22:19:46 +00:00
_materialModifiers = new List<MaterialProperty>();
_texturePropertyMaterialModifiers = new List<TextureMaterialProperty>();
2020-05-27 22:19:46 +00:00
//TODO: USE SHABBY PROVIDED METHOD HERE INSTEAD
ShaderRef = Shader.Find(shaderString);
if (ShaderRef == null) throw new FormatException($"Shader not found: {shaderString}");
foreach (ConfigNode propertyNode in node.nodes) {
try {
MaterialProperty property;
2020-05-27 22:19:46 +00:00
switch (propertyNode.name) {
case "FLOAT":
property = new FloatMaterialProperty(propertyNode);
2020-05-27 22:19:46 +00:00
break;
case "COLOR":
property = new ColorMaterialProperty(propertyNode);
2020-05-27 22:19:46 +00:00
break;
case "TEXTURE":
property = new TextureMaterialProperty(propertyNode);
var textureModifier = (TextureMaterialProperty) property;
2020-05-27 22:19:46 +00:00
if (textureModifier.IsMain) {
if (MainTextureMaterial == null) {
MainTextureMaterial = textureModifier;
2020-05-27 22:19:46 +00:00
}
else {
Debug.LogWarning(
$"Material texture property {textureModifier.TextureUrl} is marked as main, but material already has a main texture! \n" +
$"Defaulting to {MainTextureMaterial.TextureUrl}");
2020-05-27 22:19:46 +00:00
}
}
_texturePropertyMaterialModifiers.Add(textureModifier);
break;
default:
throw new FormatException($"Invalid property type '{propertyNode.name}' in material");
}
_materialModifiers.Add(property);
2020-05-27 22:19:46 +00:00
}
catch (Exception e) {
Debug.LogError(e.Message);
}
}
}
}
}