Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(MeshFilter))]
- [RequireComponent(typeof(MeshRenderer))]
- public class CrearGeometria : MonoBehaviour
- {
- public enum formas {
- Triangulo,
- Plano,
- Cubo,
- Cilindro,
- Esfera,
- Cono,
- Toro
- };
- public formas forma;
- public int particiones = 20;
- public List<Vector3> vertices;
- public List<Vector3> normals;
- public List<Vector2> textCoords;
- public List<int> triangles;
- // Función generica para crear la geometría, se invoca desde el CustomInspector
- public void crear() {
- Debug.Log("Crear geometría: " + forma);
- switch (forma) {
- // Triangulo
- case formas.Triangulo:
- crearTriangulo();
- break;
- case formas.Plano:
- crearPlano();
- break;
- case formas.Esfera:
- crearEsfera();
- break;
- // ToDo: completar el resto de formas posibles
- case formas.Cubo:
- crearCubo();
- break;
- case formas.Cilindro:
- crearCilindro();
- break;
- case formas.Cono:
- crearCono();
- break;
- case formas.Toro:
- crearToro();
- break;
- }
- }
- void crearCubo()
- {
- // Crear los vertices
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- textCoords = new List<Vector2>();
- //Cara abajo
- vertices.Add(new Vector3(0, 0, 0));//T0
- vertices.Add(new Vector3(1, 0, 0));//T1
- vertices.Add(new Vector3(1, 0, 1));//T2
- vertices.Add(new Vector3(0, 0, 1));//T3
- normals.Add(new Vector3(0, 1, 0));//0
- normals.Add(new Vector3(0, 1, 0));//1
- normals.Add(new Vector3(0, 1, 0));//2
- normals.Add(new Vector3(0, 1, 0));//3
- textCoords.Add(new Vector2(0, 0));
- textCoords.Add(new Vector2(0, 1));
- textCoords.Add(new Vector2(1, 0));
- textCoords.Add(new Vector2(1, 1));
- //Cara izquierda
- vertices.Add(new Vector3(0, 0, 0));//T4
- vertices.Add(new Vector3(0, 0, 1));//T5
- vertices.Add(new Vector3(0, 1, 1));//T6
- vertices.Add(new Vector3(0, 1, 0));//T7
- normals.Add(new Vector3(-1, 0, 0));//4
- normals.Add(new Vector3(-1, 0, 0));//5
- normals.Add(new Vector3(-1, 0, 0));//6
- normals.Add(new Vector3(-1, 0, 0));//7
- textCoords.Add(new Vector2(0, 0));
- textCoords.Add(new Vector2(0, 1));
- textCoords.Add(new Vector2(1, 0));
- textCoords.Add(new Vector2(1, 1));
- //Cara derecha
- vertices.Add(new Vector3(1, 0, 0));//T8
- vertices.Add(new Vector3(1, 0, 1));//T9
- vertices.Add(new Vector3(1, 1, 1));//T10
- vertices.Add(new Vector3(1, 1, 0));//T11
- normals.Add(new Vector3(1, 0, 0));//8
- normals.Add(new Vector3(1, 0, 0));//9
- normals.Add(new Vector3(1, 0, 0));//10
- normals.Add(new Vector3(1, 0, 0));//11
- textCoords.Add(new Vector2(0, 0));
- textCoords.Add(new Vector2(0, 1));
- textCoords.Add(new Vector2(1, 0));
- textCoords.Add(new Vector2(1, 1));
- //Cara frontal
- vertices.Add(new Vector3(0, 0, 0));//T12
- vertices.Add(new Vector3(1, 0, 0));//T13
- vertices.Add(new Vector3(1, 1, 0));//T14
- vertices.Add(new Vector3(0, 1, 0));//T15
- normals.Add(new Vector3(0, 0, -1));//12
- normals.Add(new Vector3(0, 0, -1));//13
- normals.Add(new Vector3(0, 0, -1));//14
- normals.Add(new Vector3(0, 0, -1));//15
- textCoords.Add(new Vector2(0, 0));
- textCoords.Add(new Vector2(0, 1));
- textCoords.Add(new Vector2(1, 0));
- textCoords.Add(new Vector2(1, 1));
- //Cara trasera
- vertices.Add(new Vector3(1, 0, 1));//T16
- vertices.Add(new Vector3(1, 1, 1));//T17
- vertices.Add(new Vector3(0, 1, 1));//T18
- vertices.Add(new Vector3(0, 0, 1));//T19
- normals.Add(new Vector3(0, 0, 1));//16
- normals.Add(new Vector3(0, 0, 1));//17
- normals.Add(new Vector3(0, 0, 1));//18
- normals.Add(new Vector3(0, 0, 1));//19
- textCoords.Add(new Vector2(0, 0));
- textCoords.Add(new Vector2(0, 1));
- textCoords.Add(new Vector2(1, 0));
- textCoords.Add(new Vector2(1, 1));
- //Cara Arriba
- vertices.Add(new Vector3(0, 1, 0));//T20
- vertices.Add(new Vector3(1, 1, 0));//T21
- vertices.Add(new Vector3(1, 1, 1));//T22
- vertices.Add(new Vector3(0, 1, 1));//T23
- normals.Add(new Vector3(0, -1, 0));//16
- normals.Add(new Vector3(0, -1, 0));//17
- normals.Add(new Vector3(0, -1, 0));//18
- normals.Add(new Vector3(0, -1, 0));//19
- textCoords.Add(new Vector2(0, 0));
- textCoords.Add(new Vector2(0, 1));
- textCoords.Add(new Vector2(1, 0));
- textCoords.Add(new Vector2(1, 1));
- // Crear los triángulos, a partir de los indices de los vertices en el vector de vertices
- // Cuidado! orientación mano izquierda, vertices en sentido horario
- /*Plano al suelo****************************************************************************************************/
- triangles = new List<int>();
- triangles.Add(0);
- triangles.Add(1);
- triangles.Add(2);
- triangles.Add(0);
- triangles.Add(2);
- triangles.Add(3);
- /*Plano izquierdo****************************************************************************************************/
- triangles.Add(6);
- triangles.Add(4);
- triangles.Add(5);
- triangles.Add(7);
- triangles.Add(4);
- triangles.Add(6);
- /*Plano derecho****************************************************************************************************/
- triangles.Add(10);
- triangles.Add(8);
- triangles.Add(11);
- triangles.Add(10);
- triangles.Add(9);
- triangles.Add(8);
- /*Plano frontal****************************************************************************************************/
- triangles.Add(14);
- triangles.Add(13);
- triangles.Add(12);
- triangles.Add(14);
- triangles.Add(12);
- triangles.Add(15);
- /*Plano trasero****************************************************************************************************/
- triangles.Add(16);
- triangles.Add(17);
- triangles.Add(19);
- triangles.Add(19);
- triangles.Add(17);
- triangles.Add(18);
- /*Plano arriba***************************************************************************************************/
- triangles.Add(20);
- triangles.Add(22);
- triangles.Add(21);
- triangles.Add(20);
- triangles.Add(23);
- triangles.Add(22);
- // Crear una nueva malla de triángulos a partir de los datos anteriores
- crearMesh();
- }
- void crearTriangulo() {
- // Crear los vertices
- vertices = new List<Vector3>();
- vertices.Add(new Vector3(0, 0, 0));
- vertices.Add(new Vector3(0, 0, 1));
- vertices.Add(new Vector3(1, 0, 0));
- // Crear las normales de cada vertice
- normals = new List<Vector3>();
- normals.Add(new Vector3(0, 1, 0));
- normals.Add(new Vector3(0, 1, 0));
- normals.Add(new Vector3(0, 1, 0));
- // Crear las coordenadas de textura de cada vertice
- textCoords = new List<Vector2>();
- textCoords.Add(new Vector2(0, 0));
- textCoords.Add(new Vector2(0, 1));
- textCoords.Add(new Vector2(1, 0));
- // Crear los triángulos, a partir de los indices de los vertices en el vector de vertices
- // Cuidado! orientación mano izquierda, vertices en sentido horario
- triangles = new List<int>();
- triangles.Add(0);
- triangles.Add(1);
- triangles.Add(2);
- // Crear una nueva malla de triángulos a partir de los datos anteriores
- crearMesh();
- }
- void crearPlano() {
- // Crear los vertices
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- textCoords = new List<Vector2>();
- triangles = new List<int>();
- for (int i=0; i<=particiones; i++) {
- float u = (1.0f / particiones) * i;
- for (int j=0; j<=particiones; j++) {
- float v = (1.0f / particiones) * j;
- vertices.Add(new Vector3(u,0,v));
- normals.Add(new Vector3(0,1,0));
- textCoords.Add(new Vector2(u,v));
- }
- }
- // Por cada vertice se crean dos triángulos
- int tamTira = particiones + 1;
- for (int i=1; i<=particiones; i++) {
- for (int j=1; j<=particiones; j++) {
- int currIndex = i * tamTira + j;
- triangles.Add(currIndex);
- triangles.Add(currIndex - 1);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex - tamTira);
- }
- }
- // Crear una nueva malla de triángulos a partir de los datos anteriores
- crearMesh();
- }
- void crearToro()
- {
- // Ecuación de la esfera por gajos
- // [U,V] en el rango [0,1]
- // x = radio * cos(2*PI*U) * sin(PI*V)
- // y = radio * cos(PI*V)
- // z = radio * sin(2*PI*U) * sin(PI*V)
- float radio = 1;
- float R = 2;
- // Crear los vertices
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- textCoords = new List<Vector2>();
- triangles = new List<int>();
- for (int i = 0; i <= particiones; i++)
- {
- float u = (1.0f / particiones) * i;
- for (int j = 0; j <= particiones; j++)
- {
- float v = (1.0f / particiones) * j;
- Vector3 aux = new Vector3(
- Mathf.Cos(2 * Mathf.PI * u) * (R + radio * Mathf.Sin(2* Mathf.PI * v)),
- (radio * Mathf.Cos(2 * Mathf.PI * v)),
- Mathf.Sin(2 * Mathf.PI * u) * (R + radio * Mathf.Sin(2* Mathf.PI * v))
- );
- vertices.Add(radio * aux);
- normals.Add(aux.normalized);
- textCoords.Add(new Vector2(u, 1.0f - v));
- }
- }
- // Por cada vertice se crean dos triángulos
- int tamTira = particiones + 1;
- for (int i = 1; i <= particiones; i++)
- {
- for (int j = 1; j <= particiones; j++)
- {
- int currIndex = i * tamTira + j;
- triangles.Add(currIndex);
- triangles.Add(currIndex - tamTira);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex - 1);
- }
- }
- // Crear una nueva malla de triángulos a partir de los datos anteriores
- crearMesh();
- }
- void crearCono()
- {
- float radio = 1;
- // Crear los vertices
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- textCoords = new List<Vector2>();
- triangles = new List<int>();
- vertices.Add(new Vector3(0, 0, 0));
- normals.Add(new Vector3(0, -1, 0));
- textCoords.Add(new Vector2(0f, 0f));
- //Base del cilindro parte de abajo
- for (int i = 0; i <= particiones; i++)
- {
- float u = (1.0f / particiones) * i;
- Vector3 aux = new Vector3
- (
- Mathf.Sin(2 * Mathf.PI * u),
- 0,
- Mathf.Cos(2 * Mathf.PI * u)
- );
- vertices.Add(radio * aux);
- normals.Add(new Vector3(0, -1, 0));
- textCoords.Add(new Vector2(Mathf.Sin(2 * Mathf.PI * u), Mathf.Cos(2 * Mathf.PI * u)));
- }
- for(int i = 0; i < particiones + 1; i++)
- {
- triangles.Add(0);
- triangles.Add(i+1);
- triangles.Add(i);
- }
- //Base del cilindro parte de arriba
- vertices.Add(new Vector3(0, 1, 0));
- normals.Add(new Vector3(0, 1, 0));
- textCoords.Add(new Vector2(0f, 0f));
- for (int i = 0; i <= particiones; i++)
- {
- float u = (1.0f / particiones) * i;
- Vector3 aux = new Vector3
- (
- Mathf.Sin(2 * Mathf.PI * u),
- 0,
- Mathf.Cos(2 * Mathf.PI * u)
- );
- vertices.Add((radio * aux));
- normals.Add(aux);
- textCoords.Add(new Vector2(Mathf.Sin(2 * Mathf.PI * u), Mathf.Cos(2 * Mathf.PI * u)));
- }
- for (int i = 1; i <= particiones + 2; i++)
- {
- triangles.Add(particiones + 2);
- triangles.Add(particiones + i);
- triangles.Add(particiones + (i + 1));
- }
- // Crear una nueva malla de triángulos a partir de los datos anteriores
- crearMesh();
- }
- void crearCilindro()
- {
- float radio = 1;
- // Crear los vertices
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- textCoords = new List<Vector2>();
- triangles = new List<int>();
- vertices.Add(new Vector3(0, 0, 0));
- normals.Add(new Vector3(0, -1, 0));
- textCoords.Add(new Vector2(0f, 0f));
- //Base del cilindro parte de abajo
- for (int i = 0; i <= particiones; i++)
- {
- float u = (1.0f / particiones) * i;
- Vector3 aux = new Vector3
- (
- Mathf.Sin(2 * Mathf.PI * u),
- 0,
- Mathf.Cos(2 * Mathf.PI * u)
- );
- vertices.Add(radio * aux);
- normals.Add(new Vector3(0, -1, 0));
- textCoords.Add(new Vector2(Mathf.Sin(2 * Mathf.PI * u), Mathf.Cos(2 * Mathf.PI * u)));
- }
- for (int i = 0; i < particiones + 1; i++)
- {
- triangles.Add(0);
- triangles.Add(i + 1);
- triangles.Add(i);
- }
- crearMesh();
- }
- void crearCilindroPrueba()
- {
- // Ecuación de la esfera por gajos
- // [U,V] en el rango [0,1]
- // x = radio * cos(2*PI*U) * sin(PI*V)
- // y = radio * cos(PI*V)
- // z = radio * sin(2*PI*U) * sin(PI*V)
- float radio = 1;
- // Crear los vertices
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- textCoords = new List<Vector2>();
- triangles = new List<int>();
- //Caras del cilindro
- /*for (int i = 0; i <= particiones; i++)
- {
- float u = (1.0f / particiones) * i;
- for (int j = 0; j <= particiones; j++)
- {
- float v = (1.0f / particiones) * j;
- Vector3 aux = new Vector3(
- Mathf.Cos(2 * Mathf.PI * u),
- Mathf.Cos(Mathf.PI * v),
- Mathf.Sin(2 * Mathf.PI * u)
- );
- vertices.Add(radio * aux);
- normals.Add(aux.normalized);
- textCoords.Add(new Vector2(u, 1.0f - v));
- }
- }*/
- //Base del cilindro parte de arriba
- /*for (int i = 0; i <= particiones; i++)
- {
- float u = (1.0f / particiones) * i;
- //for (int j = 0; j <= particiones; j++)
- //{
- float v = (1.0f / particiones);
- Vector3 aux = new Vector3
- (
- Mathf.Sin(2 * Mathf.PI * u),
- Mathf.Cos(Mathf.PI * v),
- Mathf.Cos(2 * Mathf.PI * u)
- );
- vertices.Add(radio * aux);
- // normals.Add(new Vector3(Mathf.Sin(2 * Mathf.PI * u), 0, Mathf.Cos(2 * Mathf.PI * u)));
- normals.Add(new Vector3(0, 1,0));
- textCoords.Add(new Vector2(u, 1.0f - v));
- /* triangles.Add(aux);
- triangles.Add(aux + 1);
- triangles.Add(radio / 2);
- //}
- }*/
- //Base del cilindro parte de abajo
- for (int i = 0; i <= particiones; i++)
- {
- float u = (1.0f / particiones) * i;
- float v = (1.0f / particiones);
- Vector3 aux = new Vector3
- (
- Mathf.Sin(2 * Mathf.PI * u),
- 0,
- Mathf.Cos(2 * Mathf.PI * u)
- );
- vertices.Add(radio * aux);
- // normals.Add(new Vector3(Mathf.Sin(2 * Mathf.PI * u), 0, Mathf.Cos(2 * Mathf.PI * u)));
- normals.Add(new Vector3(0, 1, 0));
- textCoords.Add(new Vector2(u, 1.0f - v));
- }
- /* for(int i = 0; i < particiones; i++)
- {
- triangles.Add(i);
- triangles.Add(i+1);
- triangles.Add(particiones);
- }
- */
- // Por cada vertice se crean dos triángulos
- int tamTira = particiones + 1;
- for (int i = 1; i <= particiones; i++)
- {
- for (int j = 1; j <= particiones; j++)
- {
- int currIndex = i * tamTira + j;
- triangles.Add(currIndex);
- triangles.Add(currIndex - tamTira);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex - 1);
- }
- }
- // Crear una nueva malla de triángulos a partir de los datos anteriores
- crearMesh();
- }
- void crearEsfera() {
- // Ecuación de la esfera por gajos
- // [U,V] en el rango [0,1]
- // x = radio * cos(2*PI*U) * sin(PI*V)
- // y = radio * cos(PI*V)
- // z = radio * sin(2*PI*U) * sin(PI*V)
- float radio = 1;
- // Crear los vertices
- vertices = new List<Vector3>();
- normals = new List<Vector3>();
- textCoords = new List<Vector2>();
- triangles = new List<int>();
- for (int i=0; i<=particiones; i++) {
- float u = (1.0f / particiones) * i;
- for (int j=0; j<=particiones; j++) {
- float v = (1.0f / particiones) * j;
- Vector3 aux = new Vector3(
- Mathf.Cos(2*Mathf.PI*u) * Mathf.Sin(Mathf.PI * v),
- Mathf.Cos(Mathf.PI*v),
- Mathf.Sin(2*Mathf.PI*u) * Mathf.Sin(Mathf.PI * v)
- );
- vertices.Add(radio * aux);
- normals.Add(aux.normalized);
- textCoords.Add(new Vector2(u,1.0f-v));
- }
- }
- // Por cada vertice se crean dos triángulos
- int tamTira = particiones + 1;
- for (int i=1; i<=particiones; i++) {
- for (int j=1; j<=particiones; j++) {
- int currIndex = i * tamTira + j;
- triangles.Add(currIndex);
- triangles.Add(currIndex - tamTira);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex);
- triangles.Add(currIndex - tamTira - 1);
- triangles.Add(currIndex - 1);
- }
- }
- // Crear una nueva malla de triángulos a partir de los datos anteriores
- crearMesh();
- }
- public void crearMesh() {
- // Crear la mesh con toda la información
- Mesh m = new Mesh();
- m.vertices = vertices.ToArray();
- m.normals = normals.ToArray();
- m.uv = textCoords.ToArray();
- m.triangles = triangles.ToArray();
- // Llamada obligatoria para recalcular la información
- // de la malla a partir de los vectores asignados
- m.RecalculateBounds();
- // Asignar la malla al componente MeshFilter del Gameobject
- GetComponent<MeshFilter>().sharedMesh = m;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement