Archived
135 lines
6.3 KiB
C#
135 lines
6.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using XericLibrary.Runtime.MacroLibrary;
|
|
|
|
namespace XericLibrary
|
|
{
|
|
/// <summary>
|
|
/// UILineRenderer 的工具类,提供静态方法用于线条渲染相关的操作
|
|
/// </summary>
|
|
public static class UILineRendererUtils
|
|
{
|
|
/// <summary>
|
|
/// 计算线路的总长度
|
|
/// </summary>
|
|
/// <param name="points">线路的顶点数组</param>
|
|
/// <returns>线路的总长度</returns>
|
|
public static float CalculateTotalLength(Vector2[] points)
|
|
{
|
|
if (points.Length < 2)
|
|
return 0f;
|
|
|
|
float totalLength = 0f;
|
|
for (int i = 0; i < points.Length - 1; i++)
|
|
{
|
|
totalLength += Vector2.Distance(points[i], points[i + 1]);
|
|
}
|
|
return totalLength;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建线段的顶点数据
|
|
/// 每个线段由 5 个顶点组成:
|
|
/// - 起点的左右两个顶点
|
|
/// - 终点的左右两个顶点
|
|
/// - 终点的中心点(用于线段连接)
|
|
/// </summary>
|
|
/// <param name="point1">线段的起点</param>
|
|
/// <param name="point2">线段的终点</param>
|
|
/// <param name="angle0">起点到终点与终点到下一点</param>
|
|
/// <param name="angle3">起点到终点与终点到下一点</param>
|
|
/// <param name="thickness">线条的厚度</param>
|
|
/// <param name="center">是否居中显示</param>
|
|
/// <param name="rectSize">矩形变换的尺寸</param>
|
|
/// <param name="color">线条的颜色</param>
|
|
/// <param name="vh">用于添加顶点的 VertexHelper 对象</param>
|
|
/// <param name="currentLength">当前线段起点在线路中的累积长度</param>
|
|
/// <param name="totalLength">线路的总长度</param>
|
|
public static void CreateLineSegment(Vector2 point1, Vector2 point2, float angle0, float angle3, float thickness, bool center, Vector2 rectSize, Color color, VertexHelper vh, float currentLength, float totalLength)
|
|
{
|
|
// 计算偏移量:如果需要居中显示,则偏移到组件中心
|
|
Vector2 offset = center ? (rectSize / 2) : Vector2.zero;
|
|
|
|
// 创建顶点模板,设置颜色为组件的当前颜色
|
|
UIVertex vertex = UIVertex.simpleVert;
|
|
vertex.color = color;
|
|
|
|
var vector = point2 - point1;
|
|
var vectorDistance = vector.magnitude;
|
|
|
|
// 距离过小时不绘制
|
|
if (vectorDistance <= float.Epsilon)
|
|
return;
|
|
// 线段的法线和副法线
|
|
var vectorNormal = vector / vectorDistance;
|
|
var vectorSubtangent = Vector2.Perpendicular(vectorNormal);
|
|
// 计算线段长度
|
|
float segmentLength = Vector2.Distance(point1, point2);
|
|
// 线段单侧宽度
|
|
var width = thickness / 2;
|
|
//顶点偏移量
|
|
var pointOffset1 = angle0 == 0 ? 0 : width / Mathf.Sin(angle0 / 2);
|
|
var pointOffset2 = angle3 == 0 ? 0 : width / Mathf.Sin(angle3 / 2);
|
|
Debug.Log($"{pointOffset1} , {pointOffset2}");
|
|
// 计算起点和终点的UV坐标Y值
|
|
float startUVY = totalLength > 0 ? currentLength / totalLength : 0f;
|
|
float endUVY = totalLength > 0 ? (currentLength + segmentLength) / totalLength : 0f;
|
|
|
|
// 计算起点的旋转角度,使线条垂直于线段方向
|
|
// Quaternion point1Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point1, point2) + 90);
|
|
|
|
// 添加起点的左侧顶点
|
|
// vertex.position = point1Rotation * new Vector3(-thickness / 2, 0);
|
|
vertex.position = (Vector3)(point1 +
|
|
vectorSubtangent * -width +
|
|
vectorNormal * pointOffset1) - (Vector3)offset;
|
|
vertex.uv0 = new Vector2(0f, startUVY); // 左侧顶点的UV
|
|
vh.AddVert(vertex);
|
|
|
|
// 添加起点的右侧顶点
|
|
// vertex.position = point1Rotation * new Vector3(thickness / 2, 0);
|
|
vertex.position = (Vector3)(point1 +
|
|
vectorSubtangent * width +
|
|
vectorNormal * pointOffset1) - (Vector3)offset;
|
|
vertex.uv0 = new Vector2(1f, startUVY); // 右侧顶点的UV
|
|
vh.AddVert(vertex);
|
|
|
|
// 计算终点的旋转角度,使线条垂直于线段方向
|
|
//Quaternion point2Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point2, point1) - 90);
|
|
|
|
// 添加终点的左侧顶点
|
|
//vertex.position = point2Rotation * new Vector3(-thickness / 2, 0);
|
|
vertex.position = (Vector3)(point2 +
|
|
vectorSubtangent * -width +
|
|
vectorNormal * pointOffset2) - (Vector3)offset;
|
|
vertex.uv0 = new Vector2(0f, endUVY); // 左侧顶点的UV
|
|
vh.AddVert(vertex);
|
|
|
|
// 添加终点的右侧顶点
|
|
//vertex.position = point2Rotation * new Vector3(thickness / 2, 0);
|
|
vertex.position = (Vector3)(point2 +
|
|
vectorSubtangent * width +
|
|
vectorNormal * pointOffset2) - (Vector3)offset;
|
|
vertex.uv0 = new Vector2(1f, endUVY); // 右侧顶点的UV
|
|
vh.AddVert(vertex);
|
|
|
|
// 添加终点的中心点,用于连接后续线段
|
|
vertex.position = point2 - offset;
|
|
vertex.uv0 = new Vector2(0.5f, endUVY); // 中心点的UV
|
|
vh.AddVert(vertex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算一个点指向另一个点的角度(以度为单位)
|
|
/// </summary>
|
|
/// <param name="vertex">起始点</param>
|
|
/// <param name="target">目标点</param>
|
|
/// <returns>旋转角度,范围为 -180 到 180 度</returns>
|
|
public static float RotatePointTowards(Vector2 vertex, Vector2 target)
|
|
{
|
|
// 使用反正切函数计算两点之间的角度
|
|
// 将弧度转换为角度返回
|
|
return (float)(Mathf.Atan2(target.y - vertex.y, target.x - vertex.x) * (180 / Mathf.PI));
|
|
}
|
|
}
|
|
} |