Drawing_tc1.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using System.Collections;
  4. using System;
  5. namespace WorldComposer
  6. {
  7. public static class Drawing_tc1
  8. {
  9. [Serializable]
  10. public class clip_class
  11. {
  12. public float u1;
  13. public float u2;
  14. public clip_class(float start, float end)
  15. {
  16. u1 = start;
  17. u2 = end;
  18. }
  19. }
  20. [Serializable]
  21. public class point_class
  22. {
  23. public Vector2 p1;
  24. public Vector2 p2;
  25. public point_class(Vector2 start, Vector2 end)
  26. {
  27. p1 = start;
  28. p2 = end;
  29. }
  30. }
  31. public static Texture2D aaLineTex;
  32. public static Texture2D lineTex;
  33. public static bool clippingEnabled = true;
  34. public static Rect clippingBounds;
  35. public static Material lineMaterial;
  36. static Drawing_tc1()
  37. {
  38. aaLineTex = new Texture2D(1, 3, TextureFormat.ARGB32, true);
  39. aaLineTex.SetPixel(0, 0, new Color(1, 1, 1, 0));
  40. aaLineTex.SetPixel(0, 1, Color.white);
  41. aaLineTex.SetPixel(0, 2, new Color(1, 1, 1, 0));
  42. aaLineTex.Apply();
  43. lineTex = new Texture2D(1, 1, TextureFormat.ARGB32, true);
  44. lineTex.SetPixel(0, 1, Color.white);
  45. lineTex.Apply();
  46. }
  47. public static void DrawLineMac(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
  48. {
  49. Color color2 = GUI.color;
  50. Matrix4x4 matrix = GUI.matrix;
  51. float num = width;
  52. if (antiAlias)
  53. {
  54. width *= 3;
  55. }
  56. float num2 = Vector3.Angle(pointB - pointA, Vector2.right) * ((pointA.y > pointB.y) ? -1 : 1);
  57. float magnitude = (pointB - pointA).magnitude;
  58. if (magnitude > 0.01f)
  59. {
  60. Vector3 vector = new Vector3(pointA.x, pointA.y, 0);
  61. Vector3 vector2 = new Vector3((pointB.x - pointA.x) * 0.5f, (pointB.y - pointA.y) * 0.5f, 0);
  62. Vector3 vector3 = Vector3.zero;
  63. if (antiAlias)
  64. {
  65. vector3 = new Vector3(-num * 1.5f * Mathf.Sin(num2 * 0.0174532924f), num * 1.5f * Mathf.Cos(num2 * 0.0174532924f));
  66. }
  67. else
  68. {
  69. vector3 = new Vector3(-num * 0.5f * Mathf.Sin(num2 * 0.0174532924f), num * 0.5f * Mathf.Cos(num2 * 0.0174532924f));
  70. }
  71. GUI.color = color;
  72. GUI.matrix = translationMatrix(vector) * GUI.matrix;
  73. GUIUtility.ScaleAroundPivot(new Vector2(magnitude, width), new Vector2(-0.5f, 0));
  74. GUI.matrix = translationMatrix(-vector) * GUI.matrix;
  75. GUIUtility.RotateAroundPivot(num2, Vector2.zero);
  76. GUI.matrix = translationMatrix(vector - vector3 - vector2) * GUI.matrix;
  77. if (antiAlias)
  78. {
  79. GUI.DrawTexture(new Rect(0, 0, 1, 1), Drawing_tc1.aaLineTex);
  80. }
  81. else
  82. {
  83. GUI.DrawTexture(new Rect(0, 0, 1, 1), Drawing_tc1.lineTex);
  84. }
  85. }
  86. GUI.matrix = matrix;
  87. GUI.color = color2;
  88. }
  89. public static void DrawLineWindows(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
  90. {
  91. Color color2 = GUI.color;
  92. Matrix4x4 matrix = GUI.matrix;
  93. if (antiAlias)
  94. {
  95. width *= 3;
  96. }
  97. float num = Vector3.Angle(pointB - pointA, Vector2.right) * ((pointA.y > pointB.y) ? -1 : 1);
  98. float magnitude = (pointB - pointA).magnitude;
  99. Vector3 vector = new Vector3(pointA.x, pointA.y, 0);
  100. GUI.color = color;
  101. GUI.matrix = translationMatrix(vector) * GUI.matrix;
  102. GUIUtility.ScaleAroundPivot(new Vector2(magnitude, width), new Vector2(-0.5f, 0));
  103. GUI.matrix = translationMatrix(-vector) * GUI.matrix;
  104. GUIUtility.RotateAroundPivot(num, new Vector2(0, 0));
  105. GUI.matrix = translationMatrix(vector + new Vector3(width / 2, -magnitude / 2) * Mathf.Sin(num * 0.0174532924f)) * GUI.matrix;
  106. if (!antiAlias)
  107. {
  108. GUI.DrawTexture(new Rect(0, 0, 1, 1), lineTex);
  109. }
  110. else
  111. {
  112. GUI.DrawTexture(new Rect(0, 0, 1, 1), aaLineTex);
  113. }
  114. GUI.matrix = matrix;
  115. GUI.color = color2;
  116. }
  117. public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias, Rect screen)
  118. {
  119. clippingBounds = screen;
  120. DrawLine(pointA, pointB, color, width);
  121. }
  122. public static void curveOutIn(Rect wr, Rect wr2, Color color, Color shadow, int width, Rect screen)
  123. {
  124. BezierLine(new Vector2(wr.x + wr.width, wr.y + width + wr.height / 2), new Vector2(wr.x + wr.width + Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2, wr.y + width + wr.height / 2), new Vector2(wr2.x, wr2.y + width + wr2.height / 2), new Vector2(wr2.x - Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2, wr2.y + width + wr2.height / 2), shadow, width, true, 20, screen);
  125. BezierLine(new Vector2(wr.x + wr.width, wr.y + wr.height / 2), new Vector2(wr.x + wr.width + Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2, wr.y + wr.height / 2), new Vector2(wr2.x, wr2.y + wr2.height / 2), new Vector2(wr2.x - Mathf.Abs(wr2.x - (wr.x + wr.width)) / 2, wr2.y + wr2.height / 2), color, width, true, 20, screen);
  126. }
  127. public static void BezierLine(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, bool antiAlias, int segments, Rect screen)
  128. {
  129. Vector2 pointA = cubeBezier(start, startTangent, end, endTangent, 0);
  130. for (int i = 1; i <= segments; i++)
  131. {
  132. Vector2 vector = cubeBezier(start, startTangent, end, endTangent, (i / segments));
  133. DrawLine(pointA, vector, color, width, antiAlias, screen);
  134. pointA = vector;
  135. }
  136. }
  137. public static Vector2 cubeBezier(Vector2 s, Vector2 st, Vector2 e, Vector2 et, float t)
  138. {
  139. float num = 1 - t;
  140. return s * num * num * num + 3 * st * num * num * t + 3 * et * num * t * t + e * t * t * t;
  141. }
  142. public static Matrix4x4 translationMatrix(Vector3 v)
  143. {
  144. return Matrix4x4.TRS(v, Quaternion.identity, Vector3.one);
  145. }
  146. public static bool clip_test(float p, float q, clip_class u)
  147. {
  148. float num;
  149. bool result = true;
  150. if (p < 0)
  151. {
  152. num = q / p;
  153. if (num > u.u2)
  154. {
  155. result = false;
  156. }
  157. else if (num > u.u1)
  158. {
  159. u.u1 = num;
  160. }
  161. }
  162. else if (p > 0)
  163. {
  164. num = q / p;
  165. if (num < u.u1)
  166. {
  167. result = false;
  168. }
  169. else if (num < u.u2)
  170. {
  171. u.u2 = num;
  172. }
  173. }
  174. else if (q < 0)
  175. {
  176. result = false;
  177. }
  178. return result;
  179. }
  180. public static bool segment_rect_intersection(Rect bounds, point_class p)
  181. {
  182. float num = p.p2.x - p.p1.x;
  183. float num2;
  184. clip_class clip_class = new clip_class(0, 1f);
  185. int arg_173_0;
  186. if (clip_test(-num, p.p1.x - bounds.xMin, clip_class) && clip_test(num, bounds.xMax - p.p1.x, clip_class))
  187. {
  188. num2 = p.p2.y - p.p1.y;
  189. if (clip_test(-num2, p.p1.y - bounds.yMin, clip_class) && clip_test(num2, bounds.yMax - p.p1.y, clip_class))
  190. {
  191. if (clip_class.u2 < 1f)
  192. {
  193. p.p2.x = p.p1.x + clip_class.u2 * num;
  194. p.p2.y = p.p1.y + clip_class.u2 * num2;
  195. }
  196. if (clip_class.u1 > 0)
  197. {
  198. p.p1.x = p.p1.x + clip_class.u1 * num;
  199. p.p1.y = p.p1.y + clip_class.u1 * num2;
  200. }
  201. arg_173_0 = 1;
  202. return arg_173_0 != 0;
  203. }
  204. }
  205. arg_173_0 = 0;
  206. return arg_173_0 != 0;
  207. }
  208. public static void BeginGroup(Rect position)
  209. {
  210. clippingEnabled = true;
  211. clippingBounds = new Rect(0, 0, position.width, position.height);
  212. GUI.BeginGroup(position);
  213. }
  214. public static void EndGroup()
  215. {
  216. GUI.EndGroup();
  217. clippingBounds = new Rect(0, 0, Screen.width, Screen.height);
  218. clippingEnabled = false;
  219. }
  220. public static void DrawLine(Vector2 start, Vector2 end, Color color, float width)
  221. {
  222. if (Event.current == null || Event.current.type != EventType.Repaint) return;
  223. // point_class point_class = new point_class(start, end);
  224. lineMaterial.SetPass(0);
  225. Vector3 vector, vector2;
  226. if (width == 1)
  227. {
  228. GL.Begin(1);
  229. GL.Color(color);
  230. vector = new Vector3(start.x, start.y, 0);
  231. vector2 = new Vector3(end.x, end.y, 0);
  232. GL.Vertex(vector);
  233. GL.Vertex(vector2);
  234. }
  235. else
  236. {
  237. GL.Begin(7);
  238. GL.Color(color);
  239. vector = new Vector3(end.y, start.x, 0);
  240. vector2 = new Vector3(start.y, end.x, 0);
  241. Vector3 vector3 = (vector - vector2).normalized * width / 2f;
  242. Vector3 vector4 = new Vector3(start.x, start.y, 0);
  243. Vector3 vector5 = new Vector3(end.x, end.y, 0);
  244. GL.Vertex(vector4 - vector3);
  245. GL.Vertex(vector4 + vector3);
  246. GL.Vertex(vector5 + vector3);
  247. GL.Vertex(vector5 - vector3);
  248. }
  249. GL.End();
  250. }
  251. public static void DrawBox(Rect box, Color color, float width)
  252. {
  253. Vector2 vector = new Vector2(box.xMin, box.yMin);
  254. Vector2 vector2 = new Vector2(box.xMax, box.yMin);
  255. Vector2 vector3 = new Vector2(box.xMax, box.yMax);
  256. Vector2 vector4 = new Vector2(box.xMin, box.yMax);
  257. DrawLine(vector, vector2, color, width);
  258. DrawLine(vector2, vector3, color, width);
  259. DrawLine(vector3, vector4, color, width);
  260. DrawLine(vector4, vector, color, width);
  261. }
  262. public static void DrawBox(Vector2 topLeftCorner, Vector2 bottomRightCorner, Color color, float width)
  263. {
  264. Rect box = new Rect(topLeftCorner.x, topLeftCorner.y, bottomRightCorner.x - topLeftCorner.x, bottomRightCorner.y - topLeftCorner.y);
  265. DrawBox(box, color, width);
  266. }
  267. public static void DrawRoundedBox(Rect box, float radius, Color color, float width)
  268. {
  269. Vector2 vector, vector2, vector3, vector4, vector5, vector6, vector7, vector8;
  270. vector = new Vector2(box.xMin + radius, box.yMin);
  271. vector2 = new Vector2(box.xMax - radius, box.yMin);
  272. vector3 = new Vector2(box.xMax, box.yMin + radius);
  273. vector4 = new Vector2(box.xMax, box.yMax - radius);
  274. vector5 = new Vector2(box.xMax - radius, box.yMax);
  275. vector6 = new Vector2(box.xMin + radius, box.yMax);
  276. vector7 = new Vector2(box.xMin, box.yMax - radius);
  277. vector8 = new Vector2(box.xMin, box.yMin + radius);
  278. DrawLine(vector, vector2, color, width);
  279. DrawLine(vector3, vector4, color, width);
  280. DrawLine(vector5, vector6, color, width);
  281. DrawLine(vector7, vector8, color, width);
  282. Vector2 startTangent;
  283. Vector2 endTangent;
  284. float num = radius / 2;
  285. startTangent = new Vector2(vector8.x, vector8.y + num);
  286. endTangent = new Vector2(vector.x - num, vector.y);
  287. DrawBezier(vector8, startTangent, vector, endTangent, color, width);
  288. startTangent = new Vector2(vector2.x + num, vector2.y);
  289. endTangent = new Vector2(vector3.x, vector3.y - num);
  290. DrawBezier(vector2, startTangent, vector3, endTangent, color, width);
  291. startTangent = new Vector2(vector4.x, vector4.y + num);
  292. endTangent = new Vector2(vector5.x + num, vector5.y);
  293. DrawBezier(vector4, startTangent, vector5, endTangent, color, width);
  294. startTangent = new Vector2(vector6.x - num, vector6.y);
  295. endTangent = new Vector2(vector7.x, vector7.y + num);
  296. DrawBezier(vector6, startTangent, vector7, endTangent, color, width);
  297. }
  298. public static void DrawConnectingCurve(Vector2 start, Vector2 end, Color color, float width)
  299. {
  300. Vector2 vector = start - end;
  301. Vector2 startTangent = start;
  302. startTangent.x -= (vector / 2).x;
  303. Vector2 endTangent = end;
  304. endTangent.x += (vector / 2).x;
  305. int segments = Mathf.FloorToInt((vector.magnitude / 20) * 3);
  306. DrawBezier(start, startTangent, end, endTangent, color, width, segments);
  307. }
  308. public static void DrawBezier(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width)
  309. {
  310. int segments = Mathf.FloorToInt((start - end).magnitude / 20) * 3;
  311. DrawBezier(start, startTangent, end, endTangent, color, width, segments);
  312. }
  313. public static void DrawBezier(Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, int segments)
  314. {
  315. Vector2 start2 = CubeBezier(start, startTangent, end, endTangent, 0);
  316. for (int i = 1; i <= segments; i++)
  317. {
  318. Vector2 vector = CubeBezier(start, startTangent, end, endTangent, (i / segments));
  319. DrawLine(start2, vector, color, width);
  320. start2 = vector;
  321. }
  322. }
  323. public static Vector2 CubeBezier(Vector2 s, Vector2 st, Vector2 e, Vector2 et, float t)
  324. {
  325. float num = 1 - t;
  326. float num2 = num * t;
  327. return num * num * num * s + 3 * num * num2 * st + 3 * num2 * t * et + t * t * t * e;
  328. }
  329. }
  330. }
  331. #endif