-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVT0Drawer.cs
More file actions
135 lines (124 loc) · 5.46 KB
/
VT0Drawer.cs
File metadata and controls
135 lines (124 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;
namespace VT0
{
[CustomPropertyDrawer(typeof(TextureSizeAttribute))]
public class TextureSizeDrawer : PropertyDrawer
{
private static readonly int[] sizes = Enumerable.Range(4, 12)
.Select(i => (int)Mathf.Pow(2, i)).ToArray();
private static GUIContent[] sizeNames;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight * (property.intValue > SystemInfo.maxTextureSize ? 3 : 1);
}
public override void OnGUI(Rect p, SerializedProperty property, GUIContent label)
{
var line = EditorGUIUtility.singleLineHeight;
if (sizeNames == null) {
sizeNames = sizes.Select(i => new GUIContent(i.ToString())).ToArray();
}
EditorGUI.IntPopup(new Rect(p.x, p.y, p.width, line),
property, sizeNames, sizes);
if (property.intValue > SystemInfo.maxTextureSize)
{
EditorGUI.HelpBox(new Rect(p.x, p.y + line, p.width, line*2),
string.Format("Max texture size for this platform is {0}", SystemInfo.maxTextureSize),
MessageType.Error);
}
}
}
public class VT0Drawer : MaterialPropertyDrawer
{
public static Rect WithHeight(ref Rect r, float height)
{
var ret = new Rect(r.x, r.y, r.width, height);
r.y += height;
return ret;
}
public override float GetPropertyHeight(MaterialProperty prop, string label, MaterialEditor editor)
{
var channel = VT0Info.Current.Channels.FirstOrDefault(c => c.TextureNames.Contains(prop.name));
string why;
var hasError = channel == null || !channel.IsTextureValid(prop.textureValue, out why);
return EditorGUIUtility.singleLineHeight + (hasError ? 24f : 0f);
}
private bool IsVT0Enabled(Material m)
{
return m.IsKeywordEnabled("_VT0_ON");
}
private static readonly TwoWayDictionary<Texture2D, Texture2D> _placeholderCache
= new TwoWayDictionary<Texture2D, Texture2D>();
public const string PlaceholderLocation = "Assets/VT0_thumbnails";
private static Texture2D GetPlaceholder(Texture2D t, int size)
{
if (t == null) return null;
Texture2D placeholder;
if (!_placeholderCache.TryGetValue1(t, out placeholder))
{
var path = PlaceholderLocation + "/" +
AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(t)) + ".png";
placeholder = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
if (placeholder == null || placeholder.width != t.width)
{
var tmp = new Texture2D(size, size, TextureFormat.RGBA32, false, false);
tmp.SetPixels32(placeholder.GetPixels32((int)Mathf.Log(t.width / size, 2)));
File.WriteAllBytes(path, tmp.EncodeToPNG());
AssetDatabase.Refresh();
placeholder = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
}
_placeholderCache.Add(t, placeholder);
}
return placeholder;
}
public static Texture2D GetOriginal(Texture2D t)
{
if (t == null) return null;
Texture2D original;
if (!_placeholderCache.TryGetValue2(t, out original))
{
var placeholderPath = AssetDatabase.GetAssetPath(t);
if (Path.GetDirectoryName(placeholderPath).Replace('\\', '/') != PlaceholderLocation)
{
original = t;
} else {
var path = AssetDatabase.GUIDToAssetPath(
Path.GetFileNameWithoutExtension(placeholderPath));
if (string.IsNullOrEmpty(path)) {
Debug.LogWarning("Original texture has gone!");
AssetDatabase.DeleteAsset(placeholderPath);
return null;
} else {
original = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
}
}
_placeholderCache.Add(original, t);
}
return original;
}
public override void OnGUI(Rect position, MaterialProperty prop, string label, MaterialEditor editor)
{
var enable = IsVT0Enabled((Material)prop.targets[0]);
var orig = prop.textureValue as Texture2D;
prop.textureValue = GetOriginal(orig);
editor.TexturePropertyMiniThumbnail(
WithHeight(ref position, EditorGUIUtility.singleLineHeight),
prop, label, "");
if (enable) {
prop.textureValue = GetPlaceholder(orig, 64);
}
var channel = VT0Info.Current.Channels.FirstOrDefault(c => c.TextureNames.Contains(prop.name));
string why = null;
if (channel == null || !channel.IsTextureValid(prop.textureValue, out why)) {
EditorGUI.HelpBox(WithHeight(ref position, 24f),
why ?? ("Unknown channel for property " + prop.name), MessageType.Error);
}
}
}
}
#endif