1、取得顶点的数据汇合
protected override Vector3[] GetVertices()
{var bottomPoints = new Vector3[_circularSideCount];
var topPoints = new Vector3[_circularSideCount];
for (var i = 0; i < _circularSideCount; i++)
{
var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
var bottomCos = Mathf.Cos(rad) * _bottomRadius;
var bottomSin = Mathf.Sin(rad) * _bottomRadius;
var topCos = Mathf.Cos(rad) * _topRadius;
var topSin = Mathf.Sin(rad) * _topRadius;
bottomPoints[i] = new Vector3(bottomCos, -_height * 0.5f, bottomSin) - _vertexOffset;
topPoints[i] = new Vector3(topCos, _height * 0.5f, topSin) - _vertexOffset;
}
var curIndex = 0;
var arrayLen = (_circularSideCount + 1) * 2 + (_circularSideCount + 1) * 2;
var vertices = new Vector3[arrayLen];
// 底面
vertices[curIndex++] = new Vector3(0, -_height * 0.5f, 0) - _vertexOffset;
for (var i = 0; i < _circularSideCount; i++)
{vertices[curIndex++] = bottomPoints[i];
}
// 顶面
vertices[curIndex++] = new Vector3(0, _height * 0.5f, 0) - _vertexOffset;
for (var i = 0; i < _circularSideCount; i++)
{vertices[curIndex++] = topPoints[i];
}
// 侧面
for (var i = 0; i < _circularSideCount; i++)
{vertices[curIndex++] = bottomPoints[i];
vertices[curIndex++] = topPoints[i];
}
vertices[curIndex++] = bottomPoints[0];
vertices[curIndex] = topPoints[0];
return vertices;
}
2、取得法线方向的数据汇合
protected override Vector3[] GetNormals()
{
var curIndex = 0;
var arrayLen = (_circularSideCount + 1) * 2 + (_circularSideCount + 1) * 2;
var normals = new Vector3[arrayLen];
// 底面
for (var i = 0; i <= _circularSideCount; i++)
{normals[curIndex++] = Vector3.down;
}
// 顶面
for (var i = 0; i <= _circularSideCount; i++)
{normals[curIndex++] = Vector3.up;
}
// 侧面
for (var i = 0; i <= _circularSideCount; i++)
{
var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
var cos = Mathf.Cos(rad);
var sin = Mathf.Sin(rad);
normals[curIndex++] = new Vector3(cos, 0, sin);
normals[curIndex++] = new Vector3(cos, 0, sin);
}
return normals;
}
3、取得三角面顶点的索引
protected override int[] GetTriangles()
{
var curIndex = 0;
var arrayLen = _circularSideCount * 3 + _circularSideCount * 3 + _circularSideCount * 3 * 2;
var triangles = new int[arrayLen];
// 底面
for (var i = 1; i < _circularSideCount; i++)
{triangles[curIndex++] = 0;
triangles[curIndex++] = i;
triangles[curIndex++] = i + 1;
}
triangles[curIndex++] = 0;
triangles[curIndex++] = _circularSideCount;
triangles[curIndex++] = 1;
// 顶面
for (var i = _circularSideCount + 2; i < _circularSideCount * 2 + 1; i++)
{triangles[curIndex++] = i;
triangles[curIndex++] = _circularSideCount + 1;
triangles[curIndex++] = i + 1;
}
triangles[curIndex++] = _circularSideCount * 2 + 1;
triangles[curIndex++] = _circularSideCount + 1;
triangles[curIndex++] = _circularSideCount + 2;
// 侧面
var startIndex = _circularSideCount * 2 + 2;
for (var i = 0; i < _circularSideCount; i++)
{
var index = i * 2;
triangles[curIndex++] = startIndex + index;
triangles[curIndex++] = startIndex + index + 1;
triangles[curIndex++] = startIndex + index + 3;
triangles[curIndex++] = startIndex + index + 3;
triangles[curIndex++] = startIndex + index + 2;
triangles[curIndex++] = startIndex + index;
}
return triangles;
}
4、取得 UV 坐标的数据汇合
protected override Vector2[] GetUVs()
{
var curIndex = 0;
var arrayLen = (_circularSideCount + 1) * 2 + (_circularSideCount + 1) * 2;
var uvs = new Vector2[arrayLen];
// 底面
uvs[curIndex++] = new Vector2(0.5f, 0.5f);// 圆心
for (var i = 0; i < _circularSideCount; i++)
{
var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
var cos = Mathf.Cos(rad);
var sin = Mathf.Sin(rad);
uvs[curIndex++] = new Vector2(cos, sin) * 0.5f + new Vector2(0.5f, 0.5f);
}
// 顶面
uvs[curIndex++] = new Vector2(0.5f, 0.5f);
for (var i = 0; i < _circularSideCount; i++)
{
var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
var cos = Mathf.Cos(rad);
var sin = Mathf.Sin(rad);
uvs[curIndex++] = new Vector2(cos, sin) * 0.5f + new Vector2(0.5f, 0.5f);
}
// 侧面
var value = 1.0f / _circularSideCount;
for (var i = 0; i <= _circularSideCount; i++)
{uvs[curIndex++] = new Vector2(value * i, 0);
uvs[curIndex++] = new Vector2(value * i, 1);
}
return uvs;
}
成果如下:
gitee 地址:https://gitee.com/pangdudu010…