2020-05-27 06:04:19 +00:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace ConformalDecals.MaterialModifiers {
|
2020-06-02 07:53:17 +00:00
|
|
|
public class MaterialTextureProperty : MaterialProperty {
|
2020-06-03 03:36:45 +00:00
|
|
|
public Texture2D texture;
|
2020-05-30 06:26:33 +00:00
|
|
|
|
2020-05-27 06:04:19 +00:00
|
|
|
public bool IsNormal { get; }
|
|
|
|
public bool IsMain { get; }
|
|
|
|
public bool AutoScale { get; }
|
|
|
|
|
2020-06-03 00:11:38 +00:00
|
|
|
private readonly Rect _tileRect;
|
2020-05-30 06:26:33 +00:00
|
|
|
|
2020-06-03 00:11:38 +00:00
|
|
|
public float AspectRatio => _tileRect.height / _tileRect.width;
|
2020-05-30 04:02:58 +00:00
|
|
|
|
|
|
|
private readonly Vector2 _textureOffset;
|
|
|
|
private readonly Vector2 _textureScale;
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
public MaterialTextureProperty(ConfigNode node) : base(node) {
|
2020-06-03 00:11:38 +00:00
|
|
|
IsNormal = ParsePropertyBool(node, "isNormalMap", true, PropertyName == "_BumpMap");
|
|
|
|
IsMain = ParsePropertyBool(node, "isMain", true);
|
|
|
|
AutoScale = ParsePropertyBool(node, "autoScale", true);
|
|
|
|
var textureUrl = node.GetValue("textureURL");
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-03 00:11:38 +00:00
|
|
|
if ((textureUrl == null && IsNormal) || textureUrl == "Bump") {
|
2020-06-03 03:36:45 +00:00
|
|
|
texture = Texture2D.normalTexture;
|
2020-06-03 00:11:38 +00:00
|
|
|
}
|
|
|
|
else if ((textureUrl == null && !IsNormal) || textureUrl == "White") {
|
2020-06-03 03:36:45 +00:00
|
|
|
texture = Texture2D.whiteTexture;
|
2020-06-03 00:11:38 +00:00
|
|
|
}
|
|
|
|
else if (textureUrl == "Black") {
|
2020-06-03 03:36:45 +00:00
|
|
|
texture = Texture2D.blackTexture;
|
2020-06-03 00:11:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var textureInfo = GameDatabase.Instance.GetTextureInfo(textureUrl);
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-03 00:11:38 +00:00
|
|
|
if (textureInfo == null) throw new Exception($"Cannot find texture: '{textureUrl}'");
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
texture = IsNormal ? textureInfo.normalMap : textureInfo.texture;
|
2020-06-03 00:11:38 +00:00
|
|
|
}
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
if (texture == null) throw new Exception($"Cannot get texture from texture info '{textureUrl}' isNormalMap = {IsNormal}");
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
_tileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, texture.width, texture.height));
|
2020-06-03 00:11:38 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
_textureScale.x = _tileRect.width / texture.width;
|
|
|
|
_textureScale.y = _tileRect.height / texture.height;
|
2020-06-03 00:11:38 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
_textureOffset.x = _tileRect.x / texture.width;
|
|
|
|
_textureOffset.y = _tileRect.y / texture.height;
|
2020-06-03 00:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public MaterialTextureProperty(string name, Texture2D texture, Rect tileRect = default,
|
|
|
|
bool isNormal = false, bool isMain = false, bool autoScale = false) : base(name) {
|
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
this.texture = texture;
|
2020-06-03 00:11:38 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
_tileRect = tileRect == default ? new Rect(0, 0, this.texture.width, this.texture.height) : tileRect;
|
2020-06-03 00:11:38 +00:00
|
|
|
|
|
|
|
IsNormal = isNormal;
|
|
|
|
IsMain = isMain;
|
|
|
|
AutoScale = autoScale;
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
_textureScale.x = _tileRect.width / this.texture.width;
|
|
|
|
_textureScale.y = _tileRect.height / this.texture.height;
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-03 03:36:45 +00:00
|
|
|
_textureOffset.x = _tileRect.x / this.texture.width;
|
|
|
|
_textureOffset.y = _tileRect.y / this.texture.height;
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
2020-06-03 03:36:45 +00:00
|
|
|
|
2020-05-27 06:04:19 +00:00
|
|
|
public override void Modify(Material material) {
|
2020-06-03 03:36:45 +00:00
|
|
|
material.SetTexture(_propertyID, texture);
|
2020-05-27 06:04:19 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|