Advertisement
Guest User

Untitled

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