共计 1598 个字符,预计需要花费 4 分钟才能阅读完成。
1、取得顶点的数据汇合
protected override Vector3[] GetVertices()
{
var curIndex = 0;
var arrayLen = (_latitude + 1) * (_longitude + 1);
var vertices = new Vector3[arrayLen];
for (var lat = 0; lat <= _latitude; lat++)
{
// 经线角度的取值范畴是 0~180 度
var latRad = lat * 1.0f / _latitude * Mathf.PI;
var latCos = Mathf.Cos(latRad);
var latSin = Mathf.Sin(latRad);
for (var lon = 0; lon <= _longitude; lon++)
{
// 纬线角度的取值范畴是 0~360 度
var lonRad = lon * 1.0f / _longitude * Mathf.PI * 2;// 每一圈的最初一个点和第一个点重合 (设置 UV 的坐标)
var lonCos = Mathf.Cos(lonRad);
var lonSin = Mathf.Sin(lonRad);
vertices[curIndex++] = new Vector3(latSin * lonCos, latCos, latSin * lonSin) * _radius - _vertexOffset;
}
}
return vertices;
}
2、取得法线方向的数据汇合
protected override Vector3[] GetNormals()
{
var curIndex = 0;
var vertices = Vertices;// 取得以后的顶点
var normals = new Vector3[vertices.Length];
foreach (var vertex in vertices)
{normals[curIndex++] = (vertex + _vertexOffset).normalized;
}
return normals;
}
3、取得三角面顶点的索引
protected override int[] GetTriangles()
{
var curIndex = 0;
var arrayLen = _latitude * _longitude * 3 * 2;
var triangles = new int[arrayLen];
// 侧面
for (var lat = 0; lat < _latitude; lat++)
{for (var lon = 0; lon < _longitude; lon++)
{var current = lat * (_longitude + 1) + lon;
var next = current + _longitude + 1;
triangles[curIndex++] = current;
triangles[curIndex++] = current + 1;
triangles[curIndex++] = next;
triangles[curIndex++] = next;
triangles[curIndex++] = current + 1;
triangles[curIndex++] = next + 1;
}
}
return triangles;
}
4、取得 UV 坐标的数据汇合
protected override Vector2[] GetUVs()
{
var curIndex = 0;
var arrayLen = (_latitude + 1) * (_longitude + 1);
var uvs = new Vector2[arrayLen];
for (var lat = 0; lat <= _latitude; lat++)
for (var lon = 0; lon <= _longitude; lon++)
uvs[curIndex++] = new Vector2((float)lon / _longitude, 1f - (float)lat / _latitude);
return uvs;
}
成果如下:
gitee 地址:https://gitee.com/pangdudu010…
正文完