-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGITweaksViewModes.cs
More file actions
229 lines (191 loc) · 8.52 KB
/
GITweaksViewModes.cs
File metadata and controls
229 lines (191 loc) · 8.52 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
using System.Linq;
using UnityEditor.Overlays;
namespace GITweaks
{
[InitializeOnLoad]
public static class GITweaksViewModes
{
[Overlay(typeof(SceneView), "Baked Transmission Legend", false)]
public class MyToolButtonOverlay : Overlay, ITransientOverlay
{
private static VisualElement CreateColorSwatch(string label, Color color)
{
var row = new VisualElement() { style = { flexDirection = FlexDirection.Row, marginLeft = 2 } };
row.AddToClassList("unity-base-field");
var swatchContainer = new VisualElement();
swatchContainer.AddToClassList("unity-base-field__label");
swatchContainer.AddToClassList("unity-pbr-validation-color-swatch");
var colorContent = new VisualElement() { name = "color-content" };
colorContent.style.backgroundColor = new StyleColor(color);
swatchContainer.Add(colorContent);
row.Add(swatchContainer);
var colorLabel = new Label(label) { name = "color-label" };
colorLabel.AddToClassList("unity-base-field__label");
row.Add(colorLabel);
return row;
}
public override VisualElement CreatePanelContent()
{
var root = new VisualElement() { name = "Root" };
root.Add(CreateColorSwatch("Full RGB transmission", Color.red));
root.Add(CreateColorSwatch("Alpha/Fade transmission", Color.green));
root.Add(CreateColorSwatch("Cutout transmission", Color.blue));
root.Add(CreateColorSwatch("Opaque (Missing _MainTex)", Color.magenta));
root.Add(CreateColorSwatch("Opaque", Color.white));
return root;
}
public bool visible => SceneView.lastActiveSceneView.cameraMode.name == BakedTransmissionModes;
}
const string BakedTransmissionModes = "Baked Transmission Modes";
const string BakedTransmissionTextures = "Baked Transmission Data";
static GITweaksViewModes()
{
Init();
}
public static void Init()
{
if (!GITweaksSettingsWindow.IsEnabled(GITweak.BakedTransmissionViewModes))
return;
var modes = (List<SceneView.CameraMode>)typeof(SceneView)
.GetProperty("userDefinedModes", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
.GetValue(null, new object[0]);
if (!modes.Any(x => x.name == BakedTransmissionModes))
{
SceneView.AddCameraMode(BakedTransmissionModes, "GI Tweaks");
}
if (!modes.Any(x => x.name == BakedTransmissionTextures))
{
SceneView.AddCameraMode(BakedTransmissionTextures, "GI Tweaks");
}
SceneView.beforeSceneGui -= RenderCustom;
SceneView.beforeSceneGui += RenderCustom;
}
public static void Deinit()
{
foreach (SceneView sv in SceneView.sceneViews)
{
sv.cameraMode = SceneView.GetBuiltinCameraMode(DrawCameraMode.Textured);
}
SceneView.ClearUserDefinedCameraModes();
}
enum TransparencyMode
{
RGB,
Alpha,
Cutout,
MissingMainTex,
Opaque,
}
private static TransparencyMode GetTransparencyMode(Material m, out Texture tex, out float alpha)
{
alpha = m.color.a;
if (m.HasProperty("_TransparencyLM"))
{
alpha = 1;
tex = m.GetTexture("_TransparencyLM");
return TransparencyMode.RGB;
}
// Transparent type
bool cutout = m.renderQueue >= (int)RenderQueue.AlphaTest && m.renderQueue < (int)RenderQueue.Transparent;
bool transparent = m.renderQueue >= (int)RenderQueue.Transparent && m.renderQueue < (int)RenderQueue.Overlay;
if (!transparent)
{
string renderType = m.GetTag("RenderType", false);
if (m.name.Contains("Transparent") || (m.name.Contains("Tree") && (m.name.Contains("Leaves") || m.name.Contains("Leaf"))))
{
transparent = true;
}
else if (renderType == "GrassBillboard" || renderType == "Transparent" || renderType == "Grass" || renderType == "TreeLeaf")
{
transparent = true;
}
}
if (!cutout)
{
string renderType = m.GetTag("RenderType", false);
if (renderType == "TransparentCutout" || renderType == "TreeTransparentCutout")
{
cutout = true;
}
else if (m.IsKeywordEnabled("GEOM_TYPE_FROND") || m.IsKeywordEnabled("GEOM_TYPE_LEAF"))
{
cutout = true;
}
}
if (cutout || transparent)
{
tex = m.mainTexture;
bool hasMainTexture = m.mainTexture != null;
if (!hasMainTexture)
{
if (m.HasProperty("_MainTex"))
{
hasMainTexture = true;
}
else
{
var shader = m.shader;
int propertyCount = shader.GetPropertyCount();
for (int i = 0; i < propertyCount; i++)
{
if (shader.GetPropertyType(i) != ShaderPropertyType.Texture)
continue;
if ((shader.GetPropertyFlags(i) & ShaderPropertyFlags.MainTexture) != 0)
{
hasMainTexture = true;
break;
}
}
}
}
if (!hasMainTexture)
return TransparencyMode.MissingMainTex;
if (cutout && (m.HasProperty("_Cutoff") || m.HasProperty("_AlphaTestRef")))
return TransparencyMode.Cutout;
if (transparent)
return TransparencyMode.Alpha;
}
tex = Texture2D.blackTexture;
return TransparencyMode.Opaque;
}
private static Material viewMat = null;
private static void RenderCustom(SceneView sceneView)
{
if (sceneView.cameraMode.name != BakedTransmissionModes && sceneView.cameraMode.name != BakedTransmissionTextures)
return;
bool showTextures = sceneView.cameraMode.name == BakedTransmissionTextures;
if (Event.current.type != EventType.Repaint)
return;
sceneView.SetSceneViewShaderReplace(null, "");
if (viewMat == null)
viewMat = new Material(Shader.Find("Hidden/pema99/Overlay"));
var planes = GeometryUtility.CalculateFrustumPlanes(sceneView.camera);
var mrs = Object.FindObjectsByType<MeshRenderer>(FindObjectsSortMode.None);
foreach (var mr in mrs)
{
if (!GeometryUtility.TestPlanesAABB(planes, mr.bounds))
continue;
var mode = GetTransparencyMode(mr.sharedMaterial, out var tex, out var alpha);
switch (mode)
{
case TransparencyMode.RGB: viewMat.color = Color.red; break;
case TransparencyMode.Alpha: viewMat.color = Color.green; break;
case TransparencyMode.Cutout: viewMat.color = Color.blue; break;
case TransparencyMode.MissingMainTex: viewMat.color = Color.magenta; break;
case TransparencyMode.Opaque: viewMat.color = Color.white; break;
}
viewMat.mainTexture = tex;
viewMat.SetFloat("_Alpha", alpha);
viewMat.SetInt("_Mode", showTextures ? (mode == TransparencyMode.RGB ? 2 : 1) : 0);
viewMat.SetPass(0);
Graphics.DrawMeshNow(mr.GetComponent<MeshFilter>().sharedMesh, mr.localToWorldMatrix);
}
}
}
}