Question by
UMSDev · Jun 28, 2018 at 08:26 PM ·
meshrenderermeshrenderermeshfilter
Best way to display a 2D shape from x,y positions
I would like to find the best way (or at least a working way) to display in my UI a custom 2D shape from the position of all its points.
I have tried : - to create a mesh (using a triangulator script to convert my (x,y) to the vertices+triangles, with a mesh renderer and a mesh filter. It seems to work but it not rendered in my UI even if its parent is a canvas (screen overlay). - Creating a Canvasrenderer and setting my mesh inside it using SetMesh, but nothing seems to be displayed. Here is my code if it's the good way to do it :
GameObject shapeChild = new GameObject();
var shapeChildMeshRenderer = shapeChild.AddComponent<CanvasRenderer>();
shapeChildMeshRenderer.SetMaterial(material, null);
// Use the triangulator to get indices for creating triangles
Triangulator tr = new Triangulator(part.Points);
int[] indices = tr.Triangulate();
// Create the Vector3 vertices
Vector3[] vertices = new Vector3[vertices2D.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
}
// Create the mesh
Mesh _mesh = new Mesh();
_mesh.vertices = vertices;
_mesh.triangles = indices;
_mesh.RecalculateNormals();
_mesh.RecalculateBounds();
shapeChildMeshRenderer.SetMesh(_mesh);
shapeChild.transform.SetParent(this.transform);
I would like to keep the Screen Overlay setting.
Comment