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-04 07:12:09 +00:00
|
|
|
[SerializeField] public Texture2D texture;
|
|
|
|
|
|
|
|
[SerializeField] public bool isNormal;
|
|
|
|
[SerializeField] public bool isMain;
|
|
|
|
[SerializeField] public bool autoScale;
|
|
|
|
|
|
|
|
[SerializeField] private bool _hasTile;
|
|
|
|
[SerializeField] private Rect _tileRect;
|
|
|
|
[SerializeField] private Vector2 _textureOffset = Vector2.zero;
|
|
|
|
[SerializeField] private Vector2 _textureScale = Vector2.one;
|
|
|
|
|
|
|
|
public float AspectRatio {
|
|
|
|
get {
|
|
|
|
if (texture == null) return 1;
|
|
|
|
if (!_hasTile || Mathf.Approximately(0, _tileRect.width)) return ((float) texture.height) / ((float) texture.width);
|
|
|
|
return _tileRect.height / _tileRect.width;
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 06:26:33 +00:00
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
public Rect TileRect {
|
|
|
|
get => _tileRect;
|
|
|
|
set {
|
|
|
|
_hasTile = !(Mathf.Abs(value.width) < 0.1) || !(Mathf.Abs(value.height) < 0.1);
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
_tileRect = value;
|
|
|
|
UpdateTiling();
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 06:26:33 +00:00
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
public override void ParseNode(ConfigNode node) {
|
|
|
|
base.ParseNode(node);
|
2020-05-30 04:02:58 +00:00
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
isNormal = ParsePropertyBool(node, "isNormalMap", true, (Name == "_BumpMap") || isNormal);
|
|
|
|
isMain = ParsePropertyBool(node, "isMain", true, isMain);
|
|
|
|
autoScale = ParsePropertyBool(node, "autoScale", true, autoScale);
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
SetTexture(node.GetValue("textureUrl"));
|
|
|
|
|
|
|
|
if (node.HasValue("tileRect")) {
|
|
|
|
TileRect = ParsePropertyRect(node, "tileRect", true, _tileRect);
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-04 07:12:09 +00:00
|
|
|
public void SetTexture(string textureUrl) {
|
|
|
|
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
|
|
|
}
|
2020-06-04 07:12:09 +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-04 07:12:09 +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-04 07:12:09 +00:00
|
|
|
if (texture == null) throw new Exception($"Cannot get texture from texture info '{textureUrl}', isNormalMap = {isNormal}");
|
|
|
|
UpdateTiling();
|
2020-06-03 00:11:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 06:04:19 +00:00
|
|
|
public override void Modify(Material material) {
|
2020-06-04 07:12:09 +00:00
|
|
|
if (material == null) throw new ArgumentNullException(nameof(material));
|
|
|
|
if (texture == null) {
|
|
|
|
texture = Texture2D.whiteTexture;
|
|
|
|
throw new NullReferenceException("texture is null, but should not be");
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-06-04 07:12:09 +00:00
|
|
|
if (autoScale) {
|
2020-05-27 06:04:19 +00:00
|
|
|
material.SetTextureScale(_propertyID, new Vector2(_textureScale.x * scale.x, _textureScale.y * scale.y));
|
|
|
|
}
|
|
|
|
}
|
2020-06-04 07:12:09 +00:00
|
|
|
|
|
|
|
private void UpdateTiling() {
|
|
|
|
if (_hasTile) {
|
|
|
|
_textureScale.x = Mathf.Approximately(0, _tileRect.width) ? 1 : _tileRect.width / texture.width;
|
|
|
|
_textureScale.y = Mathf.Approximately(0, _tileRect.height) ? 1 : _tileRect.height / texture.height;
|
|
|
|
|
|
|
|
_textureOffset.x = _tileRect.x / texture.width;
|
|
|
|
_textureOffset.y = _tileRect.y / texture.height;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_textureScale = Vector2.one;
|
|
|
|
_textureOffset = Vector2.zero;
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
|
|
|
}
|