No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DrawingUtility.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using UnityEditor.U2D.Common;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal static class DrawingUtility
  6. {
  7. public static readonly Color spriteBorderColor = new Color(0.25f, 0.5f, 1f, 0.75f);
  8. static readonly int s_AdjustLinearForGamma = Shader.PropertyToID("_AdjustLinearForGamma");
  9. static Vector3[] s_VertexTmpCache;
  10. public static void BeginLines(Color color)
  11. {
  12. InternalEditorBridge.ApplyWireMaterial();
  13. GL.PushMatrix();
  14. GL.MultMatrix(Handles.matrix);
  15. GL.Begin(GL.LINES);
  16. GL.Color(color);
  17. }
  18. public static void BeginSolidLines()
  19. {
  20. InternalEditorBridge.ApplyWireMaterial();
  21. GL.PushMatrix();
  22. GL.MultMatrix(Handles.matrix);
  23. GL.Begin(GL.TRIANGLES);
  24. }
  25. public static void EndLines()
  26. {
  27. GL.End();
  28. GL.PopMatrix();
  29. }
  30. static void DrawLine(Vector3 p1, Vector3 p2)
  31. {
  32. GL.Vertex(p1);
  33. GL.Vertex(p2);
  34. }
  35. public static void DrawSolidLine(float width, Vector3 p1, Vector3 p2)
  36. {
  37. DrawSolidLine(p1, p2, Vector3.forward, width, width);
  38. }
  39. static void DrawSolidLine(Vector3 p1, Vector3 p2, Vector3 normal, float widthP1, float widthP2)
  40. {
  41. GL.Color(Handles.color);
  42. var right = Vector3.Cross(normal, p2 - p1).normalized;
  43. GL.Vertex(p1 + right * (widthP1 * 0.5f));
  44. GL.Vertex(p1 - right * (widthP1 * 0.5f));
  45. GL.Vertex(p2 - right * (widthP2 * 0.5f));
  46. GL.Vertex(p1 + right * (widthP1 * 0.5f));
  47. GL.Vertex(p2 - right * (widthP2 * 0.5f));
  48. GL.Vertex(p2 + right * (widthP2 * 0.5f));
  49. }
  50. public static void DrawBox(Rect position)
  51. {
  52. var points = new Vector3[5];
  53. var i = 0;
  54. points[i++] = new Vector3(position.xMin, position.yMin, 0f);
  55. points[i++] = new Vector3(position.xMax, position.yMin, 0f);
  56. points[i++] = new Vector3(position.xMax, position.yMax, 0f);
  57. points[i] = new Vector3(position.xMin, position.yMax, 0f);
  58. DrawLine(points[0], points[1]);
  59. DrawLine(points[1], points[2]);
  60. DrawLine(points[2], points[3]);
  61. DrawLine(points[3], points[0]);
  62. }
  63. public static void DrawMesh(Mesh mesh, Material material, Matrix4x4 matrix)
  64. {
  65. Debug.Assert(mesh != null);
  66. Debug.Assert(material != null);
  67. if (Event.current.type != EventType.Repaint)
  68. return;
  69. material.SetFloat(s_AdjustLinearForGamma, PlayerSettings.colorSpace == ColorSpace.Linear ? 1.0f : 0.0f);
  70. material.SetPass(0);
  71. Graphics.DrawMeshNow(mesh, Handles.matrix * matrix);
  72. }
  73. public static void DrawGUIStyleCap(int controlID, Vector3 position, Quaternion rotation, float size, GUIStyle guiStyle)
  74. {
  75. if (Event.current.type != EventType.Repaint)
  76. return;
  77. if (Camera.current && Vector3.Dot(position - Camera.current.transform.position, Camera.current.transform.forward) < 0f)
  78. return;
  79. Handles.BeginGUI();
  80. guiStyle.Draw(GetGUIStyleRect(guiStyle, position), GUIContent.none, controlID);
  81. Handles.EndGUI();
  82. }
  83. static Rect GetGUIStyleRect(GUIStyle style, Vector3 position)
  84. {
  85. var vector = HandleUtility.WorldToGUIPoint(position);
  86. var fixedWidth = style.fixedWidth;
  87. var fixedHeight = style.fixedHeight;
  88. return new Rect(vector.x - fixedWidth / 2f, vector.y - fixedHeight / 2f, fixedWidth, fixedHeight);
  89. }
  90. public static void DrawRect(Rect rect, Vector3 position, Quaternion rotation, Color color, float rectAlpha, float outlineAlpha)
  91. {
  92. if (Event.current.type != EventType.Repaint)
  93. return;
  94. var corners = new Vector3[4];
  95. for (var i = 0; i < 4; i++)
  96. {
  97. Vector3 point = GetLocalRectPoint(rect, i);
  98. corners[i] = rotation * point + position;
  99. }
  100. var points = new Vector3[]
  101. {
  102. corners[0],
  103. corners[1],
  104. corners[2],
  105. corners[3],
  106. corners[0]
  107. };
  108. var prevColor = Handles.color;
  109. Handles.color = color;
  110. var offset = new Vector2(1f, 1f);
  111. if (!Camera.current)
  112. {
  113. offset.y *= -1;
  114. }
  115. var faceColor = new Color(1f, 1f, 1f, rectAlpha);
  116. var outlineColor = new Color(1f, 1f, 1f, outlineAlpha);
  117. Handles.DrawSolidRectangleWithOutline(points, faceColor, outlineColor);
  118. Handles.color = prevColor;
  119. }
  120. static Vector2 GetLocalRectPoint(Rect rect, int index)
  121. {
  122. switch (index)
  123. {
  124. case 0: return new Vector2(rect.xMin, rect.yMax);
  125. case 1: return new Vector2(rect.xMax, rect.yMax);
  126. case 2: return new Vector2(rect.xMax, rect.yMin);
  127. case 3: return new Vector2(rect.xMin, rect.yMin);
  128. }
  129. return Vector3.zero;
  130. }
  131. }
  132. }