[ad_1]
I’m making an attempt to replace the positions of vertices in my venture.
I’d been utilizing Unity’s in-built Line Renderer beforehand which labored superb, however had some issues when factors bought too shut to one another.
Now I’m writing my very own Line Renderer which can be wanting good to this point; it attracts strains. I’m having no bother with updating the values in inspector at runtime. Though doing it by code does not appear to do something…
I’m utilizing the Graphic
class from the UnityEngine.UI
library as a result of I need to draw the strains as UI components. And the answer I’ve discovered has been utilizing both Rebuild()
, SetAllDirty() or SetVerticesDirty()
to power the mesh to replace. This works, but additionally appears to depart previous invisible vertices and tris within the scene each time it is referred to as. After a pair seconds of working, the scene has 300k vertices and is working at 10 Fps, when the mesh itself is just about 2k verts.
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.UI;
public class UICircleBuilder : Graphic
{
public Point[] factors;
public bool makeTri2;
public Color secondColor;
public CanvasUpdate replace;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
UIVertex vertex = UIVertex.simpleVert;
vertex.colour = colour;
for (int i = 0; i < factors.Length-1; i++)
{
float vertexWidth = Mathf.Clamp(factors[i].width / 2, 0, 1000);
float subsequentVertexWidth = Mathf.Clamp(factors[i+1].width / 2, 0, 1000);
Vector2 angleBetweenPoints = (factors[i + 1].place - factors[i].place).normalized;
Vector2 levelCross = Vector3.Cross(angleBetweenPoints, Vector3.ahead).normalized;
Debug.DrawLine(factors[i + 1].place, factors[i].place, Color.inexperienced, 10);
Debug.DrawLine(factors[i].place,factors[i].place + levelCross*100, Color.blue, 10);
vertex.colour = factors[i].colour;
//Vertex i
vertex.place = (Vector3)factors[i].place - (Vector3)levelCross * vertexWidth;
vh.AddVert(vertex);
//Vertex i+1
vertex.place = (Vector3)factors[i].place + (Vector3)levelCross * vertexWidth;
vh.AddVert(vertex);
if(i != factors.Length)
{
//Vertex i+2
vertex.place = (Vector3)factors[i + 1].place - (Vector3)levelCross * subsequentVertexWidth;
vh.AddVert(vertex);
//Vertex i+3
vertex.place = (Vector3)factors[i + 1].place + (Vector3)levelCross * subsequentVertexWidth;
vh.AddVert(vertex);
}
}
for (int i = 0; i < factors.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 place;
[Range(0,1000)]
public float width = 1;
public Color colour = Color.white;
public Point(Vector2 place, float width = 1)
{
this.place = place;
this.width = width;
}
}
The mesh is generated utilizing this code, which reads an array of vector2 positions and creates the vertices, afterwards it attracts the tris
Scene stats after working for five seconds:
I’d be pleased about any concepts.
[ad_2]