spaceship/tests/test_star_coverage.gd

66 lines
2.6 KiB
GDScript3
Raw Normal View History

extends SceneTree
# Headless gameplay test: run with
# godot --headless -s res://tests/test_star_coverage.gd
# Verifies the shader-based starfield's uniforms actually track the camera:
# after moving/zooming the camera, camera_pos/camera_zoom fed to the
# starfield shader should match, at every zoom level (this replaces the old
# tile-symmetry check now that stars are a single procedural fragment
# shader pass with no CPU-side tiling to go asymmetric in the first place).
const TOLERANCE: float = 0.5
var main_scene: Node
var camera: Camera2D
var starfield: ColorRect
func _initialize() -> void:
var packed: PackedScene = load("res://Main.tscn")
main_scene = packed.instantiate()
root.add_child(main_scene)
camera = main_scene.get_node("Camera2D")
starfield = main_scene.get_node("BackgroundLayer/Starfield")
func _process(_delta: float) -> bool:
var all_ok: bool = true
for zoom_level in [1.0, 3.0, 6.0]:
camera.zoom = Vector2(zoom_level, zoom_level)
camera.global_position = Vector2(zoom_level * 100.0, -zoom_level * 50.0)
# Let the Starfield's own _process (which reads the camera and pushes
# shader uniforms) run for this frame before checking.
starfield._process(0.016)
# The camera position is handed to the shader pre-split into a cell
# index plus a fraction within that cell (so the shader never handles
# a large absolute world coordinate - see Starfield.gd). Verify the
# two parts reconstruct the real camera position exactly, and that
# the fraction really is a fraction: if it ever left [0,1) the split
# would be wrong and the shader would sample the wrong cell.
var cell: Vector2 = starfield.material.get_shader_parameter("camera_cell")
var frac: Vector2 = starfield.material.get_shader_parameter("camera_frac")
var cell_size: float = starfield.material.get_shader_parameter("cell_size")
var uniform_zoom: float = starfield.material.get_shader_parameter("camera_zoom")
var reconstructed: Vector2 = (cell + frac) * cell_size
var pos_ok: bool = reconstructed.distance_to(camera.global_position) <= TOLERANCE
var frac_ok: bool = frac.x >= 0.0 and frac.x < 1.0 and frac.y >= 0.0 and frac.y < 1.0
var zoom_ok: bool = abs(uniform_zoom - (1.0 / camera.zoom.x)) <= 0.01
var ok: bool = pos_ok and frac_ok and zoom_ok
all_ok = all_ok and ok
print("zoom=%.1f camera_pos=%s reconstructed=%s frac=%s zoom=%.2f/%.2f %s" % [
zoom_level, camera.global_position, reconstructed, frac,
camera.zoom.x, uniform_zoom, "OK" if ok else "FAIL"
])
if all_ok:
print("TEST_STAR_COVERAGE: PASS")
quit(0)
else:
print("TEST_STAR_COVERAGE: FAIL")
quit(1)
return true