Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(MeshFilter))]
  6. [RequireComponent(typeof(MeshRenderer))]
  7. public class CrearGeometria : MonoBehaviour
  8. {
  9.     public enum formas {
  10.         Triangulo,
  11.         Plano,
  12.         Cubo,
  13.         Cilindro,
  14.         Esfera,
  15.         Cono,
  16.         Toro
  17.     };
  18.     public formas forma;
  19.     public int particiones = 20;
  20.  
  21.     public List<Vector3> vertices;
  22.     public List<Vector3> normals;
  23.     public List<Vector2> textCoords;
  24.     public List<int> triangles;
  25.    
  26.     // Función generica para crear la geometría, se invoca desde el CustomInspector
  27.     public void crear() {
  28.         Debug.Log("Crear geometría: " + forma);
  29.         switch (forma) {
  30.             // Triangulo
  31.             case formas.Triangulo:
  32.                 crearTriangulo();
  33.                 break;
  34.  
  35.             case formas.Plano:
  36.                 crearPlano();
  37.                 break;
  38.  
  39.             case formas.Esfera:
  40.                 crearEsfera();
  41.                 break;
  42.            
  43.             // ToDo: completar el resto de formas posibles
  44.      
  45.             case formas.Cubo:
  46.                 crearCubo();
  47.                 break;
  48.  
  49.             case formas.Cilindro:
  50.                 crearCilindro();
  51.                 break;
  52.  
  53.              case formas.Cono:
  54.                 crearCono();
  55.                 break;
  56.  
  57.              case formas.Toro:
  58.                 crearToro();
  59.                 break;
  60.         }
  61.     }
  62.  
  63.     void crearCubo()
  64.     {
  65.         // Crear los vertices
  66.         vertices = new List<Vector3>();
  67.         normals = new List<Vector3>();
  68.         textCoords = new List<Vector2>();
  69.         //Cara abajo
  70.         vertices.Add(new Vector3(0, 0, 0));//T0
  71.         vertices.Add(new Vector3(1, 0, 0));//T1
  72.         vertices.Add(new Vector3(1, 0, 1));//T2
  73.         vertices.Add(new Vector3(0, 0, 1));//T3
  74.  
  75.         normals.Add(new Vector3(0, 1, 0));//0
  76.         normals.Add(new Vector3(0, 1, 0));//1
  77.         normals.Add(new Vector3(0, 1, 0));//2
  78.         normals.Add(new Vector3(0, 1, 0));//3
  79.  
  80.         textCoords.Add(new Vector2(0, 0));
  81.         textCoords.Add(new Vector2(0, 1));
  82.         textCoords.Add(new Vector2(1, 0));
  83.         textCoords.Add(new Vector2(1, 1));
  84.  
  85.  
  86.  
  87.         //Cara izquierda
  88.         vertices.Add(new Vector3(0, 0, 0));//T4
  89.         vertices.Add(new Vector3(0, 0, 1));//T5
  90.         vertices.Add(new Vector3(0, 1, 1));//T6
  91.         vertices.Add(new Vector3(0, 1, 0));//T7
  92.  
  93.         normals.Add(new Vector3(-1, 0, 0));//4
  94.         normals.Add(new Vector3(-1, 0, 0));//5
  95.         normals.Add(new Vector3(-1, 0, 0));//6
  96.         normals.Add(new Vector3(-1, 0, 0));//7
  97.  
  98.  
  99.         textCoords.Add(new Vector2(0, 0));
  100.         textCoords.Add(new Vector2(0, 1));
  101.         textCoords.Add(new Vector2(1, 0));
  102.         textCoords.Add(new Vector2(1, 1));
  103.  
  104.  
  105.         //Cara derecha
  106.         vertices.Add(new Vector3(1, 0, 0));//T8
  107.         vertices.Add(new Vector3(1, 0, 1));//T9
  108.         vertices.Add(new Vector3(1, 1, 1));//T10
  109.         vertices.Add(new Vector3(1, 1, 0));//T11
  110.  
  111.         normals.Add(new Vector3(1, 0, 0));//8
  112.         normals.Add(new Vector3(1, 0, 0));//9
  113.         normals.Add(new Vector3(1, 0, 0));//10
  114.         normals.Add(new Vector3(1, 0, 0));//11
  115.  
  116.  
  117.         textCoords.Add(new Vector2(0, 0));
  118.         textCoords.Add(new Vector2(0, 1));
  119.         textCoords.Add(new Vector2(1, 0));
  120.         textCoords.Add(new Vector2(1, 1));
  121.  
  122.         //Cara frontal
  123.         vertices.Add(new Vector3(0, 0, 0));//T12
  124.         vertices.Add(new Vector3(1, 0, 0));//T13
  125.         vertices.Add(new Vector3(1, 1, 0));//T14
  126.         vertices.Add(new Vector3(0, 1, 0));//T15
  127.  
  128.         normals.Add(new Vector3(0, 0, -1));//12
  129.         normals.Add(new Vector3(0, 0, -1));//13
  130.         normals.Add(new Vector3(0, 0, -1));//14
  131.         normals.Add(new Vector3(0, 0, -1));//15
  132.  
  133.  
  134.         textCoords.Add(new Vector2(0, 0));
  135.         textCoords.Add(new Vector2(0, 1));
  136.         textCoords.Add(new Vector2(1, 0));
  137.         textCoords.Add(new Vector2(1, 1));
  138.  
  139.         //Cara trasera
  140.         vertices.Add(new Vector3(1, 0, 1));//T16
  141.         vertices.Add(new Vector3(1, 1, 1));//T17
  142.         vertices.Add(new Vector3(0, 1, 1));//T18
  143.         vertices.Add(new Vector3(0, 0, 1));//T19
  144.  
  145.         normals.Add(new Vector3(0, 0, 1));//16
  146.         normals.Add(new Vector3(0, 0, 1));//17
  147.         normals.Add(new Vector3(0, 0, 1));//18
  148.         normals.Add(new Vector3(0, 0, 1));//19
  149.  
  150.  
  151.         textCoords.Add(new Vector2(0, 0));
  152.         textCoords.Add(new Vector2(0, 1));
  153.         textCoords.Add(new Vector2(1, 0));
  154.         textCoords.Add(new Vector2(1, 1));
  155.  
  156.         //Cara Arriba
  157.         vertices.Add(new Vector3(0, 1, 0));//T20
  158.         vertices.Add(new Vector3(1, 1, 0));//T21
  159.         vertices.Add(new Vector3(1, 1, 1));//T22
  160.         vertices.Add(new Vector3(0, 1, 1));//T23
  161.  
  162.         normals.Add(new Vector3(0, -1, 0));//16
  163.         normals.Add(new Vector3(0, -1, 0));//17
  164.         normals.Add(new Vector3(0, -1, 0));//18
  165.         normals.Add(new Vector3(0, -1, 0));//19
  166.  
  167.  
  168.         textCoords.Add(new Vector2(0, 0));
  169.         textCoords.Add(new Vector2(0, 1));
  170.         textCoords.Add(new Vector2(1, 0));
  171.         textCoords.Add(new Vector2(1, 1));
  172.  
  173.  
  174.  
  175.         // Crear los triángulos, a partir de los indices de los vertices en el vector de vertices
  176.         // Cuidado! orientación mano izquierda, vertices en sentido horario
  177.         /*Plano al suelo****************************************************************************************************/
  178.         triangles = new List<int>();
  179.         triangles.Add(0);
  180.         triangles.Add(1);
  181.         triangles.Add(2);
  182.  
  183.         triangles.Add(0);
  184.         triangles.Add(2);
  185.         triangles.Add(3);
  186.  
  187.         /*Plano izquierdo****************************************************************************************************/
  188.         triangles.Add(6);
  189.         triangles.Add(4);
  190.         triangles.Add(5);
  191.  
  192.         triangles.Add(7);
  193.         triangles.Add(4);
  194.         triangles.Add(6);
  195.  
  196.  
  197.         /*Plano derecho****************************************************************************************************/
  198.         triangles.Add(10);
  199.         triangles.Add(8);
  200.         triangles.Add(11);
  201.  
  202.         triangles.Add(10);
  203.         triangles.Add(9);
  204.         triangles.Add(8);
  205.  
  206.         /*Plano frontal****************************************************************************************************/
  207.         triangles.Add(14);
  208.         triangles.Add(13);
  209.         triangles.Add(12);
  210.  
  211.         triangles.Add(14);
  212.         triangles.Add(12);
  213.         triangles.Add(15);
  214.  
  215.         /*Plano trasero****************************************************************************************************/
  216.         triangles.Add(16);
  217.         triangles.Add(17);
  218.         triangles.Add(19);
  219.  
  220.         triangles.Add(19);
  221.         triangles.Add(17);
  222.         triangles.Add(18);
  223.  
  224.  
  225.         /*Plano arriba***************************************************************************************************/
  226.         triangles.Add(20);
  227.         triangles.Add(22);
  228.         triangles.Add(21);
  229.  
  230.         triangles.Add(20);
  231.         triangles.Add(23);
  232.         triangles.Add(22);
  233.  
  234.         // Crear una nueva malla de triángulos a partir de los datos anteriores
  235.         crearMesh();
  236.     }
  237.     void crearTriangulo() {
  238.  
  239.         // Crear los vertices
  240.         vertices = new List<Vector3>();
  241.         vertices.Add(new Vector3(0, 0, 0));
  242.         vertices.Add(new Vector3(0, 0, 1));
  243.         vertices.Add(new Vector3(1, 0, 0));
  244.  
  245.         // Crear las normales de cada vertice
  246.         normals = new List<Vector3>();
  247.         normals.Add(new Vector3(0, 1, 0));
  248.         normals.Add(new Vector3(0, 1, 0));
  249.         normals.Add(new Vector3(0, 1, 0));
  250.  
  251.         // Crear las coordenadas de textura de cada vertice
  252.         textCoords = new List<Vector2>();
  253.         textCoords.Add(new Vector2(0, 0));
  254.         textCoords.Add(new Vector2(0, 1));
  255.         textCoords.Add(new Vector2(1, 0));
  256.  
  257.         // Crear los triángulos, a partir de los indices de los vertices en el vector de vertices
  258.         // Cuidado! orientación mano izquierda, vertices en sentido horario
  259.         triangles = new List<int>();
  260.         triangles.Add(0);
  261.         triangles.Add(1);
  262.         triangles.Add(2);
  263.  
  264.         // Crear una nueva malla de triángulos a partir de los datos anteriores
  265.         crearMesh();
  266.     }
  267.     void crearPlano() {
  268.  
  269.         // Crear los vertices
  270.         vertices = new List<Vector3>();
  271.         normals = new List<Vector3>();
  272.         textCoords = new List<Vector2>();
  273.         triangles = new List<int>();
  274.  
  275.         for (int i=0; i<=particiones; i++) {
  276.             float u = (1.0f / particiones) * i;
  277.             for (int j=0; j<=particiones; j++) {
  278.                 float v = (1.0f / particiones) * j;
  279.                
  280.                 vertices.Add(new Vector3(u,0,v));
  281.                 normals.Add(new Vector3(0,1,0));
  282.                 textCoords.Add(new Vector2(u,v));
  283.             }
  284.         }
  285.  
  286.         // Por cada vertice se crean dos triángulos
  287.         int tamTira = particiones + 1;
  288.         for (int i=1; i<=particiones; i++) {
  289.             for (int j=1; j<=particiones; j++) {
  290.  
  291.                 int currIndex = i * tamTira + j;
  292.                 triangles.Add(currIndex);
  293.                 triangles.Add(currIndex - 1);
  294.                 triangles.Add(currIndex - tamTira - 1);
  295.  
  296.                 triangles.Add(currIndex);
  297.                 triangles.Add(currIndex - tamTira - 1);
  298.                 triangles.Add(currIndex - tamTira);
  299.             }
  300.         }
  301.  
  302.         // Crear una nueva malla de triángulos a partir de los datos anteriores
  303.         crearMesh();
  304.     }
  305.    
  306.    
  307.     void crearToro()
  308.     {
  309.         // Ecuación de la esfera por gajos
  310.         // [U,V] en el rango [0,1]
  311.         // x = radio * cos(2*PI*U) * sin(PI*V)
  312.         // y = radio * cos(PI*V)
  313.         // z = radio * sin(2*PI*U) * sin(PI*V)
  314.  
  315.         float radio = 1;
  316.         float R = 2;
  317.         // Crear los vertices
  318.         vertices = new List<Vector3>();
  319.         normals = new List<Vector3>();
  320.         textCoords = new List<Vector2>();
  321.         triangles = new List<int>();
  322.  
  323.         for (int i = 0; i <= particiones; i++)
  324.         {
  325.             float u = (1.0f / particiones) * i;
  326.             for (int j = 0; j <= particiones; j++)
  327.             {
  328.                 float v = (1.0f / particiones) * j;
  329.  
  330.                 Vector3 aux = new Vector3(
  331.                     Mathf.Cos(2 * Mathf.PI * u) * (R + radio * Mathf.Sin(2* Mathf.PI * v)),
  332.                     (radio * Mathf.Cos(2 * Mathf.PI * v)),
  333.                     Mathf.Sin(2 * Mathf.PI * u) * (R + radio * Mathf.Sin(2* Mathf.PI * v))
  334.                 );
  335.  
  336.                 vertices.Add(radio * aux);
  337.                 normals.Add(aux.normalized);
  338.                 textCoords.Add(new Vector2(u, 1.0f - v));
  339.             }
  340.         }
  341.  
  342.         // Por cada vertice se crean dos triángulos
  343.         int tamTira = particiones + 1;
  344.         for (int i = 1; i <= particiones; i++)
  345.         {
  346.             for (int j = 1; j <= particiones; j++)
  347.             {
  348.  
  349.                 int currIndex = i * tamTira + j;
  350.                 triangles.Add(currIndex);
  351.                 triangles.Add(currIndex - tamTira);
  352.                 triangles.Add(currIndex - tamTira - 1);
  353.  
  354.                 triangles.Add(currIndex);
  355.                 triangles.Add(currIndex - tamTira - 1);
  356.                 triangles.Add(currIndex - 1);
  357.             }
  358.         }
  359.  
  360.         // Crear una nueva malla de triángulos a partir de los datos anteriores
  361.         crearMesh();
  362.     }
  363.  
  364.     void crearCono()
  365.     {
  366.         float radio = 1;
  367.  
  368.         // Crear los vertices
  369.         vertices = new List<Vector3>();
  370.         normals = new List<Vector3>();
  371.         textCoords = new List<Vector2>();
  372.         triangles = new List<int>();
  373.  
  374.  
  375.         vertices.Add(new Vector3(0, 0, 0));
  376.         normals.Add(new Vector3(0, -1, 0));
  377.         textCoords.Add(new Vector2(0f, 0f));
  378.  
  379.         //Base del cilindro parte de abajo
  380.         for (int i = 0; i <= particiones; i++)
  381.         {
  382.             float u = (1.0f / particiones) * i;
  383.  
  384.             Vector3 aux = new Vector3
  385.             (
  386.                 Mathf.Sin(2 * Mathf.PI * u),
  387.                 0,
  388.                 Mathf.Cos(2 * Mathf.PI * u)
  389.             );
  390.  
  391.             vertices.Add(radio * aux);
  392.             normals.Add(new Vector3(0, -1, 0));
  393.             textCoords.Add(new Vector2(Mathf.Sin(2 * Mathf.PI * u), Mathf.Cos(2 * Mathf.PI * u)));
  394.  
  395.  
  396.         }
  397.  
  398.         for(int i = 0; i < particiones + 1; i++)
  399.         {
  400.             triangles.Add(0);
  401.             triangles.Add(i+1);
  402.             triangles.Add(i);
  403.         }
  404.  
  405.         //Base del cilindro parte de arriba
  406.        vertices.Add(new Vector3(0, 1, 0));
  407.         normals.Add(new Vector3(0, 1, 0));
  408.         textCoords.Add(new Vector2(0f, 0f));
  409.  
  410.  
  411.        for (int i = 0; i <= particiones; i++)
  412.         {
  413.             float u = (1.0f / particiones) * i;
  414.  
  415.             Vector3 aux = new Vector3
  416.             (
  417.                 Mathf.Sin(2 * Mathf.PI * u),
  418.                 0,
  419.                 Mathf.Cos(2 * Mathf.PI * u)
  420.             );
  421.  
  422.             vertices.Add((radio * aux));
  423.             normals.Add(aux);
  424.             textCoords.Add(new Vector2(Mathf.Sin(2 * Mathf.PI * u), Mathf.Cos(2 * Mathf.PI * u)));
  425.  
  426.         }
  427.  
  428.         for (int i = 1; i <= particiones + 2; i++)
  429.         {
  430.             triangles.Add(particiones + 2);
  431.             triangles.Add(particiones + i);
  432.             triangles.Add(particiones + (i + 1));
  433.         }
  434.         // Crear una nueva malla de triángulos a partir de los datos anteriores
  435.         crearMesh();
  436.     }
  437.  
  438.  
  439.     void crearCilindro()
  440.     {
  441.         float radio = 1;
  442.  
  443.         // Crear los vertices
  444.         vertices = new List<Vector3>();
  445.         normals = new List<Vector3>();
  446.         textCoords = new List<Vector2>();
  447.         triangles = new List<int>();
  448.  
  449.  
  450.         vertices.Add(new Vector3(0, 0, 0));
  451.         normals.Add(new Vector3(0, -1, 0));
  452.         textCoords.Add(new Vector2(0f, 0f));
  453.  
  454.         //Base del cilindro parte de abajo
  455.         for (int i = 0; i <= particiones; i++)
  456.         {
  457.             float u = (1.0f / particiones) * i;
  458.  
  459.             Vector3 aux = new Vector3
  460.             (
  461.                 Mathf.Sin(2 * Mathf.PI * u),
  462.                 0,
  463.                 Mathf.Cos(2 * Mathf.PI * u)
  464.             );
  465.  
  466.             vertices.Add(radio * aux);
  467.             normals.Add(new Vector3(0, -1, 0));
  468.             textCoords.Add(new Vector2(Mathf.Sin(2 * Mathf.PI * u), Mathf.Cos(2 * Mathf.PI * u)));
  469.         }
  470.  
  471.         for (int i = 0; i < particiones + 1; i++)
  472.         {
  473.             triangles.Add(0);
  474.             triangles.Add(i + 1);
  475.             triangles.Add(i);
  476.         }
  477.  
  478.         crearMesh();
  479.     }
  480.     void crearCilindroPrueba()
  481.     {
  482.  
  483.         // Ecuación de la esfera por gajos
  484.         // [U,V] en el rango [0,1]
  485.         // x = radio * cos(2*PI*U) * sin(PI*V)
  486.         // y = radio * cos(PI*V)
  487.         // z = radio * sin(2*PI*U) * sin(PI*V)
  488.  
  489.         float radio = 1;
  490.  
  491.         // Crear los vertices
  492.         vertices = new List<Vector3>();
  493.         normals = new List<Vector3>();
  494.         textCoords = new List<Vector2>();
  495.         triangles = new List<int>();
  496.  
  497.  
  498.         //Caras del cilindro
  499.        /*for (int i = 0; i <= particiones; i++)
  500.         {
  501.             float u = (1.0f / particiones) * i;
  502.             for (int j = 0; j <= particiones; j++)
  503.             {
  504.                 float v = (1.0f / particiones) * j;
  505.  
  506.                 Vector3 aux = new Vector3(
  507.                     Mathf.Cos(2 * Mathf.PI * u),
  508.                     Mathf.Cos(Mathf.PI * v),
  509.                     Mathf.Sin(2 * Mathf.PI * u)
  510.                 );
  511.  
  512.                 vertices.Add(radio * aux);
  513.                 normals.Add(aux.normalized);
  514.                 textCoords.Add(new Vector2(u, 1.0f - v));
  515.             }
  516.         }*/
  517.  
  518.         //Base del cilindro parte de arriba
  519.         /*for (int i = 0; i <= particiones; i++)
  520.         {
  521.             float u = (1.0f / particiones) * i;
  522.             //for (int j = 0; j <= particiones; j++)
  523.             //{
  524.                 float v = (1.0f / particiones);
  525.  
  526.                 Vector3 aux = new Vector3
  527.                 (
  528.                     Mathf.Sin(2 * Mathf.PI * u),
  529.                     Mathf.Cos(Mathf.PI * v),
  530.                     Mathf.Cos(2 * Mathf.PI * u)
  531.                 );
  532.  
  533.                 vertices.Add(radio * aux);
  534.  
  535.             // normals.Add(new Vector3(Mathf.Sin(2 * Mathf.PI * u), 0, Mathf.Cos(2 * Mathf.PI * u)));
  536.             normals.Add(new Vector3(0, 1,0));
  537.             textCoords.Add(new Vector2(u, 1.0f - v));
  538.  
  539.                /* triangles.Add(aux);
  540.                 triangles.Add(aux + 1);
  541.                 triangles.Add(radio / 2);
  542.             //}
  543.         }*/
  544.  
  545.  
  546.         //Base del cilindro parte de abajo
  547.         for (int i = 0; i <= particiones; i++)
  548.         {
  549.             float u = (1.0f / particiones) * i;
  550.  
  551.             float v = (1.0f / particiones);
  552.  
  553.             Vector3 aux = new Vector3
  554.             (
  555.                 Mathf.Sin(2 * Mathf.PI * u),
  556.                 0,
  557.                 Mathf.Cos(2 * Mathf.PI * u)
  558.             );
  559.  
  560.             vertices.Add(radio * aux);
  561.             // normals.Add(new Vector3(Mathf.Sin(2 * Mathf.PI * u), 0, Mathf.Cos(2 * Mathf.PI * u)));
  562.             normals.Add(new Vector3(0, 1, 0));
  563.             textCoords.Add(new Vector2(u, 1.0f - v));
  564.  
  565.         }
  566.  
  567.  
  568.  
  569.        /* for(int i = 0; i < particiones; i++)
  570.         {
  571.  
  572.             triangles.Add(i);
  573.             triangles.Add(i+1);
  574.             triangles.Add(particiones);
  575.         }
  576.         */
  577.  
  578.         // Por cada vertice se crean dos triángulos
  579.         int tamTira = particiones + 1;
  580.         for (int i = 1; i <= particiones; i++)
  581.         {
  582.             for (int j = 1; j <= particiones; j++)
  583.             {
  584.  
  585.                 int currIndex = i * tamTira + j;
  586.                 triangles.Add(currIndex);
  587.                 triangles.Add(currIndex - tamTira);
  588.                 triangles.Add(currIndex - tamTira - 1);
  589.  
  590.                 triangles.Add(currIndex);
  591.                 triangles.Add(currIndex - tamTira - 1);
  592.                 triangles.Add(currIndex - 1);
  593.             }
  594.  
  595.  
  596.         }
  597.  
  598.         // Crear una nueva malla de triángulos a partir de los datos anteriores
  599.         crearMesh();
  600.     }
  601.     void crearEsfera() {
  602.  
  603.         // Ecuación de la esfera por gajos
  604.         // [U,V] en el rango [0,1]
  605.         // x = radio * cos(2*PI*U) * sin(PI*V)
  606.         // y = radio * cos(PI*V)
  607.         // z = radio * sin(2*PI*U) * sin(PI*V)
  608.  
  609.         float radio = 1;
  610.  
  611.         // Crear los vertices
  612.         vertices = new List<Vector3>();
  613.         normals = new List<Vector3>();
  614.         textCoords = new List<Vector2>();
  615.         triangles = new List<int>();
  616.  
  617.         for (int i=0; i<=particiones; i++) {
  618.             float u = (1.0f / particiones) * i;
  619.             for (int j=0; j<=particiones; j++) {
  620.                 float v = (1.0f / particiones) * j;
  621.  
  622.                 Vector3 aux = new Vector3(
  623.                     Mathf.Cos(2*Mathf.PI*u) * Mathf.Sin(Mathf.PI * v),
  624.                     Mathf.Cos(Mathf.PI*v),
  625.                     Mathf.Sin(2*Mathf.PI*u) * Mathf.Sin(Mathf.PI * v)
  626.                 );
  627.  
  628.                 vertices.Add(radio * aux);
  629.                 normals.Add(aux.normalized);
  630.                 textCoords.Add(new Vector2(u,1.0f-v));
  631.             }
  632.         }
  633.  
  634.         // Por cada vertice se crean dos triángulos
  635.         int tamTira = particiones + 1;
  636.         for (int i=1; i<=particiones; i++) {
  637.             for (int j=1; j<=particiones; j++) {
  638.  
  639.                 int currIndex = i * tamTira + j;
  640.                 triangles.Add(currIndex);
  641.                 triangles.Add(currIndex - tamTira);
  642.                 triangles.Add(currIndex - tamTira - 1);
  643.  
  644.                 triangles.Add(currIndex);
  645.                 triangles.Add(currIndex - tamTira - 1);
  646.                 triangles.Add(currIndex - 1);
  647.             }
  648.         }
  649.  
  650.         // Crear una nueva malla de triángulos a partir de los datos anteriores
  651.         crearMesh();
  652.     }
  653.  
  654.     public void crearMesh() {
  655.  
  656.         // Crear la mesh con toda la información
  657.         Mesh m = new Mesh();
  658.         m.vertices = vertices.ToArray();
  659.         m.normals = normals.ToArray();
  660.         m.uv = textCoords.ToArray();
  661.         m.triangles = triangles.ToArray();
  662.  
  663.         // Llamada obligatoria para recalcular la información
  664.         // de la malla a partir de los vectores asignados
  665.         m.RecalculateBounds();  
  666.  
  667.         // Asignar la malla al componente MeshFilter del Gameobject
  668.         GetComponent<MeshFilter>().sharedMesh = m;
  669.     }
  670. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement