Advertisement
Guest User

Untitled

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