Archived
虽然不对,但是起码计算思路是对的
This commit is contained in:
@@ -29,6 +29,8 @@ namespace XericLibrary
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool center = true;
|
public bool center = true;
|
||||||
|
|
||||||
|
public float a = 1;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 重写 MaskableGraphic 的方法,用于生成线条的网格
|
/// 重写 MaskableGraphic 的方法,用于生成线条的网格
|
||||||
/// 当 UI 需要重建时会自动调用此方法
|
/// 当 UI 需要重建时会自动调用此方法
|
||||||
@@ -52,7 +54,10 @@ namespace XericLibrary
|
|||||||
{
|
{
|
||||||
// 使用静态工具方法创建当前点与下一个点之间的线段
|
// 使用静态工具方法创建当前点与下一个点之间的线段
|
||||||
// 传递当前累积长度和总长度用于UV坐标计算
|
// 传递当前累积长度和总长度用于UV坐标计算
|
||||||
UILineRendererUtils.CreateLineSegment(points[i], points[i+1], thickness, center, rectTransform.sizeDelta, color, vh, currentLength, totalLength);
|
UILineRendererUtils.CreateLineSegment(points[i], points[i+1],
|
||||||
|
i > 0 ? Mathf.Acos(Vector2.Dot((points[i -1] - points[i]).normalized, (points[i + 1] - points[i]).normalized)) : 0,
|
||||||
|
i < points.Length-2 ? Mathf.Acos(Vector2.Dot((points[i + 1] - points[i]).normalized, (points[i + 1] - points[i +2]).normalized)) : 0,
|
||||||
|
thickness, center, rectTransform.sizeDelta, color, vh, currentLength, totalLength);
|
||||||
|
|
||||||
// 更新累积长度
|
// 更新累积长度
|
||||||
currentLength += Vector2.Distance(points[i], points[i+1]);
|
currentLength += Vector2.Distance(points[i], points[i+1]);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
|
using XericLibrary.Runtime.MacroLibrary;
|
||||||
|
|
||||||
namespace XericLibrary
|
namespace XericLibrary
|
||||||
{
|
{
|
||||||
@@ -35,6 +36,8 @@ namespace XericLibrary
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="point1">线段的起点</param>
|
/// <param name="point1">线段的起点</param>
|
||||||
/// <param name="point2">线段的终点</param>
|
/// <param name="point2">线段的终点</param>
|
||||||
|
/// <param name="angle0">起点到终点与终点到下一点</param>
|
||||||
|
/// <param name="angle3">起点到终点与终点到下一点</param>
|
||||||
/// <param name="thickness">线条的厚度</param>
|
/// <param name="thickness">线条的厚度</param>
|
||||||
/// <param name="center">是否居中显示</param>
|
/// <param name="center">是否居中显示</param>
|
||||||
/// <param name="rectSize">矩形变换的尺寸</param>
|
/// <param name="rectSize">矩形变换的尺寸</param>
|
||||||
@@ -42,49 +45,71 @@ namespace XericLibrary
|
|||||||
/// <param name="vh">用于添加顶点的 VertexHelper 对象</param>
|
/// <param name="vh">用于添加顶点的 VertexHelper 对象</param>
|
||||||
/// <param name="currentLength">当前线段起点在线路中的累积长度</param>
|
/// <param name="currentLength">当前线段起点在线路中的累积长度</param>
|
||||||
/// <param name="totalLength">线路的总长度</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)
|
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)
|
||||||
{
|
{
|
||||||
// 计算偏移量:如果需要居中显示,则偏移到组件中心
|
// 计算偏移量:如果需要居中显示,则偏移到组件中心
|
||||||
Vector3 offset = center ? (rectSize / 2) : Vector2.zero;
|
Vector2 offset = center ? (rectSize / 2) : Vector2.zero;
|
||||||
|
|
||||||
// 创建顶点模板,设置颜色为组件的当前颜色
|
// 创建顶点模板,设置颜色为组件的当前颜色
|
||||||
UIVertex vertex = UIVertex.simpleVert;
|
UIVertex vertex = UIVertex.simpleVert;
|
||||||
vertex.color = color;
|
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);
|
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值
|
// 计算起点和终点的UV坐标Y值
|
||||||
float startUVY = totalLength > 0 ? currentLength / totalLength : 0f;
|
float startUVY = totalLength > 0 ? currentLength / totalLength : 0f;
|
||||||
float endUVY = totalLength > 0 ? (currentLength + segmentLength) / totalLength : 0f;
|
float endUVY = totalLength > 0 ? (currentLength + segmentLength) / totalLength : 0f;
|
||||||
|
|
||||||
// 计算起点的旋转角度,使线条垂直于线段方向
|
// 计算起点的旋转角度,使线条垂直于线段方向
|
||||||
Quaternion point1Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point1, point2) + 90);
|
// Quaternion point1Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point1, point2) + 90);
|
||||||
|
|
||||||
// 添加起点的左侧顶点
|
// 添加起点的左侧顶点
|
||||||
vertex.position = point1Rotation * new Vector3(-thickness / 2, 0);
|
// vertex.position = point1Rotation * new Vector3(-thickness / 2, 0);
|
||||||
vertex.position += point1 - offset;
|
vertex.position = (Vector3)(point1 +
|
||||||
|
vectorSubtangent * -width +
|
||||||
|
vectorNormal * pointOffset1) - (Vector3)offset;
|
||||||
vertex.uv0 = new Vector2(0f, startUVY); // 左侧顶点的UV
|
vertex.uv0 = new Vector2(0f, startUVY); // 左侧顶点的UV
|
||||||
vh.AddVert(vertex);
|
vh.AddVert(vertex);
|
||||||
|
|
||||||
// 添加起点的右侧顶点
|
// 添加起点的右侧顶点
|
||||||
vertex.position = point1Rotation * new Vector3(thickness / 2, 0);
|
// vertex.position = point1Rotation * new Vector3(thickness / 2, 0);
|
||||||
vertex.position += point1 - offset;
|
vertex.position = (Vector3)(point1 +
|
||||||
|
vectorSubtangent * width +
|
||||||
|
vectorNormal * pointOffset1) - (Vector3)offset;
|
||||||
vertex.uv0 = new Vector2(1f, startUVY); // 右侧顶点的UV
|
vertex.uv0 = new Vector2(1f, startUVY); // 右侧顶点的UV
|
||||||
vh.AddVert(vertex);
|
vh.AddVert(vertex);
|
||||||
|
|
||||||
// 计算终点的旋转角度,使线条垂直于线段方向
|
// 计算终点的旋转角度,使线条垂直于线段方向
|
||||||
Quaternion point2Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point2, point1) - 90);
|
//Quaternion point2Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point2, point1) - 90);
|
||||||
|
|
||||||
// 添加终点的左侧顶点
|
// 添加终点的左侧顶点
|
||||||
vertex.position = point2Rotation * new Vector3(-thickness / 2, 0);
|
//vertex.position = point2Rotation * new Vector3(-thickness / 2, 0);
|
||||||
vertex.position += point2 - offset;
|
vertex.position = (Vector3)(point2 +
|
||||||
|
vectorSubtangent * -width +
|
||||||
|
vectorNormal * pointOffset2) - (Vector3)offset;
|
||||||
vertex.uv0 = new Vector2(0f, endUVY); // 左侧顶点的UV
|
vertex.uv0 = new Vector2(0f, endUVY); // 左侧顶点的UV
|
||||||
vh.AddVert(vertex);
|
vh.AddVert(vertex);
|
||||||
|
|
||||||
// 添加终点的右侧顶点
|
// 添加终点的右侧顶点
|
||||||
vertex.position = point2Rotation * new Vector3(thickness / 2, 0);
|
//vertex.position = point2Rotation * new Vector3(thickness / 2, 0);
|
||||||
vertex.position += point2 - offset;
|
vertex.position = (Vector3)(point2 +
|
||||||
|
vectorSubtangent * width +
|
||||||
|
vectorNormal * pointOffset2) - (Vector3)offset;
|
||||||
vertex.uv0 = new Vector2(1f, endUVY); // 右侧顶点的UV
|
vertex.uv0 = new Vector2(1f, endUVY); // 右侧顶点的UV
|
||||||
vh.AddVert(vertex);
|
vh.AddVert(vertex);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user