unity 3d 實現六邊形 戰績分佈圖

 

一個園 等分成 6份 ,  得出 六個 圓上的點, 兩兩畫線得出 一個 六邊形了

C#  代碼部分:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TmpGL : MonoBehaviour
{  // When added to an object, draws colored rays from the
    // transform position.
    public int lineCount = 100;
    public float radius = 3.0f;
    Vector3[] myPoint;
    Vector3[] myPoint1; 
    Vector3[] myPoint2;
    Vector3[] myPoint3; 
    static Material lineMaterial;
    private void Start()
    {
        myPoint = new Vector3[lineCount];
        myPoint1 = new Vector3[lineCount];
        myPoint2 = new Vector3[lineCount];
        myPoint3 = new Vector3[lineCount];
        for (int i = 0; i < lineCount; ++i)
        {
            float a = i / (float)lineCount;
            float angle = a * Mathf.PI * 2;
          
          myPoint[i]= new Vector3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
            myPoint1[i] = new Vector3(Mathf.Cos(angle) *( radius-1), Mathf.Sin(angle) * (radius - 1), 0);
            myPoint2[i] = new Vector3(Mathf.Cos(angle) * (radius - 2), Mathf.Sin(angle) * (radius - 2), 0);
            myPoint3[i] = new Vector3(Mathf.Cos(angle) * (radius - 3), Mathf.Sin(angle) * (radius - 3), 0);

        }
    }
    static void CreateLineMaterial()
    {
        if (!lineMaterial)
        {
            // Unity has a built-in shader that is useful for drawing
            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
            // Turn on alpha blending
            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
            // Turn backface culling off
            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
            // Turn off depth writes
            lineMaterial.SetInt("_ZWrite", 0);
        }
    }

    // Will be called after all regular rendering is done
    public void OnRenderObject()
    {
        CreateLineMaterial();
        // Apply the line material
        lineMaterial.SetPass(0);

        GL.PushMatrix();
        // Set transformation matrix for drawing to
        // match our transform
        GL.MultMatrix(transform.localToWorldMatrix);

        // Draw lines

       GLLines();
        GLLines(myPoint);
        GLLines(myPoint1);
        GLLines(myPoint2);
        GLLines(myPoint3);
         GLTrianGles(myPoint2);
       // GLTrianGles2(myPoint2);
        GL.PopMatrix();
    }
    void GLLines()
    {
        GL.Begin(GL.LINES);
        for (int i = 0; i < lineCount; ++i)
        {
            float a = i / (float)lineCount;
            float angle = a * Mathf.PI * 2;
            // Vertex colors change from red to green
            //  GL.Color(new Color(a, 1 - a, 0, 0.8F));
            // One vertex at transform position
            GL.Vertex3(0, 0, 0);
            // Another vertex at edge of circle
            GL.Vertex3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
        }
        GL.End();
    }
    void GLLines(Vector3[] mPoint)
    {
        GL.Begin(GL.LINES);
        for (int i = 0; i < lineCount; ++i)
        {
           
            GL.Vertex(mPoint[i]);
            int tmp = (i + 1) % lineCount;
            GL.Vertex(mPoint[tmp]);
        }
        GL.End();
    }
    void GLTrianGles(Vector3[] mPoint)
    {
        GL.Begin(GL.TRIANGLES);
        for (int i = 0; i < lineCount; ++i)
        {
            GL.Color(Color.blue);
            GL.Vertex(mPoint[i]);
            int tmp = (i + 1) % lineCount;
            GL.Vertex(mPoint[tmp]);
            GL.Vertex3(0, 0, 0);
        }
        GL.End();
    }
    void GLTrianGles2(Vector3[] mPoint)
    {
        GL.Begin(GL.TRIANGLES);
        for (int i = 0; i < lineCount; ++i)
        {
            GL.Color(Color.blue);
            GL.Vertex(mPoint[i]);
            int tmp = (i + 2) % lineCount;
            GL.Vertex(myPoint[tmp]);
            int tmp1 = (i + 1) % lineCount;
            GL.Vertex(myPoint1[tmp1]);
        }
        GL.End();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章