procedurally generate a sphere mesh

后端 未结 5 1014
既然无缘
既然无缘 2020-12-13 02:38

i am looking for an algorithm ( in pseudo code) that generates the 3d coordinates of a sphere mesh like this:

\"

5条回答
  •  无人及你
    2020-12-13 03:02

    This is a working C# code for the above answer:

    using UnityEngine;
    
    [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    public class ProcSphere : MonoBehaviour
    {
    
        private Mesh mesh;
        private Vector3[] vertices;
    
        public int horizontalLines, verticalLines;
        public int radius;
    
        private void Awake()
        {
            GetComponent().mesh = mesh = new Mesh();
            mesh.name = "sphere";
            vertices = new Vector3[horizontalLines * verticalLines];
            int index = 0;
            for (int m = 0; m < horizontalLines; m++)
            {
                for (int n = 0; n < verticalLines - 1; n++)
                {
                    float x = Mathf.Sin(Mathf.PI * m/horizontalLines) * Mathf.Cos(2 * Mathf.PI * n/verticalLines);
                    float y = Mathf.Sin(Mathf.PI * m/horizontalLines) * Mathf.Sin(2 * Mathf.PI * n/verticalLines);
                    float z = Mathf.Cos(Mathf.PI * m / horizontalLines);
                    vertices[index++] = new Vector3(x, y, z) * radius;
                }
            }
            mesh.vertices = vertices;
        }
    
        private void OnDrawGizmos()
        {
            if (vertices == null) {
                return;
            }
            for (int i = 0; i < vertices.Length; i++) {
                Gizmos.color = Color.black;
                Gizmos.DrawSphere(transform.TransformPoint(vertices[i]), 0.1f);
            }
        }
    }
    

提交回复
热议问题