WIP: Bezier curve gizmo connecting the portals

Broken for some reason. Only works nicely when the source portal is NOT
rotated in any way
This commit is contained in:
Vojtěch Struhár 2025-02-20 22:57:51 +01:00
parent eeb35efa18
commit 808e1503fb

View File

@ -1,8 +1,10 @@
extends EditorNode3DGizmoPlugin
func _init():
print("[gizmo] _init")
create_material("main", Color(1,0,1), false, true, false)
create_material("secondary", Color(1,0,0))
func _get_gizmo_name() -> String:
@ -16,27 +18,57 @@ func _has_gizmo(for_node_3d: Node3D) -> bool:
func _redraw(gizmo):
var node3d = gizmo.get_node_3d() as Portal
print("[gizmo] _redraw: " + node3d.name)
var portal = gizmo.get_node_3d() as Portal
print("[gizmo] _redraw: " + portal.name)
gizmo.clear() # Always clear the gizmo
if node3d not in EditorInterface.get_selection().get_selected_nodes():
if portal not in EditorInterface.get_selection().get_selected_nodes():
return # If not selected, don't draw anything
var lines = PackedVector3Array()
lines.push_back(Vector3(0, 0, 0))
if node3d.exit_portal != null:
lines.push_back(node3d.to_local(node3d.exit_portal.global_position))
else:
lines.push_back(Vector3(5, 5, 5))
if portal.exit_portal != null:
print("[gizmo] Drawing the bezier")
# Draw a bezier curve connecting the two portals
var exit = portal.exit_portal
var D = portal.global_position.distance_to(exit.global_position)
var p0 = Vector3.ZERO
var p3 = portal.to_local(exit.global_position)
var p1 = p0 + -portal.transform.basis.z * D * 0.25 # Control point
var p2 = p3 + -exit.transform.basis.z * D * 0.25 # Control point
var RESOLUTION: int = 24
lines.push_back(p0)
for i in range(1, RESOLUTION + 1):
var t: float = float(i) / RESOLUTION
var spline_pos: Vector3 = pow(1 - t, 3) * p0 \
+ 3 * pow(1 - t, 2) * t * p1 \
+ 3 * (1 - t) * pow(t, 2) * p2 \
+ pow(t, 3) * p3
lines.push_back(spline_pos)
lines.push_back(p3)
print("[gizmo] Bezier lines: " + str(lines.size()))
gizmo.add_lines(PackedVector3Array([p0, p3, p1, p2]), get_material("secondary", gizmo))
#var handles = PackedVector3Array()
#handles.push_back(Vector3(0, 1, 0))
#handles.push_back(Vector3(0, 2, 0))
gizmo.add_lines(lines, get_material("main", gizmo), false)
gizmo.add_lines(lines, get_material("main", gizmo))
# Thicker dashed line
var l2 = lines.duplicate()
for i in range(l2.size()): l2[i].x += 0.01
gizmo.add_lines(l2, get_material("main", gizmo))
#gizmo.add_handles(handles, get_material("handles", gizmo), [])