tester.gd 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. extends WorldEnvironment
  2. const ROT_SPEED = 0.003
  3. const ZOOM_SPEED = 0.125
  4. const MAIN_BUTTONS = MOUSE_BUTTON_MASK_LEFT | MOUSE_BUTTON_MASK_RIGHT | MOUSE_BUTTON_MASK_MIDDLE
  5. var tester_index = 0
  6. var rot_x = deg_to_rad(-22.5) # This must be kept in sync with RotationX.
  7. var rot_y = deg_to_rad(90) # This must be kept in sync with CameraHolder.
  8. var zoom = 1.5
  9. var base_height = ProjectSettings.get_setting("display/window/size/viewport_height")
  10. @onready var testers = $Testers
  11. @onready var camera_holder = $CameraHolder # Has a position and rotates on Y.
  12. @onready var rotation_x = $CameraHolder/RotationX
  13. @onready var camera = $CameraHolder/RotationX/Camera3D
  14. func _ready():
  15. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  16. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  17. update_gui()
  18. func _unhandled_input(event):
  19. if event.is_action_pressed("ui_left"):
  20. _on_previous_pressed()
  21. if event.is_action_pressed("ui_right"):
  22. _on_next_pressed()
  23. if event.is_action_pressed("place_decal"):
  24. var origin = camera.global_position
  25. var target = camera.project_position(get_viewport().get_mouse_position(), 100)
  26. var query = PhysicsRayQueryParameters3D.create(origin, target)
  27. var result = camera.get_world_3d().direct_space_state.intersect_ray(query)
  28. if not result.is_empty():
  29. var decal = preload("res://decal.tscn").instantiate()
  30. add_child(decal)
  31. decal.get_node("Decal").modulate = Color(1.0,0.0,0)
  32. decal.position = result["position"]
  33. decal.transform.basis = camera.global_transform.basis
  34. if event is InputEventMouseButton:
  35. if event.button_index == MOUSE_BUTTON_WHEEL_UP:
  36. zoom -= ZOOM_SPEED
  37. if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
  38. zoom += ZOOM_SPEED
  39. zoom = clampf(zoom, 1.5, 4)
  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 = clampf(rot_x, deg_to_rad(-90), 0)
  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 X and Y coordinates are already correct.
  51. var current_position = camera_holder.global_transform.origin.z
  52. var target_position = current_tester.global_transform.origin.z
  53. camera_holder.global_transform.origin.z = lerpf(current_position, target_position, 3 * delta)
  54. camera.position.z = lerpf(camera.position.z, zoom, 10 * delta)
  55. func _on_previous_pressed():
  56. tester_index = max(0, tester_index - 1)
  57. update_gui()
  58. func _on_next_pressed():
  59. tester_index = min(tester_index + 1, testers.get_child_count() - 1)
  60. update_gui()
  61. func update_gui():
  62. $TestName.text = str(testers.get_child(tester_index).name).capitalize()
  63. $Previous.disabled = tester_index == 0
  64. $Next.disabled = tester_index == testers.get_child_count() - 1
  65. func _on_decal_filter_mode_item_selected(index):
  66. # Indices in the OptionButton match RenderingServer decal filtering constants.
  67. RenderingServer.decals_set_filter(index)