在Unity使用自定义网格生成一个球体

1.在Unity场景中新建一个空物体,在空物体上添加MeshRenderer和MeshFilter组件。

2.新建一个C#脚本命名SphereMesh,将脚本挂载到空物体上,如图:

运行场景就可以看到生成一个球体

全部代码如下:

using UnityEngine;

public class SphereMesh : MonoBehaviour
{
    void Start()
    {
        CreateSphereMesh();
    }

    void CreateSphereMesh()
    {
        MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
        meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));

        MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();

        Mesh mesh = new Mesh();

        SphereShape shape = Init(20,20);

        mesh.vertices = shape.vertices;

        mesh.triangles = shape.index;

        Vector3[] normal = new Vector3[shape.vertices.Length];
        for (int i = 0; i < shape.vertices.Length; i++) {
            normal[i] = -Vector3.forward;
        }
        mesh.normals = normal;

        mesh.uv = shape.uv;

        meshFilter.mesh = mesh;

    }

    SphereShape Init(int latCount, int lonCount)
    {
        if (latCount < 2 || lonCount < 2)
            return null;

        int totalIndexCount = 6 * (latCount - 1) * lonCount;
        int totalVertexCount = (latCount + 1) * (lonCount + 1);
        float latStep = Mathf.PI / latCount;
        float lonStep = 2 * Mathf.PI / lonCount;

        int[] index = new int[totalIndexCount];
        Vector3[] vertices = new Vector3[totalVertexCount];
        Vector2[] uv = new Vector2[totalVertexCount];

        int currentVertex = 0;
        int currentIndex = 0;

        for (int lat = 0; lat <= latCount; lat++)
        {
            for (int lon = 0; lon <= lonCount; lon++)
            {
                vertices[currentVertex] = new  Vector3(Mathf.Cos(lon * lonStep)*Mathf.Sin(lat * latStep),
                                       Mathf.Sin(lon * lonStep) * Mathf.Sin(lat * latStep),
                                       Mathf.Cos(lat * latStep - Mathf.PI)
                                       );

                uv[currentVertex] = new Vector2(
                                     (float)lon / lonCount,
                                     (float)lat / latCount
                                    );
                currentVertex++;
            }
        }

        int v = lonCount + 1;
        for (int lon = 0;lon < latCount; lon++) 
        {
            index[currentIndex++] = lon;
            index[currentIndex++] = v;
            index[currentIndex++] = v + 1;

            v++;
        }


        v = lonCount + 1;
        for (int lat = 1; lat < latCount - 1; lat++)
        {
            for (int lon = 0; lon < lonCount; lon++)
            {
               index[currentIndex++] = v;
               index[currentIndex++] = v + lonCount + 1;
               index[currentIndex++] = v + 1;

               index[currentIndex++] = v + 1;
               index[currentIndex++] = v + lonCount + 1;
               index[currentIndex++] = v + lonCount + 2;

                v += 1;
            }

            v += 1;
        }

        for (int lon = 0; lon < lonCount; lon++)
        {
            index[currentIndex++] = v;
            index[currentIndex++] = v + lonCount + 1;
            index[currentIndex++] = v + 1;

            v += 1;
        }

        SphereShape shape = new SphereShape();
        shape.index = index;
        shape.vertices = vertices;
        shape.uv = uv;

        return shape;
    }
}

class SphereShape : Shape 
{ 

}
class Shape
{
   public int[] index;
   public Vector3[] vertices;
   public Vector2[] uv;
}

 

参考链接: 

UV Sphere (winter.dev)icon-default.png?t=N7T8https://winter.dev/projects/mesh/uvsphere

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值