Advertisement
Guest User

SistemaDeBordes.cs

a guest
Apr 22nd, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SistemaDeBordes : MonoBehaviour {
  5.    
  6.     [SerializeField] private EdgeCollider2D edgeCollider;
  7.     [SerializeField] private float alto = 1;
  8.    
  9.     private void Awake()
  10.     {
  11.         int cantidadDePuntos = edgeCollider.pointCount - 1;
  12.         int cantidadDeVertices = cantidadDePuntos * 2;
  13.         int cantidadDeIndicesDeCaras = cantidadDeVertices * 3;
  14. //      Debug.Log("Puntos="+cantidadDeVertices);
  15. //      Debug.Log("Caras="+cantidadDeIndicesDeCaras);
  16.        
  17.         Mesh mesh = this.GetComponent<MeshFilter>().mesh;
  18.        
  19.         mesh.Clear();
  20.        
  21.         Vector3[] vertices = new Vector3[cantidadDeVertices];
  22.         int[] triangulos   = new int    [cantidadDeIndicesDeCaras];
  23.        
  24.         int i;
  25.        
  26.         float anguloSiguiente;
  27.         float anguloAnterior;
  28.         float anguloBisectriz;
  29.         float hipotenusa;
  30.         float anguloCartesiano;
  31.         float deltaX;
  32.         float deltaY;
  33.        
  34.         for (i = 0; i < cantidadDePuntos; i++)
  35.         {
  36.             vertices[i*2] = (Vector3)edgeCollider.points[i];
  37.            
  38.             //Calculo la coordenada donde tiene que estar el punto de union//entre el plano precedente y el antecedente
  39.             int indiceVerticeSiguiente = i + 1;
  40.             if (indiceVerticeSiguiente >= cantidadDePuntos) indiceVerticeSiguiente -= cantidadDePuntos;
  41.             int indiceVerticeAnterior  = i - 1;
  42.             if (indiceVerticeAnterior < 0) indiceVerticeAnterior += cantidadDePuntos;
  43.            
  44.             anguloSiguiente = CalcularAngulo(edgeCollider.points[i], edgeCollider.points[indiceVerticeSiguiente]);
  45. //          Debug.Log("anguloPrecedente=" + anguloSiguiente);
  46.             anguloAnterior = CalcularAngulo(edgeCollider.points[i], edgeCollider.points[indiceVerticeAnterior]);
  47. //          Debug.Log("anguloAntecedente=" + anguloAnterior);
  48.             anguloBisectriz = Mathf.Abs((anguloSiguiente - anguloAnterior) / 2);
  49. //          Debug.Log("anguloBisectriz=" + anguloBisectriz);
  50.             hipotenusa = alto / Mathf.Sin(anguloBisectriz * Mathf.Deg2Rad);
  51. //          Debug.Log("hipotenusa="+hipotenusa);
  52.            
  53.             anguloCartesiano = anguloBisectriz;
  54.             if (anguloAnterior > anguloSiguiente) anguloCartesiano += anguloSiguiente;
  55.             else anguloCartesiano += anguloAnterior;
  56.             if (Mathf.Abs(anguloSiguiente - anguloAnterior) < 180) anguloCartesiano += 180;
  57. //          if (anguloSiguiente - anguloAnterior < 0 && Mathf.Abs(anguloSiguiente - anguloAnterior) < 180) anguloCartesiano += 180;
  58. //          Debug.Log("anguloCartesiano=" + anguloCartesiano);
  59.            
  60.             deltaX = hipotenusa * Mathf.Cos(anguloCartesiano * Mathf.Deg2Rad);
  61.             deltaY = hipotenusa * Mathf.Sin(anguloCartesiano * Mathf.Deg2Rad);
  62. //          Debug.Log("deltaX="+deltaX);
  63. //          Debug.Log("deltaY="+deltaY);
  64.            
  65.             vertices[i*2+1] = (Vector3)(edgeCollider.points[i] + new Vector2(deltaX,deltaY));
  66.         }
  67.        
  68.         for (i = 0; i < cantidadDeVertices; i++)
  69.         {
  70.             triangulos[i * 3] = i;
  71.             triangulos[(i * 3 + 1) % cantidadDeIndicesDeCaras] = (i + 1) % cantidadDeVertices;
  72.             triangulos[(i * 3 + 2) % cantidadDeIndicesDeCaras] = (i + 2) % cantidadDeVertices;
  73.         }
  74.        
  75.         mesh.vertices = vertices;
  76.         mesh.triangles = triangulos;
  77.     }
  78.    
  79.     private float CalcularAngulo(Vector2 vectorA, Vector2 vectorB)
  80.     {
  81. //      Debug.Log("vectorA=" + vectorA);
  82. //      Debug.Log("vectorB=" + vectorB);
  83.         Vector2 vectorOrigen = vectorB - vectorA;
  84.         float resultado = Mathf.Atan2(vectorOrigen.y, vectorOrigen.x) * Mathf.Rad2Deg;
  85.         if (resultado < 0) resultado += 360;
  86.         else if (resultado >= 360) resultado -= 360;
  87.         return resultado;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement