Polyline vertices position with thickness

Avatar
  • updated
  • Answered

Hello,

My question is a bit complicated so I hope I can explain it right.

I'm using Shapes to draw custom shapes into UI Toolkit VisualElement. To do that, I'm calling generateVisualContent and then creating a custom mesh with datas from PolygonPath like this:

PolygonPath p = new PolygonPath();
p.AddPoints(points);
p.EnsureMeshIsReadyToRender(PolygonTriangulation.EarClipping, out Mesh newMesh);  
List<vertex> vertices = new List<vertex>();
List<ushort> indices = new List<ushort>();

var meshVertices = newMesh.vertices;
for (int j = 0; j < meshVertices.Length; ++j)
{
    var vertex = new Vertex()
    {
        position = new Vector3(meshVertices[j].x, meshVertices[j].y, Vertex.nearZ),
        tint = color,
    };
    vertices.Add(vertex);
}

for (int j = 0; j < newMesh.subMeshCount; ++j)
{
    var list = newMesh.GetIndices(j);
    for (int k = 0; k < list.Length; ++k)
    {
        indices.Add((ushort)list[k]);
    }
}

MeshWriteData mesh = context.Allocate(vertices.Count, indices.Count);
mesh.SetAllVertices(vertices.ToArray());
mesh.SetAllIndices(indices.ToArray());

Its working well for PolygonPath but when I try the same with a PolylinePath, nothing is rendered. I tried looking for the reason and found that the vertices coordinates are duplicated, as if thickness was not applied maybe?

PolylinePath p = new PolylinePath();
for (int i = 0; i < points.Count; ++i)
    p.AddPoint(points[i], thickness, Color.magenta);

Thanks for your help,

Marina

Reporting a bug? please specify Unity version:
2020.3.17
Reporting a bug? please specify Shapes version:
4.1.0
Reporting a bug? please specify Render Pipeline:
URP
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Answered

This is because, as with most Shapes, a lot of the positioning of vertices happens in the shader itself, not in the mesh. This is why changing thickness on a polyline doesn't cause it to rebuild the mesh, as it's all just an input parameter to the shader

you can look in the polyline core shader if you'd like to see how the vertices are offset, and if you want, replicate that when copying the mesh out!

Avatar
Freya Holmér creator
  • Answer
  • Answered

This is because, as with most Shapes, a lot of the positioning of vertices happens in the shader itself, not in the mesh. This is why changing thickness on a polyline doesn't cause it to rebuild the mesh, as it's all just an input parameter to the shader

you can look in the polyline core shader if you'd like to see how the vertices are offset, and if you want, replicate that when copying the mesh out!