class_name CanvasProjector extends Node3D
## 3D node which projects its children into 2D screen space.


func _ready() -> void:
	visibility_changed.connect(_on_visibility_changed)
	_on_visibility_changed()


func _process(_delta: float) -> void:
	var camera := get_viewport().get_camera_3d()
	if not camera:
		return
	var px_coord := camera.unproject_position(global_position)
	for c: Node in get_children():
		if c is Control:
			(c as Control).global_position = px_coord
		elif c is Node2D:
			(c as Node2D).global_position = px_coord


func _on_visibility_changed() -> void:
	for c: Node in get_children():
		if c is CanvasItem:
			(c as CanvasItem).visible = is_visible_in_tree()
