tester.gd 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. extends Node3D
  2. const INTERP_SPEED = 2
  3. const ROT_SPEED = 0.003
  4. const ZOOM_SPEED = 0.1
  5. const ZOOM_MAX = 2.5
  6. const MAIN_BUTTONS = MOUSE_BUTTON_MASK_LEFT | MOUSE_BUTTON_MASK_MIDDLE | MOUSE_BUTTON_MASK_RIGHT
  7. var tester_index = 0
  8. var rot_x = -0.5 # This must be kept in sync with RotationX.
  9. var rot_y = -0.5 # This must be kept in sync with CameraHolder.
  10. var zoom = 5
  11. var base_height = ProjectSettings.get_setting("display/window/size/viewport_height")
  12. var backgrounds = [
  13. { path = "res://backgrounds/schelde.hdr", name = "Riverside" },
  14. { path = "res://backgrounds/lobby.hdr", name = "Lobby" },
  15. { path = "res://backgrounds/park.hdr", name = "Park" },
  16. { path = "res://backgrounds/night.hdr", name = "Night" },
  17. { path = "res://backgrounds/experiment.hdr", name = "Experiment" },
  18. ]
  19. @onready var testers: Node3D = $Testers
  20. @onready var material_name: Label = $UI/MaterialName
  21. @onready var camera_holder: Node3D = $CameraHolder # Has a position and rotates on Y.
  22. @onready var rotation_x: Node3D = $CameraHolder/RotationX
  23. @onready var camera: Camera3D = $CameraHolder/RotationX/Camera
  24. func _ready():
  25. for background in backgrounds:
  26. get_node(^"UI/Background").add_item(background.name)
  27. update_gui()
  28. func _unhandled_input(event):
  29. if event.is_action_pressed("ui_left"):
  30. _on_previous_pressed()
  31. if event.is_action_pressed("ui_right"):
  32. _on_next_pressed()
  33. if event is InputEventMouseButton:
  34. if event.button_index == MOUSE_BUTTON_WHEEL_UP:
  35. zoom -= ZOOM_SPEED
  36. if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
  37. zoom += ZOOM_SPEED
  38. zoom = clamp(zoom, 2, 8)
  39. camera.position.z = zoom
  40. if event is InputEventMouseMotion and event.button_mask & MAIN_BUTTONS:
  41. # Compensate motion speed to be resolution-independent (based on the window height).
  42. var relative_motion = event.relative * DisplayServer.window_get_size().y / base_height
  43. rot_y -= relative_motion.x * ROT_SPEED
  44. rot_x -= relative_motion.y * ROT_SPEED
  45. rot_x = clamp(rot_x, -1.4, 0.45)
  46. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  47. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  48. func _process(delta):
  49. var current_tester = testers.get_child(tester_index)
  50. # This code assumes CameraHolder's Y and Z coordinates are already correct.
  51. var target_position = current_tester.transform.origin.x
  52. var current_position = camera_holder.transform.origin.x
  53. camera_holder.transform.origin.x = lerp(current_position, target_position, INTERP_SPEED * delta)
  54. func _on_previous_pressed():
  55. if tester_index > 0:
  56. tester_index -= 1
  57. update_gui()
  58. func _on_next_pressed():
  59. if tester_index < testers.get_child_count() - 1:
  60. tester_index += 1
  61. update_gui()
  62. func update_gui():
  63. var current_tester = testers.get_child(tester_index)
  64. material_name.text = current_tester.get_name()
  65. $UI/Previous.disabled = tester_index == 0
  66. $UI/Next.disabled = tester_index == testers.get_child_count() - 1
  67. func _on_bg_item_selected(index):
  68. var sky_material: PanoramaSkyMaterial = $WorldEnvironment.environment.sky.sky_material
  69. sky_material.panorama = load(backgrounds[index].path)
  70. func _on_quit_pressed():
  71. get_tree().quit()