Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.26 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 = 0f)
  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 = 0f)
  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 = 0f, bool full = false, Vector2f offset = null)
  189.     {
  190.         if (offset == null)
  191.             offset = new Vector2f (0, 0);
  192.  
  193.         GL.PushMatrix();
  194.         lineMaterial.SetPass(0);
  195.         GL.Begin(GL.QUADS);
  196.  
  197.         Vector2f vA = null, vB = null;
  198.         foreach (Pair2f id in Pair2f.GetList(pointsList, full)) {
  199.             vA = new Vector2f (id.A.Get () + offset.Get());
  200.             vB = new Vector2f (id.B.Get () + offset.Get());
  201.  
  202.             vA.Push (Vector2f.Atan2 (id.A, id.B), -minVertsDistance / 4);
  203.             vB.Push (Vector2f.Atan2 (id.A, id.B), minVertsDistance / 4);
  204.  
  205.             //DrawLine (vA, vB, z);
  206.             //DrawSmoothLine(new Pair2f(vA, vB), z);
  207.             DrawSmoothLine_Algorithm (new Pair2f(vA, vB), z);
  208.         }
  209.  
  210.         GL.End();
  211.         GL.PopMatrix();
  212.     }
  213.  
  214.     static public void DrawSmoothLine(Pair2f pair, float z = 0f)
  215.     {
  216.         GL.PushMatrix();
  217.         lineMaterial.SetPass(0);
  218.  
  219.         GL.Begin(GL.QUADS);
  220.  
  221.         DrawSmoothLine_Algorithm (pair, z);
  222.  
  223.         GL.End();
  224.         GL.PopMatrix();
  225.     }
  226.  
  227.     static public void DrawPolygon(Polygon poly, float z = 0f)
  228.     {
  229.         Check ();
  230.  
  231.         if (smooth) {
  232.             GL.PushMatrix ();
  233.             lineMaterial.SetPass(0);
  234.             GL.Begin(GL.QUADS);
  235.  
  236.             foreach (Pair2f p in Pair2f.GetList(poly.pointsList))
  237.                 DrawSmoothLine_Algorithm (p, z);
  238.  
  239.             GL.End();
  240.             GL.PopMatrix();
  241.         } else {
  242.             GL.PushMatrix();
  243.             defaultMaterial.SetPass(0);
  244.             GL.Begin(GL.LINES);
  245.             GL.Color(setColor);
  246.  
  247.             foreach (Pair2f p in Pair2f.GetList(poly.pointsList)) {
  248.                 GL.Vertex3(p.A.GetX(), p.A.GetY(), z);
  249.                 GL.Vertex3(p.B.GetX(), p.B.GetY(), z);
  250.             }
  251.  
  252.             GL.End ();
  253.             GL.PopMatrix();
  254.         }
  255.  
  256.         foreach (Polygon p in poly.holesList)
  257.             DrawPolygon (p, z);
  258.     }
  259.  
  260.     private static void DrawSmoothLine_Algorithm(Pair2f pair, float z = 0f)
  261.     {
  262.         float size = lineWidth;
  263.         float pi2 = Mathf.PI / 2;
  264.         float pi = Mathf.PI;
  265.  
  266.         float rot = Vector2f.Atan2 (pair.A, pair.B);
  267.  
  268.         Vector2f A1 = new Vector2f (pair.A);
  269.         Vector2f A2 = new Vector2f (pair.A);
  270.         Vector2f A3 = new Vector2f (pair.A);
  271.         Vector2f A4 = new Vector2f (pair.A);
  272.  
  273.         Vector2f B1 = new Vector2f (pair.B);
  274.         Vector2f B2 = new Vector2f (pair.B);
  275.         Vector2f B3 = new Vector2f (pair.B);
  276.         Vector2f B4 = new Vector2f (pair.B);
  277.  
  278.         A1.Push (rot + pi2, size);
  279.         A2.Push (rot - pi2, size);
  280.  
  281.         A3.Push (rot + pi2, size);
  282.         A4.Push (rot - pi2, size);
  283.         A3.Push (rot + pi, -size);
  284.         A4.Push (rot + pi, -size);
  285.  
  286.         B1.Push (rot + pi2, size);
  287.         B2.Push (rot - pi2, size);
  288.  
  289.         B3.Push (rot + pi2, size);
  290.         B4.Push (rot - pi2, size);
  291.         B3.Push (rot + pi, size);
  292.         B4.Push (rot + pi , size);
  293.  
  294.         GL.TexCoord2(0, 0);
  295.         GL.Vertex3(B1.GetX(), B1.GetY(), z);
  296.         GL.TexCoord2(1, 0);
  297.         GL.Vertex3(A1.GetX(), A1.GetY(), z);
  298.         GL.TexCoord2(1, 1);
  299.         GL.Vertex3(A2.GetX(), A2.GetY(), z);
  300.         GL.TexCoord2(0, 1);
  301.         GL.Vertex3(B2.GetX(), B2.GetY(), z);
  302.     }
  303. }
  304.    
  305. /*lineEndMaterial.SetPass(0);
  306.  
  307.         GL.Begin(GL.QUADS);
  308.  
  309.         GL.TexCoord2(0, 0);
  310.         GL.Vertex3(A3.GetX(), A3.GetY(), z);
  311.         GL.TexCoord2(0, 1);
  312.         GL.Vertex3(A4.GetX(), A4.GetY(), z);
  313.         GL.TexCoord2(1, 1);
  314.         GL.Vertex3(A2.GetX(), A2.GetY(), z);
  315.         GL.TexCoord2(1, 0);
  316.         GL.Vertex3(A1.GetX(), A1.GetY(), z);
  317.         GL.TexCoord2(0, 0);
  318.         GL.Vertex3(B4.GetX(), B4.GetY(), z);
  319.         GL.TexCoord2(0, 1);
  320.         GL.Vertex3(B3.GetX(), B3.GetY(), z);
  321.         GL.TexCoord2(1, 1);
  322.         GL.Vertex3(B1.GetX(), B1.GetY(), z);
  323.         GL.TexCoord2(1, 0);
  324.         GL.Vertex3(B2.GetX(), B2.GetY(), z);
  325.  
  326.         GL.End();*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement