国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity3D動態生成平面網格

2020-01-24 00:09:49
字體:
來源:轉載
供稿:網友

在編寫幾何著色器的時候發現默認的Plane無法滿足需求,并且頂點順序未知,于是便寫了一個網格生成代碼,便于生成指定大小的Plane,且頂點順序可控。

效果如下:

一個單元格由4個頂點,兩個三角面組成。

四個頂點如下圖

則生成面的頂點順序為:

左上三角形:0 -> 1 -> 2
右下三角形:2 -> 3 -> 0

Unity中順時針繪制為正面,逆時針繪制為反面。

實現腳本如下:

//PlaneBuilder.csusing System.Collections;using System.Collections.Generic;using UnityEngine;#region Editor#if UNITY_EDITORusing 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("更新網格")) {  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> /// 網格大小 /// </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(); //計算Plane大小 Vector2 size; size.x = _cellSize.x * _gridSize.x; size.y = _cellSize.y * _gridSize.y; //計算Plane一半大小 Vector2 halfSize = size / 2; //計算頂點及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;//計算頂點Y軸  uv.y = y * _cellSize.y / size.y;//計算頂點紋理坐標V  for (int x = 0; x < _gridSize.x + 1; x++)  {  vertice.x = x * _cellSize.x - halfSize.x;//計算頂點X軸  uv.x = x * _cellSize.x / size.x;//計算頂點紋理坐標U  vertices.Add(vertice);//添加到頂點數組  uvs.Add(uv);//添加到紋理坐標數組  } } //頂點序列 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];//頂點序列 for (int y = 0; y < _gridSize.y; y++) {  for (int x = 0; x < _gridSize.x; x++)  {  //四邊形四個頂點  a = y * (_gridSize.x + 1) + x;//0  b = (y + 1) * (_gridSize.x + 1) + x;//1  c = b + 1;//2  d = a + 1;//3  //計算在數組中的起點序號  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);//設置頂點 mesh.SetUVs(0, uvs);//設置UV mesh.SetIndices(indexs, MeshTopology.Triangles, 0);//設置頂點序列 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}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富平县| 准格尔旗| 吉木乃县| 阿图什市| 多伦县| 从江县| 贵港市| 保山市| 镇宁| 左贡县| 望江县| 商河县| 绥宁县| 南阳市| 鸡泽县| 芜湖县| 辽阳县| 太仆寺旗| 宜都市| 肃南| 洪洞县| 峨眉山市| 绥芬河市| 玉林市| 安阳县| 安义县| 万盛区| 庄浪县| 克山县| 弋阳县| 荥阳市| 微山县| 宜兴市| 嘉峪关市| 马关县| 德昌县| 天水市| 滁州市| 上栗县| 大丰市| 桦甸市|