Unity中使用Mesh绘制圆环/空心圆,可调节圆环角度与位置

public class MeshDrawRing : MonoBehaviour
{
    public Material mat;
    public Transform go;
    public float radius = 6;            //外半径 
    public float innerRadius = 3;       //内半径
    public int segments = 60;           //分割数 
    public int angleDegree = 360;       //扇形或扇面的角度

    private void Start()
    {
        transform.eulerAngles = Vector3.left * 90;
    }

    private void Update()
    {
        DrawRing(radius, innerRadius, segments, angleDegree, go.position);
    }

    /// <summary>
    /// 画圆环
    /// </summary>
    /// <param name="radius">圆半径</param>
    /// <param name="innerRadius">内圆半径</param>
    /// <param name="segments">圆的分割数</param>
    /// <param name="angleDegree">圆覆盖的角度</param>
    /// <param name="centerCircle">圆心坐标</param>
    void DrawRing(float radius, float innerRadius, int segments, int angleDegree, Vector3 centerCircle)
    {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshRenderer>();
        gameObject.GetComponent<MeshRenderer>().material = mat;
        //顶点
        Vector3[] vertices = new Vector3[segments * 2];
        angleDegree = Mathf.Clamp(angleDegree, 0, 360);
        float deltaAngle = Mathf.Deg2Rad * angleDegree / segments;
        float currentAngle = 0;
        for (int i = 0; i < vertices.Length; i += 2)
        {
            float cosA = Mathf.Cos(currentAngle);
            float sinA = Mathf.Sin(currentAngle);
            vertices[i] = new Vector3(cosA * innerRadius + centerCircle.x, sinA * innerRadius + centerCircle.y, 0);
            vertices[i + 1] = new Vector3(cosA * radius + centerCircle.x, sinA * radius + centerCircle.y, 0);
            currentAngle += deltaAngle;
        }
        //三角形
        int[] triangles = new int[segments * 6];
        for (int i = 0, j = 0; i < segments * 6; i += 6, j += 2)
        {
            triangles[i] = j;
            triangles[i + 1] = (j + 1) % vertices.Length;
            triangles[i + 2] = (j + 3) % vertices.Length;
            triangles[i + 3] = j;
            triangles[i + 4] = (j + 3) % vertices.Length;
            triangles[i + 5] = (j + 2) % vertices.Length;
        }
        //uv:
        Vector2[] uvs = new Vector2[vertices.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            uvs[i] = new Vector2(vertices[i].x / radius / 2 + 0.5f, vertices[i].z / radius / 2 + 0.5f);
        }
        Mesh mesh = GetComponent<MeshFilter>().mesh;
        mesh.Clear();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过在Unity使用Mesh绘制角三角形。下面是一个简单的示例代码,演示如何创建一个角三角形的Mesh: ```csharp using UnityEngine; public class RoundedTriangle : MonoBehaviour { public float radius = 1f; public int segments = 30; void Start() { MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>(); MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>(); Mesh mesh = new Mesh(); meshFilter.mesh = mesh; Vector3[] vertices = new Vector3[segments + 2]; int[] triangles = new int[segments * 3]; vertices[0] = Vector3.zero; // 心点 float angleIncrement = 360f / segments; float currentAngle = 0f; for (int i = 1; i < segments + 2; i++) { float x = Mathf.Cos(Mathf.Deg2Rad * currentAngle) * radius; float y = Mathf.Sin(Mathf.Deg2Rad * currentAngle) * radius; vertices[i] = new Vector3(x, y, 0f); currentAngle += angleIncrement; } for (int i = 0; i < segments; i++) { triangles[i * 3] = 0; triangles[i * 3 + 1] = i + 1; triangles[i * 3 + 2] = i + 2; } mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); // 可以在这里设置材质等其他属性 } } ``` 将脚本挂载到一个空的GameObject上,然后在Inspector面板调整radius和segments属性来控制角三角形的大小和分段数。运行游戏后,你将在场景看到绘制角三角形。 注意:此示例仅绘制了一个平面的角三角形,如果你需要在3D空间绘制一个具有深度的角三角形,你可能需要使用更复杂的算法来生成相应的顶点和三角形。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值