Unity3D之Mesh(四)繪制多邊形


來自https://www.cnblogs.com/JLZT1223/p/6086191.html

1、

總的來說繪制平面的思想十分簡單,就是將需要的平面拆分成幾個三角形然后進行繪制就可以啦,主要的思路就在於三角形的拆分。如果說一個平面有7個頂點,我們把它們分別編號0到6,拆分情況如圖所示:

即:如果用n來表示頂點的個數,那么在同一個平面內,可以分割的三角形個數是:n-2;

2、

在這里,我們選擇應用Mesh Filter組件來進行繪制,Mesh Filter組件里的Mesh屬性就是我們這次主要操作的對象,在這里,我們用到:

  mesh.vertices數組 和 mesh.triangles數組,第一個是vector3的數組,第二個是int的數組。

  其中mesh.vertices存儲的就是平面的頂點信息,對應上圖就是0到6號這六個點的坐標。

  mesh.triangles存儲的是平面繪制時,繪制三角形的頂點順序,對應上圖應該是:

  061 651 521 542 432(順時針)

  每三個一組代表一個三角形,但是大家在這里要注意一下,就是最終繪制出的小三角形是單向圖,就是一面可以看到,另一面是看不到的,所以,為了保證所有的小三角形朝向一至,要對mesh.triangles數組在進行調整,調整結果如下:

  016 156 125 245 234(逆時針)

  就是保證小三角形頂點都是按順時針或者逆時針讀取~大家想想就明白了~

  故:基本算法思想就是:

  入口參數:vector3[] vertices,儲存平面定點信息,頂點需按順序儲存

  算法思想:從數組首尾向中間遍歷,生成triangles頂點ID數組(下列代碼中注釋的部分)


 

步驟:

1、創建一個empty 的gameobject;

2、添加一個腳本給這個game object;

算法實現代碼如下:

 

復制代碼
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class quad : MonoBehaviour
{
    /*
    creat a triangle by using Mesh 
     2016/11/21
                 ————Carl    
    */
    void Start()
    {
        creatPolygon();
    }

   
    private void creatPolygon()
    {
        /* 1. 頂點,三角形,法線,uv坐標, 絕對必要的部分只有頂點和三角形。  
              如果模型中不需要場景中的光照,那么就不需要法線。如果模型不需要貼材質,那么就不需要UV */
        Vector3[] vertices =
        {
         new Vector3 (2f,0,0),
         new Vector3(4f, 0, 0),
         new Vector3(6f, 0, 0),
         new Vector3(10f, 0, 0),
         new Vector3(10f, 20f, 0),
         new Vector3(6f,10f, 0),
         new Vector3(4f, 4f, 0)
        
        };

        Vector3[] normals =
        {
            Vector3.up,
            Vector3.up,
            Vector3.up,
            Vector3.up,
            Vector3.up,
            Vector3.up,
            Vector3.up
           
        };

        Vector2[] uv =
        {
            Vector2.zero,
            -Vector2.left,
            Vector2.one,
            Vector2.right,
            Vector2.zero,
            -Vector2.left,
            Vector2.one
          
        };
        /*2. 三角形,頂點索引:  
         三角形是由3個整數確定的,各個整數就是角的頂點的index。 各個三角形的頂點的順序通常由下往上數, 可以是順時針也可以是逆時針,這通常取決於我們從哪個方向看三角形。 通常,當mesh渲染時,"逆時針" 的面會被擋掉。 我們希望保證順時針的面與法線的主向一致 */
        int[] indices = new int[15];
        indices[0] = 0;
        indices[1] = 6;
        indices[2] = 1;

        indices[3] = 6;
        indices[4] = 2;
        indices[5] = 1;

        indices[6] =6;
        indices[7] = 5;
        indices[8] = 2;

        indices[9] = 5;
        indices[10] = 4;
        indices[11] = 2;

        indices[12] = 4;
        indices[13] = 3;
        indices[14] = 2;
        //int numberOfTriangles = vertices.Length - 2;//三角形的數量等於頂點數減2
        //int[] indices = new int[numberOfTriangles * 3];//triangles數組大小等於三角形數量乘3 此時是15
        //int f = 0, b = vertices.Length - 1;//f記錄前半部分遍歷位置,b記錄后半部分遍歷位置 即0-7
        //for (int i = 1; i <= numberOfTriangles; i++)//每次給 triangles數組中的三個元素賦值,共賦值
        //{ //numberOfTriangles次
        //    if (i % 2 == 1)
        //    {
        //        indices[3 * i - 3] = f++;
        //        indices[3 * i - 2] = f;
        //        indices[3 * i - 1] = b;//正向賦值,對於i=1賦值為:0,1,2
        //    }
        //    else
        //    {
        //        indices[3 * i - 1] = b--;
        //        indices[3 * i - 2] = b;
        //        indices[3 * i - 3] = f;//逆向賦值,對於i=2賦值為:1,5,6
        //    }

        Mesh mesh = new Mesh();
            mesh.vertices = vertices;
            mesh.normals = normals;
            mesh.uv = uv;
            mesh.triangles = indices;

            MeshFilter meshfilter = this.gameObject.GetComponent<MeshFilter>();
            meshfilter.mesh = mesh;
        }

    }
復制代碼

效果圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM