unity---Mesh网格编程
Posted 格拉格拉
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity---Mesh网格编程相关的知识,希望对你有一定的参考价值。
目录
unity---Mesh网格编程(三)这里说了Mesh绘制一个正方体或立方体
1.绘制一个圆角正方体
2.代码
//X、Y、Z轴正方体数量足够多的偶数 半径取X的一半。 也可绘制出圆形
using System.Collections;
using UnityEngine;
//脚本挂载到物体时自动添加 MeshFilter 与 MeshRenderer 组件
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Mesh4 : MonoBehaviour
//x轴正方体数量、y轴正方体数量、Z轴正方体数量
public int xGridCount, yGridCount, zGridCount;
//圆角半径
public float _r;
private Vector3[] _vertices;
private int[] _triangles;
private int _t = 0, _v = 0;
private Vector3[] _normals;
void Start()
Mesh mesh = new Mesh();
MeshFilter filter = GetComponent<MeshFilter>();
filter.mesh = mesh;
mesh.name = "Mesh4";
StartCoroutine(GenerateMesh(mesh));
private IEnumerator GenerateMesh(Mesh mesh)
yield return StartCoroutine(GenerateVextex());
_normals = new Vector3[_vertices.Length];
Vector3 inner;
for (int i = 0; i < _vertices.Length; i++)
inner = SetNormal(i);
ResetVextexPos(i, inner);
mesh.vertices = _vertices;
yield return StartCoroutine(GenerateTriange(mesh));
//计算顶点数
private int GetVextexCount()
//公共-顶点数
int connerVertexCount = 8;
//边--顶点数
int edgeVertexCount = (xGridCount + yGridCount + zGridCount - 3) * 4;
//内部顶点数
int faceVertexCount = (xGridCount - 1) * (yGridCount - 1) * 2
+ (zGridCount - 1) * (yGridCount - 1) * 2
+ (xGridCount - 1) * (zGridCount - 1) * 2;
return connerVertexCount + edgeVertexCount + faceVertexCount;
//生成顶点
private IEnumerator GenerateVextex()
_vertices = new Vector3[GetVextexCount()];
int index = 0;
//循环生成左右前后4个面的顶点
for (int y = 0; y < yGridCount + 1; y++)
//循环生成左右前后4个面的顶点
for (int x = 0; x < xGridCount + 1; x++, index++)
_vertices[index] = new Vector3(x, y, 0);
yield return null;
//底部正方形-第二条边的顶点
for (int z = 1; z < zGridCount + 1; z++, index++)
_vertices[index] = new Vector3(xGridCount, y, z);
yield return null;
//底部正方形-第三条边的顶点
for (int x = xGridCount - 1; x >= 0; x--, index++)
_vertices[index] = new Vector3(x, y, zGridCount);
yield return null;
//底部正方形-第四条边的顶点
for (int z = zGridCount - 1; z > 0; z--, index++)
_vertices[index] = new Vector3(0, y, z);
yield return null;
//生成上面的顶点
for (int z = 1; z < zGridCount; z++)
for (int x = 1; x < xGridCount; x++, index++)
_vertices[index] = new Vector3(x, yGridCount, z);
yield return null;
//生成下面的顶点
for (int z = 1; z < zGridCount; z++)
for (int x = 1; x < xGridCount; x++, index++)
_vertices[index] = new Vector3(x, 0, z);
yield return null;
//计算顶点序列下标数
private int GetTriangeCount()
return xGridCount * yGridCount * 4 + xGridCount * zGridCount * 4 + yGridCount * zGridCount * 4;
//顶点绘制三角面序列
private IEnumerator GenerateTriange(Mesh mesh)
_triangles = new int[GetTriangeCount() * 3];
int circleVextexCount = 2 * xGridCount + 2 * zGridCount;
yield return StartCoroutine(GenerateSide(mesh, circleVextexCount));
yield return StartCoroutine(GenerateTop(mesh, circleVextexCount));
yield return StartCoroutine(GenerateBottom(mesh, circleVextexCount));
private IEnumerator GenerateSide(Mesh mesh, int circleVextexCount)
for (int y = 0; y < yGridCount; y++)
for (int i = 0; i < circleVextexCount; i++, _v++, _t += 6)
SetQuad(_triangles, _t, _v, _v + 1, _v + circleVextexCount, _v + 1 + circleVextexCount, circleVextexCount);
mesh.triangles = _triangles;
yield return null;
private IEnumerator GenerateTop(Mesh mesh, int circleVextexCount)
//第一行前三个面
for (int x = 0; x < xGridCount - 1; x++, _v++, _t += 6)
SetQuad(_triangles, _t, _v, _v + 1, _v + circleVextexCount - 1, _v + circleVextexCount);
mesh.triangles = _triangles;
yield return null;
//第一行第四个面
SetQuad(_triangles, _t, _v, _v + 1, _v + circleVextexCount - 1, _v + 2);
mesh.triangles = _triangles;
_t += 6;
yield return null;
int vMin = circleVextexCount * (yGridCount + 1) - 1;
int vMid = vMin + 1;
int vMax = _v + 2;
//中间行所有面
for (int z = 0; z < zGridCount - 2; z++, vMin--, vMid++, vMax++)
//第一个面
SetQuad(_triangles, _t, vMin, vMid, vMin - 1, vMid + xGridCount);
mesh.triangles = _triangles;
_t += 6;
yield return null;
//中间面片
for (int i = 0; i < xGridCount - 2; i++, _t += 6, vMid++)
SetQuad(_triangles, _t, vMid, vMid + 1, vMid + xGridCount - 1, vMid + xGridCount);
mesh.triangles = _triangles;
yield return null;
//最后一个面
SetQuad(_triangles, _t, vMid, vMax, vMid + xGridCount - 1, vMax + 1);
mesh.triangles = _triangles;
_t += 6;
yield return null;
int vTop = vMin - 2;
//第一个面
SetQuad(_triangles, _t, vMin, vMid, vMin - 1, vTop);
mesh.triangles = _triangles;
_t += 6;
yield return null;
for (int i = 0; i < xGridCount - 2; i++, vMid++, vTop--)
//中间面
SetQuad(_triangles, _t, vMid, vMid + 1, vTop, vTop - 1);
mesh.triangles = _triangles;
_t += 6;
yield return null;
//最后一行最后一个面
SetQuad(_triangles, _t, vMid, vTop - 2, vTop, vTop - 1);
mesh.triangles = _triangles;
_t += 6;
yield return null;
private IEnumerator GenerateBottom(Mesh mesh, int circleVextexCount)
int vMin = circleVextexCount - 1;
int vMid = _vertices.Length - (xGridCount - 1) * (zGridCount - 1);
//第一行,第一面
SetQuad(_triangles, _t, vMin, vMid, 0, 1);
_t += 6;
mesh.triangles = _triangles;
yield return null;
int vMax = 1;
//第一行,中间面
for (int i = 0; i < xGridCount - 2; i++, _t += 6, vMax++, vMid++)
SetQuad(_triangles, _t, vMid, vMid + 1, vMax, vMax + 1);
mesh.triangles = _triangles;
yield return null;
//第一行,最后面
SetQuad(_triangles, _t, vMid, vMax + 2, vMax, vMax + 1);
_t += 6;
mesh.triangles = _triangles;
yield return null;
vMid++;
vMax += 2;
for (int z = 0; z < zGridCount - 2; z++, vMin--, vMid++, vMax++)
//第一面
SetQuad(_triangles, _t, vMin - 1, vMid, vMin, vMid - xGridCount + 1);
_t += 6;
mesh.triangles = _triangles;
yield return null;
//中间面
for (int i = 0; i < xGridCount - 2; i++, _t += 6, vMid++)
SetQuad(_triangles, _t, vMid, vMid + 1, vMid - xGridCount + 1, vMid - xGridCount + 2);
mesh.triangles = _triangles;
yield return null;
//最后面
SetQuad(_triangles, _t, vMid, vMax + 1, vMid - xGridCount + 1, vMax);
_t += 6;
mesh.triangles = _triangles;
yield return null;
vMid = vMid - xGridCount + 1;
//最后行,第一面
SetQuad(_triangles, _t, vMin - 1, vMin - 2, vMin, vMid);
_t += 6;
mesh.triangles = _triangles;
yield return null;
int vBottom = vMin - 2;
//最后行,中间面
for (int i = 0; i < xGridCount - 2; i++, _t += 6, vBottom--, vMid++)
SetQuad(_triangles, _t, vBottom, vBottom - 1, vMid, vMid + 1);
mesh.triangles = _triangles;
yield return null;
//最后行,最后面
SetQuad(_triangles, _t, vBottom, vBottom - 1, vMid, vBottom - 2);
_t += 6;
mesh.triangles = _triangles;
yield return null;
private void SetQuad(int[] triangles, int i, int v00, int v10, int v01, int v11, int circleVextexCount)
v10 = (v00 / circleVextexCount) * circleVextexCount + v10 % circleVextexCount;
v11 = (v01 / circleVextexCount) * circleVextexCount + v11 % circleVextexCount;
SetQuad(triangles, i, v00, v10, v01, v11);
//每个面片 6个顶点下标序列
private void SetQuad(int[] triangles, int i, int v00, int v10, int v01, int v11)
//第1个三角面
triangles[i] = v00;
triangles[i + 1] = v01;
triangles[i + 2] = v10;
//第2个三角面
triangles[i + 3] = v01;
triangles[i + 4] = v11;
triangles[i + 5] = v10;
//圆角四边形部分逻辑
private Vector3 SetNormal(int i)
var vextex = _vertices[i];
var inner = vextex;
if (vextex.x < _r)
inner.x = _r;
else if (vextex.x > xGridCount - _r)
inner.x = xGridCount - _r;
if (vextex.y < _r)
inner.y = _r;
else if (vextex.y > yGridCount - _r)
inner.y = yGridCount - _r;
if (vextex.z < _r)
inner.z = _r;
else if (vextex.z > zGridCount - _r)
inner.z = zGridCount - _r;
_normals[i] = (vextex - inner).normalized;
return inner;
private void ResetVextexPos(int i, Vector3 inner)
_vertices[i] = _normals[i] * _r + inner;
以上是关于unity---Mesh网格编程的主要内容,如果未能解决你的问题,请参考以下文章