Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class SistemaDeBordes : MonoBehaviour {
- [SerializeField] private EdgeCollider2D edgeCollider;
- [SerializeField] private float alto = 1;
- private void Awake()
- {
- int cantidadDePuntos = edgeCollider.pointCount - 1;
- int cantidadDeVertices = cantidadDePuntos * 2;
- int cantidadDeIndicesDeCaras = cantidadDeVertices * 3;
- // Debug.Log("Puntos="+cantidadDeVertices);
- // Debug.Log("Caras="+cantidadDeIndicesDeCaras);
- Mesh mesh = this.GetComponent<MeshFilter>().mesh;
- mesh.Clear();
- Vector3[] vertices = new Vector3[cantidadDeVertices];
- int[] triangulos = new int [cantidadDeIndicesDeCaras];
- int i;
- float anguloSiguiente;
- float anguloAnterior;
- float anguloBisectriz;
- float hipotenusa;
- float anguloCartesiano;
- float deltaX;
- float deltaY;
- for (i = 0; i < cantidadDePuntos; i++)
- {
- vertices[i*2] = (Vector3)edgeCollider.points[i];
- //Calculo la coordenada donde tiene que estar el punto de union//entre el plano precedente y el antecedente
- int indiceVerticeSiguiente = i + 1;
- if (indiceVerticeSiguiente >= cantidadDePuntos) indiceVerticeSiguiente -= cantidadDePuntos;
- int indiceVerticeAnterior = i - 1;
- if (indiceVerticeAnterior < 0) indiceVerticeAnterior += cantidadDePuntos;
- anguloSiguiente = CalcularAngulo(edgeCollider.points[i], edgeCollider.points[indiceVerticeSiguiente]);
- // Debug.Log("anguloPrecedente=" + anguloSiguiente);
- anguloAnterior = CalcularAngulo(edgeCollider.points[i], edgeCollider.points[indiceVerticeAnterior]);
- // Debug.Log("anguloAntecedente=" + anguloAnterior);
- anguloBisectriz = Mathf.Abs((anguloSiguiente - anguloAnterior) / 2);
- // Debug.Log("anguloBisectriz=" + anguloBisectriz);
- hipotenusa = alto / Mathf.Sin(anguloBisectriz * Mathf.Deg2Rad);
- // Debug.Log("hipotenusa="+hipotenusa);
- anguloCartesiano = anguloBisectriz;
- if (anguloAnterior > anguloSiguiente) anguloCartesiano += anguloSiguiente;
- else anguloCartesiano += anguloAnterior;
- if (Mathf.Abs(anguloSiguiente - anguloAnterior) < 180) anguloCartesiano += 180;
- // if (anguloSiguiente - anguloAnterior < 0 && Mathf.Abs(anguloSiguiente - anguloAnterior) < 180) anguloCartesiano += 180;
- // Debug.Log("anguloCartesiano=" + anguloCartesiano);
- deltaX = hipotenusa * Mathf.Cos(anguloCartesiano * Mathf.Deg2Rad);
- deltaY = hipotenusa * Mathf.Sin(anguloCartesiano * Mathf.Deg2Rad);
- // Debug.Log("deltaX="+deltaX);
- // Debug.Log("deltaY="+deltaY);
- vertices[i*2+1] = (Vector3)(edgeCollider.points[i] + new Vector2(deltaX,deltaY));
- }
- for (i = 0; i < cantidadDeVertices; i++)
- {
- triangulos[i * 3] = i;
- triangulos[(i * 3 + 1) % cantidadDeIndicesDeCaras] = (i + 1) % cantidadDeVertices;
- triangulos[(i * 3 + 2) % cantidadDeIndicesDeCaras] = (i + 2) % cantidadDeVertices;
- }
- mesh.vertices = vertices;
- mesh.triangles = triangulos;
- }
- private float CalcularAngulo(Vector2 vectorA, Vector2 vectorB)
- {
- // Debug.Log("vectorA=" + vectorA);
- // Debug.Log("vectorB=" + vectorB);
- Vector2 vectorOrigen = vectorB - vectorA;
- float resultado = Mathf.Atan2(vectorOrigen.y, vectorOrigen.x) * Mathf.Rad2Deg;
- if (resultado < 0) resultado += 360;
- else if (resultado >= 360) resultado -= 360;
- return resultado;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement