Draw2D.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using UnityEngine;
  2. namespace UnityEditor.Performance.ProfileAnalyzer
  3. {
  4. internal class Draw2D
  5. {
  6. public enum Origin
  7. {
  8. TopLeft,
  9. BottomLeft
  10. };
  11. Origin m_Origin = Origin.TopLeft;
  12. GUIStyle m_GLStyle;
  13. string m_ShaderName;
  14. Material m_Material;
  15. Rect m_Rect;
  16. Vector4 m_ClipRect;
  17. bool m_ClipRectEnabled = false;
  18. public Draw2D(string shaderName)
  19. {
  20. m_ShaderName = shaderName;
  21. CheckAndSetupMaterial();
  22. }
  23. public bool CheckAndSetupMaterial()
  24. {
  25. if (m_Material == null)
  26. {
  27. m_Material = new Material(Shader.Find(m_ShaderName));
  28. }
  29. if (m_Material == null)
  30. return false;
  31. return true;
  32. }
  33. public bool IsMaterialValid()
  34. {
  35. if (m_Material == null)
  36. return false;
  37. return true;
  38. }
  39. public void OnGUI()
  40. {
  41. if (m_GLStyle == null)
  42. {
  43. m_GLStyle = new GUIStyle(GUI.skin.box);
  44. m_GLStyle.padding = new RectOffset(0, 0, 0, 0);
  45. m_GLStyle.margin = new RectOffset(0, 0, 0, 0);
  46. }
  47. }
  48. public void SetClipRect(Rect clipRect)
  49. {
  50. m_ClipRect = new Vector4(clipRect.x, clipRect.y, clipRect.x + clipRect.width, clipRect.y + clipRect.height);
  51. m_ClipRectEnabled = true;
  52. CheckAndSetupMaterial();
  53. m_Material.SetFloat("_UseClipRect", m_ClipRectEnabled ? 1f : 0f);
  54. m_Material.SetVector("_ClipRect", m_ClipRect);
  55. }
  56. public void ClearClipRect()
  57. {
  58. m_ClipRectEnabled = false;
  59. CheckAndSetupMaterial();
  60. m_Material.SetFloat("_UseClipRect", m_ClipRectEnabled ? 1f : 0f);
  61. m_Material.SetVector("_ClipRect", m_ClipRect);
  62. }
  63. public Rect GetClipRect()
  64. {
  65. return new Rect(m_ClipRect.x, m_ClipRect.y, m_ClipRect.z - m_ClipRect.x, m_ClipRect.w - m_ClipRect.y);
  66. }
  67. public bool DrawStart(Rect r, Origin origin = Origin.TopLeft)
  68. {
  69. if (Event.current.type != EventType.Repaint)
  70. return false;
  71. if (!CheckAndSetupMaterial())
  72. return false;
  73. CheckAndSetupMaterial();
  74. m_Material.SetPass(0);
  75. m_Rect = r;
  76. m_Origin = origin;
  77. return true;
  78. }
  79. public bool DrawStart(float w, float h, Origin origin = Origin.TopLeft, GUIStyle style = null)
  80. {
  81. Rect r = GUILayoutUtility.GetRect(w, h, style == null ? m_GLStyle : style, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
  82. return DrawStart(r, origin);
  83. }
  84. public void DrawEnd()
  85. {
  86. }
  87. public void Translate(ref float x, ref float y)
  88. {
  89. // Translation done CPU side so we have world space coords in the shader for clipping.
  90. if (m_Origin == Origin.BottomLeft)
  91. {
  92. x = m_Rect.xMin + x;
  93. y = m_Rect.yMax - y;
  94. }
  95. else
  96. {
  97. x = m_Rect.xMin + x;
  98. y = m_Rect.yMin + y;
  99. }
  100. }
  101. public void DrawFilledBox(float x, float y, float w, float h, Color col)
  102. {
  103. float x2 = x + w;
  104. float y2 = y + h;
  105. Translate(ref x, ref y);
  106. Translate(ref x2, ref y2);
  107. if (m_Origin == Origin.BottomLeft)
  108. {
  109. GL.Begin(GL.TRIANGLE_STRIP);
  110. GL.Color(col);
  111. GL.Vertex3(x, y, 0);
  112. GL.Vertex3(x, y2, 0);
  113. GL.Vertex3(x2, y, 0);
  114. GL.Vertex3(x2, y2, 0);
  115. GL.End();
  116. }
  117. else
  118. {
  119. GL.Begin(GL.TRIANGLE_STRIP);
  120. GL.Color(col);
  121. GL.Vertex3(x, y, 0);
  122. GL.Vertex3(x2, y, 0);
  123. GL.Vertex3(x, y2, 0);
  124. GL.Vertex3(x2, y2, 0);
  125. GL.End();
  126. }
  127. }
  128. public void DrawFilledBox(float x, float y, float w, float h, float r, float g, float b)
  129. {
  130. DrawFilledBox(x, y, w, h, new Color(r, g, b));
  131. }
  132. public void DrawLine(float x, float y, float x2, float y2, Color col)
  133. {
  134. Translate(ref x, ref y);
  135. Translate(ref x2, ref y2);
  136. GL.Begin(GL.LINES);
  137. GL.Color(col);
  138. GL.Vertex3(x, y, 0);
  139. GL.Vertex3(x2, y2, 0);
  140. GL.End();
  141. }
  142. public void DrawLine(float x, float y, float x2, float y2, float r, float g, float b)
  143. {
  144. DrawLine(x, y, x2, y2, new Color(r, g, b));
  145. }
  146. public void DrawBox(float x, float y, float w, float h, Color col)
  147. {
  148. float x2 = x + w;
  149. float y2 = y + h;
  150. Translate(ref x, ref y);
  151. Translate(ref x2, ref y2);
  152. GL.Begin(GL.LINE_STRIP);
  153. GL.Color(col);
  154. GL.Vertex3(x, y, 0);
  155. GL.Vertex3(x2, y, 0);
  156. GL.Vertex3(x2, y2, 0);
  157. GL.Vertex3(x, y2, 0);
  158. GL.Vertex3(x, y, 0);
  159. GL.End();
  160. }
  161. public void DrawBox(float x, float y, float w, float h, float r, float g, float b)
  162. {
  163. DrawBox(x, y, w, h, new Color(r, g, b));
  164. }
  165. }
  166. }