Archived
添加测试shader,抽象线路绘制功能
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
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="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(Vector3 point1, Vector3 point2, float thickness, bool center, Vector2 rectSize, Color color, VertexHelper vh, float currentLength, float totalLength)
|
||||
{
|
||||
// 计算偏移量:如果需要居中显示,则偏移到组件中心
|
||||
Vector3 offset = center ? (rectSize / 2) : Vector2.zero;
|
||||
|
||||
// 创建顶点模板,设置颜色为组件的当前颜色
|
||||
UIVertex vertex = UIVertex.simpleVert;
|
||||
vertex.color = color;
|
||||
|
||||
// 计算线段长度
|
||||
float segmentLength = Vector2.Distance(point1, point2);
|
||||
|
||||
// 计算起点和终点的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 += point1 - offset;
|
||||
vertex.uv0 = new Vector2(0f, startUVY); // 左侧顶点的UV
|
||||
vh.AddVert(vertex);
|
||||
|
||||
// 添加起点的右侧顶点
|
||||
vertex.position = point1Rotation * new Vector3(thickness / 2, 0);
|
||||
vertex.position += point1 - 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 += point2 - offset;
|
||||
vertex.uv0 = new Vector2(0f, endUVY); // 左侧顶点的UV
|
||||
vh.AddVert(vertex);
|
||||
|
||||
// 添加终点的右侧顶点
|
||||
vertex.position = point2Rotation * new Vector3(thickness / 2, 0);
|
||||
vertex.position += point2 - 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user