BuiltinDebugViewsComponent.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Rendering;
  4. namespace UnityEngine.PostProcessing
  5. {
  6. public sealed class BuiltinDebugViewsComponent : PostProcessingComponentCommandBuffer<BuiltinDebugViewsModel>
  7. {
  8. public override bool active
  9. {
  10. get
  11. {
  12. return base.model.IsModeActive(BuiltinDebugViewsModel.Mode.Depth) || base.model.IsModeActive(BuiltinDebugViewsModel.Mode.Normals) || base.model.IsModeActive(BuiltinDebugViewsModel.Mode.MotionVectors);
  13. }
  14. }
  15. public override DepthTextureMode GetCameraFlags()
  16. {
  17. BuiltinDebugViewsModel.Mode mode = base.model.settings.mode;
  18. DepthTextureMode depthTextureMode = DepthTextureMode.None;
  19. if (mode != BuiltinDebugViewsModel.Mode.Normals)
  20. {
  21. if (mode != BuiltinDebugViewsModel.Mode.MotionVectors)
  22. {
  23. if (mode == BuiltinDebugViewsModel.Mode.Depth)
  24. {
  25. depthTextureMode |= DepthTextureMode.Depth;
  26. }
  27. }
  28. else
  29. {
  30. depthTextureMode |= (DepthTextureMode.Depth | DepthTextureMode.MotionVectors);
  31. }
  32. }
  33. else
  34. {
  35. depthTextureMode |= DepthTextureMode.DepthNormals;
  36. }
  37. return depthTextureMode;
  38. }
  39. public override CameraEvent GetCameraEvent()
  40. {
  41. return (base.model.settings.mode != BuiltinDebugViewsModel.Mode.MotionVectors) ? CameraEvent.BeforeImageEffectsOpaque : CameraEvent.BeforeImageEffects;
  42. }
  43. public override string GetName()
  44. {
  45. return "Builtin Debug Views";
  46. }
  47. public override void PopulateCommandBuffer(CommandBuffer cb)
  48. {
  49. BuiltinDebugViewsModel.Settings settings = base.model.settings;
  50. Material material = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
  51. material.shaderKeywords = null;
  52. if (this.context.isGBufferAvailable)
  53. {
  54. material.EnableKeyword("SOURCE_GBUFFER");
  55. }
  56. BuiltinDebugViewsModel.Mode mode = settings.mode;
  57. if (mode != BuiltinDebugViewsModel.Mode.Depth)
  58. {
  59. if (mode != BuiltinDebugViewsModel.Mode.Normals)
  60. {
  61. if (mode == BuiltinDebugViewsModel.Mode.MotionVectors)
  62. {
  63. this.MotionVectorsPass(cb);
  64. }
  65. }
  66. else
  67. {
  68. this.DepthNormalsPass(cb);
  69. }
  70. }
  71. else
  72. {
  73. this.DepthPass(cb);
  74. }
  75. this.context.Interrupt();
  76. }
  77. private void DepthPass(CommandBuffer cb)
  78. {
  79. Material mat = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
  80. BuiltinDebugViewsModel.DepthSettings depth = base.model.settings.depth;
  81. cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._DepthScale, 1f / depth.scale);
  82. cb.Blit(null, BuiltinRenderTextureType.CameraTarget, mat, 0);
  83. }
  84. private void DepthNormalsPass(CommandBuffer cb)
  85. {
  86. Material mat = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
  87. cb.Blit(null, BuiltinRenderTextureType.CameraTarget, mat, 1);
  88. }
  89. private void MotionVectorsPass(CommandBuffer cb)
  90. {
  91. Material material = this.context.materialFactory.Get("Hidden/Post FX/Builtin Debug Views");
  92. BuiltinDebugViewsModel.MotionVectorsSettings motionVectors = base.model.settings.motionVectors;
  93. int nameID = BuiltinDebugViewsComponent.Uniforms._TempRT;
  94. cb.GetTemporaryRT(nameID, this.context.width, this.context.height, 0, FilterMode.Bilinear);
  95. cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Opacity, motionVectors.sourceOpacity);
  96. cb.SetGlobalTexture(BuiltinDebugViewsComponent.Uniforms._MainTex, BuiltinRenderTextureType.CameraTarget);
  97. cb.Blit(BuiltinRenderTextureType.CameraTarget, nameID, material, 2);
  98. if (motionVectors.motionImageOpacity > 0f && motionVectors.motionImageAmplitude > 0f)
  99. {
  100. int tempRT = BuiltinDebugViewsComponent.Uniforms._TempRT2;
  101. cb.GetTemporaryRT(tempRT, this.context.width, this.context.height, 0, FilterMode.Bilinear);
  102. cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Opacity, motionVectors.motionImageOpacity);
  103. cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Amplitude, motionVectors.motionImageAmplitude);
  104. cb.SetGlobalTexture(BuiltinDebugViewsComponent.Uniforms._MainTex, nameID);
  105. cb.Blit(nameID, tempRT, material, 3);
  106. cb.ReleaseTemporaryRT(nameID);
  107. nameID = tempRT;
  108. }
  109. if (motionVectors.motionVectorsOpacity > 0f && motionVectors.motionVectorsAmplitude > 0f)
  110. {
  111. this.PrepareArrows();
  112. float num = 1f / (float)motionVectors.motionVectorsResolution;
  113. float x = num * (float)this.context.height / (float)this.context.width;
  114. cb.SetGlobalVector(BuiltinDebugViewsComponent.Uniforms._Scale, new Vector2(x, num));
  115. cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Opacity, motionVectors.motionVectorsOpacity);
  116. cb.SetGlobalFloat(BuiltinDebugViewsComponent.Uniforms._Amplitude, motionVectors.motionVectorsAmplitude);
  117. cb.DrawMesh(this.m_Arrows.mesh, Matrix4x4.identity, material, 0, 4);
  118. }
  119. cb.SetGlobalTexture(BuiltinDebugViewsComponent.Uniforms._MainTex, nameID);
  120. cb.Blit(nameID, BuiltinRenderTextureType.CameraTarget);
  121. cb.ReleaseTemporaryRT(nameID);
  122. }
  123. private void PrepareArrows()
  124. {
  125. int motionVectorsResolution = base.model.settings.motionVectors.motionVectorsResolution;
  126. int num = motionVectorsResolution * Screen.width / Screen.height;
  127. if (this.m_Arrows == null)
  128. {
  129. this.m_Arrows = new BuiltinDebugViewsComponent.ArrowArray();
  130. }
  131. if (this.m_Arrows.columnCount != num || this.m_Arrows.rowCount != motionVectorsResolution)
  132. {
  133. this.m_Arrows.Release();
  134. this.m_Arrows.BuildMesh(num, motionVectorsResolution);
  135. }
  136. }
  137. public override void OnDisable()
  138. {
  139. if (this.m_Arrows != null)
  140. {
  141. this.m_Arrows.Release();
  142. }
  143. this.m_Arrows = null;
  144. }
  145. private const string k_ShaderString = "Hidden/Post FX/Builtin Debug Views";
  146. private BuiltinDebugViewsComponent.ArrowArray m_Arrows;
  147. private static class Uniforms
  148. {
  149. internal static readonly int _DepthScale = Shader.PropertyToID("_DepthScale");
  150. internal static readonly int _TempRT = Shader.PropertyToID("_TempRT");
  151. internal static readonly int _Opacity = Shader.PropertyToID("_Opacity");
  152. internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
  153. internal static readonly int _TempRT2 = Shader.PropertyToID("_TempRT2");
  154. internal static readonly int _Amplitude = Shader.PropertyToID("_Amplitude");
  155. internal static readonly int _Scale = Shader.PropertyToID("_Scale");
  156. }
  157. private enum Pass
  158. {
  159. Depth,
  160. Normals,
  161. MovecOpacity,
  162. MovecImaging,
  163. MovecArrows
  164. }
  165. private class ArrowArray
  166. {
  167. public Mesh mesh { get; private set; }
  168. public int columnCount { get; private set; }
  169. public int rowCount { get; private set; }
  170. public void BuildMesh(int columns, int rows)
  171. {
  172. Vector3[] array = new Vector3[]
  173. {
  174. new Vector3(0f, 0f, 0f),
  175. new Vector3(0f, 1f, 0f),
  176. new Vector3(0f, 1f, 0f),
  177. new Vector3(-1f, 1f, 0f),
  178. new Vector3(0f, 1f, 0f),
  179. new Vector3(1f, 1f, 0f)
  180. };
  181. int num = 6 * columns * rows;
  182. List<Vector3> list = new List<Vector3>(num);
  183. List<Vector2> list2 = new List<Vector2>(num);
  184. for (int i = 0; i < rows; i++)
  185. {
  186. for (int j = 0; j < columns; j++)
  187. {
  188. Vector2 item = new Vector2((0.5f + (float)j) / (float)columns, (0.5f + (float)i) / (float)rows);
  189. for (int k = 0; k < 6; k++)
  190. {
  191. list.Add(array[k]);
  192. list2.Add(item);
  193. }
  194. }
  195. }
  196. int[] array2 = new int[num];
  197. for (int l = 0; l < num; l++)
  198. {
  199. array2[l] = l;
  200. }
  201. this.mesh = new Mesh
  202. {
  203. hideFlags = HideFlags.DontSave
  204. };
  205. this.mesh.SetVertices(list);
  206. this.mesh.SetUVs(0, list2);
  207. this.mesh.SetIndices(array2, MeshTopology.Lines, 0);
  208. this.mesh.UploadMeshData(true);
  209. this.columnCount = columns;
  210. this.rowCount = rows;
  211. }
  212. public void Release()
  213. {
  214. GraphicsUtils.Destroy(this.mesh);
  215. this.mesh = null;
  216. }
  217. }
  218. }
  219. }