Mesh概念:Mesh是Unity中的一个组件,称为网格组件。通俗的讲,Mesh是指模型的网格,3D模型是由多边形拼接而成,而多边形实际上是由多个三角形拼接而成的。所以一个3D模型的表面其实是由多个彼此相连的三角面构成。三维空间中,构成这些三角形的点和边的集合就是Mesh。
Mesh组成:
1、顶点坐标数组vertexes
2、顶点在uv坐标系中的位置信息数组uvs
3、三角形顶点顺时针或者逆时针索引数组triangles
4、MeshFiler组件,用于增加mesh属性
5、MeshRender组件,增加材质并渲染出来。
6、可能还需要每个顶点的法线的数组normals
using UnityEngine;
using UnityEditor;
using System.Collections; public class GenMesh
{
[MenuItem("GameEditor/scene/Weather/GenMesh")]
static public void GenMeshM()
{
Mesh m1 = CreateRect();
AssetDatabase.CreateAsset(m1, "Assets/_Resource/model/prefab/weather/m1.asset");
} public static Mesh CreateRect()
{
Mesh mesh = new Mesh(); int particleNum = ;
//顶点坐标
Vector3[] verts = new Vector3[ * particleNum];
//uv坐标
Vector2[] uvs = new Vector2[ * particleNum];
//三角形索引
int[] tris = new int[ * * particleNum]; Vector3 position;
for (int i = ; i < particleNum; i++)
{
int i4 = i * ;
int i6 = i * ; position.x = * i;
position.y = * i;
position.z = ; //顶点坐标
verts[i4 + ] = position;
verts[i4 + ] = position + new Vector3(, , );
verts[i4 + ] = position + new Vector3(, , );
verts[i4 + ] = position + new Vector3(, , ); //四个顶点在UV坐标系中的位置
uvs[i4 + ] = new Vector2(0.0f, 0.0f);
uvs[i4 + ] = new Vector2(1.0f, 0.0f);
uvs[i4 + ] = new Vector2(1.0f, 1.0f);
uvs[i4 + ] = new Vector2(0.0f, 1.0f); //顺时针绘制三角形0 1 2 / 0 2 3
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
} mesh.vertices = verts;
mesh.triangles = tris;
mesh.uv = uvs;
mesh.RecalculateBounds(); return mesh;
} public static Mesh CreateCircle()
{
Mesh mesh = new Mesh(); int particleNum = ;
Vector3[] verts = new Vector3[ * particleNum];
Vector2[] uvs = new Vector2[ * particleNum];
int[] tris = new int[ * * particleNum]; Vector3 position;
for (int i = ; i < particleNum; i++)
{
int i4 = i * ;
int i6 = i * ; position.x = * i;
position.y = * i;
position.z = ; verts[i4 + ] = position;
verts[i4 + ] = position + new Vector3(, , );
verts[i4 + ] = position + new Vector3(, , );
verts[i4 + ] = position + new Vector3(, , ); uvs[i4 + ] = new Vector2(0.0f, 0.0f);
uvs[i4 + ] = new Vector2(1.0f, 0.0f);
uvs[i4 + ] = new Vector2(1.0f, 1.0f);
uvs[i4 + ] = new Vector2(0.0f, 1.0f); //顺时针绘制三角形0 1 2 / 0 2 3
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
tris[i6 + ] = i4 + ;
} mesh.vertices = verts;
mesh.triangles = tris;
mesh.uv = uvs;
mesh.RecalculateBounds(); return mesh;
}
}
代码示例生成的mesh是这个样子,未添加MeshRender,即没有材质和渲染的情况:
添加MeshRender,即有材质和渲染的情况:
using UnityEngine;
using System.Collections; /*仅仅创建Mesh
*
*
*/
public class CreatMesh : MonoBehaviour
{ private GameObject mMesh;
private Material mMaterial; /* Mesh属性
* 长宽
* 段数
* 高度
* 高度差
*/
private Vector2 size;//长度和宽度
private float minHeight = -;//最小高度
private float maxHeight = ;//最大高度
private Vector2 segment;//长度的段数和宽度的段数
private float unitH;//最小高度和最大高度只差,值为正 /* 顶点属性
* 顶点
* uv
* 三角形
*/
private Vector3[] vertexes;//顶点数
private Vector2 uvs;//uvs坐标
private int[] triangles;//三角形索引 void Start()
{
creatMesh(, , , , -, );
} private void creatMesh(float width, float height, uint segmentX, uint segmentY, int min, int max)
{
size = new Vector2(width, height);
maxHeight = max;
minHeight = min;
unitH = maxHeight - minHeight;
segment = new Vector2(segmentX, segmentY); if (mMesh != null)
{
Destroy(mMesh);
}
mMesh = new GameObject();
mMesh.name = "CreateMesh"; computeVertexes();
DrawMesh();
} private void computeVertexes()
{
int sum = Mathf.FloorToInt((segment.x + ) * (segment.y + ));//顶点总数
float w = size.x / segment.x;//每一段的长度
float h = size.y / segment.y; GetTriangles(); int index = ;
vertexes = new Vector3[sum];
for (int i = ; i < segment.y + ; i++)
{
for (int j = ; j < segment.x + ; j++)
{
float tempHeight = ;
vertexes[index] = new Vector3(j * w, , i * h);
index++;
}
}
} private void DrawMesh()
{
Mesh mesh = mMesh.AddComponent<MeshFilter>().mesh;//网格
mMesh.AddComponent<MeshRenderer>();//网格渲染器 mMaterial = new Material(Shader.Find("Diffuse"));//材质 mMesh.GetComponent<Renderer>().material = mMaterial; /*设置mesh*/
mesh.Clear();//更新
mesh.vertices = vertexes;
//mesh.uv
mesh.triangles = triangles; mesh.RecalculateNormals();
mesh.RecalculateBounds(); } private int[] GetTriangles()
{
int sum = Mathf.FloorToInt(segment.x * segment.y * );//三角形顶点总数
triangles = new int[sum];
uint index = ;
for (int i = ; i < segment.y; i++)
{
for (int j = ; j < segment.x; j++)
{
int role = Mathf.FloorToInt(segment.x) + ;
int self = j + (i * role);
int next = j + ((i + ) * role);
//顺时针
triangles[index] = self;
triangles[index + ] = next + ;
triangles[index + ] = self + ;
triangles[index + ] = self;
triangles[index + ] = next;
triangles[index + ] = next + ;
index += ;
}
}
return triangles;
}
}