Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.83 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class Max2D
  5. {
  6. private static Material lineMaterial;
  7. private static Material lineEndMaterial;
  8. private static Material defaultMaterial;
  9.  
  10. private static float lineWidth = 0.2f;
  11. private static bool smooth = false;
  12. private static bool setBorder = false;
  13. private static Color setColor = Color.white;
  14.  
  15. static public void SetBorder(bool border)
  16. {
  17. setBorder = border;
  18. }
  19.  
  20. static public void SetSmooth(bool _smooth)
  21. {
  22. smooth = _smooth;
  23. }
  24.  
  25. public static void SetLineWidth(float size)
  26. {
  27. lineWidth = Mathf.Max(.01f, size / 5f);
  28. }
  29.  
  30. static public void SetColor(Color color)
  31. {
  32. Check();
  33. lineMaterial.SetColor("_Emission", color);
  34. lineEndMaterial.SetColor("_Emission", color);
  35. setColor = color;
  36. }
  37.  
  38. private static void Check()
  39. {
  40. if (lineMaterial == null || lineEndMaterial == null)
  41. {
  42. lineMaterial = new Material(Shader.Find("Legacy Shaders/Transparent/VertexLit"));
  43. lineMaterial.mainTexture = Resources.Load("Textures/LineTexture") as Texture;
  44. lineEndMaterial = new Material(Shader.Find("Legacy Shaders/Transparent/VertexLit"));
  45. lineEndMaterial.mainTexture = Resources.Load("Textures/LineEndTexture") as Texture;
  46. lineEndMaterial.mainTextureScale = new Vector2(0.52f, 1f);
  47. lineEndMaterial.mainTextureOffset = new Vector2(-0.58f, 0f);
  48. lineMaterial.SetInt("_ZWrite", 0);
  49. lineEndMaterial.SetInt("_ZWrite", 0);
  50. }
  51. if (defaultMaterial == null)
  52. {
  53. Shader shader = Shader.Find("Hidden/Internal-Colored");
  54. defaultMaterial = new Material(shader);
  55. defaultMaterial.hideFlags = HideFlags.HideAndDontSave;
  56. defaultMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  57. defaultMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  58. defaultMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
  59. defaultMaterial.SetInt("_ZWrite", 0);
  60. }
  61. }
  62.  
  63. static public void iDrawMesh(Mesh mesh, Transform transform, Vector2f offset, float z = 0f)
  64. {
  65. if (mesh == null)
  66. return;
  67.  
  68. List<Vector2> list = new List<Vector2>();
  69. for (int i = 0; i < mesh.triangles.GetLength(0); i++)
  70. {
  71. list.Add(transform.TransformPoint(mesh.vertices[mesh.triangles[i]]));
  72. if (list.Count > 2)
  73. {
  74. DrawTriangle(new Vector2f(list[0]), new Vector2f(list[1]), new Vector2f(list[2]), offset, z);
  75. list.Clear();
  76. }
  77. }
  78. }
  79.  
  80. static public void iDrawImage(Material material, Vector2f pos, Vector2f size, float z = 0f)
  81. {
  82. GL.PushMatrix();
  83. material.SetPass(0);
  84.  
  85. GL.Begin(GL.QUADS);
  86. GL.TexCoord2(0, 0);
  87. GL.Vertex3(pos.GetX() - size.GetX(), pos.GetY() - size.GetY(), z);
  88. GL.TexCoord2(0, 1);
  89. GL.Vertex3(pos.GetX() - size.GetX(), pos.GetY() + size.GetY(), z);
  90. GL.TexCoord2(1, 1);
  91. GL.Vertex3(pos.GetX() + size.GetX(), pos.GetY() + size.GetY(), z);
  92. GL.TexCoord2(1, 0);
  93. GL.Vertex3(pos.GetX() + size.GetX(), pos.GetY() - size.GetY(), z);
  94. GL.End();
  95.  
  96. GL.PopMatrix();
  97. }
  98.  
  99. static public void iDrawLine(float x0, float y0, float x1, float y1, float z = 0f)
  100. {
  101. Check();
  102.  
  103. if (smooth == true)
  104. DrawSmoothLine(new Pair2f(new Vector2f(x0, y0), new Vector2f(x1, y1)), z);
  105. else
  106. {
  107. GL.PushMatrix();
  108. defaultMaterial.SetPass(0);
  109. GL.Begin(GL.LINES);
  110. GL.Color(setColor);
  111. GL.Vertex3(x0, y0, z);
  112. GL.Vertex3(x1, y1, z);
  113. GL.End();
  114. GL.PopMatrix();
  115. }
  116. }
  117.  
  118. static public void DrawTriangle(Vector2f p0, Vector2f p1, Vector2f p2, Vector2f offset, float z = 0f)
  119. {
  120. iDrawTriangle(p0.GetX(), p0.GetY(), p1.GetX(), p1.GetY(), p2.GetX(), p2.GetY(), offset, z);
  121. }
  122.  
  123. static public void iDrawTriangle(float x0, float y0, float x1, float y1, float x2, float y2, Vector2f offset, float z = 0f)
  124. {
  125. GL.PushMatrix();
  126. defaultMaterial.SetPass(0);
  127. GL.Begin(GL.TRIANGLES);
  128. GL.Color(setColor);
  129. GL.Vertex3(x0 + offset.GetX(), y0 + offset.GetY(), z);
  130. GL.Vertex3(x1 + offset.GetX(), y1 + offset.GetY(), z);
  131. GL.Vertex3(x2 + offset.GetX(), y2 + offset.GetY(), z);
  132. GL.End();
  133. GL.PopMatrix();
  134. }
  135.  
  136. static public void Vertex3(Vector2f p, float z = 0f)
  137. {
  138. GL.Vertex3(p.GetX(), p.GetY(), z);
  139. }
  140.  
  141. static public void iDrawRect(float x, float y, float w, float h, float z = 0f)
  142. {
  143. DrawLine(new Vector2f(x, y), new Vector2f(x + w, y), z);
  144. DrawLine(new Vector2f(x + w, y), new Vector2f(x + w, y + h), z);
  145. DrawLine(new Vector2f(x + w, y + h), new Vector2f(x, y + h), z);
  146. DrawLine(new Vector2f(x, y + h), new Vector2f(x, y), z);
  147. }
  148.  
  149. static public void DrawSquare(Vector2f p, float size, float z = 0f)
  150. {
  151. iDrawRect(p.GetX() - size / 2f, p.GetY() - size / 2f, size, size, z);
  152. }
  153.  
  154. static public void DrawSquareFilled(Vector2f p, float size, float z = 0f)
  155. {
  156. Vector2f p0 = new Vector2f(p.GetX() - size, p.GetY() - size);
  157. Vector2f p1 = new Vector2f(p.GetX() + size, p.GetY() - size);
  158. Vector2f p2 = new Vector2f(p.GetX() + size, p.GetY() + size);
  159. Vector2f p3 = new Vector2f(p.GetX() - size, p.GetY() + size);
  160.  
  161. DrawTriangle(p0, p1, p2, new Vector2f(0, 0), z);
  162. DrawTriangle(p2, p3, p0, new Vector2f(0, 0), z);
  163. }
  164.  
  165. static public void DrawLine(Vector2f p0, Vector2f p1, float z = 0f)
  166. {
  167. if (setBorder == true)
  168. {
  169. Color tmcColor = setColor;
  170. float tmpWidth = lineWidth;
  171. SetColor(Color.black);
  172. SetLineWidth(1);
  173. iDrawLine(p0.GetX(), p0.GetY(), p1.GetX(), p1.GetY(), z);
  174. SetColor(tmcColor);
  175. SetLineWidth(0.5f);
  176. iDrawLine(p0.GetX(), p0.GetY(), p1.GetX(), p1.GetY(), z);
  177. SetLineWidth(tmpWidth);
  178. }
  179. else
  180. {
  181. iDrawLine(p0.GetX(), p0.GetY(), p1.GetX(), p1.GetY(), z);
  182. }
  183. }
  184.  
  185. static public void DrawSlice(List<Vector2f> slice, float z = 0f)
  186. {
  187. foreach (Pair2f p in Pair2f.GetList(slice, false))
  188. DrawLine(p.A, p.B, z);
  189. }
  190.  
  191. static public void DrawPolygonList(List<Polygon> polyList, float z = 0f)
  192. {
  193. foreach (Polygon p in polyList)
  194. DrawPolygon(p, z);
  195. }
  196.  
  197. static public void DrawStrippedLine(List<Vector2f> pointsList, float minVertsDistance, float z = 0f, bool full = false, Vector2f offset = null)
  198. {
  199. if (offset == null)
  200. offset = new Vector2f(0, 0);
  201.  
  202. GL.PushMatrix();
  203. lineMaterial.SetPass(0);
  204. GL.Begin(GL.QUADS);
  205.  
  206. Vector2f vA = null, vB = null;
  207. foreach (Pair2f id in Pair2f.GetList(pointsList, full))
  208. {
  209. vA = new Vector2f(id.A.Get() + offset.Get());
  210. vB = new Vector2f(id.B.Get() + offset.Get());
  211.  
  212. vA.Push(Vector2f.Atan2(id.A, id.B), -minVertsDistance / 4);
  213. vB.Push(Vector2f.Atan2(id.A, id.B), minVertsDistance / 4);
  214.  
  215. //DrawLine (vA, vB, z);
  216. //DrawSmoothLine(new Pair2f(vA, vB), z);
  217. DrawSmoothLine_Algorithm(new Pair2f(vA, vB), z);
  218. }
  219.  
  220. GL.End();
  221. GL.PopMatrix();
  222. }
  223.  
  224. static public void DrawSmoothLine(Pair2f pair, float z = 0f)
  225. {
  226. GL.PushMatrix();
  227. lineMaterial.SetPass(0);
  228.  
  229. GL.Begin(GL.QUADS);
  230.  
  231. DrawSmoothLine_Algorithm(pair, z);
  232.  
  233. GL.End();
  234. GL.PopMatrix();
  235. }
  236.  
  237. static public void DrawPolygon(Polygon poly, float z = 0f)
  238. {
  239. Check();
  240.  
  241. if (smooth)
  242. {
  243. GL.PushMatrix();
  244. lineMaterial.SetPass(0);
  245. GL.Begin(GL.QUADS);
  246.  
  247. foreach (Pair2f p in Pair2f.GetList(poly.pointsList))
  248. DrawSmoothLine_Algorithm(p, z);
  249.  
  250. GL.End();
  251. GL.PopMatrix();
  252. }
  253. else
  254. {
  255. GL.PushMatrix();
  256. defaultMaterial.SetPass(0);
  257. GL.Begin(GL.LINES);
  258. GL.Color(setColor);
  259.  
  260. foreach (Pair2f p in Pair2f.GetList(poly.pointsList))
  261. {
  262. GL.Vertex3(p.A.GetX(), p.A.GetY(), z);
  263. GL.Vertex3(p.B.GetX(), p.B.GetY(), z);
  264. }
  265.  
  266. GL.End();
  267. GL.PopMatrix();
  268. }
  269.  
  270. foreach (Polygon p in poly.holesList)
  271. DrawPolygon(p, z);
  272. }
  273.  
  274. private static void DrawSmoothLine_Algorithm(Pair2f pair, float z = 0f)
  275. {
  276. float size = lineWidth;
  277. float pi2 = Mathf.PI / 2;
  278. float pi = Mathf.PI;
  279.  
  280. float rot = Vector2f.Atan2(pair.A, pair.B);
  281.  
  282. Vector2f A1 = new Vector2f(pair.A);
  283. Vector2f A2 = new Vector2f(pair.A);
  284. Vector2f A3 = new Vector2f(pair.A);
  285. Vector2f A4 = new Vector2f(pair.A);
  286.  
  287. Vector2f B1 = new Vector2f(pair.B);
  288. Vector2f B2 = new Vector2f(pair.B);
  289. Vector2f B3 = new Vector2f(pair.B);
  290. Vector2f B4 = new Vector2f(pair.B);
  291.  
  292. A1.Push(rot + pi2, size);
  293. A2.Push(rot - pi2, size);
  294.  
  295. A3.Push(rot + pi2, size);
  296. A4.Push(rot - pi2, size);
  297. A3.Push(rot + pi, -size);
  298. A4.Push(rot + pi, -size);
  299.  
  300. B1.Push(rot + pi2, size);
  301. B2.Push(rot - pi2, size);
  302.  
  303. B3.Push(rot + pi2, size);
  304. B4.Push(rot - pi2, size);
  305. B3.Push(rot + pi, size);
  306. B4.Push(rot + pi, size);
  307.  
  308. GL.TexCoord2(0, 0);
  309. GL.Vertex3(B1.GetX(), B1.GetY(), z);
  310. GL.TexCoord2(1, 0);
  311. GL.Vertex3(A1.GetX(), A1.GetY(), z);
  312. GL.TexCoord2(1, 1);
  313. GL.Vertex3(A2.GetX(), A2.GetY(), z);
  314. GL.TexCoord2(0, 1);
  315. GL.Vertex3(B2.GetX(), B2.GetY(), z);
  316. }
  317. }
  318.  
  319. /*lineEndMaterial.SetPass(0);
  320.  
  321. GL.Begin(GL.QUADS);
  322.  
  323. GL.TexCoord2(0, 0);
  324. GL.Vertex3(A3.GetX(), A3.GetY(), z);
  325. GL.TexCoord2(0, 1);
  326. GL.Vertex3(A4.GetX(), A4.GetY(), z);
  327. GL.TexCoord2(1, 1);
  328. GL.Vertex3(A2.GetX(), A2.GetY(), z);
  329. GL.TexCoord2(1, 0);
  330. GL.Vertex3(A1.GetX(), A1.GetY(), z);
  331. GL.TexCoord2(0, 0);
  332. GL.Vertex3(B4.GetX(), B4.GetY(), z);
  333. GL.TexCoord2(0, 1);
  334. GL.Vertex3(B3.GetX(), B3.GetY(), z);
  335. GL.TexCoord2(1, 1);
  336. GL.Vertex3(B1.GetX(), B1.GetY(), z);
  337. GL.TexCoord2(1, 0);
  338. GL.Vertex3(B2.GetX(), B2.GetY(), z);
  339.  
  340. GL.End();*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement