本文介绍: 【代码】Unity 圆角 线段 绘制 LineRender。
需求 绘制圆角
核心函数
/// <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();
}
}
原文地址:https://blog.csdn.net/qq_35030499/article/details/135404228
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_51784.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。