Mesh網格基礎知識


下面是摘自網上的一篇教程,寫得不錯
// 通過object對象名 face 得到網格渲染器對象   
MeshFilter meshFilter = (MeshFilter)GameObject.Find( " face ").GetComponent( typeof(MeshFilter));  
          
// 通過渲染器對象得到網格對象   
Mesh mesh = meshFilter.mesh;  
      
// API中寫的不是提清楚,我詳細的在說一遍   
          
// 設置頂點,這個屬性非常重要   
// 三個點確定一個面,所以Vector3數組的數量一定是3個倍數   
// 遵循順時針三點確定一面   
// 這里的數量為6 也就是我創建了2個三角面   
// 依次填寫3D坐標點   
mesh.vertices =  new Vector3[] { new Vector3( 500),  new Vector3( 050),  new Vector3( 005), new Vector3(- 500),  new Vector3( 0, - 50),  new Vector3( 00, - 5)};  
// mesh.vertices = new Vector3[] {new Vector3(5, 0, 0), new Vector3(0, 5, 0), new Vector3(0, 0, 5)};  
      
// 設置貼圖點,因為面確定出來以后就是就是2D    
// 所以貼紙貼圖數量為Vector2    
// 第一個三角形設置5個貼圖   
// 第二個三角形設置一個貼圖   
// 數值數量依然要和頂點的數量一樣   
mesh.uv =  new Vector2[] { new Vector2( 00),  new Vector2( 05),  new Vector2( 55)};  
// mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
          
// 設置三角形索引,這個索引是根據上面頂點坐標數組的索引   
// 對應着定點數組Vector3中的每一項   
// 最后將兩個三角形繪制在平面中   
// 數值數量依然要和頂點的數量一樣   
mesh.triangles=  new  int []{ 0, 1, 2};  

 
我測試時導入了一個立方體,測試時發現導入的立方體頂點是由4*6組成的,也就是說4個點組成一個面,立方體共6個面

頂點是由4*6組成的,但u3d還是會以三角網去解析,關鍵就是triangles,它負責存儲每個三角網的頂點,也就是立方體的一個面由兩個三角形組成

MeshFilter meshFilter = (MeshFilter)GameObject.Find( " box ").GetComponent( typeof(MeshFilter));
Mesh mesh = meshFilter.mesh;
string strVertices =  "";
foreach(Vector3 v3  in mesh.vertices){
     if (strVertices== ""){
        strVertices = v3.ToString();
    }
     else{
        strVertices +=  " , " + v3.ToString();
    }
}
Debug.Log(mesh.vertices.Length +  "   " + strVertices);

string strUV =  "";
foreach(Vector2 v2  in mesh.uv){
     if (strUV== ""){
        strUV = v2.ToString();
    }
     else{
        strUV +=  " , " + v2.ToString();
    }
}
Debug.Log(mesh.uv.Length +  "   "+ strUV);
        
string strTriangles =  "";
foreach( int t  in mesh.triangles){
     if (strTriangles== ""){
        strTriangles = t.ToString();
    }
     else{
        strTriangles +=  " , " + t.ToString();
    }
}
Debug.Log(mesh.triangles.Length +  "   " + strTriangles);

輸出的信息如下:

(0.6, -0.6, 0.0),(-0.6, -0.6, 0.0),(0.6, 0.6, 0.0),(-0.6, 0.6, 0.0),
(0.6, -0.6, 0.7),(-0.6, -0.6, 0.7),(0.6, 0.6, 0.7),(-0.6, 0.6, 0.7),
(0.6, -0.6, 0.0),(-0.6, -0.6, 0.7),(-0.6, -0.6, 0.0),(0.6, -0.6, 0.7),
(-0.6, -0.6, 0.0),(-0.6, 0.6, 0.7),(-0.6, 0.6, 0.0),(-0.6, -0.6, 0.7),
(-0.6, 0.6, 0.0),(0.6, 0.6, 0.7),(0.6, 0.6, 0.0),(-0.6, 0.6, 0.7),
(0.6, 0.6, 0.0),(0.6, -0.6, 0.7),(0.6, -0.6, 0.0),(0.6, 0.6, 0.7)

(0.6, 0.1),(0.4, 0.1),(0.6, 0.4),(0.4, 0.4),
(0.4, 0.4),(0.6, 0.4),(0.4, 0.6),(0.6, 0.6),
(0.6, 0.4),(0.9, 0.6),(0.9, 0.4),(0.6, 0.6),
(0.3, 0.7),(0.6, 0.9),(0.6, 0.7),(0.3, 0.9),
(0.6, 0.7),(0.9, 0.9),(0.9, 0.7),(0.6, 0.9),
(0.1, 0.4),(0.3, 0.6),(0.3, 0.4),(0.1, 0.6)

2,0,3,
3,0,1,
5,4,7,
7,4,6,
10,8,9,
9,8,11,
14,12,13,
13,12,15,
18,16,17,
17,16,19,
22,20,21,
21,20,23


可以看出第一個平面是由0,1,2,3這四個點組成,在triangles中指定2,0,3這三個點繪制一個三角網,3,0,1這三個點繪制一個三角網。


免責聲明!

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



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