1、取得顶点的数据汇合
protected override Vector3[] GetVertices()
{var points = new Vector3[_circularSideCount];// 保留底面圆形的点汇合
for (int i = 0; i < _circularSideCount; i++)
{
var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
var cos = Mathf.Cos(rad) * _radius;
var sin = Mathf.Sin(rad) * _radius;
points[i] = new Vector3(cos, -_height * 0.5f, sin) - _verticeOffset;
}
int curIndex = 0;// 以后索引
int arrayLen = (_circularSideCount + 1) * 3;// 顶点数组的长度
var vertices = new Vector3[arrayLen];
// 底面
vertices[curIndex++] = new Vector3(0, -_height * 0.5f, 0) - _verticeOffset;
for (int i = 0; i < _circularSideCount; i++)
{vertices[curIndex++] = points[i];
}
// 侧面
var topPoint = new Vector3(0, _height * 0.5f, 0) - _verticeOffset;// 顶点的地位
for (int i = 0; i < _circularSideCount; i++)
{vertices[curIndex++] = points[i];
vertices[curIndex++] = topPoint;
}
vertices[curIndex] = points[0];
vertices[curIndex++] = topPoint;
return vertices;
}
2、取得法线方向的数据汇合
protected override Vector3[] GetNormals()
{
int curIndex = 0;
int arrayLen = (_circularSideCount + 1) * 3;// 法线数组的长度
var normals = new Vector3[arrayLen];
// 底面
for (int i = 0; i <= _circularSideCount; i++)
{normals[curIndex++] = Vector3.down;
}
// 侧面
for (int 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()
{
int curIndex = 0;
int arrayLen = _circularSideCount * 3 + _circularSideCount * 3;// 数组长度
var triangles = new int[arrayLen];
// 底面
for (int i = 0; i < _circularSideCount - 1; i++)
{triangles[curIndex++] = 0;
triangles[curIndex++] = i + 1;
triangles[curIndex++] = i + 2;
}
triangles[curIndex++] = 0;
triangles[curIndex++] = _circularSideCount;
triangles[curIndex++] = 1;
// 侧面
int startIndex = _circularSideCount + 1;
for (int i = 0; i < (_circularSideCount - 1) * 2; i += 2)
{triangles[curIndex++] = startIndex + i;
triangles[curIndex++] = startIndex + i + 1;
triangles[curIndex++] = startIndex + i + 2;
}
triangles[curIndex++] = startIndex + (_circularSideCount - 1) * 2;
triangles[curIndex++] = startIndex + (_circularSideCount - 1) * 2 + 1;
triangles[curIndex] = startIndex;
return triangles;
}
4、取得 UV 坐标的数据汇合
protected override Vector2[] GetUVs()
{
int curIndex = 0;
int arrayLen = (_circularSideCount + 1) * 3;
var uvs = new Vector2[arrayLen];
// 底面
uvs[curIndex++] = new Vector2(0.5f, 0.5f);
for (int 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);
}
// 侧面
float value = 1.0f / _circularSideCount;
for (int 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…