Unity 圆角 线段 绘制 LineRender

需求 绘制圆角

Unity 圆角 线段 绘制 LineRender_第1张图片Unity 圆角 线段 绘制 LineRender_第2张图片

Unity 圆角 线段 绘制 LineRender_第3张图片

核心函数

     /// 
    /// 点ABC 形成的角度必须为90° 点c为中间的点
    /// 
    /// 
    /// 
    /// 
    /// 圆角半径,不可为负数
    /// 圆角数量,越多则越圆滑
    /// 
    public static Vector3[] Rounded(Vector3 a, Vector3 b, Vector3 c, float radius, int count)
    {
        Vector3 ca = (a - c).normalized;
        Vector3 cb = (b - c).normalized;

        Vector3 d = c + ca * radius + cb * radius;

        Vector3 a1 = c + ca * radius;
        Vector3 b1 = c + cb * radius;

        Vector3 db1 = (b1 - d).normalized;



        Vector3 cross = Vector3.Cross(ca, cb);

        List points = new List();

        points.Add(b);
        for (int i = 0; i <= count; i++)
        {
            Vector3 p = d + Quaternion.AngleAxis((float)i / (float)count * 90, cross) * db1 * radius;
            points.Add(p);
        }
        points.Add(a);
        return points.ToArray();
    }

测试代码

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

public class Test : MonoBehaviour
{
    public float Radius = 1;
    public int Count = 25;


    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnDrawGizmos()
    {
        Vector3[] v = Rounded(Vector3.up * 2, Vector3.right * 2, Vector3.zero, Radius, Count);

        for (int i = 0; i < v.Length - 1; i++)
        {
            Gizmos.DrawLine(v[i], v[i + 1]);
            Gizmos.DrawSphere(v[i], 0.01f);
        }
    }

      /// 
    /// 点ABC 形成的角度必须为90° 点c为中间的点
    /// 
    /// 
    /// 
    /// 
    /// 圆角半径,不可为负数
    /// 圆角数量,越多则越圆滑
    /// 
    public static Vector3[] Rounded(Vector3 a, Vector3 b, Vector3 c, float radius, int count)
    {
        Vector3 ca = (a - c).normalized;
        Vector3 cb = (b - c).normalized;

        Vector3 d = c + ca * radius + cb * radius;

        Vector3 a1 = c + ca * radius;
        Vector3 b1 = c + cb * radius;

        Vector3 db1 = (b1 - d).normalized;



        Vector3 cross = Vector3.Cross(ca, cb);

        List points = new List();

        points.Add(b);
        for (int i = 0; i <= count; i++)
        {
            Vector3 p = d + Quaternion.AngleAxis((float)i / (float)count * 90, cross) * db1 * radius;
            points.Add(p);
        }
        points.Add(a);
        return points.ToArray();
    }
}

你可能感兴趣的:(Unity3D,unity,圆角,LineRender)