mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Begin writing material data parsing code
This commit is contained in:
parent
6a000c805e
commit
84c2107288
@ -10,7 +10,8 @@
|
||||
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<LangVersion>7</LangVersion>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<RootNamespace>ConformalDecals</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@ -43,6 +44,12 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="MaterialModifiers\ColorPropertyMaterialModifier.cs" />
|
||||
<Compile Include="MaterialModifiers\FloatPropertyMaterialModifier.cs" />
|
||||
<Compile Include="MaterialModifiers\MaterialModifier.cs" />
|
||||
<Compile Include="MaterialModifiers\TexturePropertyMaterialModifier.cs" />
|
||||
<Compile Include="ModuleConformalDecal.cs" />
|
||||
<Compile Include="Logging.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
|
23
Source/ConformalDecals/Logging.cs
Normal file
23
Source/ConformalDecals/Logging.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals {
|
||||
public static class Logging {
|
||||
public static void Log(this PartModule module, string message) => Debug.Log(FormatMessage(module, message));
|
||||
|
||||
public static void LogWarning(this PartModule module, string message) =>
|
||||
Debug.LogWarning(FormatMessage(module, message));
|
||||
|
||||
public static void LogError(this PartModule module, string message) =>
|
||||
Debug.LogError(FormatMessage(module, message));
|
||||
|
||||
public static void LogException(this PartModule module, string message, Exception exception) =>
|
||||
Debug.LogException(new Exception(FormatMessage(module, message), exception));
|
||||
|
||||
|
||||
private static string FormatMessage(PartModule module, string message) =>
|
||||
$"[{GetPartName(module.part)} {module.GetType()}] {message}";
|
||||
|
||||
private static string GetPartName(Part part) => part.partInfo?.name ?? part.name;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals.MaterialModifiers {
|
||||
public class ColorPropertyMaterialModifier : MaterialModifier {
|
||||
private readonly Color _color;
|
||||
|
||||
public ColorPropertyMaterialModifier(ConfigNode node) : base(node) {
|
||||
_color = ParsePropertyColor(node, "color", false);
|
||||
}
|
||||
|
||||
public override void Modify(Material material) {
|
||||
material.SetColor(_propertyID, _color);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals.MaterialModifiers {
|
||||
public class FloatPropertyMaterialModifier : MaterialModifier {
|
||||
private readonly float _value;
|
||||
|
||||
public FloatPropertyMaterialModifier(ConfigNode node) : base(node) {
|
||||
_value = ParsePropertyFloat(node, "value", false);
|
||||
}
|
||||
|
||||
public override void Modify(Material material) {
|
||||
material.SetFloat(_propertyID, _value);
|
||||
}
|
||||
}
|
||||
}
|
78
Source/ConformalDecals/MaterialModifiers/MaterialModifier.cs
Normal file
78
Source/ConformalDecals/MaterialModifiers/MaterialModifier.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals.MaterialModifiers {
|
||||
public abstract class MaterialModifier {
|
||||
public string Name { get; }
|
||||
|
||||
protected readonly int _propertyID;
|
||||
|
||||
|
||||
protected MaterialModifier(ConfigNode node) {
|
||||
Name = node.GetValue("name");
|
||||
|
||||
if (Name == null)
|
||||
throw new FormatException("name not found, cannot create material modifier");
|
||||
|
||||
if (Name == string.Empty)
|
||||
throw new FormatException("name is empty, cannot create material modifier");
|
||||
|
||||
_propertyID = Shader.PropertyToID(Name);
|
||||
}
|
||||
|
||||
public abstract void Modify(Material material);
|
||||
|
||||
private delegate bool TryParseDelegate<T>(string valueString, out T value);
|
||||
|
||||
protected bool ParsePropertyBool(ConfigNode node, string valueName, bool isOptional = false, bool defaultValue = false) {
|
||||
return ParseProperty<bool>(node, valueName, bool.TryParse, isOptional, defaultValue);
|
||||
}
|
||||
|
||||
protected float ParsePropertyFloat(ConfigNode node, string valueName, bool isOptional = false, float defaultValue = 0.0f) {
|
||||
return ParseProperty<float>(node, valueName, float.TryParse, isOptional, defaultValue);
|
||||
}
|
||||
|
||||
protected int ParsePropertyInt(ConfigNode node, string valueName, bool isOptional = false, int defaultValue = 0) {
|
||||
return ParseProperty<int>(node, valueName, int.TryParse, isOptional, defaultValue);
|
||||
}
|
||||
|
||||
protected Color ParsePropertyColor(ConfigNode node, string valueName, bool isOptional = false, Color defaultValue = default(Color)) {
|
||||
return ParseProperty<Color>(node, valueName, ParseExtensions.TryParseColor, isOptional, defaultValue);
|
||||
}
|
||||
|
||||
protected Rect ParsePropertyRect(ConfigNode node, string valueName, bool isOptional = false, Rect defaultValue = default(Rect)) {
|
||||
return ParseProperty<Rect>(node, valueName, ParseExtensions.TryParseRect, isOptional, defaultValue);
|
||||
}
|
||||
|
||||
protected Vector2 ParsePropertyVector2(ConfigNode node, string valueName, bool isOptional = false, Vector2 defaultValue = default(Vector2)) {
|
||||
return ParseProperty<Vector2>(node, valueName, ParseExtensions.TryParseVector2, isOptional, defaultValue);
|
||||
}
|
||||
|
||||
private T ParseProperty<T>(ConfigNode node, string valueName, TryParseDelegate<T> tryParse, bool isOptional = false, T defaultValue = default(T)) {
|
||||
string valueString = node.GetValue(valueName);
|
||||
|
||||
if (isOptional) {
|
||||
if (string.IsNullOrEmpty(valueString)) return defaultValue;
|
||||
}
|
||||
else {
|
||||
if (valueString == null)
|
||||
throw new FormatException($"Missing {typeof(T)} value {valueName} in property '{Name}'");
|
||||
|
||||
if (valueString == string.Empty)
|
||||
throw new FormatException($"Empty {typeof(T)} value {valueName} in property '{Name}'");
|
||||
}
|
||||
|
||||
if (tryParse(valueString, out var value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (isOptional) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
else {
|
||||
throw new FormatException($"Improperly formatted {typeof(T)} value {valueName} in property '{Name}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ConformalDecals.MaterialModifiers {
|
||||
public class TexturePropertyMaterialModifier : MaterialModifier {
|
||||
private readonly string _textureURL;
|
||||
private readonly Texture2D _texture;
|
||||
|
||||
private Vector2 _textureOffset;
|
||||
private Vector2 _textureScale;
|
||||
|
||||
public bool IsNormal { get; }
|
||||
public bool IsMain { get; }
|
||||
public bool AutoScale { get; }
|
||||
|
||||
public Rect TileRect { get; }
|
||||
|
||||
public TexturePropertyMaterialModifier(ConfigNode node) : base(node) {
|
||||
_textureURL = node.GetValue("textureURL");
|
||||
|
||||
var textureInfo = GameDatabase.Instance.GetTextureInfo(_textureURL);
|
||||
|
||||
if (textureInfo == null)
|
||||
throw new Exception($"Cannot find texture: '{_textureURL}'");
|
||||
|
||||
_texture = IsNormal ? textureInfo.normalMap : textureInfo.texture;
|
||||
|
||||
if (_texture == 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));
|
||||
|
||||
_textureScale.x = TileRect.width / _texture.width;
|
||||
_textureScale.y = TileRect.height / _texture.height;
|
||||
|
||||
_textureOffset.x = TileRect.x / _texture.width;
|
||||
_textureOffset.y = TileRect.y / _texture.height;
|
||||
}
|
||||
|
||||
public override void Modify(Material material) {
|
||||
material.SetTexture(_propertyID, _texture);
|
||||
material.SetTextureOffset(_propertyID, _textureOffset);
|
||||
material.SetTextureScale(_propertyID, _textureScale);
|
||||
}
|
||||
|
||||
public void UpdateScale(Material material, Vector2 scale) {
|
||||
if (AutoScale) {
|
||||
material.SetTextureScale(_propertyID, new Vector2(_textureScale.x * scale.x, _textureScale.y * scale.y));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
5
Source/ConformalDecals/ModuleConformalDecal.cs
Normal file
5
Source/ConformalDecals/ModuleConformalDecal.cs
Normal file
@ -0,0 +1,5 @@
|
||||
namespace ConformalDecals {
|
||||
public class ModuleConformalDecal : PartModule {
|
||||
[KSPField] public string decalPreviewTransform = "";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user