waypoint.gd 3.9 KB

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