How do you draw a line?


I know this is a very basic question, but how do you draw lines in amethyst? I want to draw a line between two 2d sprites with a given width and color.

I’ve looked through the documentation, and found a few potential solutions, none of which I’m satisfied with.

Use DebugLines. This seems like the most straightforward solution, but

  • I can’t control the line’s thickness.
  • I’m not using this to “debug” anything.

Use a rectangular Sprite and transform it to get the appropriate length, angle, and width.

  • This is a workable solution, but the result looks a bit ugly. How do I control AntiAliasing?

Use a 2d Mesh.

  • I haven’t been able to get this working. How do I create a Mesh from 4 points? (The only examples I’ve found show how to load meshes from a file, which seems like overkill for a short line)

    let mesh = Mesh::builder()
    // The above gives a `MeshData`, but I think I need a
    // `Handle<Mesh>` component to add to the entity
(Azriel Hoh) #2

Heya, looks like you listed all the ready made solutions :stuck_out_tongue:, some commentary:

  1. I wanted to remove “Debug” from the name, and make it use Transform for its coordinates instead of being in absolute coordinates always, but other things have taken priority.
  2. Don’t think anti-aliasing is controllable in this one.
  3. Handle<Mesh> is returned when you go loader.load_from_data(mesh_data, (), progress_counter). See step 4 in How to Use Assets.

Hope that helps :v:

1 Like

Thanks. It seems I overlooked the load_from_data function.

I guess I’ll have to live with aliasing for now. Both the mesh solution and the sprite solution have the same jaggies at the edges.

Also, I may have found a bug: It seems that calls to mesh_builder.with_prim_type(TriangleStrips) are ignored, and I need 6 vertices instead of 4 (amethyst 0.14.0; vulkan backend).

let poss = vec![
    Position([-0.5, -0.5, 0.0]),
    Position([0.5, -0.5, 0.0]), Position([-0.5, 0.5, 0.0]),
    Position([-0.5, 0.5, 0.0]), Position([0.5, -0.5, 0.0]), // These shouldn't be needed
    Position([0.5, 0.5, 0.0])
let norms = vec![Normal([0.0, 0.0, 1.0]); 6];
let texs = vec![TexCoord([0.0, 0.0]); 6];
let mesh_data: MeshData = MeshBuilder::new()
let mesh: Handle<Mesh> = loader.load_from_data(mesh_data, (), mesh_storage);
1 Like
(Azriel Hoh) #4

whoa, must’ve been a difficult bug to figure out :bug: (guess it’s doing Primitive::TriangleList behaviour).

There is one more solution for lines’ anti-aliasing – you can create a texture in memory, and use that with UiImage::NineSlice.

1 Like
(Abovegame) #5

You could look into how DebugDrawingPass is implemented and just implement your own but make it so it doesn’t take a uniform (constant) line width but settable for each line you draw.