To confirm, ImmediateModeElement is the way to do this in 2019.1/.2 but starting with 2019.3 there will be a new mesh API for UIElements that lets you properly insert your own meshes in the render queue. More details will come closer to 2019.3 release.
Sorry I gave you class names that have actually been refactored (except MeshWriteData which is still there). Have a look at these documentation entries:
I canāt get anything drawn in the window for some reason. Iām also not sure what coordinate space I am drawing in. I extent the Visual Element and add this method to the geneateVisualContent Action.
What is the relationship between z depth and element sorting? When a visual element is sent to back or brought to front, is it the z value determining the sort order?
I wish the USS property ācolorā would also apply to the content generated by generateVisualContent. ābackground-colorā colors the bounds. Is there a workaround for that or is the only option to pass colors to vertex.tint?
At the moment depth is strictly controlled by the order in the visual element tree. This property will become meaningful once UIElements support 3D rendering but at the moment it should always be set to Vertex.nearZ.
Itās not done automatically by design because even though an element may have additional visual contents it is still able to render a background. Since ācolorā only applies to text it doesnāt seem appropriate to use it here.
But since the MeshGenerationContext contains the visual element reference, you can use visualElement.resolvedStyle.backgroundColor if youād like to use the same property as well.
Right, I agree. But in my case Iām not drawing text in the element so I will go with this using resolvedStyle. Itās a prettier workaround than reading the colors from C# (like in my related post ).
Hey guys, Iām moving over to using the MeshGenerationContext and having a really strange issueā¦
The mesh will only display if I add the class āunity-buttonā to the VisualElement!
In the following video Iām drawing the ācablesā using MeshGenerationContext, but as you can see, they only appear when I add this āunity-buttonā class.
I must be something Iām doing in my code because I can get the example from cecarlson to work.
Iām calling the following method from a foreach loop that iterates over each cable:
public static void DrawCable(Vector3[] points, float thickness, Color color, MeshGenerationContext context)
{
List<Vertex> vertices = new List<Vertex>();
List<ushort> indices = new List<ushort>();
for (int i = 0; i < points.Length - 1; i++)
{
var pointA = points[i];
var pointB = points[i + 1];
float angle = Mathf.Atan2(pointB.y - pointA.y, pointB.x - pointA.x);
float offsetX = thickness / 2 * Mathf.Sin(angle);
float offsetY = thickness / 2 * Mathf.Cos(angle);
vertices.Add(new Vertex()
{
position = new Vector3(pointA.x + offsetX, pointA.y - offsetY, Vertex.nearZ),
tint = color
});
vertices.Add(new Vertex()
{
position = new Vector3(pointB.x + offsetX, pointB.y - offsetY, Vertex.nearZ),
tint = color
});
vertices.Add(new Vertex()
{
position = new Vector3(pointB.x - offsetX, pointB.y + offsetY, Vertex.nearZ),
tint = color
});
vertices.Add(new Vertex()
{
position = new Vector3(pointB.x - offsetX, pointB.y + offsetY, Vertex.nearZ),
tint = color
});
vertices.Add(new Vertex()
{
position = new Vector3(pointA.x - offsetX, pointA.y + offsetY, Vertex.nearZ),
tint = color
});
vertices.Add(new Vertex()
{
position = new Vector3(pointA.x + offsetX, pointA.y - offsetY, Vertex.nearZ),
tint = color
});
ushort indexOffset(int value) => (ushort)(value + (i * 6));
indices.Add(indexOffset(0));
indices.Add(indexOffset(1));
indices.Add(indexOffset(2));
indices.Add(indexOffset(3));
indices.Add(indexOffset(4));
indices.Add(indexOffset(5));
}
var mesh = context.Allocate(vertices.Count, indices.Count);
mesh.SetAllVertices(vertices.ToArray());
mesh.SetAllIndices(indices.ToArray());
}
This worked just fine when I was calling GL.Vertex3 in a GL.TRIANGLES context (without worrying about indices however).
I was calling MarkDirtyRepaint without any success.
Although itās working just fine nowā¦ and I havenāt made any changes in my code since this morning. That makes me think it was a bug in the editor. But Iām not sure what was causing it. If I see this again Iāll try and figure out how to reproduce it and file it as a bug.