waypoint.gd 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. extends Control
  2. # Some margin to keep the marker away from the screen's corners.
  3. const MARGIN = 8
  4. @onready var camera = get_viewport().get_camera_3d()
  5. @onready var parent = get_parent()
  6. @onready var label = $Label
  7. @onready var marker = $Marker
  8. # The waypoint's text.
  9. @export var text = "Waypoint":
  10. set(value):
  11. text = value
  12. # The label's text can only be set once the node is ready.
  13. if is_inside_tree():
  14. label.text = value
  15. # If `true`, the waypoint sticks to the viewport's edges when moving off-screen.
  16. @export var sticky = true
  17. func _ready() -> void:
  18. self.text = text
  19. if not parent is Node3D:
  20. push_error("The waypoint's parent node must inherit from Node3D.")
  21. func _process(_delta):
  22. if not camera.current:
  23. # If the camera we have isn't the current one, get the current camera.
  24. camera = get_viewport().get_camera_3d()
  25. var parent_position = parent.global_transform.origin
  26. var camera_transform = camera.global_transform
  27. var camera_position = camera_transform.origin
  28. # We would use "camera.is_position_behind(parent_position)", except
  29. # that it also accounts for the near clip plane, which we don't want.
  30. var is_behind = camera_transform.basis.z.dot(parent_position - camera_position) > 0
  31. # Fade the waypoint when the camera gets close.
  32. var distance = camera_position.distance_to(parent_position)
  33. modulate.a = clamp(remap(distance, 0, 2, 0, 1), 0, 1 )
  34. var unprojected_position = camera.unproject_position(parent_position)
  35. # `get_size_override()` will return a valid size only if the stretch mode is `2d`.
  36. # Otherwise, the viewport size is used directly.
  37. var viewport_base_size = (
  38. get_viewport().content_scale_size if get_viewport().content_scale_size > Vector2i(0, 0)
  39. else get_viewport().size
  40. )
  41. if not sticky:
  42. # For non-sticky waypoints, we don't need to clamp and calculate
  43. # the position if the waypoint goes off screen.
  44. position = unprojected_position
  45. visible = not is_behind
  46. return
  47. # We need to handle the axes differently.
  48. # For the screen's X axis, the projected position is useful to us,
  49. # but we need to force it to the side if it's also behind.
  50. if is_behind:
  51. if unprojected_position.x < viewport_base_size.x / 2:
  52. unprojected_position.x = viewport_base_size.x - MARGIN
  53. else:
  54. unprojected_position.x = MARGIN
  55. # For the screen's Y axis, the projected position is NOT useful to us
  56. # because we don't want to indicate to the user that they need to look
  57. # up or down to see something behind them. Instead, here we approximate
  58. # the correct position using difference of the X axis Euler angles
  59. # (up/down rotation) and the ratio of that with the camera's FOV.
  60. # This will be slightly off from the theoretical "ideal" position.
  61. if is_behind or unprojected_position.x < MARGIN or \
  62. unprojected_position.x > viewport_base_size.x - MARGIN:
  63. var look = camera_transform.looking_at(parent_position, Vector3.UP)
  64. var diff = angle_diff(look.basis.get_euler().x, camera_transform.basis.get_euler().x)
  65. unprojected_position.y = viewport_base_size.y * (0.5 + (diff / deg_to_rad(camera.fov)))
  66. position = Vector2(
  67. clamp(unprojected_position.x, MARGIN, viewport_base_size.x - MARGIN),
  68. clamp(unprojected_position.y, MARGIN, viewport_base_size.y - MARGIN)
  69. )
  70. label.visible = true
  71. rotation = 0
  72. # Used to display a diagonal arrow when the waypoint is displayed in
  73. # one of the screen corners.
  74. var overflow = 0
  75. if position.x <= MARGIN:
  76. # Left overflow.
  77. overflow = -TAU / 8.0
  78. label.visible = false
  79. rotation = TAU / 4.0
  80. elif position.x >= viewport_base_size.x - MARGIN:
  81. # Right overflow.
  82. overflow = TAU / 8.0
  83. label.visible = false
  84. rotation = TAU * 3.0 / 4.0
  85. if position.y <= MARGIN:
  86. # Top overflow.
  87. label.visible = false
  88. rotation = TAU / 2.0 + overflow
  89. elif position.y >= viewport_base_size.y - MARGIN:
  90. # Bottom overflow.
  91. label.visible = false
  92. rotation = -overflow
  93. static func angle_diff(from, to):
  94. var diff = fmod(to - from, TAU)
  95. return fmod(2.0 * diff, TAU) - diff