2022-02-05 13:15:04 +00:00
|
|
|
extends Control
|
|
|
|
|
|
|
|
# Some margin to keep the marker away from the screen's corners.
|
|
|
|
const MARGIN = 8
|
|
|
|
|
|
|
|
@onready var camera:Camera3D = get_viewport().get_camera_3d()
|
|
|
|
@onready var parent:Node3D = get_parent()
|
|
|
|
@onready var label:Label = $Label
|
|
|
|
@onready var marker:TextureRect = $Marker
|
|
|
|
#
|
|
|
|
## The waypoint's text.
|
|
|
|
@export var text:String:
|
|
|
|
get:
|
|
|
|
return $Label.text
|
|
|
|
set(value):
|
|
|
|
print("Setting some_exported_property to %s" % value)
|
|
|
|
$Label.text = value
|
|
|
|
|
|
|
|
|
|
|
|
# If `true`, the waypoint sticks to the viewport's edges when moving off-screen.
|
|
|
|
@export var sticky = true
|
|
|
|
|
|
|
|
|
|
|
|
func set_text(p_text:String):
|
|
|
|
text = p_text
|
|
|
|
|
|
|
|
# The label's text can only be set once the node is ready.
|
|
|
|
if is_inside_tree():
|
|
|
|
label.text = p_text
|
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
if not parent is Node3D:
|
|
|
|
push_error("The waypoint's parent node must inherit from Node3D.")
|
|
|
|
|
|
|
|
|
|
|
|
func _process(_delta):
|
2022-02-15 20:41:14 +00:00
|
|
|
if !weakref(camera).get_ref() or not camera.is_current():
|
2022-02-05 13:15:04 +00:00
|
|
|
# If the camera we have isn't the current one, get the current camera.
|
|
|
|
camera = get_viewport().get_camera_3d()
|
|
|
|
|
|
|
|
var parent_translation = parent.get_global_transform().origin
|
|
|
|
var camera_transform = camera.get_global_transform()
|
|
|
|
var camera_translation = camera_transform.origin
|
|
|
|
|
|
|
|
# We would use "camera.is_position_behind(parent_translation)", except
|
|
|
|
# that it also accounts for the near clip plane, which we don't want.
|
|
|
|
var is_behind = camera_transform.basis.z.dot(parent_translation - camera_translation) > 0
|
|
|
|
|
|
|
|
# Fade the waypoint when the camera gets close.
|
|
|
|
var distance = camera_translation.distance_to(parent_translation)
|
2022-10-31 18:43:59 +00:00
|
|
|
modulate.a = clamp(remap(distance, 0, 2, 0, 1), 0, 1 )
|
2022-02-05 13:15:04 +00:00
|
|
|
|
|
|
|
var unprojected_position = camera.unproject_position(parent_translation)
|
|
|
|
|
|
|
|
var viewport_base_size = get_viewport().size
|
|
|
|
|
|
|
|
if not sticky:
|
|
|
|
# For non-sticky waypoints, we don't need to clamp and calculate
|
|
|
|
# the position if the waypoint goes off screen.
|
2022-03-25 19:52:26 +00:00
|
|
|
position = unprojected_position
|
2022-02-05 13:15:04 +00:00
|
|
|
visible = not is_behind
|
|
|
|
return
|
|
|
|
|
|
|
|
# We need to handle the axes differently.
|
|
|
|
# For the screen's X axis, the projected position is useful to us,
|
|
|
|
# but we need to force it to the side if it's also behind.
|
|
|
|
if is_behind:
|
|
|
|
if unprojected_position.x < viewport_base_size.x / 2:
|
|
|
|
unprojected_position.x = viewport_base_size.x - MARGIN
|
|
|
|
else:
|
|
|
|
unprojected_position.x = MARGIN
|
|
|
|
|
|
|
|
# For the screen's Y axis, the projected position is NOT useful to us
|
|
|
|
# because we don't want to indicate to the user that they need to look
|
|
|
|
# up or down to see something behind them. Instead, here we approximate
|
|
|
|
# the correct position using difference of the X axis Euler angles
|
|
|
|
# (up/down rotation) and the ratio of that with the camera's FOV.
|
|
|
|
# This will be slightly off from the theoretical "ideal" position.
|
|
|
|
if is_behind or unprojected_position.x < MARGIN or \
|
|
|
|
unprojected_position.x > viewport_base_size.x - MARGIN:
|
|
|
|
var look = camera_transform.looking_at(parent_translation, Vector3.UP)
|
|
|
|
var diff = angle_diff(look.basis.get_euler().x, camera_transform.basis.get_euler().x)
|
2022-10-31 18:43:59 +00:00
|
|
|
unprojected_position.y = viewport_base_size.y * (0.5 + (diff / deg_to_rad(camera.fov)))
|
2022-02-05 13:15:04 +00:00
|
|
|
|
2022-03-25 19:52:26 +00:00
|
|
|
position = Vector2(
|
2022-02-05 13:15:04 +00:00
|
|
|
clamp(unprojected_position.x, MARGIN, viewport_base_size.x - MARGIN),
|
|
|
|
clamp(unprojected_position.y, MARGIN, viewport_base_size.y - MARGIN)
|
|
|
|
)
|
|
|
|
|
|
|
|
label.visible = true
|
2022-03-25 19:52:26 +00:00
|
|
|
rotation = 0
|
2022-02-05 13:15:04 +00:00
|
|
|
# Used to display a diagonal arrow when the waypoint is displayed in
|
|
|
|
# one of the screen corners.
|
|
|
|
var overflow = 0
|
|
|
|
|
2022-03-25 19:52:26 +00:00
|
|
|
if position.x <= MARGIN:
|
2022-02-05 13:15:04 +00:00
|
|
|
# Left overflow.
|
|
|
|
overflow = -45
|
|
|
|
label.visible = false
|
2022-03-25 19:52:26 +00:00
|
|
|
rotation = 90
|
|
|
|
elif position.x >= viewport_base_size.x - MARGIN:
|
2022-02-05 13:15:04 +00:00
|
|
|
# Right overflow.
|
|
|
|
overflow = 45
|
|
|
|
label.visible = false
|
2022-03-25 19:52:26 +00:00
|
|
|
rotation = 270
|
2022-02-05 13:15:04 +00:00
|
|
|
|
2022-03-25 19:52:26 +00:00
|
|
|
if position.y <= MARGIN:
|
2022-02-05 13:15:04 +00:00
|
|
|
# Top overflow.
|
|
|
|
label.visible = false
|
2022-03-25 19:52:26 +00:00
|
|
|
rotation = 180 + overflow
|
|
|
|
elif position.y >= viewport_base_size.y - MARGIN:
|
2022-02-05 13:15:04 +00:00
|
|
|
# Bottom overflow.
|
|
|
|
label.visible = false
|
2022-03-25 19:52:26 +00:00
|
|
|
rotation = -overflow
|
2022-02-05 13:15:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
static func angle_diff(from, to):
|
|
|
|
var diff = fmod(to - from, TAU)
|
|
|
|
return fmod(2.0 * diff, TAU) - diff
|