unity通過Mesh網(wǎng)格繪制圖形(三角形、正方體、圓柱)
一、介紹
Mesh類:通過腳本創(chuàng)建或是獲取網(wǎng)格的類,網(wǎng)格包含多個頂點和三角形數(shù)組。頂點信息包含坐標和所在面的法線。
unity中3D的世界的所有圖形全部都是由三角形構(gòu)成的。
比如unity已經(jīng)裝配好的幾種圖形我們可以看一下:
我們可以在unity中通過Mesh類來繪制圖形。
所以在我們繪制其它圖形之前,首先完成一個小目標,畫一個三角形。
二、繪制三角形
首先做準備工作:
1.在場景中創(chuàng)建一個空物體,并掛載MeshRenderer和MeshFilter組件。//我們先不考慮碰撞器的問題,所以不添加collider
2.創(chuàng)建一個默認材質(zhì),拖入MeshRenderer組件中,為可視化做準備。
3.創(chuàng)建一個腳本,掛載在空物體上。然后開始編輯代碼
using System.Collections.Generic; using UnityEngine; public class test : MonoBehaviour { // 網(wǎng)格渲染器 MeshRenderer meshRenderer; // 網(wǎng)格過濾器 MeshFilter meshFilter; // 用來存放頂點數(shù)據(jù) List<Vector3> verts; // 頂點列表 List<int> indices; // 序號列表 private void Start() { verts = new List<Vector3>(); indices = new List<int>(); meshRenderer = GetComponent<MeshRenderer>(); meshFilter = GetComponent<MeshFilter>(); Generate(); } public void Generate() { // 把頂點和序號數(shù)據(jù)填寫在列表里 AddMeshData1();//三角形 // 用列表數(shù)據(jù)創(chuàng)建網(wǎng)格Mesh對象 Mesh mesh = new Mesh(); mesh.SetVertices(verts); //mesh.vertices = verts.ToArray(); mesh.SetIndices(indices, MeshTopology.Triangles, 0); //mesh.triangles = indices.ToArray(); // 自動計算法線 mesh.RecalculateNormals(); // 自動計算物體的整體邊界 mesh.RecalculateBounds(); // 將mesh對象賦值給網(wǎng)格過濾器,就完成了 meshFilter.mesh = mesh; } // 填寫頂點和序號列表 void AddMeshData1() { verts.Add(new Vector3(0, 0, 0)); verts.Add(new Vector3(0, 0, 1)); verts.Add(new Vector3(1, 0, 1)); indices.Add(0); indices.Add(1); indices.Add(2); } }
運行后是這個樣子的:
然后來解讀一下代碼:
準備了一個List<Vector3> verts
來存放三角形的各個頂點坐標
準備了一個List<int> indices
來存放讀取的頂點坐標的順序
獲取了空物體的MeshRenderer
和MeshFilter
。
注意:
MeshFilter: 網(wǎng)格過濾器
作用:指定mesh(物體的幾何形狀)
MeshRenderer: 網(wǎng)格渲染器
網(wǎng)格渲染器從網(wǎng)格過濾器中獲得幾何體的形狀然后進行渲染。
即:Mesh是網(wǎng)格,MeshFilter是從網(wǎng)格中獲取圖形的組件,MeshRenderer是渲染從MeshFilter獲取到的圖形的組件。
然后為List<int> indices
增加了三個點,(0,0,0),(0,0,1),(1,0,1),并且就是按照這個順序讀取
mesh.SetVertices(verts); //mesh.vertices = verts.ToArray();
使用這兩種方法任選其一為mesh添加頂點
mesh.SetIndices(indices, MeshTopology.Triangles, 0); //mesh.triangles = indices.ToArray();
使用這兩種方法任選其一為mesh添加三角形的頂點順序
// 自動計算法線 mesh.RecalculateNormals(); // 自動計算物體的整體邊界 mesh.RecalculateBounds(); // 將mesh對象賦值給網(wǎng)格過濾器,就完成了 meshFilter.mesh = mesh;
這里注意一點,三角形會在三個頂點分別生成法線垂直于平面,法線的朝向遵循左手定則
即,一個三角形面有三條法線。所以這個三角形是朝上的,攝像頭從上而下就能看到此三角形。如果將攝像頭放在三角形的下方朝上看,那么就會看不到此三角形。
自下而上去看,并不能看到三角形。
三、繪制正方體
正方體有6個面,一共12個三角形組成,這邊我們偷個懶,只拼裝三個面。
那么就需要7個頂點,6個三角形。
注意遵循左手定則,則我們需要添加的六個三角形是:
012 023 145 152 253 356
void AddMeshData2() { verts.Add(new Vector3(0, 0, 0)); verts.Add(new Vector3(0, 1, 0)); verts.Add(new Vector3(1, 1, 0)); verts.Add(new Vector3(1, 0, 0)); verts.Add(new Vector3(0, 1, 1)); verts.Add(new Vector3(1, 1, 1)); verts.Add(new Vector3(1, 0, 1)); indices.Add(0); indices.Add(1); indices.Add(2); indices.Add(0); indices.Add(2); indices.Add(3); indices.Add(1); indices.Add(4); indices.Add(5); indices.Add(1); indices.Add(5); indices.Add(2); indices.Add(2); indices.Add(5); indices.Add(3); indices.Add(3); indices.Add(5); indices.Add(6); }
繪制出來是這樣的:
將線框關(guān)閉,我們發(fā)現(xiàn)三個面就好像融合在一起了一樣:
我們知道,unity是根據(jù)面(三角形的三個頂點)的法線的方向的不同,來判斷面的朝向,而在上面的示例中,2號點,既在0123平面上,也在2563平面上,也在1254平面上,而它并沒有三根法線(這里其實是四個三角形共享2號點,有四個法線,但是不知道同面的兩根法線是否都參與mesh.RecalculateNormals()中的計算導致法線平均值向左方向偏移,等大佬指正)。
所以在調(diào)用mesh.RecalculateNormals();
unity會自動將這三個法線計算,最終得出一個平均值,作為最后的法線。當然1,3,5三個點也是因為是交界處也會計算法線的平均值。最后渲染出像是都處在同一個平面的效果。
解決的辦法也很簡單,就是將重合的點不共用,拆開來進行計算
即添加12個點,6個三角形分別是:
012 023 456 467 8910 81011
void AddMeshData4() { verts.Add(new Vector3(0, 0, 0));//0 verts.Add(new Vector3(0, 1, 0));//1 verts.Add(new Vector3(1, 1, 0));//2 verts.Add(new Vector3(1, 0, 0));//3 verts.Add(new Vector3(0, 1, 0));//4 verts.Add(new Vector3(0, 1, 1));//5 verts.Add(new Vector3(1, 1, 1));//6 verts.Add(new Vector3(1, 1, 0));//7 verts.Add(new Vector3(1, 0, 0));//8 verts.Add(new Vector3(1, 1, 0));//9 verts.Add(new Vector3(1, 1, 1));//10 verts.Add(new Vector3(1, 0, 1));//11 indices.Add(0); indices.Add(1); indices.Add(2); indices.Add(0); indices.Add(2); indices.Add(3); indices.Add(4); indices.Add(5); indices.Add(6); indices.Add(4); indices.Add(6); indices.Add(7); indices.Add(8); indices.Add(9); indices.Add(10); indices.Add(8); indices.Add(10); indices.Add(11); }
這樣產(chǎn)生的就是棱角分明的正方體了(背面空空)
四、繪制圓柱體
首先看一下成品的效果
其實就是用兩個圓形面加上側(cè)面的一些拼接成的矩形構(gòu)成
注意點:
1.側(cè)面和上下圓形不共享使用頂點,而側(cè)面各個三角形需要共享使用頂點。原因是法線的朝向問題,正方形那里講的很清楚了。
2.我這里用Mathf.Sin方法和Mathf.Cos方法來計算圓形面上各個頂點的坐標,方法參數(shù)是弧度。
void AddMeshData7(int n)//n為圓形包含三角形的數(shù)量 { int a = 360 / n; verts.Add(new Vector3(0, 0, 0)); for (int i = 0; i < n; i++) //上面 { verts.Add(new Vector3(Mathf.Sin(a * Mathf.Deg2Rad), 0, Mathf.Cos(a * Mathf.Deg2Rad))); a += 360 / n; } verts.Add(new Vector3(0, -2, 0)); for (int i = 1; i < n + 1; i++) //下面 { verts.Add(verts[i] + new Vector3(0, -2, 0)); } Debug.Log("verts.Count: " + verts.Count); for (int i = 0; i < n; i++) //繪制上面 { indices.Add(0); indices.Add(i + 1); if (i + 1 >= n) { indices.Add(1); } else { indices.Add(i + 2); } } for (int i = n + 1; i < 2 * n + 1; i++) //繪制下面 { Debug.Log("i " + i); indices.Add(n + 1); if (i + 2 >= 2 * n + 2) { indices.Add(n + 2); } else { indices.Add(i + 2); } indices.Add(i + 1); } int oldcount = verts.Count; Debug.Log(oldcount); for (int i = 0; i < oldcount; i++) { verts.Add(verts[i]); } for (int i = oldcount + 1; i <= oldcount + n; i++) //繪制側(cè)面 { indices.Add(i); indices.Add(i + n + 1); if (i + 1 >= n + oldcount) { indices.Add(oldcount + 1); } else { indices.Add(i + 1); Debug.Log(verts[i + 1]); } indices.Add(i + n + 1); if (i + n + 2 > 2 * n + 1 + oldcount) { indices.Add(oldcount + n + 2); indices.Add(oldcount + 1); } else { indices.Add(i + n + 2); indices.Add(i + 1); } } }
下一篇討論unity通過Mesh網(wǎng)格繪制球體&通過柏林噪聲繪制地形
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#異步迭代IAsyncEnumerable應用實現(xiàn)
IAsyncEnumerable可以來實現(xiàn)異步迭代,本文就主要介紹了C#異步迭代IAsyncEnumerable應用實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06c#中使用BackgroundWorker的實現(xiàn)
本文主要介紹了c#中使用BackgroundWorker的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06