summaryrefslogtreecommitdiff
path: root/UnityEngine.PostProcessing/BuiltinDebugViewsComponent.cs
blob: a1a6d440a2f2ef84ea97ae0d1667142b465f6447 (plain)
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
using System.Collections.Generic;
using UnityEngine.Rendering;

namespace UnityEngine.PostProcessing;

public sealed class BuiltinDebugViewsComponent : PostProcessingComponentCommandBuffer<BuiltinDebugViewsModel>
{
	private static class Uniforms
	{
		internal static readonly int _DepthScale = Shader.PropertyToID("_DepthScale");

		internal static readonly int _TempRT = Shader.PropertyToID("_TempRT");

		internal static readonly int _Opacity = Shader.PropertyToID("_Opacity");

		internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");

		internal static readonly int _TempRT2 = Shader.PropertyToID("_TempRT2");

		internal static readonly int _Amplitude = Shader.PropertyToID("_Amplitude");

		internal static readonly int _Scale = Shader.PropertyToID("_Scale");
	}

	private enum Pass
	{
		Depth,
		Normals,
		MovecOpacity,
		MovecImaging,
		MovecArrows
	}

	private class ArrowArray
	{
		public Mesh mesh { get; private set; }

		public int columnCount { get; private set; }

		public int rowCount { get; private set; }

		public void BuildMesh(int columns, int rows)
		{
			Vector3[] array = new Vector3[6]
			{
				new Vector3(0f, 0f, 0f),
				new Vector3(0f, 1f, 0f),
				new Vector3(0f, 1f, 0f),
				new Vector3(-1f, 1f, 0f),
				new Vector3(0f, 1f, 0f),
				new Vector3(1f, 1f, 0f)
			};
			int num = 6 * columns * rows;
			List<Vector3> list = new List<Vector3>(num);
			List<Vector2> list2 = new List<Vector2>(num);
			for (int i = 0; i < rows; i++)
			{
				for (int j = 0; j < columns; j++)
				{
					Vector2 item = new Vector2((0.5f + (float)j) / (float)columns, (0.5f + (float)i) / (float)rows);
					for (int k = 0; k < 6; k++)
					{
						list.Add(array[k]);
						list2.Add(item);
					}
				}
			}
			int[] array2 = new int[num];
			for (int l = 0; l < num; l++)
			{
				array2[l] = l;
			}
			mesh = new Mesh
			{
				hideFlags = HideFlags.DontSave
			};
			mesh.SetVertices(list);
			mesh.SetUVs(0, list2);
			mesh.SetIndices(array2, MeshTopology.Lines, 0);
			mesh.UploadMeshData(markNoLogerReadable: true);
			columnCount = columns;
			rowCount = rows;
		}

		public void Release()
		{
			GraphicsUtils.Destroy(mesh);
			mesh = null;
		}
	}

	private const string k_ShaderString = "Hidden/Post FX/Builtin Debug Views";

	private ArrowArray m_Arrows;

	public override bool active => base.model.IsModeActive(BuiltinDebugViewsModel.Mode.Depth) || base.model.IsModeActive(BuiltinDebugViewsModel.Mode.Normals) || base.model.IsModeActive(BuiltinDebugViewsModel.Mode.MotionVectors);

	public override DepthTextureMode GetCameraFlags()
	{
		BuiltinDebugViewsModel.Mode mode = base.model.settings.mode;
		DepthTextureMode depthTextureMode = DepthTextureMode.None;
		switch (mode)
		{
		case BuiltinDebugViewsModel.Mode.Normals:
			depthTextureMode |= DepthTextureMode.DepthNormals;
			break;
		case BuiltinDebugViewsModel.Mode.MotionVectors:
			depthTextureMode |= DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
			break;
		case BuiltinDebugViewsModel.Mode.Depth:
			depthTextureMode |= DepthTextureMode.Depth;
			break;
		}
		return depthTextureMode;
	}

	public override CameraEvent GetCameraEvent()
	{
		return (base.model.settings.mode != BuiltinDebugViewsModel.Mode.MotionVectors) ? CameraEvent.BeforeImageEffectsOpaque : CameraEvent.BeforeImageEffects;
	}

	public override string GetName()
	{
		return "Builtin Debug Views";
	}

