Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(Mesh))]
- [RequireComponent(typeof(MeshRenderer))]
- [ExecuteInEditMode]
- public class Borders : MonoBehaviour {
- private List<BorderAnimator> animatorsList = new List<BorderAnimator>();
- public List<Vector2> coliderVertices = new List<Vector2>();
- public List<Vector2> borderVertices = new List<Vector2>();
- public List<Line> lines = new List<Line>();
- private PolygonCollider2D colider;
- private Triangulator trangulator;
- private Mesh mesh;
- private Vector2[] points;
- private Vector3 position;
- private int[] triangles;
- public Vector3[] vertices;
- public float thikness = 0;
- public float z = 0;
- public float animationTime = 100;
- public float delay = .6f;
- public class BorderAnimator {
- private GameObject instance;
- private List<Vector2> animationPoints, animatedPoints, distanceToPoint, destiniyOfPoint;
- private VerticesToMesh mesh;
- private int entryIndex, exitIndex, verticesCount;
- private Vector2 centerOfShape;
- public bool animating = true;
- private float animationTime;
- public BorderAnimator(Borders parent, Vector2[] shape, Vector2 entryPoint, Vector2 exitPoint) {
- animationPoints = new List<Vector2>(shape);
- distanceToPoint = new List<Vector2>();
- destiniyOfPoint = new List<Vector2>();
- instance = new GameObject("borderAnimation"); //setting up opbject
- instance.transform.parent = parent.transform;
- instance.transform.position = new Vector3(0, 0, parent.z);
- mesh = instance.AddComponent<VerticesToMesh>();
- instance.GetComponent<MeshRenderer>().material = parent.GetComponent<MeshRenderer>().material;
- BorderCalc borderCalc = new BorderCalc(shape, parent.thikness);
- animationPoints = borderCalc.CallculateBorders();
- //animationPoints.Sort(new ClockwiseComparer(instance.transform.position));
- entryIndex = borderCalc.indexOf(entryPoint);
- exitIndex = borderCalc.indexOf(exitPoint);
- Debug.Log("entry " + entryIndex);
- Debug.Log("exit: " + exitIndex);
- exitPoint = animationPoints[exitIndex];
- entryPoint = animationPoints[entryIndex];
- int smallerPoint = entryIndex > exitIndex ? exitIndex : entryIndex;
- while(smallerPoint != 0) {
- Vector2 firstElement = animationPoints[0];
- for(int i = 0; i < animationPoints.Count; i++) {
- try {
- animationPoints[i] = animationPoints[i + 1];
- } catch { }
- }
- animationPoints[animationPoints.Count - 1] = firstElement;
- smallerPoint--;
- entryIndex--;
- exitIndex--;
- }
- Debug.Log("entry after " + entryIndex);
- Debug.Log("exit after: " + exitIndex);
- if (exitIndex + 1 == entryIndex || entryIndex + 1 == exitIndex) {
- Vector2 removed = animationPoints[0];
- animationPoints.RemoveAt(0);
- animationPoints.Add(removed);
- if(entryIndex > exitIndex) {
- entryIndex = animationPoints.Count-1;
- }else {
- exitIndex = animationPoints.Count-1;
- }
- }
- //LineCutter.addEntryPoint(animationPoints[0]);
- //LineCutter.addEntryPoint(animationPoints[animationPoints.Count - 1]);
- ReduceThiknessOnPoint(entryIndex, parent);
- ReduceThiknessOnPoint(exitIndex, parent);
- mesh.SetPoints(animationPoints.ToArray());
- centerOfShape = GameObject.Find("shape").GetComponent<Renderer>().bounds.center;
- verticesCount = animationPoints.Count - 2; //entrypoint, exitpoint
- Debug.Log(animationPoints.Count);
- animationTime = parent.animationTime;
- animatedPoints = new List<Vector2>();
- for (int i = 0; i < animationPoints.Count; i++) {
- if (i != entryIndex && i != exitIndex) {
- animatedPoints.Add(animationPoints[i]);
- }
- }
- if (verticesCount < 2) {
- foreach (Vector2 point in animatedPoints) {
- Vector2 distance = centerOfShape - point;
- distanceToPoint.Add(distance);
- destiniyOfPoint.Add(centerOfShape);
- }
- } else if (verticesCount == 2) {
- addStart();
- addEnd();
- } else if (verticesCount > 2) {
- addStart();
- for(int i = 1; i < animatedPoints.Count-1; i++) {
- Vector2 distance = centerOfShape - animatedPoints[i];
- distanceToPoint.Add(distance);
- destiniyOfPoint.Add(centerOfShape);
- }
- addEnd();
- }
- }
- private void addStart() {
- Vector2 distance = animationPoints[0] - animatedPoints[0];
- distanceToPoint.Add(distance);
- destiniyOfPoint.Add(animationPoints[0]);
- }
- private void addEnd() {
- Vector2 distance2 = animationPoints[animationPoints.Count - 1] - animatedPoints[animatedPoints.Count-1];
- distanceToPoint.Add(distance2);
- destiniyOfPoint.Add(animationPoints[animationPoints.Count - 1]);
- animationTime = animationTime / 2; //when cutting varius verices is slower
- }
- private void ReduceThiknessOnPoint(int indexPoint, Borders parent) {
- Vector2 v;
- if (indexPoint <= verticesCount/2 ) {
- v = NodesOfMesh.getNextNode(animationPoints, animationPoints[indexPoint]) - animationPoints[indexPoint];
- }else {
- v = NodesOfMesh.getPrevNode(animationPoints, animationPoints[indexPoint]) - animationPoints[indexPoint];
- }
- Vector2 reduction = animationPoints[indexPoint] + (v.normalized * parent.thikness * 2);
- animationPoints[indexPoint] = reduction;
- }
- public void Update() {
- int arrivedPoints = 0;
- for (int i = 0; i < animatedPoints.Count; i++) { //work the points
- 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)) {
- Vector2 newSpot = animatedPoints[i] + distanceToPoint[i] / animationTime;
- animatedPoints[i] = newSpot;
- } else {
- arrivedPoints++;
- Debug.Log("arrived");
- }
- }
- for(int i = 1; i < animationPoints.Count-1; i++ ) {
- animationPoints[i] = animatedPoints[i - 1];
- }
- mesh.points = animationPoints.ToArray();
- if(arrivedPoints >= animatedPoints.Count) {
- animating = false;
- DestroyObject(instance);
- }
- }
- }
- void Awake() {
- MeshFilter meshFilter = gameObject.GetComponent(typeof(MeshFilter)) as MeshFilter;
- mesh = new Mesh();
- mesh.name = "Poly Mesh";
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- meshFilter.mesh = mesh;
- NotifyChange();
- }
- // Update is called once per frame
- void Update() {
- try {
- position = new Vector3(colider.transform.position.x, colider.transform.position.y, z);
- position.z = z;
- gameObject.transform.position = position;
- } catch { } //catch when shape is cutteds
- for (int i = 0; i < animatorsList.Count; i++) {
- animatorsList[i].Update();
- if (!animatorsList[i].animating) {
- animatorsList.RemoveAt(i);
- }
- }
- }
- public void UpdateBorders(Vector2[] shape, Vector2 entryPoint, Vector2 exitPoint) {
- BorderAnimator animator = new BorderAnimator(this, shape, entryPoint, exitPoint);
- animatorsList.Add(animator);
- StartCoroutine(InternalUpdate());
- }
- private IEnumerator InternalUpdate() {
- yield return new WaitForSeconds(delay);
- NotifyChange();
- //updateShape = true;
- }
- public void NotifyChange() {
- getPoints();
- UpdateMesh();
- }
- private void UpdateMesh() {
- trangulator = new Triangulator(points);
- triangles = trangulator.Triangulate();
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- }
- private void getPoints() {
- colider = GameObject.Find("shape").GetComponent(typeof(PolygonCollider2D)) as PolygonCollider2D;
- points = colider.points;
- vertices = new Vector3[points.Length];
- lines.Clear();
- borderVertices.Clear();
- coliderVertices.Clear();
- coliderVertices.AddRange(points);
- BorderCalc borders = new BorderCalc(coliderVertices, thikness);
- borderVertices = borders.CallculateBorders();
- points = borderVertices.ToArray();
- for (int i = 0; i < points.Length; i++) {
- vertices[i] = points[i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement