Archived
虽然不对,但是起码计算思路是对的
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XericLibrary.Runtime.MacroLibrary;
|
||||
|
||||
namespace XericLibrary
|
||||
{
|
||||
@@ -35,6 +36,8 @@ namespace XericLibrary
|
||||
/// </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>
|
||||
@@ -42,49 +45,71 @@ namespace XericLibrary
|
||||
/// <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)
|
||||
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;
|
||||
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);
|
||||
// Quaternion point1Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point1, point2) + 90);
|
||||
|
||||
// 添加起点的左侧顶点
|
||||
vertex.position = point1Rotation * new Vector3(-thickness / 2, 0);
|
||||
vertex.position += point1 - offset;
|
||||
// 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 += point1 - offset;
|
||||
// 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);
|
||||
//Quaternion point2Rotation = Quaternion.Euler(0, 0, RotatePointTowards(point2, point1) - 90);
|
||||
|
||||
// 添加终点的左侧顶点
|
||||
vertex.position = point2Rotation * new Vector3(-thickness / 2, 0);
|
||||
vertex.position += point2 - offset;
|
||||
//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 += point2 - offset;
|
||||
//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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user