mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Add parser for material properties
This commit is contained in:
parent
c0a16e9bbd
commit
1ed0cfacb7
@ -47,6 +47,7 @@
|
||||
<Compile Include="MaterialModifiers\ColorPropertyMaterialModifier.cs" />
|
||||
<Compile Include="MaterialModifiers\FloatPropertyMaterialModifier.cs" />
|
||||
<Compile Include="MaterialModifiers\MaterialModifier.cs" />
|
||||
<Compile Include="MaterialModifiers\MaterialModifierCollection.cs" />
|
||||
<Compile Include="MaterialModifiers\TexturePropertyMaterialModifier.cs" />
|
||||
<Compile Include="ModuleConformalDecal.cs" />
|
||||
<Compile Include="Logging.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<MaterialModifier> _materialModifiers;
|
||||
private List<TexturePropertyMaterialModifier> _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<MaterialModifier>();
|
||||
_texturePropertyMaterialModifiers = new List<TexturePropertyMaterialModifier>();
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user