0
\$\begingroup\$

I'm trying to update the positions of vertices in my project.

I'd been using Unity's built in Line Renderer previously which worked fine, but had some problems when points got too close to each other.

Now I'm writing my own Line Renderer which is also looking good so far; it draws lines. I'm having no trouble with updating the values in inspector at runtime. Though doing it through code doesn't seem to do anything...

I'm using the Graphic class from the UnityEngine.UI library because I want to draw the lines as UI elements. And the solution I've found has been using either Rebuild(), SetAllDirty() or SetVerticesDirty() to force the mesh to update. This works, but also seems to leave old invisible vertices and tris in the scene every time it's called. After a couple seconds of running, the scene has 300k vertices and is running at 10 Fps, when the mesh itself is only about 2k verts.

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UICircleBuilder : Graphic
{
    public Point[] points;
    public bool makeTri2;
    public Color secondColor;

    public CanvasUpdate update;
  
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();
        UIVertex vertex = UIVertex.simpleVert;
        vertex.color = color;

        for (int i = 0; i < points.Length-1; i++)
        {




            float vertexWidth = Mathf.Clamp(points[i].width / 2, 0, 1000);
            float nextVertexWidth = Mathf.Clamp(points[i+1].width / 2, 0, 1000);


            Vector2 angleBetweenPoints = (points[i + 1].position - points[i].position).normalized;
            Vector2 pointCross = Vector3.Cross(angleBetweenPoints, Vector3.forward).normalized;
            Debug.DrawLine(points[i + 1].position, points[i].position, Color.green, 10);
            Debug.DrawLine(points[i].position,points[i].position + pointCross*100, Color.blue, 10);


            vertex.color = points[i].color;
            //Vertex i
            vertex.position = (Vector3)points[i].position - (Vector3)pointCross * vertexWidth;
            vh.AddVert(vertex);


            
            //Vertex i+1
            vertex.position = (Vector3)points[i].position + (Vector3)pointCross * vertexWidth;
            vh.AddVert(vertex);

            
            if(i != points.Length)
            {
                //Vertex i+2
                vertex.position = (Vector3)points[i + 1].position - (Vector3)pointCross * nextVertexWidth;
                vh.AddVert(vertex);



                //Vertex i+3
                vertex.position = (Vector3)points[i + 1].position + (Vector3)pointCross * nextVertexWidth;
                vh.AddVert(vertex);
            }

        }


        for (int i = 0; i < points.Length-1; i++)
        {
            vh.AddTriangle(i*4, i*4+1, (i*4+3));
            Debug.Log(i*4 + ", " + (i*4 + 1) + ", " + (i*4 + 3));
                
            if (makeTri2)
            {
                vh.AddTriangle((i * 4 + 3), (i * 4 + 2), i * 4);
                Debug.Log((i * 4 + 3) + ", " + (i * 4 + 2) + ", " + i * 4);
            }

        }
        
    }

    public void UpdateMesh()
    {
        SetVerticesDirty();
    }
   
}

[System.Serializable]
public class Point
{
    public Vector2 position;
    [Range(0,1000)]
    public float width = 1;
    public Color color = Color.white;
    public Point(Vector2 position, float width = 1)
    {
        this.position = position;
        this.width = width;
    }
}

The mesh is generated using this code, which reads an array of vector2 positions and creates the vertices, afterwards it draws the tris

Scene stats after running for 5 seconds:

Scene stats after running for 5 seconds

I'd be grateful for any ideas.

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

dont use Deafult Materil. it draw a lot times , but I don't know why.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .