-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathPostProcessDebug.cs
More file actions
160 lines (132 loc) · 5.16 KB
/
Copy pathPostProcessDebug.cs
File metadata and controls
160 lines (132 loc) · 5.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
namespace UnityEngine.Rendering.PostProcessing
{
/// <summary>
/// This component holds a set of debugging utilities related to post-processing.
/// </summary>
/// <remarks>
/// These utilities can be used at runtime to debug on device.
/// </remarks>
#if UNITY_2018_3_OR_NEWER
[ExecuteAlways]
#else
[ExecuteInEditMode]
#endif
[AddComponentMenu("Rendering/Post-process Debug", 1002)]
public sealed class PostProcessDebug : MonoBehaviour
{
/// <summary>
/// A reference to a <see cref="PostProcessLayer"/> to debug.
/// </summary>
public PostProcessLayer postProcessLayer;
PostProcessLayer m_PreviousPostProcessLayer;
/// <summary>
/// Holds settings for the light meter.
/// </summary>
public bool lightMeter;
/// <summary>
/// Holds settings for the histogram.
/// </summary>
public bool histogram;
/// <summary>
/// Holds settings for the waveform.
/// </summary>
public bool waveform;
/// <summary>
/// Holds settings for the vectorscope.
/// </summary>
public bool vectorscope;
/// <summary>
/// The currently set overlay.
/// </summary>
public DebugOverlay debugOverlay = DebugOverlay.None;
Camera m_CurrentCamera;
CommandBuffer m_CmdAfterEverything;
void OnEnable()
{
m_CmdAfterEverything = new CommandBuffer { name = "Post-processing Debug Overlay" };
#if UNITY_EDITOR
// Update is only called on object change when ExecuteInEditMode is set, but we need it
// to execute on every frame no matter what when not in play mode, so we'll use the
// editor update loop instead...
UnityEditor.EditorApplication.update += UpdateStates;
#endif
}
void OnDisable()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.update -= UpdateStates;
#endif
if (m_CurrentCamera != null)
m_CurrentCamera.RemoveCommandBuffer(CameraEvent.AfterImageEffects, m_CmdAfterEverything);
m_CurrentCamera = null;
m_PreviousPostProcessLayer = null;
}
#if !UNITY_EDITOR
void Update()
{
UpdateStates();
}
#endif
void Reset()
{
postProcessLayer = GetComponent<PostProcessLayer>();
}
void UpdateStates()
{
if (m_PreviousPostProcessLayer != postProcessLayer)
{
// Remove cmdbuffer from previously set camera
if (m_CurrentCamera != null)
{
m_CurrentCamera.RemoveCommandBuffer(CameraEvent.AfterImageEffects, m_CmdAfterEverything);
m_CurrentCamera = null;
}
m_PreviousPostProcessLayer = postProcessLayer;
// Add cmdbuffer to the currently set camera
if (postProcessLayer != null)
{
m_CurrentCamera = postProcessLayer.GetComponent<Camera>();
m_CurrentCamera.AddCommandBuffer(CameraEvent.AfterImageEffects, m_CmdAfterEverything);
}
}
if (postProcessLayer == null || !postProcessLayer.enabled)
return;
// Monitors
if (lightMeter) postProcessLayer.debugLayer.RequestMonitorPass(MonitorType.LightMeter);
if (histogram) postProcessLayer.debugLayer.RequestMonitorPass(MonitorType.Histogram);
if (waveform) postProcessLayer.debugLayer.RequestMonitorPass(MonitorType.Waveform);
if (vectorscope) postProcessLayer.debugLayer.RequestMonitorPass(MonitorType.Vectorscope);
// Overlay
postProcessLayer.debugLayer.RequestDebugOverlay(debugOverlay);
}
void OnPostRender()
{
m_CmdAfterEverything.Clear();
if (postProcessLayer == null || !postProcessLayer.enabled || !postProcessLayer.debugLayer.debugOverlayActive)
return;
m_CmdAfterEverything.Blit(postProcessLayer.debugLayer.debugOverlayTarget, BuiltinRenderTextureType.CameraTarget);
}
void OnGUI()
{
if (postProcessLayer == null || !postProcessLayer.enabled)
return;
// Some SRPs don't unbind render targets and leave them as-is
RenderTexture.active = null;
var rect = new Rect(5, 5, 0, 0);
var debugLayer = postProcessLayer.debugLayer;
DrawMonitor(ref rect, debugLayer.lightMeter, lightMeter);
DrawMonitor(ref rect, debugLayer.histogram, histogram);
DrawMonitor(ref rect, debugLayer.waveform, waveform);
DrawMonitor(ref rect, debugLayer.vectorscope, vectorscope);
}
void DrawMonitor(ref Rect rect, Monitor monitor, bool enabled)
{
if (!enabled || monitor.output == null)
return;
rect.width = monitor.output.width;
rect.height = monitor.output.height;
GUI.DrawTexture(rect, monitor.output);
rect.x += monitor.output.width + 5f;
}
}
}