camera.gd 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. extends Camera3D
  2. const MOUSE_SENSITIVITY = 0.002
  3. const MOVE_SPEED = 0.6
  4. var volumetric_fog_volume_size = ProjectSettings.get_setting("rendering/environment/volumetric_fog/volume_size")
  5. var volumetric_fog_volume_depth = ProjectSettings.get_setting("rendering/environment/volumetric_fog/volume_depth")
  6. var rot = Vector3()
  7. var velocity = Vector3()
  8. @onready var label = $Label
  9. func _ready():
  10. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  11. update_label()
  12. func _process(delta):
  13. var motion = Vector3(
  14. Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
  15. 0,
  16. Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
  17. )
  18. # Normalize motion to prevent diagonal movement from being
  19. # `sqrt(2)` times faster than straight movement.
  20. motion = motion.normalized()
  21. velocity += MOVE_SPEED * delta * (transform.basis * motion)
  22. velocity *= 0.85
  23. position += velocity
  24. func _input(event):
  25. # Mouse look (only if the mouse is captured).
  26. if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  27. # Horizontal mouse look.
  28. rot.y -= event.relative.x * MOUSE_SENSITIVITY
  29. # Vertical mouse look.
  30. rot.x = clamp(rot.x - event.relative.y * MOUSE_SENSITIVITY, -1.57, 1.57)
  31. transform.basis = Basis.from_euler(rot)
  32. if event.is_action_pressed("toggle_mouse_capture"):
  33. if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  34. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  35. else:
  36. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  37. if event.is_action_pressed("toggle_temporal_reprojection"):
  38. get_world_3d().environment.volumetric_fog_temporal_reprojection_enabled = not get_world_3d().environment.volumetric_fog_temporal_reprojection_enabled
  39. update_label()
  40. elif event.is_action_pressed("increase_temporal_reprojection"):
  41. get_world_3d().environment.volumetric_fog_temporal_reprojection_amount = clamp(get_world_3d().environment.volumetric_fog_temporal_reprojection_amount + 0.01, 0.5, 0.99)
  42. update_label()
  43. elif event.is_action_pressed("decrease_temporal_reprojection"):
  44. get_world_3d().environment.volumetric_fog_temporal_reprojection_amount = clamp(get_world_3d().environment.volumetric_fog_temporal_reprojection_amount - 0.01, 0.5, 0.99)
  45. update_label()
  46. elif event.is_action_pressed("increase_fog_density"):
  47. get_world_3d().environment.volumetric_fog_density = clamp(get_world_3d().environment.volumetric_fog_density + 0.01, 0.0, 1.0)
  48. update_label()
  49. elif event.is_action_pressed("decrease_fog_density"):
  50. get_world_3d().environment.volumetric_fog_density = clamp(get_world_3d().environment.volumetric_fog_density - 0.01, 0.0, 1.0)
  51. update_label()
  52. elif event.is_action_pressed("increase_volumetric_fog_quality"):
  53. volumetric_fog_volume_size = clamp(volumetric_fog_volume_size + 16, 16, 384)
  54. volumetric_fog_volume_depth = clamp(volumetric_fog_volume_depth + 16, 16, 384)
  55. RenderingServer.environment_set_volumetric_fog_volume_size(volumetric_fog_volume_size, volumetric_fog_volume_depth)
  56. update_label()
  57. elif event.is_action_pressed("decrease_volumetric_fog_quality"):
  58. volumetric_fog_volume_size = clamp(volumetric_fog_volume_size - 16, 16, 384)
  59. volumetric_fog_volume_depth = clamp(volumetric_fog_volume_depth - 16, 16, 384)
  60. RenderingServer.environment_set_volumetric_fog_volume_size(volumetric_fog_volume_size, volumetric_fog_volume_depth)
  61. update_label()
  62. func update_label():
  63. if get_world_3d().environment.volumetric_fog_temporal_reprojection_enabled:
  64. label.text = "Fog density: %.2f\nTemporal reprojection: Enabled\nTemporal reprojection strength: %.2f\nVolumetric fog quality: %d×%d×%d" % [
  65. get_world_3d().environment.volumetric_fog_density,
  66. get_world_3d().environment.volumetric_fog_temporal_reprojection_amount,
  67. volumetric_fog_volume_size,
  68. volumetric_fog_volume_size,
  69. volumetric_fog_volume_depth,
  70. ]
  71. else:
  72. label.text = "Fog density: %.2f\nTemporal reprojection: Disabled\nVolumetric fog quality: %d×%d×%d" % [
  73. get_world_3d().environment.volumetric_fog_density,
  74. volumetric_fog_volume_size,
  75. volumetric_fog_volume_size,
  76. volumetric_fog_volume_depth,
  77. ]