Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.82 KB | None | 0 0
  1.  
  2. [RequireComponent(typeof(MeshFilter))]
  3. [RequireComponent(typeof(Mesh))]
  4. [RequireComponent(typeof(MeshRenderer))]
  5. [ExecuteInEditMode]
  6. public class Borders : MonoBehaviour {
  7.  
  8.     private List<BorderAnimator> animatorsList = new List<BorderAnimator>();
  9.     public List<Vector2> coliderVertices = new List<Vector2>();
  10.     public List<Vector2> borderVertices = new List<Vector2>();
  11.     public List<Line> lines = new List<Line>();
  12.     private PolygonCollider2D colider;
  13.     private Triangulator trangulator;
  14.     private Mesh mesh;
  15.     private Vector2[] points;
  16.     private Vector3 position;
  17.     private int[] triangles;
  18.     public Vector3[] vertices;
  19.     public float thikness = 0;
  20.     public float z = 0;
  21.     public float animationTime = 100;
  22.     public float delay = .6f;
  23.  
  24.  
  25.  
  26.  
  27.     public class BorderAnimator {
  28.         private GameObject instance;
  29.         private List<Vector2> animationPoints, animatedPoints, distanceToPoint, destiniyOfPoint;
  30.         private VerticesToMesh mesh;
  31.         private int entryIndex, exitIndex, verticesCount;
  32.         private Vector2 centerOfShape;
  33.         public bool animating = true;
  34.         private float animationTime;
  35.  
  36.         public BorderAnimator(Borders parent, Vector2[] shape, Vector2 entryPoint, Vector2 exitPoint) {
  37.  
  38.             animationPoints = new List<Vector2>(shape);
  39.             distanceToPoint = new List<Vector2>();
  40.             destiniyOfPoint = new List<Vector2>();
  41.  
  42.             instance = new GameObject("borderAnimation");   //setting up opbject
  43.             instance.transform.parent = parent.transform;
  44.             instance.transform.position = new Vector3(0, 0, parent.z);
  45.             mesh = instance.AddComponent<VerticesToMesh>();
  46.             instance.GetComponent<MeshRenderer>().material = parent.GetComponent<MeshRenderer>().material;
  47.  
  48.  
  49.             BorderCalc borderCalc = new BorderCalc(shape, parent.thikness);
  50.             animationPoints = borderCalc.CallculateBorders();
  51.             //animationPoints.Sort(new ClockwiseComparer(instance.transform.position));
  52.  
  53.             entryIndex = borderCalc.indexOf(entryPoint);
  54.             exitIndex = borderCalc.indexOf(exitPoint);
  55.             Debug.Log("entry " + entryIndex);
  56.             Debug.Log("exit: " + exitIndex);
  57.  
  58.             exitPoint = animationPoints[exitIndex];
  59.             entryPoint = animationPoints[entryIndex];
  60.  
  61.             int smallerPoint = entryIndex > exitIndex ? exitIndex : entryIndex;
  62.             while(smallerPoint != 0) {
  63.                 Vector2 firstElement = animationPoints[0];
  64.                 for(int i = 0; i < animationPoints.Count; i++) {
  65.                     try {
  66.                         animationPoints[i] = animationPoints[i + 1];
  67.                     } catch { }
  68.                 }
  69.                 animationPoints[animationPoints.Count - 1] = firstElement;
  70.                 smallerPoint--;
  71.                 entryIndex--;
  72.                 exitIndex--;
  73.                              
  74.             }
  75.  
  76.             Debug.Log("entry after " + entryIndex);
  77.             Debug.Log("exit after: " + exitIndex);
  78.  
  79.             if (exitIndex + 1 == entryIndex || entryIndex + 1 == exitIndex) {
  80.                
  81.                 Vector2 removed = animationPoints[0];
  82.                 animationPoints.RemoveAt(0);
  83.                 animationPoints.Add(removed);
  84.                                
  85.                 if(entryIndex > exitIndex) {
  86.                     entryIndex = animationPoints.Count-1;
  87.                 }else {
  88.                     exitIndex = animationPoints.Count-1;
  89.                 }
  90.                
  91.             }
  92.  
  93.             //LineCutter.addEntryPoint(animationPoints[0]);
  94.             //LineCutter.addEntryPoint(animationPoints[animationPoints.Count - 1]);
  95.  
  96.             ReduceThiknessOnPoint(entryIndex, parent);
  97.             ReduceThiknessOnPoint(exitIndex, parent);
  98.            
  99.            
  100.             mesh.SetPoints(animationPoints.ToArray());
  101.             centerOfShape = GameObject.Find("shape").GetComponent<Renderer>().bounds.center;
  102.            
  103.             verticesCount = animationPoints.Count - 2; //entrypoint, exitpoint
  104.             Debug.Log(animationPoints.Count);
  105.  
  106.             animationTime = parent.animationTime;
  107.             animatedPoints = new List<Vector2>();
  108.             for (int i = 0; i < animationPoints.Count; i++) {
  109.                 if (i != entryIndex && i != exitIndex) {
  110.                     animatedPoints.Add(animationPoints[i]);
  111.                 }
  112.  
  113.             }
  114.  
  115.             if (verticesCount < 2) {
  116.  
  117.                 foreach (Vector2 point in animatedPoints) {
  118.                     Vector2 distance = centerOfShape - point;
  119.                     distanceToPoint.Add(distance);
  120.                     destiniyOfPoint.Add(centerOfShape);
  121.                 }
  122.  
  123.             } else if (verticesCount == 2) {
  124.  
  125.                 addStart();
  126.                 addEnd();                
  127.  
  128.             } else if (verticesCount > 2) {
  129.                 addStart();
  130.                
  131.  
  132.                 for(int i = 1; i < animatedPoints.Count-1; i++) {
  133.  
  134.                     Vector2 distance = centerOfShape - animatedPoints[i];
  135.                     distanceToPoint.Add(distance);
  136.                     destiniyOfPoint.Add(centerOfShape);
  137.  
  138.                 }
  139.  
  140.                 addEnd();
  141.  
  142.  
  143.             }
  144.  
  145.         }
  146.  
  147.         private void addStart() {
  148.             Vector2 distance = animationPoints[0] - animatedPoints[0];
  149.             distanceToPoint.Add(distance);
  150.             destiniyOfPoint.Add(animationPoints[0]);
  151.  
  152.         }
  153.  
  154.         private void addEnd() {
  155.             Vector2 distance2 = animationPoints[animationPoints.Count - 1] - animatedPoints[animatedPoints.Count-1];
  156.             distanceToPoint.Add(distance2);
  157.             destiniyOfPoint.Add(animationPoints[animationPoints.Count - 1]);
  158.             animationTime = animationTime / 2; //when cutting varius verices is slower
  159.  
  160.         }
  161.  
  162.         private void ReduceThiknessOnPoint(int indexPoint, Borders parent) {
  163.             Vector2 v;
  164.             if (indexPoint <= verticesCount/2 ) {
  165.                 v = NodesOfMesh.getNextNode(animationPoints, animationPoints[indexPoint]) - animationPoints[indexPoint];
  166.  
  167.             }else {
  168.                 v = NodesOfMesh.getPrevNode(animationPoints, animationPoints[indexPoint]) - animationPoints[indexPoint];
  169.  
  170.             }
  171.             Vector2 reduction = animationPoints[indexPoint] + (v.normalized * parent.thikness * 2);
  172.             animationPoints[indexPoint] = reduction;
  173.         }
  174.  
  175.         public void Update() {
  176.  
  177.          
  178.             int arrivedPoints = 0;
  179.             for (int i = 0; i < animatedPoints.Count; i++) { //work the points
  180.        
  181.                     if (Mathf.Abs(animatedPoints[i].x - destiniyOfPoint[i].x) > Mathf.Abs(distanceToPoint[i].x/animationTime) && Mathf.Abs(animatedPoints[i].y - destiniyOfPoint[i].y) > Mathf.Abs(distanceToPoint[i].y / animationTime)) {
  182.                         Vector2 newSpot = animatedPoints[i] + distanceToPoint[i] / animationTime;
  183.                         animatedPoints[i] = newSpot;
  184.                     } else {
  185.                         arrivedPoints++;
  186.                         Debug.Log("arrived");
  187.  
  188.                     }
  189.             }
  190.  
  191.             for(int i = 1; i < animationPoints.Count-1; i++ ) {
  192.                 animationPoints[i] = animatedPoints[i - 1];
  193.             }
  194.  
  195.             mesh.points = animationPoints.ToArray();
  196.  
  197.             if(arrivedPoints >= animatedPoints.Count) {
  198.                 animating = false;
  199.                 DestroyObject(instance);
  200.             }
  201.         }
  202.     }
  203.  
  204.     void Awake() {
  205.  
  206.         MeshFilter meshFilter = gameObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
  207.         mesh = new Mesh();
  208.         mesh.name = "Poly Mesh";
  209.         mesh.vertices = vertices;
  210.         mesh.triangles = triangles;
  211.         meshFilter.mesh = mesh;
  212.  
  213.         NotifyChange();
  214.  
  215.     }
  216.  
  217.  
  218.     // Update is called once per frame
  219.     void Update() {
  220.        
  221.         try {
  222.             position = new Vector3(colider.transform.position.x, colider.transform.position.y, z);
  223.             position.z = z;
  224.             gameObject.transform.position = position;
  225.  
  226.  
  227.         } catch { } //catch when shape is cutteds
  228.  
  229.             for (int i = 0; i < animatorsList.Count; i++) {
  230.                 animatorsList[i].Update();
  231.  
  232.                 if (!animatorsList[i].animating) {
  233.                     animatorsList.RemoveAt(i);
  234.                 }
  235.             }
  236.  
  237.     }
  238.  
  239.     public void UpdateBorders(Vector2[] shape, Vector2 entryPoint, Vector2 exitPoint) {
  240.        
  241.         BorderAnimator animator = new BorderAnimator(this, shape, entryPoint, exitPoint);
  242.         animatorsList.Add(animator);
  243.         StartCoroutine(InternalUpdate());
  244.  
  245.     }
  246.  
  247.     private IEnumerator InternalUpdate() {
  248.         yield return new WaitForSeconds(delay);
  249.         NotifyChange();
  250.         //updateShape = true;
  251.  
  252.     }
  253.  
  254.     public void NotifyChange() {
  255.         getPoints();
  256.         UpdateMesh();
  257.     }
  258.  
  259.     private void UpdateMesh() {
  260.         trangulator = new Triangulator(points);
  261.         triangles = trangulator.Triangulate();
  262.         mesh.vertices = vertices;
  263.         mesh.triangles = triangles;
  264.        
  265.         mesh.vertices = vertices;
  266.         mesh.triangles = triangles;
  267.  
  268.     }
  269.  
  270.  
  271.     private void getPoints() {
  272.  
  273.         colider = GameObject.Find("shape").GetComponent(typeof(PolygonCollider2D)) as PolygonCollider2D;
  274.         points = colider.points;
  275.  
  276.         vertices = new Vector3[points.Length];
  277.         lines.Clear();
  278.         borderVertices.Clear();
  279.  
  280.         coliderVertices.Clear();
  281.         coliderVertices.AddRange(points);
  282.  
  283.         BorderCalc borders = new BorderCalc(coliderVertices, thikness);
  284.         borderVertices = borders.CallculateBorders();
  285.         points = borderVertices.ToArray();
  286.         for (int i = 0; i < points.Length; i++) {
  287.             vertices[i] = points[i];
  288.  
  289.         }
  290.  
  291.     }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement