如何解决如何统一绘制线条平滑c#基本编码问题
我想从 unityUI 画线。 (我不想统一使用 Line Renderer)。
所以我想出了下面的编码,但我的问题是行大小不是恒定的。
public class MyUILineRenderer : Graphic
{
public Vector2[] pointPos;
public float[] angles;
public MyUIGridRenderer gridRenderer;
public Vector2Int gridSize = new Vector2Int(1,1);
public float lineThickness = 0.5f;
public float width;
public float height;
public float unitWidth;
public float unitHeight;
public float angleHelp;
public bool calAngle;
public Color[] colors;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
width = rectTransform.rect.width;
height = rectTransform.rect.height;
unitWidth = width / (float)gridSize.x;
unitHeight = height / (float)gridSize.y;
if (calAngle)
{
angles = new float[pointPos.Length];
}
if (pointPos.Length < 2)
{
return;
}
float angle = 0;
for (int i = 0; i < pointPos.Length; i++)
{
Vector2 point = pointPos[i];
if (i < pointPos.Length - 1)
{
if (calAngle)
{
angle = angles[i] = -GetAngle(pointPos[i],pointPos[i + 1]) + angleHelp;
}
else
{
angle = angles[i];
}
}
DrawLine(vh,point,angle);
}
int count = pointPos.Length * 2 - 2;
for (int i = 0; i < count; i += 2)
{
vh.AddTriangle(i + 0,i + 1,i + 3);
vh.AddTriangle(i + 0,i + 2,i + 3);
}
}
public float GetAngle(Vector2 currentPos,Vector2 targetPos) {
return (float)(Mathf.Atan2(targetPos.y - currentPos.y,targetPos.x - currentPos.x) * (180 / Mathf.PI));
}
private void DrawLine(VertexHelper vh,Vector2 point,float angle)
{
UIVertex vertex = UIVertex.simpleVert;
vertex.color = colors[0];
vertex.position = Quaternion.Euler(0,angle) * new Vector3(-lineThickness,0);
vertex.position += new Vector3(unitWidth * point.x,unitHeight * point.y);
vh.AddVert(vertex);
vertex.color = colors[1];
vertex.position = Quaternion.Euler(0,angle) * new Vector3(lineThickness,unitHeight * point.y);
vh.AddVert(vertex);
}
}
当我像这样统一绘制
正如你们所看到的,线条粗细不是恒定的。
我如何制作如下图所示的线条?
解决方法
如果有用,请从 docs 中找到这个改编的脚本来画线。
using UnityEngine;
public class Example : MonoBehaviour
{
// Draws a line from "startVertex" var to the curent mouse position.
public Material mat;
Vector3 startVertex;
Vector3 mousePos;
void Start()
{
startVertex = Vector3.zero;
}
void Update()
{
mousePos = Input.mousePosition;
// Press space to update startVertex
if (Input.GetKeyDown(KeyCode.Space))
{
startVertex = new Vector3(mousePos.x / Screen.width,mousePos.y / Screen.height,0);
}
}
void OnPostRender()
{
if (!mat)
{
Debug.LogError("Please Assign a material on the inspector");
return;
}
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(Color.red);
//GL.Vertex(startVertex);
//GL.Vertex(new Vector3(mousePos.x / Screen.width,0));
GL.Vertex(new Vector3(0,0));
GL.Vertex(new Vector3(0.3f,0.3f,0f));
GL.Vertex(new Vector3(0.3f,0f));
GL.Vertex(new Vector3(0.3f,0.5f,0f));
GL.Vertex(new Vector3(0.7f,0f));
GL.End();
GL.PopMatrix();
}
}
检查您需要如何在屏幕坐标中插入每对点。请注意,Unity 对附加到与启用的相机组件相同的游戏对象的 MonoBehaviour 调用 OnPostRender,因此您需要在此处附加此脚本才能使其工作。
找到红线的截图:
如果您可能需要在世界空间的场景中绘制点,您可以获得 The Size of the Frustum at a Given Distance from the Camera 将您需要的点转换为屏幕空间。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。