Mesh drawing (cylinder, sphere, cone)

All drawing codes can be directly dragged to Cube...

Modify the object's Collider to Mesh Collider

ball

 public int num = 8;
    public float r = 5;
    
    void Start()
    {
        VertexHelper vh=new VertexHelper();
        float ang = 2 * Mathf.PI / num;
        for (int i = 0; i < num/2+1; i++)
        {
            float y=Mathf.Cos(ang*i)*r;
            float xr=Mathf.Sin(ang*i)*r;
            float uvy = (float)i / (num / 2);
            for (int j = 0; j < num; j++)
            {
                float x = Mathf.Sin(j * ang) * xr;
                float z = Mathf.Cos(j * ang) * xr;
                float uvx = (float)j / num;
                vh.AddVert(new Vector3(x, y, z), Color.white, new Vector2(uvx, uvy));
                if(j==num-1)
                {
                    float x0 = Mathf.Sin(0) * xr;
                    float z0 = Mathf.Cos(0) * xr;
                    vh.AddVert(new Vector3(x0, y, z0), Color.white, new Vector2(1, uvy));
                    
                }
                if(i<num/2)
                {
                    vh.AddTriangle(i * (num + 1) + j, (i + 1) * (num + 1) + j, i * (num + 1) + j + 1);
                    vh.AddTriangle(i * (num + 1) + j + 1, (i + 1) * (num + 1) + j , (i + 1) * (num + 1) + j+1);
                }
            }
        }
        Mesh mesh=new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
    }

cylinder

float num = 20;
    float angle;
    float r = 5;
    void Start()
    {
        angle = 2 * Mathf.PI / num;
        Mesh mesh = new Mesh();
        VertexHelper vh = new VertexHelper();
        //0
        vh.AddVert(Vector3.zero, Color.white,new Vector2(0.5f, 0.5f));
        //1
        vh.AddVert(new Vector3(0,1,0), Color.white,new Vector2(0.5f, 0.5f));
        for (int i = 0; i < num; i++)
        {
            float x = Mathf.Sin(angle * i) * r;//x轴
            float z=Mathf.Cos(angle * i) * r;//z轴

            vh.AddVert(new Vector3(x, 0, z), Color.white, new Vector2(0.5f, 0.5f));
            vh.AddVert(new Vector3(x, 1, z), Color.white, new Vector2(0.5f, 0.5f));

            if(i==num-1)
            {
                //上面的面
                vh.AddTriangle(3, 1, (i+1) * 2 + 1);
                //下面的面
                vh.AddTriangle((i + 1) * 2, 0,2 );

                //边缘
                vh.AddTriangle(3, (i + 1) * 2 + 1, (i + 1) * 2);
                vh.AddTriangle(3, (i + 1) * 2, 2);
            }
            else
            {
                //上面的面
                vh.AddTriangle(1, (i+1) * 2 + 1, (i + 2) * 2 + 1);
                //下面的面
                vh.AddTriangle((i + 2) * 2, (i+1) * 2 ,0 );
                //边缘
                vh.AddTriangle((i + 1) * 2 + 1, (i + 1) * 2, (i + 2) * 2 + 1);
                vh.AddTriangle((i + 2) * 2 + 1, (i + 1) * 2, (i + 2) * 2);
            }
        }

        vh.FillMesh(mesh);
        GetComponent<MeshCollider>().sharedMesh = mesh;   
        GetComponent<MeshFilter>().mesh = mesh;

    }

cone

  float num=10;
    float angle;
    float r = 5;
    void Start()
    {
        angle=2*Mathf.PI/num;
        Mesh mesh = new Mesh();
        VertexHelper vh=new VertexHelper();
        vh.AddVert(Vector3.zero, Color.blue,new Vector2(0.5f, 0.5f));
        vh.AddVert(new Vector3(0,1,0), Color.blue,new Vector2(0.5f, 0.5f));

       
       for(int i=0; i<num; i++)
        {
            float x = Mathf.Sin(angle * i) * r;
            float z = Mathf.Cos(angle * i) * r;

            float uvx = (x + r) / (2 * r);
            float uvz = (z + r) / (2 * r);
            vh.AddVert(new Vector3(x, 0, z), Color.blue, new Vector2(uvx, uvz));
           
            if(i==num-1)
            {
                vh.AddTriangle(i+2, 0, 2);//底
                vh.AddTriangle(1, i + 2, 2);
            }
            else
            {
                vh.AddTriangle(0, i + 3, i + 2);//底
                vh.AddTriangle(1, i + 2, i + 3);
            }

        }
       vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh=mesh;
        GetComponent<MeshCollider>().sharedMesh=mesh;
    }

Guess you like

Origin blog.csdn.net/Steel_nails/article/details/132890505