Unity3D動(dòng)態(tài)生成平面網(wǎng)格
在編寫幾何著色器的時(shí)候發(fā)現(xiàn)默認(rèn)的Plane無法滿足需求,并且頂點(diǎn)順序未知,于是便寫了一個(gè)網(wǎng)格生成代碼,便于生成指定大小的Plane,且頂點(diǎn)順序可控。
效果如下:

一個(gè)單元格由4個(gè)頂點(diǎn),兩個(gè)三角面組成。
四個(gè)頂點(diǎn)如下圖

則生成面的頂點(diǎn)順序?yàn)椋?/p>
左上三角形:0 -> 1 -> 2
右下三角形:2 -> 3 -> 0
Unity中順時(shí)針繪制為正面,逆時(shí)針繪制為反面。
實(shí)現(xiàn)腳本如下:
//PlaneBuilder.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#region Editor
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(PlaneBuilder))]
public class PlaneBuilderEditor : Editor
{
public override void OnInspectorGUI()
{
PlaneBuilder builder = (PlaneBuilder)target;
EditorGUI.BeginChangeCheck();
base.OnInspectorGUI();
if (EditorGUI.EndChangeCheck())
{
builder.UpdateMesh();
}
if (GUILayout.Button("更新網(wǎng)格"))
{
builder.UpdateMesh();
}
}
}
#endif
#endregion Editor
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class PlaneBuilder : MonoBehaviour
{
[SerializeField]
private MeshFilter _meshFilter;
[SerializeField]
private MeshRenderer _meshRenderer;
/// <summary>
/// 單元格大小
/// </summary>
[SerializeField]
private Vector2 _cellSize = new Vector2(1, 1);
/// <summary>
/// 網(wǎng)格大小
/// </summary>
[SerializeField]
private Vector2Int _gridSize = new Vector2Int(2, 2);
public MeshRenderer MeshRenderer
{
get
{
return _meshRenderer;
}
}
public MeshFilter MeshFilter
{
get
{
return _meshFilter;
}
}
private void Awake()
{
_meshFilter = GetComponent<MeshFilter>();
_meshRenderer = GetComponent<MeshRenderer>();
UpdateMesh();
}
public void UpdateMesh()
{
Mesh mesh = new Mesh();
//計(jì)算Plane大小
Vector2 size;
size.x = _cellSize.x * _gridSize.x;
size.y = _cellSize.y * _gridSize.y;
//計(jì)算Plane一半大小
Vector2 halfSize = size / 2;
//計(jì)算頂點(diǎn)及UV
List<Vector3> vertices = new List<Vector3>();
List<Vector2> uvs = new List<Vector2>();
Vector3 vertice = Vector3.zero;
Vector2 uv = Vector3.zero;
for (int y = 0; y < _gridSize.y + 1; y++)
{
vertice.z = y * _cellSize.y - halfSize.y;//計(jì)算頂點(diǎn)Y軸
uv.y = y * _cellSize.y / size.y;//計(jì)算頂點(diǎn)紋理坐標(biāo)V
for (int x = 0; x < _gridSize.x + 1; x++)
{
vertice.x = x * _cellSize.x - halfSize.x;//計(jì)算頂點(diǎn)X軸
uv.x = x * _cellSize.x / size.x;//計(jì)算頂點(diǎn)紋理坐標(biāo)U
vertices.Add(vertice);//添加到頂點(diǎn)數(shù)組
uvs.Add(uv);//添加到紋理坐標(biāo)數(shù)組
}
}
//頂點(diǎn)序列
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int startIndex = 0;
int[] indexs = new int[_gridSize.x * _gridSize.y * 2 * 3];//頂點(diǎn)序列
for (int y = 0; y < _gridSize.y; y++)
{
for (int x = 0; x < _gridSize.x; x++)
{
//四邊形四個(gè)頂點(diǎn)
a = y * (_gridSize.x + 1) + x;//0
b = (y + 1) * (_gridSize.x + 1) + x;//1
c = b + 1;//2
d = a + 1;//3
//計(jì)算在數(shù)組中的起點(diǎn)序號
startIndex = y * _gridSize.x * 2 * 3 + x * 2 * 3;
//左上三角形
indexs[startIndex] = a;//0
indexs[startIndex + 1] = b;//1
indexs[startIndex + 2] = c;//2
//右下三角形
indexs[startIndex + 3] = c;//2
indexs[startIndex + 4] = d;//3
indexs[startIndex + 5] = a;//0
}
}
//
mesh.SetVertices(vertices);//設(shè)置頂點(diǎn)
mesh.SetUVs(0, uvs);//設(shè)置UV
mesh.SetIndices(indexs, MeshTopology.Triangles, 0);//設(shè)置頂點(diǎn)序列
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.RecalculateTangents();
_meshFilter.mesh = mesh;
}
#if UNITY_EDITOR
private void OnValidate()
{
if (null == _meshFilter)
{
_meshFilter = GetComponent<MeshFilter>();
}
if (null == _meshRenderer)
{
_meshRenderer = GetComponent<MeshRenderer>();
if (null == _meshRenderer.sharedMaterial)
{
_meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
}
}
}
#endif
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
這篇文章主要介紹了C#導(dǎo)入導(dǎo)出EXCEL文件代碼實(shí)例,代碼的流程和方法都很詳細(xì),需要的朋友可以參考下2014-04-04
npoi2.0將datatable對象轉(zhuǎn)換為excel2007示例
這篇文章主要介紹了npoi2.0將datatable對象轉(zhuǎn)換為excel2007示例的相關(guān)資料2014-04-04
C#使用itextsharp打印pdf的實(shí)現(xiàn)代碼
提到打印,恐怕對于很多人都不會(huì)陌生,無論是開發(fā)者,還是非計(jì)算機(jī)專業(yè)的人員都會(huì)接觸到打印,?在.NET中實(shí)現(xiàn)PDF打印的組件比較多,例如PDFsharp、Report.NET、sharpPDF、itextSharp等等,今天主要簡單的介紹itextSharp組件,需要的朋友可以參考下2024-04-04
C# 文件下載之?dāng)帱c(diǎn)續(xù)傳實(shí)現(xiàn)代碼
本篇文章主要介紹了C# 文件下載之?dāng)帱c(diǎn)續(xù)傳實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01

