如何统一绘制线条(C#基本编码问题) [英] How to draw line smooth in unity (c# base coding problem)

查看:63
本文介绍了如何统一绘制线条(C#基本编码问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从unityUI画线.(我不想统一使用Line Renderer).

所以我想出了下面的代码,但是我的问题是行大小不是恒定的.

 公共类MyUILineRenderer:图形{public Vector2 [] pointPos;public float []角度;公共MyUIGridRenderer gridRenderer;public Vector2Int gridSize = new Vector2Int(1,1);公共浮动线厚度= 0.5f;公共浮动宽度;公共浮标高度;public float unitWidth;public float unitHeight;公共浮子角度帮助;公共布尔calAngle;公共Color []颜色;受保护的重写void OnPopulateMesh(VertexHelper vh){vh.Clear();宽度= rectTransform.rect.width;height = rectTransform.rect.height;unitWidth =宽度/(float)gridSize.x;unitHeight =高度/(float)gridSize.y;如果(calAngle){angles =新的float [pointPos.Length];}如果(pointPos.Length< 2){返回;}浮角= 0;对于(int i = 0; i< pointPos.Length; i ++){Vector2 point = pointPos [i];如果(i< pointPos.Length-1){如果(calAngle){angle = angles [i] = -GetAngle(pointPos [i],pointPos [i + 1])+ angleHelp;}别的{angle = angles [i];}}DrawLine(vh,点,角度);}int count = pointPos.Length * 2-2;对于(int i = 0; i< count; i + = 2){vh.AddTriangle(i + 0,i + 1,i + 3);vh.AddTriangle(i + 0,i + 2,i + 3);}}公共浮动GetAngle(Vector2 currentPos,Vector2 targetPos){return(float)(Mathf.Atan2(targetPos.y-currentPos.y,targetPos.x-currentPos.x)*(180/Mathf.PI));}私有void DrawLine(VertexHelper vh,Vector2点,浮角){UIVertex顶点= UIVertex.simpleVert;vertex.color = colors [0];vertex.position =四元数.Euler(0,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 =四元数.Euler(0,0,angle)* new Vector3(lineThickness,0);vertex.position + = new Vector3(unitWidth * point.x,unitHeight * point.y);vh.AddVert(vertex);}} 

当我像这样团结一致时

如果您可能需要从世界空间中的场景中绘制点,则可以获取在距相机一定距离处的视锥的大小以转换所需的点到屏幕空间.

I want to draw line from unityUI. (i dont want to use Line Renderer in unity).

So I figured out the coding below, but my problem is the line sizes are not constant.

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, 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, 0, angle) * new Vector3(lineThickness, 0);
        vertex.position += new Vector3(unitWidth * point.x, unitHeight * point.y);
        vh.AddVert(vertex);
    }
}

and when i draw in unity like this

https://ibb.co/0MS63Ly

As you guys can see that line thickness is not constant.

How could i make line like picture below?

https://ibb.co/JdhRxqH

解决方案

In case it's useful, find this adapted script from the docs to draw lines.

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, mousePos.y / Screen.height, 0));
        
        GL.Vertex(new Vector3(0, 0, 0));
        GL.Vertex(new Vector3(0.3f, 0.3f, 0f));

        GL.Vertex(new Vector3(0.3f, 0.3f, 0f));
        GL.Vertex(new Vector3(0.3f, 0.5f, 0f));

        GL.Vertex(new Vector3(0.3f, 0.5f, 0f));
        GL.Vertex(new Vector3(0.7f, 0.5f, 0f));

        GL.End();

        GL.PopMatrix();
    }
}

Check how you need to insert every pair of points in screen coordinates. Be aware that Unity calls OnPostRender on MonoBehaviours that are attached to the same GameObject as an enabled Camera component, so there is where you would need to attach this script for it to work.

Find screenShot of the red lines:

In case you might need to draw points from the scene in the world space, you can obtain The Size of the Frustum at a Given Distance from the Camera to transform the points you need to the screen space.

这篇关于如何统一绘制线条(C#基本编码问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