Unity Gizmos 绘制扇形

Unity Gizmos 绘制扇形_第1张图片 Unity Gizmos 绘制扇形_第2张图片

线框扇形是用线条拼起来的,实心的扇形的是提前生成好mesh,然后用drawmesh。


using UnityEngine;

public static class GizmosTools
{
    /// 
    /// 绘制半圆
    /// 
    public static void DrawWireSemicircle(Vector3 origin,Vector3 direction,float radius,int angle)
    {
        DrawWireSemicircle(origin,direction,radius,angle,Vector3.up);
    }
    public static void DrawWireSemicircle(Vector3 origin,Vector3 direction,float radius,int angle,Vector3 axis)
    {
        Vector3 leftdir = Quaternion.AngleAxis(-angle/2, axis)*direction;
        Vector3 rightdir = Quaternion.AngleAxis(angle/2, axis)*direction;

        Vector3 currentP = origin + leftdir * radius;
        Vector3 oldP;
        if (angle!=360)
        {
            Gizmos.DrawLine(origin,currentP);
        }
        for (int i = 0; i < angle/10; i++)
        {
            Vector3 dir= Quaternion.AngleAxis(10*i, axis)*leftdir;
            oldP = currentP;
            currentP=origin + dir * radius;
            Gizmos.DrawLine(oldP,currentP);
        }
        oldP = currentP;
        currentP=origin + rightdir * radius;
        Gizmos.DrawLine(oldP,currentP);
        if (angle!=360)
        {
            Gizmos.DrawLine(currentP,origin);
        }
       
    }

    public static Mesh SemicircleMesh(float radius,int angle,Vector3 axis)
    {
        Vector3 leftdir = Quaternion.AngleAxis(-angle/2, axis)*Vector3.forward;
        Vector3 rightdir = Quaternion.AngleAxis(angle/2, axis)*Vector3.forward;
        int pcount = angle / 10;
        //顶点
        Vector3[] vertexs = new Vector3[3+pcount];
        vertexs[0] = Vector3.zero;
        int index = 1;
        vertexs[index] = leftdir * radius;
        index++;
        for (int i = 0; i < pcount; i++)
        {
            Vector3 dir= Quaternion.AngleAxis(10*i, axis)*leftdir;
            vertexs[index]= dir * radius;
            index++;
        }
        vertexs[index] = rightdir * radius;
        //三角面
        int[] triangles=new int[3*(1+pcount)];
        for (int i = 0; i < 1+pcount; i++)
        {
            triangles[3 * i] = 0;
            triangles[3 * i + 1] = i+1;
            triangles[3 * i + 2] = i+2;
        }
        
        Mesh mesh=new Mesh();
        mesh.vertices = vertexs;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();
        return mesh;
    }
}

 

你可能感兴趣的:(Unity)