tester.gd 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.0
  11. var base_height := int(ProjectSettings.get_setting("display/window/size/viewport_height"))
  12. var backgrounds: Array[Dictionary] = [
  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() -> void:
  25. for background in backgrounds:
  26. get_node(^"UI/Background").add_item(background.name)
  27. update_gui()
  28. func _unhandled_input(event: InputEvent) -> void:
  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: Vector2 = event.relative * DisplayServer.window_get_size().y / base_height
  43. rot_y -= relative_motion.x * ROT_SPEED
  44. rot_y = clamp(rot_y, -1.95, 1.95)
  45. rot_x -= relative_motion.y * ROT_SPEED
  46. rot_x = clamp(rot_x, -1.4, 0.45)
  47. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  48. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  49. func _process(delta: float) -> void:
  50. var current_tester: Node3D = testers.get_child(tester_index)
  51. # This code assumes CameraHolder's Y and Z coordinates are already correct.
  52. var target_position := current_tester.transform.origin.x
  53. var current_position := camera_holder.transform.origin.x
  54. camera_holder.transform.origin.x = lerp(current_position, target_position, INTERP_SPEED * delta)
  55. func _on_previous_pressed() -> void:
  56. if tester_index > 0:
  57. tester_index -= 1
  58. update_gui()
  59. func _on_next_pressed() -> void:
  60. if tester_index < testers.get_child_count() - 1:
  61. tester_index += 1
  62. update_gui()
  63. func update_gui() -> void:
  64. var current_tester := testers.get_child(tester_index)
  65. material_name.text = current_tester.get_name()
  66. $UI/Previous.disabled = tester_index == 0
  67. $UI/Next.disabled = tester_index == testers.get_child_count() - 1
  68. func _on_bg_item_selected(index: int) -> void:
  69. var sky_material: PanoramaSkyMaterial = $WorldEnvironment.environment.sky.sky_material
  70. sky_material.panorama = load(backgrounds[index].path)
  71. # Force reflection probes to update by moving them slightly.
  72. for reflection_probe: ReflectionProbe in get_tree().get_nodes_in_group(&"reflection_probe"):
  73. reflection_probe.position.y += randf_range(-0.0001, 0.0001)
  74. func _on_reflection_probes_item_selected(index: int) -> void:
  75. match index:
  76. 0: # No Reflection Probes
  77. for reflection_probe: ReflectionProbe in get_tree().get_nodes_in_group(&"reflection_probe"):
  78. reflection_probe.visible = false
  79. 1: # Reflection Probes (Reflection only)
  80. for reflection_probe: ReflectionProbe in get_tree().get_nodes_in_group(&"reflection_probe"):
  81. reflection_probe.visible = true
  82. reflection_probe.ambient_mode = ReflectionProbe.AMBIENT_DISABLED
  83. 2: # Reflection Probes (Reflection + Ambient)
  84. for reflection_probe: ReflectionProbe in get_tree().get_nodes_in_group(&"reflection_probe"):
  85. reflection_probe.visible = true
  86. reflection_probe.ambient_mode = ReflectionProbe.AMBIENT_ENVIRONMENT
  87. func _on_quit_pressed() -> void:
  88. get_tree().quit()