Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.60 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.5f, 1f);
  37.             lineEndMaterial.mainTextureOffset = new Vector2 (-0.5f, 0f);
  38.             lineMaterial.SetInt("_ZWrite", 0);
  39.             //lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
  40.             lineEndMaterial.SetInt("_ZWrite", 0);
  41.         }
  42.         if (defaultMaterial == null) {
  43.             Shader shader = Shader.Find("Hidden/Internal-Colored");
  44.             defaultMaterial = new Material(shader);
  45.             defaultMaterial.hideFlags = HideFlags.HideAndDontSave;
  46.             defaultMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  47.             defaultMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  48.             defaultMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
  49.             defaultMaterial.SetInt("_ZWrite", 0);
  50.         }
  51.     }
  52.  
  53.     static public void SetColor(Color color)
  54.     {
  55.         Check ();
  56.         lineMaterial.SetColor ("_Emission", color);
  57.         lineEndMaterial.SetColor ("_Emission", color);
  58.         setColor = color;
  59.     }
  60.  
  61.     static public void DrawMesh(Mesh mesh, Transform transform, Vector2f offset, float z = 0f)
  62.     {
  63.         if (mesh == null)
  64.             return;
  65.        
  66.         GL.PushMatrix ();
  67.         defaultMaterial.SetPass (0);
  68.         GL.Begin(GL.TRIANGLES);
  69.  
  70.         List<Vector2> list = new List<Vector2>();
  71.         for (int i = 0; i <  mesh.triangles.GetLength (0); i++ ) {
  72.             list.Add (transform.TransformPoint(mesh.vertices [mesh.triangles [i]]));
  73.             if (list.Count > 2) {
  74.                 DrawTriangle_Matrix (list [0].x, list [0].y, list [1].x, list [1].y, list [2].x, list [2].y, offset, z);
  75.                 list.Clear ();
  76.             }
  77.         }
  78.  
  79.         GL.End ();
  80.         GL.PopMatrix ();
  81.     }
  82.  
  83.     static public void DrawTriangle(Vector2f p0, Vector2f p1, Vector2f p2, Vector2f offset, float z = 0f)
  84.     {
  85.         DrawTrianglef (p0.GetX (), p0.GetY (), p1.GetX (), p1.GetY (), p2.GetX (), p2.GetY (), offset, z);
  86.     }
  87.  
  88.     static public void DrawSquare(Vector2f p, float size, float z = 0f)
  89.     {
  90.         Vector2f p0 = new Vector2f (p.GetX () - size, p.GetY () - size);
  91.         Vector2f p1 = new Vector2f (p.GetX () + size, p.GetY () - size);
  92.         Vector2f p2 = new Vector2f (p.GetX () + size, p.GetY () + size);
  93.         Vector2f p3 = new Vector2f (p.GetX () - size, p.GetY () + size);
  94.  
  95.         DrawTriangle (p0, p1, p2, new Vector2f(0, 0), z);
  96.         DrawTriangle (p2, p3, p0, new Vector2f(0, 0), z);
  97.     }
  98.  
  99.     static public void DrawImage(Material material, Vector2f pos, Vector2f size, float z = 0f)
  100.     {
  101.         GL.PushMatrix ();
  102.         material.SetPass (0);
  103.         GL.Begin (GL.QUADS);
  104.  
  105.         GL.TexCoord2 (0, 0);
  106.         GL.Vertex3 (pos.GetX() - size.GetX(), pos.GetY() - size.GetY(), z);
  107.         GL.TexCoord2 (0, 1);
  108.         GL.Vertex3 (pos.GetX() - size.GetX(), pos.GetY() + size.GetY(), z);
  109.         GL.TexCoord2 (1, 1);
  110.         GL.Vertex3 (pos.GetX() + size.GetX(), pos.GetY() + size.GetY(), z);
  111.         GL.TexCoord2 (1, 0);
  112.         GL.Vertex3 (pos.GetX() + size.GetX(), pos.GetY() - size.GetY(), z);
  113.  
  114.         GL.End ();
  115.         GL.PopMatrix ();
  116.     }
  117.  
  118.     static public void DrawLineSquare(Vector2f p, float size, float z = 0f)
  119.     {
  120.         DrawLineRectf (p.GetX() - size / 2f, p.GetY() - size / 2f, size, size, z);
  121.     }
  122.  
  123.     static public void DrawLine(Vector2f p0, Vector2f p1, float z = 0f)
  124.     {
  125.         if (setBorder == true) {
  126.             Color tmcColor = setColor;
  127.             float tmpWidth = lineWidth;
  128.             SetColor(Color.black);
  129.             lineWidth = tmpWidth * 2f;
  130.             DrawLinef (p0.GetX (), p0.GetY (), p1.GetX (), p1.GetY (), z);
  131.             SetColor(tmcColor);
  132.             lineWidth = tmpWidth;
  133.             DrawLinef (p0.GetX (), p0.GetY (), p1.GetX (), p1.GetY (), z);
  134.             lineWidth = tmpWidth;
  135.         } else {
  136.             DrawLinef(p0.GetX (), p0.GetY (), p1.GetX (), p1.GetY (), z);
  137.         }
  138.     }
  139.        
  140.     static public void DrawLinef(float x0, float y0, float x1, float y1, float z = 0f)
  141.     {
  142.         Check ();
  143.  
  144.         if (smooth == true)
  145.             DrawSmoothLine (new Pair2f (new Vector2f (x0, y0), new Vector2f (x1, y1)), z);
  146.         else {
  147.             GL.PushMatrix();
  148.             defaultMaterial.SetPass(0);
  149.             GL.Begin(GL.LINES);
  150.             GL.Color(setColor);
  151.  
  152.             DrawLine_Matrix (x0, y0, x1, y1, z);
  153.  
  154.             GL.End();
  155.             GL.PopMatrix();
  156.         }
  157.     }
  158.        
  159.     static public void DrawTrianglef(float x0, float y0, float x1, float y1, float x2, float y2, Vector2f offset, float z = 0f)
  160.     {
  161.         GL.PushMatrix();
  162.         defaultMaterial.SetPass(0);
  163.         GL.Begin(GL.TRIANGLES);
  164.         GL.Color(setColor);
  165.  
  166.         DrawTriangle_Matrix (x0, y0, x1, y1, x2, y2, offset, z);
  167.  
  168.         GL.End();
  169.         GL.PopMatrix();
  170.     }
  171.  
  172.     static public void DrawLineRectf(float x, float y, float w, float h, float z = 0f)
  173.     {
  174.         if (smooth) {
  175.             if (setBorder == true) {
  176.                 Color tmcColor = setColor;
  177.                 float tmpWidth = lineWidth;
  178.  
  179.                 SetColor (Color.black);
  180.                 lineWidth = tmpWidth * 2f;
  181.  
  182.                 GL.PushMatrix ();
  183.                 lineMaterial.SetPass (0);
  184.                 GL.Begin (GL.QUADS);
  185.  
  186.                 DrawLine_Smooth_Matrix (new Pair2f (new Vector2f (x, y), new Vector2f (x + w, y)), z);
  187.                 DrawLine_Smooth_Matrix (new Pair2f (new Vector2f (x, y), new Vector2f (x, y + h)), z);
  188.                 DrawLine_Smooth_Matrix (new Pair2f (new Vector2f (x + w, y), new Vector2f (x + w, y + h)), z);
  189.                 DrawLine_Smooth_Matrix (new Pair2f (new Vector2f (x, y + h), new Vector2f (x + w, y + h)), z);
  190.  
  191.                 GL.End ();
  192.                 GL.PopMatrix ();
  193.  
  194.                 GL.PushMatrix ();
  195.                 lineEndMaterial.SetPass (0);
  196.                 GL.Begin (GL.QUADS);
  197.  
  198.                 DrawLineEnd_Smooth_Matrix (new Pair2f (new Vector2f (x, y), new Vector2f (x + w, y)), z);
  199.                 DrawLineEnd_Smooth_Matrix (new Pair2f (new Vector2f (x, y), new Vector2f (x, y + h)), z);
  200.                 DrawLineEnd_Smooth_Matrix (new Pair2f (new Vector2f (x + w, y), new Vector2f (x + w, y + h)), z);
  201.                 DrawLineEnd_Smooth_Matrix (new Pair2f (new Vector2f (x, y + h), new Vector2f (x + w, y + h)), z);
  202.  
  203.                 GL.End ();
  204.                 GL.PopMatrix ();
  205.  
  206.                 SetColor (tmcColor);
  207.                 lineWidth = tmpWidth;
  208.             }
  209.  
  210.             float tmpLine = lineWidth;
  211.             lineWidth = tmpLine * 1f;
  212.  
  213.             GL.PushMatrix();
  214.             SetColor (setColor);
  215.             lineMaterial.SetPass(0);
  216.             GL.Begin(GL.QUADS);
  217.  
  218.             DrawLine_Smooth_Matrix (new Pair2f(new Vector2f(x, y), new Vector2f(x + w, y)), z);
  219.             DrawLine_Smooth_Matrix (new Pair2f(new Vector2f(x, y), new Vector2f(x, y + h)), z);
  220.             DrawLine_Smooth_Matrix (new Pair2f(new Vector2f(x + w, y), new Vector2f(x + w, y+ h)), z);
  221.             DrawLine_Smooth_Matrix (new Pair2f(new Vector2f(x, y + h), new Vector2f(x + w, y+ h)), z);
  222.  
  223.             GL.End();
  224.             GL.PopMatrix();
  225.  
  226.             lineWidth = tmpLine;
  227.  
  228.         } else {
  229.             DrawLine (new Vector2f (x, y), new Vector2f (x + w, y), z);
  230.             DrawLine (new Vector2f (x + w, y), new Vector2f (x + w, y + h), z);
  231.             DrawLine (new Vector2f (x + w, y + h)new Vector2f (x, y + h), z);
  232.             DrawLine (new Vector2f (x, y + h), new Vector2f (x, y), z);
  233.         }
  234.     }
  235.  
  236.     static public void DrawSlice(List< Vector2f> slice, float z = 0f)
  237.     {
  238.         foreach (Pair2f p in Pair2f.GetList(slice, false))
  239.             DrawLine (p.A, p.B, z);
  240.     }
  241.  
  242.     static public void DrawPolygonList(List<Polygon> polyList, float z = 0f)
  243.     {
  244.         foreach (Polygon p in polyList)
  245.             DrawPolygon (p, z);
  246.     }
  247.  
  248.     static public void DrawStrippedLine(List<Vector2f> pointsList, float minVertsDistance, float z = 0f, bool full = false, Vector2f offset = null)
  249.     {
  250.         if (offset == null)
  251.             offset = new Vector2f (0, 0);
  252.  
  253.         Vector2f vA = null, vB = null;
  254.  
  255.         if (setBorder == true) {
  256.             Color tmcColor = setColor;
  257.             float tmpWidth = lineWidth;
  258.  
  259.             GL.PushMatrix();
  260.             SetColor (Color.black);
  261.             lineMaterial.SetPass(0);
  262.             GL.Begin(GL.QUADS);
  263.  
  264.             lineWidth = 2f * tmpWidth;
  265.  
  266.             foreach (Pair2f id in Pair2f.GetList(pointsList, full)) {
  267.                 vA = new Vector2f (id.A.Get () + offset.Get());
  268.                 vB = new Vector2f (id.B.Get () + offset.Get());
  269.  
  270.                 vA.Push (Vector2f.Atan2 (id.A, id.B), -minVertsDistance / 4);
  271.                 vB.Push (Vector2f.Atan2 (id.A, id.B), minVertsDistance / 4);
  272.  
  273.                 DrawLine_Smooth_Matrix (new Pair2f(vA, vB), z);
  274.             }
  275.  
  276.             GL.End();
  277.             GL.PopMatrix();
  278.  
  279.  
  280.             GL.PushMatrix();
  281.             SetColor (Color.black);
  282.             lineEndMaterial.SetPass(0);
  283.             GL.Begin(GL.QUADS);
  284.  
  285.             lineWidth = 2f * tmpWidth;
  286.  
  287.             foreach (Pair2f id in Pair2f.GetList(pointsList, full)) {
  288.                 vA = new Vector2f (id.A.Get () + offset.Get());
  289.                 vB = new Vector2f (id.B.Get () + offset.Get());
  290.  
  291.                 vA.Push (Vector2f.Atan2 (id.A, id.B), -minVertsDistance / 4);
  292.                 vB.Push (Vector2f.Atan2 (id.A, id.B), minVertsDistance / 4);
  293.  
  294.                 DrawLineEnd_Smooth_Matrix (new Pair2f(vA, vB), z);
  295.             }
  296.  
  297.             GL.End();
  298.             GL.PopMatrix();
  299.  
  300.             SetColor (tmcColor);
  301.             lineWidth = tmpWidth;
  302.         }
  303.  
  304.         GL.PushMatrix();
  305.         lineMaterial.SetPass(0);
  306.         GL.Begin(GL.QUADS);
  307.  
  308.         foreach (Pair2f id in Pair2f.GetList(pointsList, full)) {
  309.             vA = new Vector2f (id.A.Get () + offset.Get());
  310.             vB = new Vector2f (id.B.Get () + offset.Get());
  311.  
  312.             vA.Push (Vector2f.Atan2 (id.A, id.B), -minVertsDistance / 4);
  313.             vB.Push (Vector2f.Atan2 (id.A, id.B), minVertsDistance / 4);
  314.  
  315.             DrawLine_Smooth_Matrix (new Pair2f(vA, vB), z);
  316.         }
  317.  
  318.         GL.End();
  319.         GL.PopMatrix();
  320.     }
  321.  
  322.     static public void DrawSmoothLine(Pair2f pair, float z = 0f)
  323.     {
  324.         GL.PushMatrix();
  325.         lineMaterial.SetPass(0);
  326.         GL.Begin(GL.QUADS);
  327.  
  328.         DrawLine_Smooth_Matrix (pair, z);
  329.  
  330.         GL.End();
  331.         GL.PopMatrix();
  332.     }
  333.  
  334.     static public void DrawPolygon(Polygon poly, float z = 0f)
  335.     {
  336.         Check ();
  337.  
  338.         if (smooth) {
  339.             GL.PushMatrix ();
  340.             lineMaterial.SetPass(0);
  341.             GL.Begin(GL.QUADS);
  342.  
  343.             DrawSlice_Smooth_Matrix (poly.pointsList, z);
  344.  
  345.             GL.End();
  346.             GL.PopMatrix();
  347.  
  348.             GL.PushMatrix ();
  349.             lineEndMaterial.SetPass(0);
  350.             GL.Begin(GL.QUADS);
  351.  
  352.             foreach (Pair2f p in Pair2f.GetList(poly.pointsList))
  353.                 DrawLineEnd_Smooth_Matrix (p, z);
  354.            
  355.             GL.End();
  356.             GL.PopMatrix();
  357.  
  358.         } else {
  359.             GL.PushMatrix();
  360.             defaultMaterial.SetPass(0);
  361.             GL.Begin(GL.LINES);
  362.             GL.Color(setColor);
  363.  
  364.             DrawSlice_Matrix (poly.pointsList, z);
  365.  
  366.             GL.End ();
  367.             GL.PopMatrix();
  368.         }
  369.  
  370.         foreach (Polygon p in poly.holesList)
  371.             DrawPolygon (p, z);
  372.     }
  373.  
  374.     private static void DrawSlice_Smooth_Matrix(List<Vector2f> list, float z)
  375.     {
  376.         foreach (Pair2f p in Pair2f.GetList(list))
  377.             DrawLine_Smooth_Matrix (p, z);
  378.        
  379.     }
  380.  
  381.     private static void DrawSlice_Matrix(List<Vector2f> list, float z)
  382.     {
  383.         foreach (Pair2f p in Pair2f.GetList(list)) {
  384.             GL.Vertex3(p.A.GetX(), p.A.GetY(), z);
  385.             GL.Vertex3(p.B.GetX(), p.B.GetY(), z);
  386.         }
  387.     }
  388.  
  389.     private static void DrawTriangle_Matrix(float x0, float y0, float x1, float y1, float x2, float y2, Vector2f offset, float z = 0f)
  390.     {
  391.         GL.Vertex3(x0 + offset.GetX(), y0 + offset.GetY(), z);
  392.         GL.Vertex3(x1 + offset.GetX(), y1 + offset.GetY(), z);
  393.         GL.Vertex3(x2 + offset.GetX(), y2 + offset.GetY(), z);
  394.     }
  395.  
  396.     private static void DrawLine_Matrix(float x0, float y0, float x1, float y1, float z = 0f)
  397.     {
  398.         GL.Vertex3(x0, y0, z);
  399.         GL.Vertex3(x1, y1, z);
  400.     }
  401.  
  402.     private static void DrawLine_Smooth_Matrix(Pair2f pair, float z = 0f)
  403.     {
  404.         float size = lineWidth;
  405.         float pi2 = Mathf.PI / 2;
  406.  
  407.         float rot = Vector2f.Atan2 (pair.A, pair.B);
  408.  
  409.         Vector2f A1 = new Vector2f (pair.A);
  410.         Vector2f A2 = new Vector2f (pair.A);
  411.         Vector2f B1 = new Vector2f (pair.B);
  412.         Vector2f B2 = new Vector2f (pair.B);
  413.  
  414.         A1.Push (rot + pi2, size);
  415.         A2.Push (rot - pi2, size);
  416.         B1.Push (rot + pi2, size);
  417.         B2.Push (rot - pi2, size);
  418.  
  419.         GL.TexCoord2(0, 0);
  420.         GL.Vertex3(B1.GetX(), B1.GetY(), z);
  421.         GL.TexCoord2(1, 0);
  422.         GL.Vertex3(A1.GetX(), A1.GetY(), z);
  423.         GL.TexCoord2(1, 1);
  424.         GL.Vertex3(A2.GetX(), A2.GetY(), z);
  425.         GL.TexCoord2(0, 1);
  426.         GL.Vertex3(B2.GetX(), B2.GetY(), z);
  427.     }
  428.  
  429.     private static void DrawLineEnd_Smooth_Matrix(Pair2f pair, float z = 0f)
  430.     {
  431.         float size = lineWidth;
  432.         float pi2 = Mathf.PI / 2;
  433.         float pi = Mathf.PI;
  434.  
  435.         float rot = Vector2f.Atan2 (pair.A, pair.B);
  436.  
  437.         Vector2f A1 = new Vector2f (pair.A);
  438.         Vector2f A2 = new Vector2f (pair.A);
  439.         Vector2f A3 = new Vector2f (pair.A);
  440.         Vector2f A4 = new Vector2f (pair.A);
  441.  
  442.         Vector2f B1 = new Vector2f (pair.B);
  443.         Vector2f B2 = new Vector2f (pair.B);
  444.         Vector2f B3 = new Vector2f (pair.B);
  445.         Vector2f B4 = new Vector2f (pair.B);
  446.  
  447.         A1.Push (rot + pi2, size);
  448.         A2.Push (rot - pi2, size);
  449.  
  450.         A3.Push (rot + pi2, size);
  451.         A4.Push (rot - pi2, size);
  452.         A3.Push (rot + pi, -size);
  453.         A4.Push (rot + pi, -size);
  454.  
  455.         B1.Push (rot + pi2, size);
  456.         B2.Push (rot - pi2, size);
  457.  
  458.         B3.Push (rot + pi2, size);
  459.         B4.Push (rot - pi2, size);
  460.         B3.Push (rot + pi, size);
  461.         B4.Push (rot + pi , size);
  462.  
  463.  
  464.         GL.TexCoord2(0, 0);
  465.         GL.Vertex3(A3.GetX(), A3.GetY(), z);
  466.         GL.TexCoord2(0, 1);
  467.         GL.Vertex3(A4.GetX(), A4.GetY(), z);
  468.         GL.TexCoord2(1, 1);
  469.         GL.Vertex3(A2.GetX(), A2.GetY(), z);
  470.         GL.TexCoord2(1, 0);
  471.         GL.Vertex3(A1.GetX(), A1.GetY(), z);
  472.         GL.TexCoord2(0, 0);
  473.         GL.Vertex3(B4.GetX(), B4.GetY(), z);
  474.         GL.TexCoord2(0, 1);
  475.         GL.Vertex3(B3.GetX(), B3.GetY(), z);
  476.         GL.TexCoord2(1, 1);
  477.         GL.Vertex3(B1.GetX(), B1.GetY(), z);
  478.         GL.TexCoord2(1, 0);
  479.         GL.Vertex3(B2.GetX(), B2.GetY(), z);
  480.     }
  481. }
  482.  
  483.  
  484. /*
  485.         lineEndMaterial.SetPass(0);
  486.         GL.Begin(GL.QUADS);
  487.  
  488.    
  489.         GL.End();
  490. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement