HistogramMonitor.cs
16 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.PostProcessing;
namespace UnityEditor.PostProcessing
{
using HistogramMode = PostProcessingProfile.MonitorSettings.HistogramMode;
public class HistogramMonitor : PostProcessingMonitor
{
static GUIContent s_MonitorTitle = new GUIContent("Histogram");
ComputeShader m_ComputeShader;
ComputeBuffer m_Buffer;
Material m_Material;
RenderTexture m_HistogramTexture;
Rect m_MonitorAreaRect;
public HistogramMonitor()
{
m_ComputeShader = EditorResources.Load<ComputeShader>("Monitors/HistogramCompute.compute");
}
public override void Dispose()
{
GraphicsUtils.Destroy(m_Material);
GraphicsUtils.Destroy(m_HistogramTexture);
if (m_Buffer != null)
m_Buffer.Release();
m_Material = null;
m_HistogramTexture = null;
m_Buffer = null;
}
public override bool IsSupported()
{
return m_ComputeShader != null && GraphicsUtils.supportsDX11;
}
public override GUIContent GetMonitorTitle()
{
return s_MonitorTitle;
}
public override void OnMonitorSettings()
{
EditorGUI.BeginChangeCheck();
bool refreshOnPlay = m_MonitorSettings.refreshOnPlay;
var mode = m_MonitorSettings.histogramMode;
refreshOnPlay = GUILayout.Toggle(refreshOnPlay, new GUIContent(FxStyles.playIcon, "Keep refreshing the histogram in play mode; this may impact performances."), FxStyles.preButton);
mode = (HistogramMode)EditorGUILayout.EnumPopup(mode, FxStyles.preDropdown, GUILayout.MaxWidth(100f));
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(m_BaseEditor.serializedObject.targetObject, "Histogram Settings Changed");
m_MonitorSettings.refreshOnPlay = refreshOnPlay;
m_MonitorSettings.histogramMode = mode;
InternalEditorUtility.RepaintAllViews();
}
}
public override void OnMonitorGUI(Rect r)
{
if (Event.current.type == EventType.Repaint)
{
// If m_MonitorAreaRect isn't set the preview was just opened so refresh the render to get the histogram data
if (Mathf.Approximately(m_MonitorAreaRect.width, 0) && Mathf.Approximately(m_MonitorAreaRect.height, 0))
InternalEditorUtility.RepaintAllViews();
// Sizing
float width = m_HistogramTexture != null
? Mathf.Min(m_HistogramTexture.width, r.width - 65f)
: r.width;
float height = m_HistogramTexture != null
? Mathf.Min(m_HistogramTexture.height, r.height - 45f)
: r.height;
m_MonitorAreaRect = new Rect(
Mathf.Floor(r.x + r.width / 2f - width / 2f),
Mathf.Floor(r.y + r.height / 2f - height / 2f - 5f),
width, height
);
if (m_HistogramTexture != null)
{
Graphics.DrawTexture(m_MonitorAreaRect, m_HistogramTexture);
var color = Color.white;
const float kTickSize = 5f;
// Rect, lines & ticks points
if (m_MonitorSettings.histogramMode == HistogramMode.RGBSplit)
{
// A B C D E
// N F
// M G
// L K J I H
var A = new Vector3(m_MonitorAreaRect.x - 1f, m_MonitorAreaRect.y - 1f);
var E = new Vector3(A.x + m_MonitorAreaRect.width + 2f, m_MonitorAreaRect.y - 1f);
var H = new Vector3(E.x, E.y + m_MonitorAreaRect.height + 2f);
var L = new Vector3(A.x, H.y);
var N = new Vector3(A.x, A.y + (L.y - A.y) / 3f);
var M = new Vector3(A.x, A.y + (L.y - A.y) * 2f / 3f);
var F = new Vector3(E.x, E.y + (H.y - E.y) / 3f);
var G = new Vector3(E.x, E.y + (H.y - E.y) * 2f / 3f);
var C = new Vector3(A.x + (E.x - A.x) / 2f, A.y);
var J = new Vector3(L.x + (H.x - L.x) / 2f, L.y);
var B = new Vector3(A.x + (C.x - A.x) / 2f, A.y);
var D = new Vector3(C.x + (E.x - C.x) / 2f, C.y);
var I = new Vector3(J.x + (H.x - J.x) / 2f, J.y);
var K = new Vector3(L.x + (J.x - L.x) / 2f, L.y);
// Borders
Handles.color = color;
Handles.DrawLine(A, E);
Handles.DrawLine(E, H);
Handles.DrawLine(H, L);
Handles.DrawLine(L, new Vector3(A.x, A.y - 1f));
// Vertical ticks
Handles.DrawLine(A, new Vector3(A.x - kTickSize, A.y));
Handles.DrawLine(N, new Vector3(N.x - kTickSize, N.y));
Handles.DrawLine(M, new Vector3(M.x - kTickSize, M.y));
Handles.DrawLine(L, new Vector3(L.x - kTickSize, L.y));
Handles.DrawLine(E, new Vector3(E.x + kTickSize, E.y));
Handles.DrawLine(F, new Vector3(F.x + kTickSize, F.y));
Handles.DrawLine(G, new Vector3(G.x + kTickSize, G.y));
Handles.DrawLine(H, new Vector3(H.x + kTickSize, H.y));
// Horizontal ticks
Handles.DrawLine(A, new Vector3(A.x, A.y - kTickSize));
Handles.DrawLine(B, new Vector3(B.x, B.y - kTickSize));
Handles.DrawLine(C, new Vector3(C.x, C.y - kTickSize));
Handles.DrawLine(D, new Vector3(D.x, D.y - kTickSize));
Handles.DrawLine(E, new Vector3(E.x, E.y - kTickSize));
Handles.DrawLine(L, new Vector3(L.x, L.y + kTickSize));
Handles.DrawLine(K, new Vector3(K.x, K.y + kTickSize));
Handles.DrawLine(J, new Vector3(J.x, J.y + kTickSize));
Handles.DrawLine(I, new Vector3(I.x, I.y + kTickSize));
Handles.DrawLine(H, new Vector3(H.x, H.y + kTickSize));
// Separators
Handles.DrawLine(N, F);
Handles.DrawLine(M, G);
// Labels
GUI.color = color;
GUI.Label(new Rect(L.x - 15f, L.y + kTickSize - 4f, 30f, 30f), "0.0", FxStyles.tickStyleCenter);
GUI.Label(new Rect(J.x - 15f, J.y + kTickSize - 4f, 30f, 30f), "0.5", FxStyles.tickStyleCenter);
GUI.Label(new Rect(H.x - 15f, H.y + kTickSize - 4f, 30f, 30f), "1.0", FxStyles.tickStyleCenter);
}
else
{
// A B C D E
// P F
// O G
// N H
// M L K J I
var A = new Vector3(m_MonitorAreaRect.x, m_MonitorAreaRect.y);
var E = new Vector3(A.x + m_MonitorAreaRect.width + 1f, m_MonitorAreaRect.y);
var I = new Vector3(E.x, E.y + m_MonitorAreaRect.height + 1f);
var M = new Vector3(A.x, I.y);
var C = new Vector3(A.x + (E.x - A.x) / 2f, A.y);
var G = new Vector3(E.x, E.y + (I.y - E.y) / 2f);
var K = new Vector3(M.x + (I.x - M.x) / 2f, M.y);
var O = new Vector3(A.x, A.y + (M.y - A.y) / 2f);
var P = new Vector3(A.x, A.y + (O.y - A.y) / 2f);
var F = new Vector3(E.x, E.y + (G.y - E.y) / 2f);
var N = new Vector3(A.x, O.y + (M.y - O.y) / 2f);
var H = new Vector3(E.x, G.y + (I.y - G.y) / 2f);
var B = new Vector3(A.x + (C.x - A.x) / 2f, A.y);
var L = new Vector3(M.x + (K.x - M.x) / 2f, M.y);
var D = new Vector3(C.x + (E.x - C.x) / 2f, A.y);
var J = new Vector3(K.x + (I.x - K.x) / 2f, M.y);
// Borders
Handles.color = color;
Handles.DrawLine(A, E);
Handles.DrawLine(E, I);
Handles.DrawLine(I, M);
Handles.DrawLine(M, new Vector3(A.x, A.y - 1f));
// Vertical ticks
Handles.DrawLine(A, new Vector3(A.x - kTickSize, A.y));
Handles.DrawLine(P, new Vector3(P.x - kTickSize, P.y));
Handles.DrawLine(O, new Vector3(O.x - kTickSize, O.y));
Handles.DrawLine(N, new Vector3(N.x - kTickSize, N.y));
Handles.DrawLine(M, new Vector3(M.x - kTickSize, M.y));
Handles.DrawLine(E, new Vector3(E.x + kTickSize, E.y));
Handles.DrawLine(F, new Vector3(F.x + kTickSize, F.y));
Handles.DrawLine(G, new Vector3(G.x + kTickSize, G.y));
Handles.DrawLine(H, new Vector3(H.x + kTickSize, H.y));
Handles.DrawLine(I, new Vector3(I.x + kTickSize, I.y));
// Horizontal ticks
Handles.DrawLine(A, new Vector3(A.x, A.y - kTickSize));
Handles.DrawLine(B, new Vector3(B.x, B.y - kTickSize));
Handles.DrawLine(C, new Vector3(C.x, C.y - kTickSize));
Handles.DrawLine(D, new Vector3(D.x, D.y - kTickSize));
Handles.DrawLine(E, new Vector3(E.x, E.y - kTickSize));
Handles.DrawLine(M, new Vector3(M.x, M.y + kTickSize));
Handles.DrawLine(L, new Vector3(L.x, L.y + kTickSize));
Handles.DrawLine(K, new Vector3(K.x, K.y + kTickSize));
Handles.DrawLine(J, new Vector3(J.x, J.y + kTickSize));
Handles.DrawLine(I, new Vector3(I.x, I.y + kTickSize));
// Labels
GUI.color = color;
GUI.Label(new Rect(A.x - kTickSize - 34f, A.y - 15f, 30f, 30f), "1.0", FxStyles.tickStyleRight);
GUI.Label(new Rect(O.x - kTickSize - 34f, O.y - 15f, 30f, 30f), "0.5", FxStyles.tickStyleRight);
GUI.Label(new Rect(M.x - kTickSize - 34f, M.y - 15f, 30f, 30f), "0.0", FxStyles.tickStyleRight);
GUI.Label(new Rect(E.x + kTickSize + 4f, E.y - 15f, 30f, 30f), "1.0", FxStyles.tickStyleLeft);
GUI.Label(new Rect(G.x + kTickSize + 4f, G.y - 15f, 30f, 30f), "0.5", FxStyles.tickStyleLeft);
GUI.Label(new Rect(I.x + kTickSize + 4f, I.y - 15f, 30f, 30f), "0.0", FxStyles.tickStyleLeft);
GUI.Label(new Rect(M.x - 15f, M.y + kTickSize - 4f, 30f, 30f), "0.0", FxStyles.tickStyleCenter);
GUI.Label(new Rect(K.x - 15f, K.y + kTickSize - 4f, 30f, 30f), "0.5", FxStyles.tickStyleCenter);
GUI.Label(new Rect(I.x - 15f, I.y + kTickSize - 4f, 30f, 30f), "1.0", FxStyles.tickStyleCenter);
}
}
}
}
public override void OnFrameData(RenderTexture source)
{
if (Application.isPlaying && !m_MonitorSettings.refreshOnPlay)
return;
if (Mathf.Approximately(m_MonitorAreaRect.width, 0) || Mathf.Approximately(m_MonitorAreaRect.height, 0))
return;
float ratio = (float)source.width / (float)source.height;
int h = 512;
int w = Mathf.FloorToInt(h * ratio);
var rt = RenderTexture.GetTemporary(w, h, 0, source.format);
Graphics.Blit(source, rt);
ComputeHistogram(rt);
m_BaseEditor.Repaint();
RenderTexture.ReleaseTemporary(rt);
}
void CreateBuffer(int width, int height)
{
m_Buffer = new ComputeBuffer(width * height, sizeof(uint) << 2);
}
void ComputeHistogram(RenderTexture source)
{
if (m_Buffer == null)
{
CreateBuffer(256, 1);
}
else if (m_Buffer.count != 256)
{
m_Buffer.Release();
CreateBuffer(256, 1);
}
if (m_Material == null)
{
m_Material = new Material(Shader.Find("Hidden/Post FX/Monitors/Histogram Render")) { hideFlags = HideFlags.DontSave };
}
var channels = Vector4.zero;
switch (m_MonitorSettings.histogramMode)
{
case HistogramMode.Red: channels.x = 1f; break;
case HistogramMode.Green: channels.y = 1f; break;
case HistogramMode.Blue: channels.z = 1f; break;
case HistogramMode.Luminance: channels.w = 1f; break;
default: channels = new Vector4(1f, 1f, 1f, 0f); break;
}
var cs = m_ComputeShader;
int kernel = cs.FindKernel("KHistogramClear");
cs.SetBuffer(kernel, "_Histogram", m_Buffer);
cs.Dispatch(kernel, 1, 1, 1);
kernel = cs.FindKernel("KHistogramGather");
cs.SetBuffer(kernel, "_Histogram", m_Buffer);
cs.SetTexture(kernel, "_Source", source);
cs.SetInt("_IsLinear", GraphicsUtils.isLinearColorSpace ? 1 : 0);
cs.SetVector("_Res", new Vector4(source.width, source.height, 0f, 0f));
cs.SetVector("_Channels", channels);
cs.Dispatch(kernel, Mathf.CeilToInt(source.width / 16f), Mathf.CeilToInt(source.height / 16f), 1);
kernel = cs.FindKernel("KHistogramScale");
cs.SetBuffer(kernel, "_Histogram", m_Buffer);
cs.Dispatch(kernel, 1, 1, 1);
if (m_HistogramTexture == null || m_HistogramTexture.width != source.width || m_HistogramTexture.height != source.height)
{
GraphicsUtils.Destroy(m_HistogramTexture);
m_HistogramTexture = new RenderTexture(source.width, source.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear)
{
hideFlags = HideFlags.DontSave,
wrapMode = TextureWrapMode.Clamp,
filterMode = FilterMode.Bilinear
};
}
m_Material.SetBuffer("_Histogram", m_Buffer);
m_Material.SetVector("_Size", new Vector2(m_HistogramTexture.width, m_HistogramTexture.height));
m_Material.SetColor("_ColorR", new Color(1f, 0f, 0f, 1f));
m_Material.SetColor("_ColorG", new Color(0f, 1f, 0f, 1f));
m_Material.SetColor("_ColorB", new Color(0f, 0f, 1f, 1f));
m_Material.SetColor("_ColorL", new Color(1f, 1f, 1f, 1f));
m_Material.SetInt("_Channel", (int)m_MonitorSettings.histogramMode);
int pass = 0;
if (m_MonitorSettings.histogramMode == HistogramMode.RGBMerged)
pass = 1;
else if (m_MonitorSettings.histogramMode == HistogramMode.RGBSplit)
pass = 2;
Graphics.Blit(null, m_HistogramTexture, m_Material, pass);
}
}
}