How to draw a line ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rpggeek

I want to draw a basic line using fragment() function,but i don’t know how to.

Perspective grid animated - Godot Shaders
enter image description here

rakkarage | 2022-02-19 20:45

:bust_in_silhouette: Reply From: Zylann

Are you sure you want to use shaders to do this?

You can draw lines without a shader by simply using the Line2D node, or by drawing it in code inside the _draw function:

extends Node2D

func _draw():
    draw_line(Vector2(10, 10), Vector2(50, 30), Color(1, 1, 0))

If you know all that but want to do it in a fragment shader, this is a whole different story… you’d probably need to use the distance field of such line (a formula giving the distance from it) and then output pixels of the color of your choice when that distance is lower than some amount, which sounds quite involved for what you want to achieve.

Yes i am sure i want to use shaders because i’m trying to learn shader programming.
Found a bunch of examples about shader programming on shadertoy,but seems godot use a bit different shader code style and documentation about it is very poor i think.

rpggeek | 2020-03-30 18:40

See Migrating to Godot’s shading language for shader code differences.

Xrayez | 2020-03-30 19:49