亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

unity通過Mesh網(wǎng)格繪制圖形(三角形、正方體、圓柱)

 更新時間:2021年11月17日 11:53:23   作者:akuojustdoit  
這篇文章主要為大家詳細介紹了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來存放讀取的頂點坐標的順序
獲取了空物體的MeshRendererMeshFilter

注意:

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)

    C#異步迭代IAsyncEnumerable應用實現(xiàn)

    IAsyncEnumerable可以來實現(xiàn)異步迭代,本文就主要介紹了C#異步迭代IAsyncEnumerable應用實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • c#中使用BackgroundWorker的實現(xiàn)

    c#中使用BackgroundWorker的實現(xiàn)

    本文主要介紹了c#中使用BackgroundWorker的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • C#使用RSA加密解密文件

    C#使用RSA加密解密文件

    這篇文章主要為大家詳細介紹了C#使用RSA加密解密文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 剖析設(shè)計模式編程中C#對于組合模式的運用

    剖析設(shè)計模式編程中C#對于組合模式的運用

    這篇文章主要介紹了設(shè)計模式編程中C#對于組合模式的運用,理論上來說組合模式包含抽象構(gòu)件、樹葉構(gòu)件和樹枝構(gòu)件三個角色,需要的朋友可以參考下
    2016-02-02
  • c#字符串編碼編碼(encoding)使用方法示例

    c#字符串編碼編碼(encoding)使用方法示例

    System.Text提供了Encoding的抽象類,這個類提供字符串編碼的方法。使Unicode字符數(shù)組的字符串,轉(zhuǎn)換為指定編碼的字節(jié)數(shù)組,或者反之,看下面的例子
    2013-12-12
  • C#使用ZXing實現(xiàn)二維碼和條形碼的生成

    C#使用ZXing實現(xiàn)二維碼和條形碼的生成

    這篇文章主要為大家詳細介紹了C#如何使用ZXing實現(xiàn)二維碼和條形碼的生成與識別,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • C#中使用CliWrap讓命令行交互舉重若輕

    C#中使用CliWrap讓命令行交互舉重若輕

    這篇文章介紹了C#中使用CliWrap讓命令行交互舉重若輕,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • C# double類型變量比較分析

    C# double類型變量比較分析

    這篇文章主要介紹了C# double類型變量比較分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C#實現(xiàn)批量更改文件名稱大小寫或擴展名

    C#實現(xiàn)批量更改文件名稱大小寫或擴展名

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)批量更改文件名稱大小寫或擴展名的功能,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • C#中跨線程訪問控件問題解決方案分享

    C#中跨線程訪問控件問題解決方案分享

    這篇文章主要介紹了C#中跨線程訪問控件問題解決方案,有需要的朋友可以參考一下
    2013-11-11

最新評論