From 1ed0cfacb7e687e6d5f8458c62d0c751ac44ff3b Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Wed, 27 May 2020 15:19:46 -0700 Subject: [PATCH] Add parser for material properties --- Source/ConformalDecals/ConformalDecals.csproj | 1 + .../MaterialModifierCollection.cs | 73 +++++++++++++++++++ .../TexturePropertyMaterialModifier.cs | 28 +++---- 3 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 Source/ConformalDecals/MaterialModifiers/MaterialModifierCollection.cs diff --git a/Source/ConformalDecals/ConformalDecals.csproj b/Source/ConformalDecals/ConformalDecals.csproj index 3b85b28..f5ff08c 100644 --- a/Source/ConformalDecals/ConformalDecals.csproj +++ b/Source/ConformalDecals/ConformalDecals.csproj @@ -47,6 +47,7 @@ + diff --git a/Source/ConformalDecals/MaterialModifiers/MaterialModifierCollection.cs b/Source/ConformalDecals/MaterialModifiers/MaterialModifierCollection.cs new file mode 100644 index 0000000..532d897 --- /dev/null +++ b/Source/ConformalDecals/MaterialModifiers/MaterialModifierCollection.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +namespace ConformalDecals.MaterialModifiers { + public class MaterialModifierCollection { + public Shader ShaderRef { get; } + public TexturePropertyMaterialModifier MainTexture { get; } + + private List _materialModifiers; + private List _texturePropertyMaterialModifiers; + + public MaterialModifierCollection(ConfigNode node) { + var shaderString = node.GetValue("shader"); + + if (shaderString == null) + throw new FormatException($"Missing shader name in material"); + + if (shaderString == string.Empty) + throw new FormatException($"Empty shader name in material"); + + _materialModifiers = new List(); + _texturePropertyMaterialModifiers = new List(); + + //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 { + MaterialModifier modifier; + switch (propertyNode.name) { + case "FLOAT": + modifier = new FloatPropertyMaterialModifier(propertyNode); + break; + + case "COLOR": + modifier = new ColorPropertyMaterialModifier(propertyNode); + break; + + case "TEXTURE": + modifier = new TexturePropertyMaterialModifier(propertyNode); + var textureModifier = modifier as TexturePropertyMaterialModifier; + if (textureModifier.IsMain) { + if (MainTexture != null) { + MainTexture = textureModifier; + } + else { + Debug.LogWarning( + $"Material texture property {textureModifier.TextureUrl} is marked as main, but material already has a main texture! \n" + + $"Defaulting to {MainTexture.TextureUrl}"); + } + } + + _texturePropertyMaterialModifiers.Add(textureModifier); + break; + + default: + throw new FormatException($"Invalid property type '{propertyNode.name}' in material"); + break; + } + + _materialModifiers.Add(modifier); + } + + catch (Exception e) { + Debug.LogError(e.Message); + } + } + } + } +} \ No newline at end of file diff --git a/Source/ConformalDecals/MaterialModifiers/TexturePropertyMaterialModifier.cs b/Source/ConformalDecals/MaterialModifiers/TexturePropertyMaterialModifier.cs index 4fff832..f78cb8e 100644 --- a/Source/ConformalDecals/MaterialModifiers/TexturePropertyMaterialModifier.cs +++ b/Source/ConformalDecals/MaterialModifiers/TexturePropertyMaterialModifier.cs @@ -3,8 +3,8 @@ using UnityEngine; namespace ConformalDecals.MaterialModifiers { public class TexturePropertyMaterialModifier : MaterialModifier { - private readonly string _textureURL; - private readonly Texture2D _texture; + public string TextureUrl { get; } + public Texture2D TextureRef { get; } private Vector2 _textureOffset; private Vector2 _textureScale; @@ -16,32 +16,32 @@ namespace ConformalDecals.MaterialModifiers { public Rect TileRect { get; } public TexturePropertyMaterialModifier(ConfigNode node) : base(node) { - _textureURL = node.GetValue("textureURL"); + TextureUrl = node.GetValue("textureURL"); - var textureInfo = GameDatabase.Instance.GetTextureInfo(_textureURL); + var textureInfo = GameDatabase.Instance.GetTextureInfo(TextureUrl); if (textureInfo == null) - throw new Exception($"Cannot find texture: '{_textureURL}'"); + throw new Exception($"Cannot find texture: '{TextureUrl}'"); - _texture = IsNormal ? textureInfo.normalMap : textureInfo.texture; + TextureRef = IsNormal ? textureInfo.normalMap : textureInfo.texture; - if (_texture == null) - throw new Exception($"Cannot get texture from texture info '{_textureURL}' isNormalMap = {IsNormal}"); + if (TextureRef == null) + throw new Exception($"Cannot get texture from texture info '{TextureUrl}' isNormalMap = {IsNormal}"); IsNormal = ParsePropertyBool(node, "isNormalMap", true, false); IsMain = ParsePropertyBool(node, "isMain", true, false); AutoScale = ParsePropertyBool(node, "autoScale", true, false); - TileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, _texture.width, _texture.height)); + TileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, TextureRef.width, TextureRef.height)); - _textureScale.x = TileRect.width / _texture.width; - _textureScale.y = TileRect.height / _texture.height; + _textureScale.x = TileRect.width / TextureRef.width; + _textureScale.y = TileRect.height / TextureRef.height; - _textureOffset.x = TileRect.x / _texture.width; - _textureOffset.y = TileRect.y / _texture.height; + _textureOffset.x = TileRect.x / TextureRef.width; + _textureOffset.y = TileRect.y / TextureRef.height; } public override void Modify(Material material) { - material.SetTexture(_propertyID, _texture); + material.SetTexture(_propertyID, TextureRef); material.SetTextureOffset(_propertyID, _textureOffset); material.SetTextureScale(_propertyID, _textureScale); }