extends Node

func _ready():
	var viewportMaterial = StandardMaterial3D.new()
	viewportMaterial.albedo_texture = $SubViewport.get_texture()
	viewportMaterial.albedo_texture.resource_local_to_scene = true
	viewportMaterial.resource_local_to_scene = true
	viewportMaterial.flags_transparent = true
	viewportMaterial.resource_local_to_scene = true
	viewportMaterial.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
	$Pivot/MeshInstance3D.set_surface_override_material(0, viewportMaterial)

func _process(_delta):
	if Engine.is_editor_hint():
		return
	var v = get_parent().global_transform.basis * Vector3(0, 0, 1)
	var angle = atan2(v.x, v.z)
	$Pivot/MeshInstance3D.basis = Basis().rotated(Vector3(0, 1, 0), angle)
	$Pivot/MeshInstance3D.position = get_parent().global_position
