1. 程式人生 > >Unity使用Mesh元件畫圓環

Unity使用Mesh元件畫圓環

最近專案中有畫圓環的需求,這裡把程式碼貼上來。轉載請註明出處。

效果

//呼叫
void Start ()
		{
			Material material = Resources.Load<Material>("Materials/GreenLine");
			DrawUtils.DrawRing(new GameObject(),Vector3.zero,40,1.2f,0.2f,material );
		}

在這裡插入圖片描述

核心程式碼

	/// <summary>
    /// 畫圓環
    /// </summary>
    /// <param name="gameObject">依附物件</param>
    /// <param name="center">中心點</param>
    /// <param name="segments">分割段數</param>
    /// <param name="innerRadius">內徑</param>
    /// <param name="thickness">厚度,向外擴的寬度</param>
    /// <param name="material">材質</param>
    public static void DrawRing(GameObject gameObject, Vector3 center, int segments,
        float innerRadius, float thickness, Material material)

    {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshRenderer>();
        gameObject.GetComponent<MeshRenderer>().material = material;
        Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;
        mesh.Clear();

        //分割16塊,共有32個點,96 三角形
        Vector3[] innerVertices = GetVerticesFromCenter(center, segments, innerRadius);
        Vector3[] outerVertices = GetVerticesFromCenter(center, segments, innerRadius + thickness);
        List<Vector3> list = outerVertices.ToList();
        list.AddRange(innerVertices);
        var bounds = list.ToArray();

        //設定頂點
        mesh.vertices = bounds;

        int[] array = new int[segments * 2 * 3];

//        Debug.Log("bounds.Length-1="+(bounds.Length-1));
        for (int i = 0, j = 0; j < segments; i += 6, j++)
        {
            if (j != segments - 1)
            {
                array[i] = j;
                array[i + 1] = j + 1;
                array[i + 2] = segments + 1 + j;

                array[i + 3] = j;
                array[i + 4] = segments + 1 + j;
                array[i + 5] = segments + j;
            }
            //最後一對三角形時,需要單獨處理
            else
            {
                array[i] = j;
                array[i + 1] = 0;
                array[i + 2] = segments;

                array[i + 3] = j;
                array[i + 4] = segments;
                array[i + 5] = segments + j;
            }

//            Debug.Log("i="+i+","+"j="+j+"|"+array[i]+","+array[i+1]+","+array[i+2]+","+array[i+3]+","+array[i+4]+","+array[i+5]);
        }

        mesh.triangles = array;
    }


    /// <summary>
    /// 根據圓心獲取獲取所有點的陣列
    /// </summary>
    /// <param name="center"></param>
    /// <param name="segments"></param>
    /// <param name="radius"></param>
    /// <returns></returns>
    private static Vector3[] GetVerticesFromCenter(Vector3 center, int segments, float radius)
    {
        //內
        Vector3[] points = new Vector3[segments];

        //每一份的角度
        float angle = Mathf.Deg2Rad * 360f / segments;

        Debug.Log("angle=" + angle);

        for (int i = 0; i < segments; i++)
        {
            //計算x點和z點,內圈
            float inX = center.x + radius * Mathf.Sin(angle * i);
            float inZ = center.z + radius * Mathf.Cos(angle * i);
            points[i] = new Vector3(inX, center.y, inZ);

//            DrawCircle(new GameObject("circle " + i), 0.01f, 20, points[i],
//                Resources.Load<Material>("Materials/GreenLine"));
        }

        return points;
    }

貼上分析過程

在這裡插入圖片描述