Unity Mesh详解

发表于2017-08-29
评论0 6.8k浏览
Mesh是Unity中的一个组件,称为网格组件。通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而一个复杂的多边形,实际上是由多个三角面拼接而成。所以一个3D模型的表面是由多个彼此相连的三角面构成。三维空间中,构成这些三角面的点以及三角形的边的集合就是Mesh。

下面是代码创建Mesh:
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4.   
  5. public class MeshTest : MonoBehaviour   
  6. {  
  7.     public Texture2D texture2d;  
  8.   
  9.     void Start ()   
  10.     {  
  11.         PlaneData planeData = new PlaneData();  
  12.         planeData.Width = 5.12f;  
  13.         planeData.Hight = 2.56f;  
  14.         planeData.widthSegments = 50;  
  15.         planeData.hightSegments = 50;  
  16.         planeData.PlaneName = "onePlane";  
  17.   
  18.         Plane plane = new Plane(planeData);  
  19.         plane.PlaneGameobject.GetComponent().material.mainTexture = texture2d;  
  20.         plane.PlaneGameobject.AddComponent();  
  21.     }  
  22.   
  23.   
  24.     //绘制一个包含四个顶点的面片,需要:  
  25.     //1、四个顶点的坐标数组  
  26.     //2、三角形顶点的顺序排列的数组  
  27.     //3、每个顶点的法线的数组  
  28.     //4、每个顶点在0-1坐标范围内的信息的数组  
  29.     //5、需要MeshFiler组件  
  30.     //6、需要MeshRender组件  
  31.     //http://docs.unity3d.com/Manual/Example-CreatingaBillboardPlane.html  
  32.     void CreatePlane()  
  33.     {  
  34.         //plane宽度  
  35.         float width = 50f;  
  36.         //plane高度  
  37.         float hight = 50f;  
  38.         //顶点坐标数组  
  39.         Vector3[] newVertices;  
  40.         //三角形顶点的顺序排列的数组  
  41.         int[] newTriangles;  
  42.         //每个顶点的法线的数组  
  43.         Vector3[] newNormals;  
  44.         //每个顶点在0-1坐标范围内的信息的数组  
  45.         Vector2[] newUV;  
  46.   
  47.   
  48.         //创建一个GameObject  
  49.         GameObject meshObj = new GameObject("plane");  
  50.         meshObj.transform.localPosition = Vector3.zero;  
  51.         meshObj.transform.localScale = Vector3.one;  
  52.   
  53.         //添加MeshFilter组件  
  54.         MeshFilter meshFilter = meshObj.AddComponent();  
  55.   
  56.         //添加MeshRenderer组件  
  57.         MeshRenderer meshRender = meshObj.AddComponent();  
  58.   
  59.         //设置一个shader  
  60.         meshRender.material = new Material(Shader.Find("Sprites/Default"));  
  61.   
  62.         //创建一个Mesh  
  63.         Mesh mesh = new Mesh();  
  64.   
  65.         //设置点的数组  
  66.         newVertices = new Vector3[]  
  67.         {  
  68.             new Vector3(0, 0, 0),  
  69.             new Vector3(width, 0, 0),  
  70.             new Vector3(0, hight , 0),  
  71.             new Vector3(width, hight, 0)  
  72.         };  
  73.   
  74.         //一个四边形由两个三角形构建,两个三角形由点构建时的点的顺序  
  75.         //2            2  --  3  
  76.         //  |\            \ |    
  77.         //  | \            \|   
  78.         //0  --  1            1   
  79.   
  80.         //0 -> 1 -> 2  
  81.         //2 -> 1 -> 3  
  82.   
  83.         newTriangles = new int[6];  
  84.         newTriangles[0] = 0;  
  85.         newTriangles[1] = 1;  
  86.         newTriangles[2] = 2;  
  87.         newTriangles[3] = 2;  
  88.         newTriangles[4] = 1;  
  89.         newTriangles[5] = 3;  
  90.   
  91.   
  92.         //四个顶点的法线  
  93.         newNormals = new Vector3[4]  
  94.         {  
  95.             Vector3.forward,  
  96.             Vector3.forward,  
  97.             Vector3.forward,  
  98.             Vector3.forward  
  99.         };  
  100.   
  101.   
  102.         //四个顶点在UV坐标系中的位置   
  103.         newUV = new Vector2[4]   
  104.         {  
  105.            new Vector2(0, 0),  
  106.            new Vector2(1, 0),  
  107.            new Vector2(0, 1),  
  108.            new Vector2(1, 1)  
  109.         };  
  110.   
  111.   
  112.         mesh.vertices = newVertices;  
  113.         mesh.uv = newUV;  
  114.         mesh.triangles = newTriangles;  
  115.   
  116.         meshFilter.mesh = mesh;  
  117.     }  
  118.   
  119. }  
  120.   
  121.   
  122. public class PlaneData  
  123. {  
  124.     //plane宽度  
  125.     public float Width = 1f;  
  126.   
  127.     //plane高度  
  128.     public float Hight = 1f;  
  129.   
  130.     //宽度分段  
  131.     public int widthSegments = 1;  
  132.   
  133.     //高度分段  
  134.     public int hightSegments = 1;  
  135.   
  136.     //面片的名字  
  137.     public string PlaneName = "plane";  
  138. }  
  139.   
  140. public class Plane  
  141. {  
  142.     PlaneData PlaneData;  
  143.       
  144.     public GameObject PlaneGameobject;  
  145.   
  146.     public Plane(PlaneData planeData)  
  147.     {  
  148.         PlaneData = planeData;  
  149.   
  150.         PlaneGameobject = CreatePlane();  
  151.     }  
  152.   
  153.     public GameObject GetPlaneGameobject()  
  154.     {  
  155.         return PlaneGameobject;  
  156.     }  
  157.   
  158.   
  159.     GameObject CreatePlane()  
  160.     {  
  161.         int widthCount = PlaneData.widthSegments   1;  
  162.         //高上有的点数  
  163.         int hightCount = PlaneData.hightSegments   1;  
  164.         //总共的三角形点数  
  165.         int numTriangles = PlaneData.widthSegments * PlaneData.hightSegments * 6;  
  166.         //总共的点数  
  167.         int numVertices = widthCount * hightCount;  
  168.   
  169.         Vector3[] vertices = new Vector3[numVertices];  
  170.         Vector2[] uvs = new Vector2[numVertices];  
  171.         int[] triangles = new int[numTriangles];  
  172.         Vector3[] newNormals = new Vector3[numVertices];  
  173.   
  174.         float uvFactorX = 1.0f / PlaneData.widthSegments;  
  175.         float uvFactorY = 1.0f / PlaneData.hightSegments;  
  176.         float scaleX = PlaneData.Width / PlaneData.widthSegments;  
  177.         float scaleY = PlaneData.Hight / PlaneData.hightSegments;  
  178.   
  179.         //构建顶点数组 UV数组 法线数组  
  180.         for (int x = 0, index = 0; x < widthCount; x )  
  181.         {  
  182.             for (int y = 0; y < hightCount; y )  
  183.             {  
  184.                 vertices[index] = new Vector3(x * scaleX, y * scaleY, 0.0f);  
  185.   
  186.                 uvs[index] = new Vector2(x * uvFactorX, y * uvFactorY);  
  187.   
  188.                 newNormals[index ] = Vector3.back;  
  189.             }  
  190.         }  
  191.   
  192.         //构建点的顺序的数组  
  193.         for (int x = 0, index = 0; x < PlaneData.widthSegments; x )  
  194.         {  
  195.             for (int y = 0; y < PlaneData.hightSegments; y )  
  196.             {  
  197.                 triangles[index] = x * widthCount   y;  
  198.                 triangles[index   1] = x * widthCount   1   y;  
  199.                 triangles[index   2] = (x   1) * widthCount   y;  
  200.                 triangles[index   3] = (x   1) * widthCount   y;  
  201.                 triangles[index   4] = x * widthCount   1   y;  
  202.                 triangles[index   5] = (x   1) * widthCount   y   1;  
  203.   
  204.                 index  = 6;  
  205.             }  
  206.         }  
  207.   
  208.         //创建一个GameObject  
  209.         GameObject meshObj = new GameObject(PlaneData.PlaneName);  
  210.         meshObj.transform.localPosition = Vector3.zero;  
  211.         meshObj.transform.localScale = Vector3.one;  
  212.   
  213.         //添加MeshFilter组件  
  214.         MeshFilter meshFilter = meshObj.AddComponent();  
  215.   
  216.         //添加MeshRenderer组件  
  217.         MeshRenderer meshRender = meshObj.AddComponent();  
  218.   
  219.         //设置一个shader  
  220.         meshRender.material = new Material(Shader.Find("Sprites/Default"));  
  221.   
  222.         //创建一个Mesh  
  223.         Mesh mesh = new Mesh();  
  224.   
  225.         mesh.vertices = vertices;  
  226.         mesh.uv = uvs;  
  227.         mesh.triangles = triangles;  
  228.   
  229.         meshFilter.mesh = mesh;  
  230.   
  231.         return meshObj;  
  232.     }  
  233.   
  234. }  

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签:

0个评论