extends SceneTree
## Renders the real arena (scene.tscn) with the billboard crowd wired in, from a camera
## near the arena centre, to confirm the stands populate and face inward in-game.
##   godot --script tools/preview_crowd_scene.gd
## Output: tools/preview/scene_crowd.png

const OUT := "res://tools/preview"


func _init() -> void:
	_run.call_deferred()


func _run() -> void:
	DirAccess.make_dir_recursive_absolute(OUT)
	var ps := load("res://scene.tscn") as PackedScene
	var scene := ps.instantiate()
	root.add_child(scene)
	current_scene = scene

	await create_timer(0.4).timeout

	var cam := Camera3D.new()
	cam.fov = 74.0
	scene.add_child(cam)
	# Out past one stand looking across the arena: the near crowd has its back to us,
	# the far crowd faces us — the case the front/back sheet must get right.
	cam.look_at_from_position(Vector3(0.0, 9.0, -22.0), Vector3(0.0, 4.0, 6.0), Vector3.UP)
	cam.current = true

	await create_timer(0.3).timeout
	var crowd := scene.get_node_or_null(^"CrowdBillboards") as MultiMeshInstance3D
	if crowd != null and crowd.multimesh != null:
		print("crowd instances = ", crowd.multimesh.instance_count)
	else:
		print("crowd node/multimesh missing")

	var img := root.get_viewport().get_texture().get_image()
	if img != null:
		img.save_png(OUT + "/scene_crowd.png")
		print("captured scene_crowd.png  ", img.get_width(), "x", img.get_height())
	quit(0)
