需求 绘制圆角
核心函数
/// <summary>
/// 点ABC 形成的角度必须为90° 点c为中间的点
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="c"></param>
/// <param name="radius">圆角半径,不可为负数</param>
/// <param name="count">圆角数量,越多则越圆滑</param>
/// <returns></returns>
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<Vector3> points = new List<Vector3>();
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);
}
}
/// <summary>
/// 点ABC 形成的角度必须为90° 点c为中间的点
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="c"></param>
/// <param name="radius">圆角半径,不可为负数</param>
/// <param name="count">圆角数量,越多则越圆滑</param>
/// <returns></returns>
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<Vector3> points = new List<Vector3>();
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();
}
}