Drawing.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Reflection;
  3. using UnityEngine;
  4. public static class Drawing
  5. {
  6. static Drawing()
  7. {
  8. Drawing.Initialize();
  9. }
  10. public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
  11. {
  12. float num = pointB.x - pointA.x;
  13. float num2 = pointB.y - pointA.y;
  14. float num3 = Mathf.Sqrt(num * num + num2 * num2);
  15. if (num3 < 0.001f)
  16. {
  17. return;
  18. }
  19. Texture2D texture;
  20. Material mat;
  21. if (antiAlias)
  22. {
  23. width *= 3f;
  24. texture = Drawing.aaLineTex;
  25. mat = Drawing.blendMaterial;
  26. }
  27. else
  28. {
  29. texture = Drawing.lineTex;
  30. mat = Drawing.blitMaterial;
  31. }
  32. float num4 = width * num2 / num3;
  33. float num5 = width * num / num3;
  34. Matrix4x4 identity = Matrix4x4.identity;
  35. identity.m00 = num;
  36. identity.m01 = -num4;
  37. identity.m03 = pointA.x + 0.5f * num4;
  38. identity.m10 = num2;
  39. identity.m11 = num5;
  40. identity.m13 = pointA.y - 0.5f * num5;
  41. GL.PushMatrix();
  42. GL.MultMatrix(identity);
  43. Graphics.DrawTexture(Drawing.lineRect, texture, Drawing.lineRect, 0, 0, 0, 0, color, mat);
  44. GL.PopMatrix();
  45. }
  46. public static void DrawCircle(Vector2 center, int radius, Color color, float width, int segmentsPerQuarter)
  47. {
  48. Drawing.DrawCircle(center, radius, color, width, false, segmentsPerQuarter);
  49. }
  50. public static void DrawCircle(Vector2 center, int radius, Color color, float width, bool antiAlias, int segmentsPerQuarter)
  51. {
  52. float num = (float)radius / 2f;
  53. Vector2 vector = new Vector2(center.x, center.y - (float)radius);
  54. Vector2 endTangent = new Vector2(center.x - num, center.y - (float)radius);
  55. Vector2 startTangent = new Vector2(center.x + num, center.y - (float)radius);
  56. Vector2 vector2 = new Vector2(center.x + (float)radius, center.y);
  57. Vector2 endTangent2 = new Vector2(center.x + (float)radius, center.y - num);
  58. Vector2 startTangent2 = new Vector2(center.x + (float)radius, center.y + num);
  59. Vector2 vector3 = new Vector2(center.x, center.y + (float)radius);
  60. Vector2 startTangent3 = new Vector2(center.x - num, center.y + (float)radius);
  61. Vector2 endTangent3 = new Vector2(center.x + num, center.y + (float)radius);
  62. Vector2 vector4 = new Vector2(center.x - (float)radius, center.y);
  63. Vector2 startTangent4 = new Vector2(center.x - (float)radius, center.y - num);
  64. Vector2 endTangent4 = new Vector2(center.x - (float)radius, center.y + num);
  65. Drawing.DrawBezierLine(vector, startTangent, vector2, endTangent2, color, width, antiAlias, segmentsPerQuarter);
  66. Drawing.DrawBezierLine(vector2, startTangent2, vector3, endTangent3, color, width, antiAlias, segmentsPerQuarter);
  67. Drawing.DrawBezierLine(vector3, startTangent3, vector4, endTangent4, color, width, antiAlias, segmentsPerQuarter);
  68. Drawing.DrawBezierLine(vector4, startTangent4, vector, endTangent, color, width, antiAlias, segmentsPerQuarter);
  69. }
  70. public static void DrawBezierLine(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, bool antiAlias, int segments)
  71. {
  72. Vector2 pointA = Drawing.CubeBezier(start, startTangent, end, endTangent, 0f);
  73. for (int i = 1; i < segments + 1; i++)
  74. {
  75. Vector2 vector = Drawing.CubeBezier(start, startTangent, end, endTangent, (float)i / (float)segments);
  76. Drawing.DrawLine(pointA, vector, color, width, antiAlias);
  77. pointA = vector;
  78. }
  79. }
  80. private static Vector2 CubeBezier(Vector2 s, Vector2 st, Vector2 e, Vector2 et, float t)
  81. {
  82. float num = 1f - t;
  83. return num * num * num * s + 3f * num * num * t * st + 3f * num * t * t * et + t * t * t * e;
  84. }
  85. private static void Initialize()
  86. {
  87. if (Drawing.lineTex == null)
  88. {
  89. Drawing.lineTex = new Texture2D(1, 1, TextureFormat.ARGB32, false);
  90. Drawing.lineTex.SetPixel(0, 1, Color.white);
  91. Drawing.lineTex.Apply();
  92. }
  93. if (Drawing.aaLineTex == null)
  94. {
  95. Drawing.aaLineTex = new Texture2D(1, 3, TextureFormat.ARGB32, false);
  96. Drawing.aaLineTex.SetPixel(0, 0, new Color(1f, 1f, 1f, 0f));
  97. Drawing.aaLineTex.SetPixel(0, 1, Color.white);
  98. Drawing.aaLineTex.SetPixel(0, 2, new Color(1f, 1f, 1f, 0f));
  99. Drawing.aaLineTex.Apply();
  100. }
  101. Drawing.blitMaterial = (Material)typeof(GUI).GetMethod("get_blitMaterial", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, null);
  102. Drawing.blendMaterial = (Material)typeof(GUI).GetMethod("get_blendMaterial", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, null);
  103. }
  104. private static Texture2D aaLineTex = null;
  105. private static Texture2D lineTex = null;
  106. private static Material blitMaterial = null;
  107. private static Material blendMaterial = null;
  108. private static Rect lineRect = new Rect(0f, 0f, 1f, 1f);
  109. }