Drawing thick 3D lines

xyzxyz Posts: 41Member

I can't seem to find a way to draw thick three-dimensional lines. Line width parameter in the spatial shader says it's disabled. I also cannot use custom shaders because there is no access to vertex data in screen space. Is there a way to draw 3D lines in Godot? My project depends on this ability.


  • CalinouCalinou Posts: 919Admin Godot Developer
    edited September 13

    Godot currently has no built-in way to draw 3D lines in projects, so you have to resort to procedural geometry generation or use primitive meshes instead. You can use add-ons such as https://github.com/RedefineGamedev/Line3D (more full-featured) or https://github.com/jegor377/Line3D (simpler).

    Alternatively, you can create a MeshInstance with a BoxShape or CylinderShape and stretch it to draw a thick straight line.

    Line width parameter in the spatial shader says it's disabled.

    The Line Width property only works for line-based geometry, not triangle-based geometry. Even then, relying on the Line Width property is ill-advised as its behavior is driver-specific (OpenGL line drawing is used). For instance, AMD GPUs don't support any line widths greater than 1.

  • xyzxyz Posts: 41Member
    edited September 14

    Initial idea required drawing splines. I then simplified the whole concept to use circular arcs (still in 3d space). As per visual concept, I need to stroke the same line/curve with multiple superimposed strokes of different widths. Most elegant way to handle this would be by screen space shaders.

    In the end I settled for a single stroke. Generating it via immediate geometry node as a 3d tube (i.e. thin partial torus positioned in 3d space). It's not exactly what I wanted but it'll have to do.

    Thanks for the insights!

  • cyberealitycybereality Posts: 1,733Moderator

    You can render wireframe with barycentric coordinates. Not sure if that is what you need, but it may give you some ideas. This is a Unity tutorial, but all the math is there if you want to port to Godot.

  • xyzxyz Posts: 41Member

    Thanks @cybereality. Not sure if it's really applicable to standalone lines but it's still good to know. In fact I'm using similar approach in current project to render meridians and parallels on a globe.

  • cyberealitycybereality Posts: 1,733Moderator

    No, it wouldn't work for standalone lines, it's for rendering geometry in wireframe.

  • xyzxyz Posts: 41Member
    edited September 14

    I think Godot currently can't do this in an elegant way. Adding a shader option to skip projection transforms would be enough to allow for making custom line stroking shaders. Similar to already exsisting skip_vertex_transform. But I'm guessing this may break something down in the spatial material pipeline.

  • cyberealitycybereality Posts: 1,733Moderator

    Are you sure about that? You can do whatever you want in the vertex shader, including not doing anything (and just leaving the points in local space). Also, with that wireframe shader I posted, you can draw a standalone line by rendering a triangle and not processing the 2nd two edges. It's a bit of a round-about, but it works fine.

  • xyzxyz Posts: 41Member
    edited September 15

    Local space is not enough. I need to be able to control them in screen space. Documentation explicitly states that when you disable vertex transforms, projection transformation happens anyway "later". There's even an example that shows "manual" vertex-matrix multiplication and it does only modelview multiplication. Even though you get the projection matrix as a built-in uniform, there's no use for it in this regard. Projection part is seemingly automatic and hidden from the user. I suspect there's a good reason for it.

    Anyway, drawing straight lines is not my main concern. I need 3d curves; smooth, thick and with controllable, possibly animated width. Best way to do it is to expand triangle strips in screen space. I even have operational glsl shaders from my previous gl projects. It would be trivial to adapt them if automatic projection multiplication could be disabled and instead be done manually.

    That all being said I still love Godot's light abstraction of glsl.

  • cyberealitycybereality Posts: 1,733Moderator

    Interesting. I don't think I tried to draw in screen space so maybe that is why I never encountered that problem. I understand what you are saying.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file