	public override void PopulateCommandBuffer(CommandBuffer cb)
	{
		BuiltinDebugViewsModel.Settings settings = base.model.settings;
		Material material = context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
		material.shaderKeywords = null;
		if (context.isGBufferAvailable)
		{
			material.EnableKeyword("SOURCE_GBUFFER");
		}
		switch (settings.mode)
		{
		case BuiltinDebugViewsModel.Mode.Depth:
			DepthPass(cb);
			break;
		case BuiltinDebugViewsModel.Mode.Normals:
			DepthNormalsPass(cb);
			break;
		case BuiltinDebugViewsModel.Mode.MotionVectors:
			MotionVectorsPass(cb);
			break;
		}
		context.Interrupt();
	}

	private void DepthPass(CommandBuffer cb)
	{
		Material mat = context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
		BuiltinDebugViewsModel.DepthSettings depth = base.model.settings.depth;
		cb.SetGlobalFloat(Uniforms._DepthScale, 1f / depth.scale);
		cb.Blit(null, BuiltinRenderTextureType.CameraTarget, mat, 0);
	}

	private void DepthNormalsPass(CommandBuffer cb)
	{
		Material mat = context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
		cb.Blit(null, BuiltinRenderTextureType.CameraTarget, mat, 1);
	}

	private void MotionVectorsPass(CommandBuffer cb)
	{
		Material material = context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
		BuiltinDebugViewsModel.MotionVectorsSettings motionVectors = base.model.settings.motionVectors;
		int num = Uniforms._TempRT;
		cb.GetTemporaryRT(num, context.width, context.height, 0, FilterMode.Bilinear);
		cb.SetGlobalFloat(Uniforms._Opacity, motionVectors.sourceOpacity);
		cb.SetGlobalTexture(Uniforms._MainTex, BuiltinRenderTextureType.CameraTarget);
		cb.Blit(BuiltinRenderTextureType.CameraTarget, num, material, 2);
		if (motionVectors.motionImageOpacity > 0f && motionVectors.motionImageAmplitude > 0f)
		{
			int tempRT = Uniforms._TempRT2;
			cb.GetTemporaryRT(tempRT, context.width, context.height, 0, FilterMode.Bilinear);
			cb.SetGlobalFloat(Uniforms._Opacity, motionVectors.motionImageOpacity);
			cb.SetGlobalFloat(Uniforms._Amplitude, motionVectors.motionImageAmplitude);
			cb.SetGlobalTexture(Uniforms._MainTex, num);
			cb.Blit(num, tempRT, material, 3);
			cb.ReleaseTemporaryRT(num);
			num = tempRT;
		}
		if (motionVectors.motionVectorsOpacity > 0f && motionVectors.motionVectorsAmplitude > 0f)
		{
			PrepareArrows();
			float num2 = 1f / (float)motionVectors.motionVectorsResolution;
			float x = num2 * (float)context.height / (float)context.width;
			cb.SetGlobalVector(Uniforms._Scale, new Vector2(x, num2));
			cb.SetGlobalFloat(Uniforms._Opacity, motionVectors.motionVectorsOpacity);
			cb.SetGlobalFloat(Uniforms._Amplitude, motionVectors.motionVectorsAmplitude);
			cb.DrawMesh(m_Arrows.mesh, Matrix4x4.identity, material, 0, 4);
		}
		cb.SetGlobalTexture(Uniforms._MainTex, num);
		cb.Blit(num, BuiltinRenderTextureType.CameraTarget);
		cb.ReleaseTemporaryRT(num);
	}

	private void PrepareArrows()
	{
		int motionVectorsResolution = base.model.settings.motionVectors.motionVectorsResolution;
		int num = motionVectorsResolution * Screen.width / Screen.height;
		if (m_Arrows == null)
		{
			m_Arrows = new ArrowArray();
		}
		if (m_Arrows.columnCount != num || m_Arrows.rowCount != motionVectorsResolution)
		{
			m_Arrows.Release();
			m_Arrows.BuildMesh(num, motionVectorsResolution);
		}
	}

	public override void OnDisable()
	{
		if (m_Arrows != null)
		{
			m_Arrows.Release();
		}
		m_Arrows = null;
	}
}