+1 vote

Hello dear community!

I've been told many times that the godot editor was implemented purely in godot. I have recently begun working with VR a bit and tried to implement my own teleportation with a technique i found that is called tall ray casting. I have been able to sucessfully implement the feature however i am missing a proper visualization for where I am teleporting.

Having tried multiple different approaches that all looked quite performance-heavy, i started wondering how the 2D visualization of a Path3D in the editor actually works as it looks promissingly performance-light. Can someone explain to me how i could render a simple line like this in 3D?

Edit: I should probably note that i would require a bezier line, not just a simple "raycast".

in Engine by (35 points)

2 Answers

+1 vote
Best answer

You could try using the ImmediateGeometry node, which allows you to draw lines from script in real time: https://docs.godotengine.org/en/stable/classes/class_immediategeometry.html

by (27,902 points)
selected by

Does it support bezier behaviour somehow? Looking into the docs I have not found anything like that. I really wonder how the godot editor implements the visualization.

It just draws lines, really.
Bezier is not something you will find in the drawing methods themselves, it's rather the data you are using to draw such lines.

The Path3D node can look curvy because it uses a Curve3D resource, which can provide bezier positions by interpolating its points.
So if you want to draw bezier curves, you need to use a Curve3D resource, add points to it, define its control points, and use interpolate_baked(), or tessellate() to obtain points along the curve: https://docs.godotengine.org/en/stable/classes/class_curve3d.html

Now to draw it, all you need is to draw a line between each of the points you obtained.
Here is how Godot does it: https://github.com/godotengine/godot/blob/80582ffa662bb4dea223003856320ede37365603/editor/plugins/path_editor_plugin.cpp#L228
It uses a Gizmo API which generates a mesh behind the scenes, but you can also do it with ImmediateGeometry, SurfaceTool or ArrayMesh.add_surface_from_arrays.

Just figured that out myself yesterday. Thank you, functioning just as needed :)

0 votes

I've been trying to do a similar thing. Here's the ImmediateGeometry code I came up with. The hardest part was setting a color. I'm running this code in _process() because my path changes each frame.

var drawn_line: ImmediateGeometry
var path: Path = your_path

var curve = path.curve

if drawn_line == null:
    drawn_line = ImmediateGeometry.new()
    get_tree().root.add_child(drawn_line)
    var m = SpatialMaterial.new()
    m.flags_use_point_size = true
    m.params_point_size = 5
    m.vertex_color_use_as_albedo = true
    m.albedo_color = Color(0, 0, 1)
    drawn_line.set_material_override(m)
    curve.bake_interval = 0.0001


var curve_length = curve.get_point_count()

drawn_line.clear()
drawn_line.begin(Mesh.PRIMITIVE_POINTS)


for index in range(0, curve_length):
    drawn_line.add_vertex(curve.get_point_position(index))

drawn_line.end()
by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.