2

Last time i asked how to draw line without use line renderer, and i found answer myself

(How to draw line smooth in unity (c# base coding problem))

But when i done coding, the angle of the line increases, the line shows a bavel shape.

(2 compare lines below)

sds

enter image description here

This picture is the concept of my line drawing.

enter image description here

what am i wrong?

my main code is



public class MyUILineRenderer : Graphic{
    public Vector3[] 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 List<Vector3> edgePos;
    public List<Vector3> segmentPos = new List<Vector3>();

protected override void OnPopulateMesh(VertexHelper vh)


        vh.Clear();
        edgePos.Clear();
        segmentPos.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];
        }

        //case of point count less than 2 
        if (pointPos.Length < 2)
        {
            return;
        }

        //Get Edge Points
        for (int i = 0; i < pointPos.Length - 1; i++)
        {

            int index = i * 4;
            GetEdgePoint(vh, pointPos[i], pointPos[i + 1], index);
        }

        //Get segment points
        GetSegmentPoint(vh);


        //draw line with segment points
        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);
        }


    }
}

i get edge points From this code

    private void GetEdgePoint(VertexHelper vh, Vector3 point, Vector3 nextPoint, int i)
    {
        UIVertex vertex = UIVertex.simpleVert;
        //vertex.color = color;

        //Calculate grid size to screen size
        Vector3 pos = new Vector3(unitWidth * point.x, unitHeight * point.y);
        Vector3 nextpos = new Vector3(unitWidth * nextPoint.x, unitHeight * nextPoint.y);
        Vector3 normal = Vector3.Cross(pos, nextpos);
        Vector3 side = Vector3.Cross(normal, nextpos - pos);
        side.Normalize();

        //get edge Point (4 points)
        vertex.position = pos + side * (lineThickness);
        edgePos.Add(vertex.position);

        vertex.position = pos + side * (-lineThickness);
        edgePos.Add(vertex.position);

        vertex.position = nextpos + side * (lineThickness);
        edgePos.Add(vertex.position);

        vertex.position = nextpos + side * (-lineThickness);
        edgePos.Add(vertex.position);
    }

get segment points code

    private void GetSegmentPoint(VertexHelper vh)
    {
        UIVertex vertex = UIVertex.simpleVert;
        vertex.color = color;
        segmentPos.Add(edgePos[0]);
        vertex.position = edgePos[0];
        vh.AddVert(vertex);
        segmentPos.Add(edgePos[1]);
        vertex.position = edgePos[1];
        vh.AddVert(vertex);


        //Get segment points
        if (pointPos.Length > 2)
        {
            int index = 0;
            for (int i = 0; i < pointPos.Length - 2; i++)
            {
                Vector3 vec = Vector3.zero;
                if (MyMath.LineLineIntersection(out vec, edgePos[index], edgePos[index + 2] - edgePos[index], edgePos[index + 6], edgePos[index + 4] - edgePos[index + 6]))
                {
                    segmentPos.Add(vec);
                    vertex.position = vec;
                    vh.AddVert(vertex);
                }
                else
                {
                    vec = vertex.position = edgePos[index + 3] + (edgePos[index + 5] - edgePos[index + 3]) / 2;
                    segmentPos.Add(vec);
                    vh.AddVert(vertex);

                    Debug.Log(index + " up");
                }

                if (MyMath.LineLineIntersection(out vec, edgePos[index + 1], edgePos[index + 3] - edgePos[index + 1], edgePos[index + 7], edgePos[index + 5] - edgePos[index + 7]))
                {
                    segmentPos.Add(vec);
                    vertex.position = vec;
                    vh.AddVert(vertex);
                }
                else
                {
                    vec = vertex.position = edgePos[index + 2] + (edgePos[index + 4] - edgePos[index + 2]) / 2;
                    segmentPos.Add(vec);
                    vh.AddVert(vertex);

                    Debug.Log(index + " down");

                }
                index += 4;


            }

        }

        //Set last edge points to segment points
        segmentPos.Add(edgePos[edgePos.Count - 2]);
        vertex.position = edgePos[edgePos.Count - 2];
        vh.AddVert(vertex);
        segmentPos.Add(edgePos[edgePos.Count - 1]);
        vertex.position = edgePos[edgePos.Count - 1];
        vh.AddVert(vertex);
    }

calculate lineintersection from edge points code

        public static bool LineIntersection(Vector3 a, Vector3 b, Vector3 c, Vector3 d, out Vector3 x)
        {
            Vector3 lineVec3 = c - a;
            Vector3 vec1 = b - a;
            Vector3 vec2 = d - c;

            //Vector3 lineVec3 = linePoint2 - linePoint1;
            Vector3 crossVec1and2 = Vector3.Cross(vec1, vec2);
            Vector3 crossVec3and2 = Vector3.Cross(lineVec3, vec2);

            float det = Vector3.Dot(lineVec3, crossVec1and2);
            //두선이 평행인 경우
            if (Mathf.Abs(det) < 0.0001f)
            {
                x = Vector3.zero;
                return false;
            }
            x = a + (vec1 * (Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude));
            return true;
        }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

1 Answers1

0

Why mix lines and verts if you have your buffer ready?

//draw line with segment points
    
    for (int i = 0; i < vh.currentVertCount; i += 4)
    {
        vh.AddTriangle(i + 0, i + 1, i + 3);
        vh.AddTriangle(i + 0, i + 2, i + 3);
    }
joreldraw
  • 1,736
  • 1
  • 14
  • 28
  • In fact, this code draws a square instead of a line. The reason is that This is because I want to set the thickness of the line and I want to color the rectangle using a shader or a special effect (like animation) using a shader graph. – Arbus Omana Jan 04 '21 at 11:55
  • yes is ok,but you have done all calc rights and have a buffer of all your needed vertex in "vh". Replace this part of code to do the mesh with this points with an i+4 index for each rectangle (2 tris) and prevent duplicate lines of your code and bad vertex order. – joreldraw Jan 04 '21 at 12:02