共计 2967 个字符,预计需要花费 8 分钟才能阅读完成。
Unity 中 mesh 次要通过 Vertexs(顶点)、Normals(法线)、Triangles(三角面)、UVs(uv 坐标)这四个局部数据生成。
一、Mesh 的轴心地位的类型
通过这个类型判断生成坐标的偏移量
public enum MeshPivot
{
/// <summary>
/// 核心
/// </summary>
CENTER,
/// <summary>
/// 上
/// </summary>
TOP,
/// <summary>
/// 下
/// </summary>
BOTTOM,
/// <summary>
/// 左
/// </summary>
LEFT,
/// <summary>
/// 右
/// </summary>
RIGHT,
/// <summary>
/// 前
/// </summary>
FRONT,
/// <summary>
/// 后
/// </summary>
BACK,
}
二、形态父类
设置成抽象类,须要必须要继承这个类并实现相干形象办法
public abstract class Shape
{
#region protected members
protected MeshPivot _meshPivot = MeshPivot.CENTER;// 轴心的地位
protected string _meshName;
protected Vector3 _verticeOffset;// 顶点的偏移量
protected Vector3[] _vertices;// 顶点数据
protected Vector3[] _normals;// 法线数据
protected int[] _triangles;// 三角面顶点的索引数据
protected Vector2[] _uvs;//uv 坐标数据
protected Mesh _shapeMesh;// 保留生成的 mesh
#endregion
#region public properties
/// <summary>
/// 轴心的地位
/// </summary>
public MeshPivot MeshPivot
{get { return _meshPivot;}
}
/// <summary>
/// 顶点数据
/// </summary>
public Vector3[] Vertices
{
get
{if (_vertices == null) _vertices = GetVertices();
return _vertices;
}
}
/// <summary>
/// 法线数据
/// </summary>
public Vector3[] Normals
{
get
{if (_normals == null) _normals = GetNormals();
return _normals;
}
}
/// <summary>
/// 三角面顶点的索引数据
/// </summary>
public int[] Triangles
{
get
{if (_triangles == null) _triangles = GetTriangles();
return _triangles;
}
}
/// <summary>
/// uv 坐标数据
/// </summary>
public Vector2[] UVs
{
get
{if (_uvs == null) _uvs = GetUVs();
return _uvs;
}
}
/// <summary>
/// 生成的 mesh 数据
/// </summary>
public Mesh ShapeMesh
{
get
{if (_shapeMesh == null) _shapeMesh = GenerateMesh();
return _shapeMesh;
}
}
#endregion
#region ctor
public Shape(MeshPivot meshPivot, string meshName)
{
_meshPivot = meshPivot;
_meshName = meshName;
}
#endregion
#region protected functions
/// <summary>
/// 生成 mesh
/// </summary>
/// <returns></returns>
protected Mesh GenerateMesh()
{var mesh = new Mesh();
mesh.name = _meshName;
_vertices = GetVertices();
_normals = GetNormals();
_triangles = GetTriangles();
_uvs = GetUVs();
mesh.vertices = _vertices;
mesh.normals = _normals;
mesh.triangles = _triangles;
mesh.uv = _uvs;
mesh.RecalculateTangents();
mesh.RecalculateBounds();
mesh.Optimize();
return mesh;
}
#endregion
#region abstract
/// <summary>
/// 依据 mesh 中心点的地位,取得顶点地位的偏移量
/// </summary>
/// <returns></returns>
protected abstract Vector3 GetVertexOffset();
/// <summary>
/// 取得顶点的数据汇合
/// </summary>
/// <returns></returns>
protected abstract Vector3[] GetVertices();
/// <summary>
/// 取得法线方向的数据汇合
/// </summary>
/// <returns></returns>
protected abstract Vector3[] GetNormals();
/// <summary>
/// 取得三角面顶点的索引
/// </summary>
/// <returns></returns>
protected abstract int[] GetTriangles();
/// <summary>
/// 取得 UV 坐标的数据汇合
/// </summary>
/// <returns></returns>
protected abstract Vector2[] GetUVs();
#endregion
#region protected static functions
/// <summary>
/// 取得圆心切割的边数(依照固定长度)
/// </summary>
/// <param name="radius"> 半径 </param>
/// <param name="arcLen"> 弧度的长度 </param>
/// <returns></returns>
protected static int GetCircularSideCount(float radius, float arcLen = 0.1f)
{
const int minSideCount = 3;
const int maxSideCount = 3000;
int sideCount = Mathf.RoundToInt(Mathf.PI * 2 * radius / arcLen);
sideCount = Mathf.Min(Mathf.Max(minSideCount, sideCount), maxSideCount);
Debug.Log("sideCount:" + sideCount);
return sideCount;
}
#endregion
}
正文完