unity 增加顶点数,熟悉mesh的构成
2017-10-25 10:43
567 查看
有减面减顶点数的需求,应该也有增面增加顶点数的需求吧就算没有,这个功能的实现,也有利于理解unity中mesh的构成。
using UnityEngine;using System.Collections.Generic;[RequireComponent(typeof(MeshFilter))]public class DoubleVerticals : MonoBehaviour{Mesh mesh;Vector3[] origVertices;int[] origTrangles;// Use this for initializationvoid Start(){mesh = transform.GetComponent<MeshFilter>().mesh;}// Update is called once per framevoid Update(){if (Input.GetKeyDown("space")){IncreseVertices();}}void IncreseVertices(){if (mesh.vertexCount >= 18432){Debug.Log("Too Many");return;}origVertices = mesh.vertices;origTrangles = mesh.triangles;Dictionary<Vector3, int> verticesResultDic = new Dictionary<Vector3, int>();List<int> tranglesResultList = new List<int>();//计算三角面的个数int k = origTrangles.Length / 3;int index = 0;for (int i = 0; i < k; i++){//取出一个三角面(的顶点)Vector3[] trangle = new Vector3[3] { origVertices[origTrangles[i * 3]], origVertices[origTrangles[i * 3 + 1]], origVertices[origTrangles[i * 3 + 2]] };//通过取三条边的中心点//原来三个顶点,变成六个顶点Vector3[] result = new Vector3[6];Vector3 v01 = (trangle[0] + trangle[1]) * 0.5f;Vector3 v12 = (trangle[1] + trangle[2]) * 0.5f;Vector3 v02 = (trangle[0] + trangle[2]) * 0.5f;if (AddVertices(verticesResultDic, trangle[0], index)) index++;if (AddVertices(verticesResultDic, trangle[1], index)) index++;if (AddVertices(verticesResultDic, trangle[2], index)) index++;if (AddVertices(verticesResultDic, v01, index)) index++;if (AddVertices(verticesResultDic, v12, index)) index++;if (AddVertices(verticesResultDic, v02, index)) index++;// 将原三角面分成新的四个三角面// 注意左手法则,逆时针顺序//三角形数组存储的是顶点在顶点数组中的序号tranglesResultList.Add(verticesResultDic[trangle[0]]);tranglesResultList.Add(verticesResultDic[v01]);tranglesResultList.Add(verticesResultDic[v02]);tranglesResultList.Add(verticesResultDic[v01]);tranglesResultList.Add(verticesResultDic[trangle[1]]);tranglesResultList.Add(verticesResultDic[v12]);tranglesResultList.Add(verticesResultDic[trangle[2]]);tranglesResultList.Add(verticesResultDic[v02]);tranglesResultList.Add(verticesResultDic[v12]);tranglesResultList.Add(verticesResultDic[v02]);tranglesResultList.Add(verticesResultDic[v01]);tranglesResultList.Add(verticesResultDic[v12]);}mesh.vertices = GetReusltVertices(verticesResultDic);mesh.triangles = tranglesResultList.ToArray();mesh.RecalculateBounds();//由于normal没有增加,导致表面看起来不平滑(如果要重新计算normals参考顶点的计算)mesh.RecalculateNormals();}bool AddVertices(Dictionary<Vector3, int> verticesResultDic, Vector3 vertice, int index){if (verticesResultDic.ContainsValue(index) || verticesResultDic.ContainsKey(vertice))return false;verticesResultDic.Add(vertice, index);return true;}Vector3[] GetReusltVertices( Dictionary< Vector3,int> verticesResultDic){int length = verticesResultDic.Keys.Count;Vector3[] result = new Vector3[length];List<Vector3> temp = new List<Vector3>(verticesResultDic.Keys);for (int i = 0; i < length; i++){result[i] = temp[i];}return result;}}
相关文章推荐
- Unity mesh texture开启 read 会增加内存畅想
- [引擎]之四:unity中查看简单mesh顶点顺序的小工具——修改
- unity 在Mesh中的顶点或者mesh 三角形的中点生成物体
- [引擎]之二:unity中查看简单mesh顶点顺序的小工具
- 【unity实用技能】Unity Mesh更新的时候增加内存
- Unity Mesh Collider 顶点太多报错
- unity 编辑mesh顶点位置
- 【小松教你手游开发】【unity实用技能】Unity Mesh更新的时候增加内存
- Unity SkinnedMesh 换装
- unity 一个mesh renderer上有两个材质球更换材质球的方法
- unity, SkinnedMeshRenderer.updateWhenOffscreen
- Non-convex MeshCollider with non-kinematic Rigidbody is no longer supported in Unity 5.
- unity 脚本创建 MESH(网格)
- Unity5.3 不改变原代码,将ModifyVertices换成ModifyMesh的一个方法
- 判断三个顶点能否构成三角形
- Unity -Shader精讲(四)创建和理解vert和frag函数(顶点函数和片元函数)
- 图片旋转--修改老外的代码,返回中增加第四个顶点
- unity中利用mesh动态创建墙体
- Unity动态创建的Mesh,导出为Obj模型文件,并生成Prefab文件
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_3_顶点照明和Unity存放光源的第二种方式