概述
本篇為Unity Mesh基礎的第一篇,通過一個最基本的平面四邊形網(wǎng)格來進行闡述。在此之前先對網(wǎng)格(mesh)做一個簡介。網(wǎng)格為最基本的模型表達,通過點構成線,再由線構成三角形,最后由三角形構成面,然后通過材質來進行網(wǎng)格表面的表達,如陰影,貼圖等。Unity除了ui的mesh,其他都需要MeshFilter來確定網(wǎng)格形狀,通過Material以及MeshRenderer來進行網(wǎng)格渲染。
Mesh的組成
Mesh由頂點組成,再有三個頂點構成。所以Mesh必須定義頂點以及頂點如何構成三角形,這是最基本的三個要素,其次還需要定義uv來確定貼圖如何覆蓋在圖形表面,如果需要還需定義頂點的法線或者切向,法線跟光照有關,而切向則會影響紋理凹凸問題,本文以四邊形為例進行闡述。
四邊形頂點
頂點個數(shù)為4,即左下,右下,右上以及左上。size為正方向尺寸。
protected override Vector3[] Vertices
{
get
{
Vector3[] vertices = new Vector3[4];
vertices[0] = Vector3.zero * size;
vertices[1] = Vector3.right * size;
vertices[2] = new Vector3(1, 1, 0) * size;
vertices[3] = Vector3.up * size;
return vertices;
}
}
三角形排列
三角形排列為int類型數(shù)組,保存頂點的索引號,每三個組成一個三角形。下述代碼即為左下,左上,右下組成第一個三角形,剩下組成第二個三角形,排列順序為順時針方向。
protected override int[] Triangles
{
get
{
int[] triangles = new int[6]
{
0,3,1,1,3,2
};
return triangles;
}
}
Mesh的uv
如果給網(wǎng)格賦值貼圖,則需要定義uv,如下定義則為圖片左下角對應mesh的左下角,右上角對應右上角。
protected override Vector2[] Uvs
{
get
{
Vector2[] uvs = new Vector2[4];
uvs[0] = new Vector2(0, 0);
uvs[1] = new Vector2(1, 0);
uvs[2] = new Vector2(1, 1);
uvs[3] = new Vector2(0, 1);
return uvs;
}
}
完整代碼
基類
基類主要是通過OnDrawGizmos來繪制頂點,利于更直觀觀察。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class CreateMeshBase : MonoBehaviour
{
MeshFilter meshFilter;
protected Mesh mesh;
protected virtual Vector3[] Vertices { get; }
protected virtual int[] Triangles { get; }
protected virtual Vector3[] Normals { get; }
protected virtual Vector2[] Uvs { get; }
protected virtual string MeshName { get; }
protected virtual void Start()
{
GetMeshFilter();
}
protected virtual void Reset()
{
GetMeshFilter();
}
protected virtual void OnValidate()
{
GetMeshFilter();
}
void GetMeshFilter()
{
if (meshFilter == null)
{
meshFilter = GetComponent<MeshFilter>();
mesh = new Mesh();
}
mesh.triangles = null;
mesh.uv = null;
mesh.vertices = null;
mesh.name = MeshName;
mesh.vertices = Vertices;
mesh.triangles = Triangles;
mesh.uv = Uvs;
meshFilter.mesh = mesh;
}
private void OnDrawGizmos()
{
if (Vertices == null) return;
Gizmos.color = Color.red;
Gizmos.DrawSphere(Vector3.zero, 0.5f);
Gizmos.color = Color.blue;
for (int i = 0; i < Vertices.Length; i )
{
Gizmos.DrawSphere(Vertices[i], 0.3f);
}
}
}
Quad參數(shù)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateQuad : CreateMeshBase
{
public int size = 5;
protected override Vector3[] Vertices
{
get
{
Vector3[] vertices = new Vector3[4];
vertices[0] = Vector3.zero * size;
vertices[1] = Vector3.right * size;
vertices[2] = new Vector3(1, 1, 0) * size;
vertices[3] = Vector3.up * size;
return vertices;
}
}
protected override int[] Triangles
{
get
{
int[] triangles = new int[6]
{
0,3,1,1,3,2
};
return triangles;
}
}
protected override string MeshName
{
get
{
return "Simple quad";
}
}
protected override Vector2[] Uvs
{
get
{
Vector2[] uvs = new Vector2[4];
uvs[0] = new Vector2(0, 0);
uvs[1] = new Vector2(1, 0);
uvs[2] = new Vector2(1, 1);
uvs[3] = new Vector2(0, 1);
return uvs;
}
}
}
使用
使用時新建一個空游戲物體,掛載上述腳本,然后新建材質,并將材質賦值給MeshRenderer即可。
來源:https://www./content-4-629601.html
|