anti_aliasing.gd 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. extends Node
  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 = -TAU / 16 # This must be kept in sync with RotationX.
  7. var rot_y = TAU / 8 # This must be kept in sync with CameraHolder.
  8. var camera_distance = 2.0
  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. @onready var fps_label = $FPSLabel
  15. func _ready():
  16. # Disable V-Sync to uncap framerate on supported platforms. This makes performance comparison
  17. # easier on high-end machines that easily reach the monitor's refresh rate.
  18. DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
  19. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  20. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  21. update_gui()
  22. get_viewport().size_changed.connect(_on_viewport_size_changed)
  23. func _unhandled_input(event):
  24. if event.is_action_pressed("ui_left"):
  25. _on_previous_pressed()
  26. if event.is_action_pressed("ui_right"):
  27. _on_next_pressed()
  28. if event is InputEventMouseButton:
  29. if event.button_index == MOUSE_BUTTON_WHEEL_UP:
  30. camera_distance -= ZOOM_SPEED
  31. if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
  32. camera_distance += ZOOM_SPEED
  33. camera_distance = clamp(camera_distance, 1.5, 6)
  34. if event is InputEventMouseMotion and event.button_mask & MAIN_BUTTONS:
  35. # Compensate motion speed to be resolution-independent (based on the window height).
  36. var relative_motion = event.relative * DisplayServer.window_get_size().y / base_height
  37. rot_y -= relative_motion.x * ROT_SPEED
  38. rot_x -= relative_motion.y * ROT_SPEED
  39. rot_x = clamp(rot_x, -1.57, 0)
  40. camera_holder.transform.basis = Basis.from_euler(Vector3(0, rot_y, 0))
  41. rotation_x.transform.basis = Basis.from_euler(Vector3(rot_x, 0, 0))
  42. func _process(delta):
  43. var current_tester = testers.get_child(tester_index)
  44. # This code assumes CameraHolder's X and Y coordinates are already correct.
  45. var current_position = camera_holder.global_transform.origin.z
  46. var target_position = current_tester.global_transform.origin.z
  47. camera_holder.global_transform.origin.z = lerpf(current_position, target_position, 3 * delta)
  48. camera.position.z = lerpf(camera.position.z, camera_distance, 10 * delta)
  49. fps_label.text = "%d FPS (%.2f mspf)" % [Engine.get_frames_per_second(), 1000.0 / Engine.get_frames_per_second()]
  50. # Color FPS counter depending on framerate.
  51. # The Gradient resource is stored as metadata within the FPSLabel node (accessible in the inspector).
  52. fps_label.modulate = fps_label.get_meta("gradient").sample(remap(Engine.get_frames_per_second(), 0, 180, 0.0, 1.0))
  53. func _on_previous_pressed():
  54. tester_index = max(0, tester_index - 1)
  55. update_gui()
  56. func _on_next_pressed():
  57. tester_index = min(tester_index + 1, testers.get_child_count() - 1)
  58. update_gui()
  59. func update_gui():
  60. $TestName.text = str(testers.get_child(tester_index).name).capitalize()
  61. $Previous.disabled = tester_index == 0
  62. $Next.disabled = tester_index == testers.get_child_count() - 1
  63. func _on_msaa_item_selected(index):
  64. # Multi-sample anti-aliasing. High quality, but slow. It also does not smooth out the edges of
  65. # transparent (alpha scissor) textures.
  66. get_viewport().msaa_3d = index
  67. func _on_limit_fps_scale_value_changed(value):
  68. # The rendering FPS affects the appearance of TAA, as higher framerates allow it to converge faster.
  69. # On high refresh rate monitors, TAA ghosting issues may appear less noticeable as a result
  70. # (if the GPU can keep up).
  71. $Antialiasing/LimitFPSContainer/Value.text = str(value)
  72. Engine.max_fps = value
  73. func _on_render_scale_value_changed(value):
  74. get_viewport().scaling_3d_scale = value
  75. $Antialiasing/RenderScaleContainer/Value.text = "%d%%" % (value * 100)
  76. # Update viewport resolution text.
  77. _on_viewport_size_changed()
  78. # FSR 1.0 is only effective if render scale is below 100%, so hide the setting if at native resolution or higher.
  79. $Antialiasing/FidelityFXFSR.visible = value < 1.0
  80. $Antialiasing/FSRSharpness.visible = get_viewport().scaling_3d_mode == Viewport.SCALING_3D_MODE_FSR and value < 1.0
  81. func _on_amd_fidelityfx_fsr1_toggled(button_pressed):
  82. get_viewport().scaling_3d_mode = Viewport.SCALING_3D_MODE_FSR if button_pressed else Viewport.SCALING_3D_MODE_BILINEAR
  83. # FSR 1.0 is only effective if render scale is below 100%, so hide the setting if at native resolution or higher.
  84. $Antialiasing/FSRSharpness.visible = button_pressed
  85. func _on_fsr_sharpness_item_selected(index):
  86. # *Lower* values of FSR sharpness are sharper.
  87. match index:
  88. 0:
  89. get_viewport().fsr_sharpness = 2.0
  90. 1:
  91. get_viewport().fsr_sharpness = 0.8
  92. 2:
  93. get_viewport().fsr_sharpness = 0.4
  94. 3:
  95. get_viewport().fsr_sharpness = 0.2
  96. 4:
  97. get_viewport().fsr_sharpness = 0.0
  98. func _on_viewport_size_changed():
  99. $ViewportResolution.text = "Viewport resolution: %d×%d" % [get_viewport().size.x * get_viewport().scaling_3d_scale, get_viewport().size.y * get_viewport().scaling_3d_scale]
  100. func _on_v_sync_item_selected(index):
  101. # Vsync is enabled by default.
  102. # Vertical synchronization locks framerate and makes screen tearing not visible at the cost of
  103. # higher input latency and stuttering when the framerate target is not met.
  104. # Adaptive V-Sync automatically disables V-Sync when the framerate target is not met, and enables
  105. # V-Sync otherwise. This prevents suttering and reduces input latency when the framerate target
  106. # is not met, at the cost of visible tearing.
  107. if index == 0: # Disabled (default)
  108. DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)
  109. elif index == 1: # Adaptive
  110. DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ADAPTIVE)
  111. elif index == 2: # Enabled
  112. DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED)
  113. func _on_taa_item_selected(index):
  114. # Temporal antialiasing. Smooths out everything including specular aliasing, but can introduce
  115. # ghosting artifacts and blurring in motion. Moderate performance cost.
  116. get_viewport().use_taa = index == 1
  117. func _on_fxaa_item_selected(index):
  118. # Fast approximate anti-aliasing. Much faster than MSAA (and works on alpha scissor edges),
  119. # but blurs the whole scene rendering slightly.
  120. get_viewport().screen_space_aa = int(index == 1) as Viewport.ScreenSpaceAA