options.gd 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. extends Control
  2. @export var sun: DirectionalLight3D
  3. @export var lightbulb_1: OmniLight3D
  4. @export var lightbulb_2: OmniLight3D
  5. @export var world_environment: WorldEnvironment
  6. ## Returns color from a given temperature in kelvins (6500K is nearly white).
  7. ## Valid range is [1000; 15000].
  8. ## As explained in the Filament documentation:
  9. ## https://google.github.io/filament/Filament.md.html#lighting/directlighting/lightsparameterization
  10. ##
  11. ## This is the same function as used internally by the engine when setting a
  12. ## Light3D's `light_temperature`, but converted to GDScript.
  13. func get_color_from_temperature(p_temperature: float) -> Color:
  14. var t2 := p_temperature * p_temperature
  15. var u := (
  16. (0.860117757 + 1.54118254e-4 * p_temperature + 1.28641212e-7 * t2) /
  17. (1.0 + 8.42420235e-4 * p_temperature + 7.08145163e-7 * t2)
  18. )
  19. var v := (
  20. (0.317398726 + 4.22806245e-5 * p_temperature + 4.20481691e-8 * t2) /
  21. (1.0 - 2.89741816e-5 * p_temperature + 1.61456053e-7 * t2)
  22. )
  23. # Convert to xyY space.
  24. var d := 1.0 / (2.0 * u - 8.0 * v + 4.0)
  25. var x := 3.0 * u * d
  26. var y := 2.0 * v * d
  27. # Convert to XYZ space.
  28. var a := 1.0 / maxf(y, 1e-5)
  29. var xyz := Vector3(x * a, 1.0, (1.0 - x - y) * a)
  30. # Convert from XYZ to sRGB(linear).
  31. var linear := Vector3(
  32. 3.2404542 * xyz.x - 1.5371385 * xyz.y - 0.4985314 * xyz.z,
  33. -0.9692660 * xyz.x + 1.8760108 * xyz.y + 0.0415560 * xyz.z,
  34. 0.0556434 * xyz.x - 0.2040259 * xyz.y + 1.0572252 * xyz.z
  35. )
  36. linear /= maxf(1e-5, linear[linear.max_axis_index()])
  37. # Normalize, clamp, and convert to sRGB.
  38. return Color(linear.x, linear.y, linear.z).clamp().linear_to_srgb()
  39. func _on_time_of_day_value_changed(value: float) -> void:
  40. var offset := TAU * 0.25
  41. sun.rotation.x = remap(value, 0, 1440, 0 + offset, TAU + offset)
  42. # Improve and prevent light leaks by hiding the sun if it's below the horizon.
  43. const EPSILON = 0.0001
  44. sun.visible = sun.rotation.x > TAU * 0.5 + EPSILON and sun.rotation.x < TAU - EPSILON
  45. $Light/TimeOfDay/Value.text = "%02d:%02d" % [value / 60, fmod(value, 60)]
  46. func _on_sun_intensity_value_changed(value: float) -> void:
  47. sun.light_intensity_lux = value
  48. $Light/SunIntensity/Value.text = "%d lux" % value
  49. func _on_lightbulb1_intensity_value_changed(value: float) -> void:
  50. lightbulb_1.light_intensity_lumens = value
  51. $Light/Lightbulb1Intensity/Value.text = "%d lm" % value
  52. func _on_lightbulb1_temperature_value_changed(value: float) -> void:
  53. lightbulb_1.light_temperature = value
  54. $Light/Lightbulb1Temperature/Value.text = "%d K" % value
  55. $Light/Lightbulb1Temperature/Value.add_theme_color_override("font_color", get_color_from_temperature(value))
  56. func _on_lightbulb2_intensity_value_changed(value: float) -> void:
  57. lightbulb_2.light_intensity_lumens = value
  58. $Light/Lightbulb2Intensity/Value.text = "%d lm" % value
  59. func _on_lightbulb2_temperature_value_changed(value: float) -> void:
  60. lightbulb_2.light_temperature = value
  61. $Light/Lightbulb2Temperature/Value.text = "%d K" % value
  62. $Light/Lightbulb2Temperature/Value.add_theme_color_override("font_color", get_color_from_temperature(value))
  63. func _on_focus_distance_value_changed(value: float) -> void:
  64. get_viewport().get_camera_3d().attributes.frustum_focus_distance = value
  65. $Camera/FocusDistance/Value.text = "%.1f m" % value
  66. func _on_focal_length_value_changed(value: float) -> void:
  67. get_viewport().get_camera_3d().attributes.frustum_focal_length = value
  68. $Camera/FocalLength/Value.text = "%d mm" % value
  69. func _on_aperture_value_changed(value: float) -> void:
  70. get_viewport().get_camera_3d().attributes.exposure_aperture = value
  71. $Camera/Aperture/Value.text = "%.1f f-stop" % value
  72. func _on_shutter_speed_value_changed(value: float) -> void:
  73. get_viewport().get_camera_3d().attributes.exposure_shutter_speed = value
  74. $Camera/ShutterSpeed/Value.text = "1/%d" % value
  75. func _on_sensitivity_value_changed(value: float) -> void:
  76. get_viewport().get_camera_3d().attributes.exposure_sensitivity = value
  77. $Camera/Sensitivity/Value.text = "%d ISO" % value
  78. func _on_autoexposure_speed_value_changed(value: float) -> void:
  79. get_viewport().get_camera_3d().attributes.auto_exposure_speed = value
  80. $Camera/AutoexposureSpeed/Value.text = "%.1f" % value
  81. func _on_sdfgi_button_toggled(button_pressed: bool) -> void:
  82. world_environment.environment.sdfgi_enabled = button_pressed