2020-05-27 06:04:19 +00:00
|
|
|
using System;
|
2020-06-20 04:44:43 +00:00
|
|
|
using ConformalDecals.Util;
|
2020-05-27 06:04:19 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
2020-06-19 23:35:10 +00:00
|
|
|
namespace ConformalDecals.MaterialProperties {
|
2020-06-08 02:52:17 +00:00
|
|
|
public class MaterialTextureProperty : MaterialProperty {
|
2020-06-04 07:12:09 +00:00
|
|
|
[SerializeField] public bool isNormal;
|
|
|
|
[SerializeField] public bool isMain;
|
|
|
|
[SerializeField] public bool autoScale;
|
2020-06-06 20:20:50 +00:00
|
|
|
[SerializeField] public bool autoTile;
|
2020-06-04 07:12:09 +00:00
|
|
|
|
2020-06-08 02:52:17 +00:00
|
|
|
[SerializeField] private string _textureUrl;
|
2020-06-20 04:44:43 +00:00
|
|
|
[SerializeField] private Texture2D _texture = Texture2D.whiteTexture;
|
2020-06-04 07:12:09 +00:00
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
[SerializeField] private bool _hasTile;
|
|
|
|
[SerializeField] private Rect _tileRect;
|
2020-06-05 07:29:23 +00:00
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
[SerializeField] private Vector2 _scale = Vector2.one;
|
|
|
|
[SerializeField] private Vector2 _textureOffset;
|
|
|
|
[SerializeField] private Vector2 _textureScale = Vector2.one;
|
2020-06-05 07:29:23 +00:00
|
|
|
|
2020-06-15 22:39:05 +00:00
|
|
|
public Texture2D Texture {
|
|
|
|
get => _texture;
|
|
|
|
set => _texture = value;
|
|
|
|
}
|
2020-06-08 02:39:09 +00:00
|
|
|
|
|
|
|
public string TextureUrl {
|
|
|
|
get => _textureUrl;
|
|
|
|
set {
|
|
|
|
_texture = LoadTexture(value, isNormal);
|
|
|
|
_textureUrl = value;
|
2020-06-04 07:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-30 06:26:33 +00:00
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
public int Width => _texture.width;
|
|
|
|
public int Height => _texture.height;
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
public int MaskedWidth => _hasTile ? (int) _tileRect.width : _texture.width;
|
|
|
|
public int MaskedHeight => _hasTile ? (int) _tileRect.height : _texture.height;
|
|
|
|
|
|
|
|
public Vector2 Dimensions => new Vector2(_texture.width, _texture.height);
|
|
|
|
public Vector2 MaskedDimensions => _hasTile ? _tileRect.size : Dimensions;
|
|
|
|
|
|
|
|
public float AspectRatio => MaskedHeight / (float) MaskedWidth;
|
|
|
|
|
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-20 04:44:43 +00:00
|
|
|
ParseUtil.ParseBoolIndirect(ref isMain, node, "isMain");
|
|
|
|
ParseUtil.ParseBoolIndirect(ref isNormal, node, "isNormalMap");
|
|
|
|
ParseUtil.ParseBoolIndirect(ref autoScale, node, "autoScale");
|
|
|
|
ParseUtil.ParseBoolIndirect(ref autoTile, node, "autoTile");
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-20 04:44:43 +00:00
|
|
|
if (!autoTile) {
|
|
|
|
ParseUtil.ParseRectIndirect(ref _tileRect, node, "tile");
|
2020-06-08 02:39:09 +00:00
|
|
|
}
|
2020-05-27 06:04:19 +00:00
|
|
|
|
2020-06-20 04:44:43 +00:00
|
|
|
if (ParseUtil.ParseStringIndirect(ref _textureUrl, node, "textureUrl")) {
|
|
|
|
_texture = LoadTexture(_textureUrl, isNormal);
|
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));
|
2020-06-08 02:39:09 +00:00
|
|
|
if (_texture == null) {
|
|
|
|
_texture = Texture2D.whiteTexture;
|
2020-06-04 07:12:09 +00:00
|
|
|
throw new NullReferenceException("texture is null, but should not be");
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
material.SetTexture(_propertyID, _texture);
|
2020-05-27 06:04:19 +00:00
|
|
|
material.SetTextureOffset(_propertyID, _textureOffset);
|
2020-06-08 02:39:09 +00:00
|
|
|
material.SetTextureScale(_propertyID, _textureScale * _scale);
|
2020-06-29 01:18:04 +00:00
|
|
|
if (_propertyName != "_Decal") material.EnableKeyword("DECAL" + _propertyName.ToUpper());
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 02:40:00 +00:00
|
|
|
public override void Remove(Material material) {
|
|
|
|
if (material == null) throw new ArgumentNullException(nameof(material));
|
|
|
|
base.Remove(material);
|
|
|
|
|
|
|
|
if (_propertyName != "_Decal") material.DisableKeyword("DECAL" + _propertyName.ToUpper());
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
public void SetScale(Vector2 scale) {
|
|
|
|
_scale = scale;
|
2020-06-06 20:20:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
public void SetTile(Rect tile) {
|
2020-06-08 02:52:17 +00:00
|
|
|
SetTile(tile, Dimensions);
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
2020-06-04 07:12:09 +00:00
|
|
|
|
2020-06-08 02:39:09 +00:00
|
|
|
public void SetTile(Rect tile, Vector2 mainTexDimensions) {
|
|
|
|
var scale = tile.size;
|
|
|
|
var offset = tile.position;
|
|
|
|
|
|
|
|
// invert y axis to deal with DXT image orientation
|
|
|
|
offset.y = mainTexDimensions.y - offset.y - tile.height;
|
|
|
|
|
|
|
|
// fit to given dimensions
|
|
|
|
scale /= mainTexDimensions;
|
|
|
|
offset /= mainTexDimensions;
|
|
|
|
_tileRect = tile;
|
|
|
|
_hasTile = true;
|
|
|
|
_textureScale = scale;
|
|
|
|
_textureOffset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Texture2D LoadTexture(string textureUrl, bool isNormal) {
|
|
|
|
Debug.Log($"loading texture '{textureUrl}', isNormalMap = {isNormal}");
|
|
|
|
if ((string.IsNullOrEmpty(textureUrl) && isNormal) || textureUrl == "Bump") {
|
|
|
|
return Texture2D.normalTexture;
|
2020-06-04 07:12:09 +00:00
|
|
|
}
|
2020-06-08 02:39:09 +00:00
|
|
|
|
|
|
|
if ((string.IsNullOrEmpty(textureUrl) && !isNormal) || textureUrl == "White") {
|
|
|
|
return Texture2D.whiteTexture;
|
2020-06-04 07:12:09 +00:00
|
|
|
}
|
2020-06-08 02:39:09 +00:00
|
|
|
|
|
|
|
if (textureUrl == "Black") {
|
|
|
|
return Texture2D.blackTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
var texture = GameDatabase.Instance.GetTexture(textureUrl, isNormal);
|
|
|
|
if (texture == null) throw new Exception($"Cannot get texture '{textureUrl}', isNormalMap = {isNormal}");
|
|
|
|
return texture;
|
2020-06-04 07:12:09 +00:00
|
|
|
}
|
2020-05-27 06:04:19 +00:00
|
|
|
}
|
|
|
|
}
|