DrawingUtility.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using UnityEditor.U2D.Common;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal class DrawingUtility
  6. {
  7. public static readonly Color kSpriteBorderColor = new Color(0.25f, 0.5f, 1f, 0.75f);
  8. public static void DrawLine(Vector3 p1, Vector3 p2, Vector3 normal, float width)
  9. {
  10. DrawLine(p1, p2, normal, width, width);
  11. }
  12. public static void DrawLine(Vector3 p1, Vector3 p2, Vector3 normal, float widthP1, float widthP2)
  13. {
  14. DrawLine(p1, p2, normal, widthP1, widthP2, Handles.color);
  15. }
  16. public static void DrawLine(Vector3 p1, Vector3 p2, Vector3 normal, float widthP1, float widthP2, Color color)
  17. {
  18. if (Event.current.type != EventType.Repaint)
  19. return;
  20. Vector3 up = Vector3.Cross(normal, p2 - p1).normalized;
  21. Shader.SetGlobalFloat("_HandleSize", 1);
  22. InternalEditorBridge.ApplyWireMaterial();
  23. GL.PushMatrix();
  24. GL.MultMatrix(Handles.matrix);
  25. GL.Begin(4);
  26. GL.Color(color);
  27. GL.Vertex(p1 + up * widthP1 * 0.5f);
  28. GL.Vertex(p1 - up * widthP1 * 0.5f);
  29. GL.Vertex(p2 - up * widthP2 * 0.5f);
  30. GL.Vertex(p1 + up * widthP1 * 0.5f);
  31. GL.Vertex(p2 - up * widthP2 * 0.5f);
  32. GL.Vertex(p2 + up * widthP2 * 0.5f);
  33. GL.End();
  34. GL.PopMatrix();
  35. }
  36. public static void BeginLines(Color color)
  37. {
  38. InternalEditorBridge.ApplyWireMaterial();
  39. GL.PushMatrix();
  40. GL.MultMatrix(Handles.matrix);
  41. GL.Begin(GL.LINES);
  42. GL.Color(color);
  43. }
  44. public static void BeginSolidLines()
  45. {
  46. InternalEditorBridge.ApplyWireMaterial();
  47. GL.PushMatrix();
  48. GL.MultMatrix(Handles.matrix);
  49. GL.Begin(GL.TRIANGLES);
  50. }
  51. public static void EndLines()
  52. {
  53. GL.End();
  54. GL.PopMatrix();
  55. }
  56. public static void DrawLine(Vector3 p1, Vector3 p2)
  57. {
  58. GL.Vertex(p1);
  59. GL.Vertex(p2);
  60. }
  61. public static void DrawSolidLine(float width, Vector3 p1, Vector3 p2)
  62. {
  63. DrawSolidLine(p1, p2, Vector3.forward, width, width);
  64. }
  65. public static void DrawSolidLine(Vector3 p1, Vector3 p2, Vector3 normal, float widthP1, float widthP2)
  66. {
  67. GL.Color(Handles.color);
  68. Vector3 right = Vector3.Cross(normal, p2 - p1).normalized;
  69. GL.Vertex(p1 + right * widthP1 * 0.5f);
  70. GL.Vertex(p1 - right * widthP1 * 0.5f);
  71. GL.Vertex(p2 - right * widthP2 * 0.5f);
  72. GL.Vertex(p1 + right * widthP1 * 0.5f);
  73. GL.Vertex(p2 - right * widthP2 * 0.5f);
  74. GL.Vertex(p2 + right * widthP2 * 0.5f);
  75. }
  76. public static void DrawBox(Rect position)
  77. {
  78. Vector3[] points = new Vector3[5];
  79. int i = 0;
  80. points[i++] = new Vector3(position.xMin, position.yMin, 0f);
  81. points[i++] = new Vector3(position.xMax, position.yMin, 0f);
  82. points[i++] = new Vector3(position.xMax, position.yMax, 0f);
  83. points[i++] = new Vector3(position.xMin, position.yMax, 0f);
  84. DrawLine(points[0], points[1]);
  85. DrawLine(points[1], points[2]);
  86. DrawLine(points[2], points[3]);
  87. DrawLine(points[3], points[0]);
  88. }
  89. public static void DrawMesh(Mesh mesh, Material material, Matrix4x4 matrix)
  90. {
  91. Debug.Assert(mesh != null);
  92. Debug.Assert(material != null);
  93. if (Event.current.type != EventType.Repaint)
  94. return;
  95. material.SetFloat("_AdjustLinearForGamma", PlayerSettings.colorSpace == ColorSpace.Linear ? 1.0f : 0.0f);
  96. material.SetPass(0);
  97. Graphics.DrawMeshNow(mesh, Handles.matrix * matrix);
  98. }
  99. public static void DrawGUIStyleCap(int controlID, Vector3 position, Quaternion rotation, float size, GUIStyle guiStyle)
  100. {
  101. if (Event.current.type != EventType.Repaint)
  102. return;
  103. if (Camera.current && Vector3.Dot(position - Camera.current.transform.position, Camera.current.transform.forward) < 0f)
  104. return;
  105. Handles.BeginGUI();
  106. guiStyle.Draw(GetGUIStyleRect(guiStyle, position), GUIContent.none, controlID);
  107. Handles.EndGUI();
  108. }
  109. private static Rect GetGUIStyleRect(GUIStyle style, Vector3 position)
  110. {
  111. Vector2 vector = HandleUtility.WorldToGUIPoint(position);
  112. float fixedWidth = style.fixedWidth;
  113. float fixedHeight = style.fixedHeight;
  114. return new Rect(vector.x - fixedWidth / 2f, vector.y - fixedHeight / 2f, fixedWidth, fixedHeight);
  115. }
  116. public static void DrawRect(Rect rect, Vector3 position, Quaternion rotation, Color color, float rectAlpha, float outlineAlpha)
  117. {
  118. if (Event.current.type != EventType.Repaint)
  119. return;
  120. Vector3[] corners = new Vector3[4];
  121. for (int i = 0; i < 4; i++)
  122. {
  123. Vector3 point = GetLocalRectPoint(rect, i);
  124. corners[i] = rotation * point + position;
  125. }
  126. Vector3[] points = new Vector3[]
  127. {
  128. corners[0],
  129. corners[1],
  130. corners[2],
  131. corners[3],
  132. corners[0]
  133. };
  134. Color l_color = Handles.color;
  135. Handles.color = color;
  136. Vector2 offset = new Vector2(1f, 1f);
  137. if (!Camera.current)
  138. {
  139. offset.y *= -1;
  140. }
  141. Handles.DrawSolidRectangleWithOutline(points, new Color(1f, 1f, 1f, rectAlpha), new Color(1f, 1f, 1f, outlineAlpha));
  142. Handles.color = l_color;
  143. }
  144. private static Vector2 GetLocalRectPoint(Rect rect, int index)
  145. {
  146. switch (index)
  147. {
  148. case (0): return new Vector2(rect.xMin, rect.yMax);
  149. case (1): return new Vector2(rect.xMax, rect.yMax);
  150. case (2): return new Vector2(rect.xMax, rect.yMin);
  151. case (3): return new Vector2(rect.xMin, rect.yMin);
  152. }
  153. return Vector3.zero;
  154. }
  155. private static void SetDiscSectionPoints(Vector3[] dest, int count, Vector3 normal, Vector3 from, float angle)
  156. {
  157. from.Normalize();
  158. Quaternion rotation = Quaternion.AngleAxis(angle / (float)(count - 1), normal);
  159. Vector3 vector = from;
  160. for (int i = 0; i < count; i++)
  161. {
  162. dest[i] = vector;
  163. vector = rotation * vector;
  164. }
  165. }
  166. static Vector3[] s_array;
  167. public static void DrawSolidArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius, int numSamples = 60)
  168. {
  169. if (Event.current.type != EventType.Repaint)
  170. return;
  171. numSamples = Mathf.Clamp(numSamples, 3, 60);
  172. if (s_array == null)
  173. s_array = new Vector3[60];
  174. Color color = Handles.color;
  175. SetDiscSectionPoints(s_array, numSamples, normal, from, angle);
  176. InternalEditorBridge.ApplyWireMaterial();
  177. GL.PushMatrix();
  178. GL.MultMatrix(Handles.matrix);
  179. GL.Begin(GL.TRIANGLES);
  180. for (int i = 1; i < numSamples; i++)
  181. {
  182. GL.Color(color);
  183. GL.Vertex(center);
  184. GL.Vertex(center + s_array[i - 1] * radius);
  185. GL.Vertex(center + s_array[i] * radius);
  186. }
  187. GL.End();
  188. GL.PopMatrix();
  189. }
  190. public static void DrawSolidArc(Vector3 center, Vector3 normal, Vector3 from, float angle, float radius, float outlineScale, int numSamples = 60)
  191. {
  192. if (Event.current.type != EventType.Repaint)
  193. return;
  194. numSamples = Mathf.Clamp(numSamples, 3, 60);
  195. if(s_array == null)
  196. s_array = new Vector3[60];
  197. Color color = Handles.color;
  198. SetDiscSectionPoints(s_array, numSamples, normal, from, angle);
  199. InternalEditorBridge.ApplyWireMaterial();
  200. GL.PushMatrix();
  201. GL.MultMatrix(Handles.matrix);
  202. GL.Begin(4);
  203. for (int i = 1; i < numSamples; i++)
  204. {
  205. GL.Color(color);
  206. GL.Vertex(center + s_array[i - 1] * radius * outlineScale);
  207. GL.Vertex(center + s_array[i - 1] * radius);
  208. GL.Vertex(center + s_array[i] * radius);
  209. GL.Vertex(center + s_array[i - 1] * radius * outlineScale);
  210. GL.Vertex(center + s_array[i] * radius);
  211. GL.Vertex(center + s_array[i] * radius * outlineScale);
  212. }
  213. GL.End();
  214. GL.PopMatrix();
  215. }
  216. }
  217. }