camera_attributes.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**************************************************************************/
  2. /* camera_attributes.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "camera_attributes.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/rendering_server.h"
  33. void CameraAttributes::set_exposure_multiplier(float p_multiplier) {
  34. exposure_multiplier = p_multiplier;
  35. _update_exposure();
  36. emit_changed();
  37. }
  38. float CameraAttributes::get_exposure_multiplier() const {
  39. return exposure_multiplier;
  40. }
  41. void CameraAttributes::set_exposure_sensitivity(float p_sensitivity) {
  42. exposure_sensitivity = p_sensitivity;
  43. _update_exposure();
  44. emit_changed();
  45. }
  46. float CameraAttributes::get_exposure_sensitivity() const {
  47. return exposure_sensitivity;
  48. }
  49. void CameraAttributes::_update_exposure() {
  50. float exposure_normalization = 1.0;
  51. // Ignore physical properties if not using physical light units.
  52. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  53. exposure_normalization = calculate_exposure_normalization();
  54. }
  55. RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, exposure_multiplier, exposure_normalization);
  56. }
  57. void CameraAttributes::set_auto_exposure_enabled(bool p_enabled) {
  58. auto_exposure_enabled = p_enabled;
  59. _update_auto_exposure();
  60. notify_property_list_changed();
  61. }
  62. bool CameraAttributes::is_auto_exposure_enabled() const {
  63. return auto_exposure_enabled;
  64. }
  65. void CameraAttributes::set_auto_exposure_speed(float p_auto_exposure_speed) {
  66. auto_exposure_speed = p_auto_exposure_speed;
  67. _update_auto_exposure();
  68. }
  69. float CameraAttributes::get_auto_exposure_speed() const {
  70. return auto_exposure_speed;
  71. }
  72. void CameraAttributes::set_auto_exposure_scale(float p_auto_exposure_scale) {
  73. auto_exposure_scale = p_auto_exposure_scale;
  74. _update_auto_exposure();
  75. }
  76. float CameraAttributes::get_auto_exposure_scale() const {
  77. return auto_exposure_scale;
  78. }
  79. RID CameraAttributes::get_rid() const {
  80. return camera_attributes;
  81. }
  82. void CameraAttributes::_validate_property(PropertyInfo &p_property) const {
  83. if (!GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units") && p_property.name == "exposure_sensitivity") {
  84. p_property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  85. return;
  86. }
  87. if (p_property.name.begins_with("auto_exposure_") && p_property.name != "auto_exposure_enabled" && !auto_exposure_enabled) {
  88. p_property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  89. return;
  90. }
  91. }
  92. void CameraAttributes::_bind_methods() {
  93. ClassDB::bind_method(D_METHOD("set_exposure_multiplier", "multiplier"), &CameraAttributes::set_exposure_multiplier);
  94. ClassDB::bind_method(D_METHOD("get_exposure_multiplier"), &CameraAttributes::get_exposure_multiplier);
  95. ClassDB::bind_method(D_METHOD("set_exposure_sensitivity", "sensitivity"), &CameraAttributes::set_exposure_sensitivity);
  96. ClassDB::bind_method(D_METHOD("get_exposure_sensitivity"), &CameraAttributes::get_exposure_sensitivity);
  97. ClassDB::bind_method(D_METHOD("set_auto_exposure_enabled", "enabled"), &CameraAttributes::set_auto_exposure_enabled);
  98. ClassDB::bind_method(D_METHOD("is_auto_exposure_enabled"), &CameraAttributes::is_auto_exposure_enabled);
  99. ClassDB::bind_method(D_METHOD("set_auto_exposure_speed", "exposure_speed"), &CameraAttributes::set_auto_exposure_speed);
  100. ClassDB::bind_method(D_METHOD("get_auto_exposure_speed"), &CameraAttributes::get_auto_exposure_speed);
  101. ClassDB::bind_method(D_METHOD("set_auto_exposure_scale", "exposure_grey"), &CameraAttributes::set_auto_exposure_scale);
  102. ClassDB::bind_method(D_METHOD("get_auto_exposure_scale"), &CameraAttributes::get_auto_exposure_scale);
  103. ADD_GROUP("Exposure", "exposure_");
  104. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure_sensitivity", PROPERTY_HINT_RANGE, "0.1,32000.0,0.1,suffix:ISO"), "set_exposure_sensitivity", "get_exposure_sensitivity");
  105. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure_multiplier", PROPERTY_HINT_RANGE, "0.0,8.0,0.001,or_greater"), "set_exposure_multiplier", "get_exposure_multiplier");
  106. ADD_GROUP("Auto Exposure", "auto_exposure_");
  107. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_exposure_enabled"), "set_auto_exposure_enabled", "is_auto_exposure_enabled");
  108. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_scale", PROPERTY_HINT_RANGE, "0.01,64,0.01"), "set_auto_exposure_scale", "get_auto_exposure_scale");
  109. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_speed", PROPERTY_HINT_RANGE, "0.01,64,0.01"), "set_auto_exposure_speed", "get_auto_exposure_speed");
  110. }
  111. CameraAttributes::CameraAttributes() {
  112. camera_attributes = RS::get_singleton()->camera_attributes_create();
  113. }
  114. CameraAttributes::~CameraAttributes() {
  115. ERR_FAIL_NULL(RenderingServer::get_singleton());
  116. RS::get_singleton()->free(camera_attributes);
  117. }
  118. //////////////////////////////////////////////////////
  119. /* CameraAttributesPractical */
  120. void CameraAttributesPractical::set_dof_blur_far_enabled(bool p_enabled) {
  121. dof_blur_far_enabled = p_enabled;
  122. _update_dof_blur();
  123. notify_property_list_changed();
  124. }
  125. bool CameraAttributesPractical::is_dof_blur_far_enabled() const {
  126. return dof_blur_far_enabled;
  127. }
  128. void CameraAttributesPractical::set_dof_blur_far_distance(float p_distance) {
  129. dof_blur_far_distance = p_distance;
  130. _update_dof_blur();
  131. }
  132. float CameraAttributesPractical::get_dof_blur_far_distance() const {
  133. return dof_blur_far_distance;
  134. }
  135. void CameraAttributesPractical::set_dof_blur_far_transition(float p_distance) {
  136. dof_blur_far_transition = p_distance;
  137. _update_dof_blur();
  138. }
  139. float CameraAttributesPractical::get_dof_blur_far_transition() const {
  140. return dof_blur_far_transition;
  141. }
  142. void CameraAttributesPractical::set_dof_blur_near_enabled(bool p_enabled) {
  143. dof_blur_near_enabled = p_enabled;
  144. _update_dof_blur();
  145. notify_property_list_changed();
  146. }
  147. bool CameraAttributesPractical::is_dof_blur_near_enabled() const {
  148. return dof_blur_near_enabled;
  149. }
  150. void CameraAttributesPractical::set_dof_blur_near_distance(float p_distance) {
  151. dof_blur_near_distance = p_distance;
  152. _update_dof_blur();
  153. }
  154. float CameraAttributesPractical::get_dof_blur_near_distance() const {
  155. return dof_blur_near_distance;
  156. }
  157. void CameraAttributesPractical::set_dof_blur_near_transition(float p_distance) {
  158. dof_blur_near_transition = p_distance;
  159. _update_dof_blur();
  160. }
  161. float CameraAttributesPractical::get_dof_blur_near_transition() const {
  162. return dof_blur_near_transition;
  163. }
  164. void CameraAttributesPractical::set_dof_blur_amount(float p_amount) {
  165. dof_blur_amount = p_amount;
  166. _update_dof_blur();
  167. }
  168. float CameraAttributesPractical::get_dof_blur_amount() const {
  169. return dof_blur_amount;
  170. }
  171. void CameraAttributesPractical::_update_dof_blur() {
  172. RS::get_singleton()->camera_attributes_set_dof_blur(
  173. get_rid(),
  174. dof_blur_far_enabled,
  175. dof_blur_far_distance,
  176. dof_blur_far_transition,
  177. dof_blur_near_enabled,
  178. dof_blur_near_distance,
  179. dof_blur_near_transition,
  180. dof_blur_amount);
  181. }
  182. float CameraAttributesPractical::calculate_exposure_normalization() const {
  183. return exposure_sensitivity / 3072007.0; // Matches exposure normalization for default CameraAttributesPhysical at ISO 100.
  184. }
  185. void CameraAttributesPractical::set_auto_exposure_min_sensitivity(float p_min) {
  186. auto_exposure_min = p_min;
  187. _update_auto_exposure();
  188. }
  189. float CameraAttributesPractical::get_auto_exposure_min_sensitivity() const {
  190. return auto_exposure_min;
  191. }
  192. void CameraAttributesPractical::set_auto_exposure_max_sensitivity(float p_max) {
  193. auto_exposure_max = p_max;
  194. _update_auto_exposure();
  195. }
  196. float CameraAttributesPractical::get_auto_exposure_max_sensitivity() const {
  197. return auto_exposure_max;
  198. }
  199. void CameraAttributesPractical::_update_auto_exposure() {
  200. RS::get_singleton()->camera_attributes_set_auto_exposure(
  201. get_rid(),
  202. auto_exposure_enabled,
  203. auto_exposure_min * ((12.5 / 100.0) / exposure_sensitivity), // Convert from Sensitivity to Luminance
  204. auto_exposure_max * ((12.5 / 100.0) / exposure_sensitivity), // Convert from Sensitivity to Luminance
  205. auto_exposure_speed,
  206. auto_exposure_scale);
  207. emit_changed();
  208. }
  209. void CameraAttributesPractical::_validate_property(PropertyInfo &p_property) const {
  210. if ((!dof_blur_far_enabled && (p_property.name == "dof_blur_far_distance" || p_property.name == "dof_blur_far_transition")) ||
  211. (!dof_blur_near_enabled && (p_property.name == "dof_blur_near_distance" || p_property.name == "dof_blur_near_transition"))) {
  212. p_property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  213. }
  214. }
  215. void CameraAttributesPractical::_bind_methods() {
  216. // DOF blur
  217. ClassDB::bind_method(D_METHOD("set_dof_blur_far_enabled", "enabled"), &CameraAttributesPractical::set_dof_blur_far_enabled);
  218. ClassDB::bind_method(D_METHOD("is_dof_blur_far_enabled"), &CameraAttributesPractical::is_dof_blur_far_enabled);
  219. ClassDB::bind_method(D_METHOD("set_dof_blur_far_distance", "distance"), &CameraAttributesPractical::set_dof_blur_far_distance);
  220. ClassDB::bind_method(D_METHOD("get_dof_blur_far_distance"), &CameraAttributesPractical::get_dof_blur_far_distance);
  221. ClassDB::bind_method(D_METHOD("set_dof_blur_far_transition", "distance"), &CameraAttributesPractical::set_dof_blur_far_transition);
  222. ClassDB::bind_method(D_METHOD("get_dof_blur_far_transition"), &CameraAttributesPractical::get_dof_blur_far_transition);
  223. ClassDB::bind_method(D_METHOD("set_dof_blur_near_enabled", "enabled"), &CameraAttributesPractical::set_dof_blur_near_enabled);
  224. ClassDB::bind_method(D_METHOD("is_dof_blur_near_enabled"), &CameraAttributesPractical::is_dof_blur_near_enabled);
  225. ClassDB::bind_method(D_METHOD("set_dof_blur_near_distance", "distance"), &CameraAttributesPractical::set_dof_blur_near_distance);
  226. ClassDB::bind_method(D_METHOD("get_dof_blur_near_distance"), &CameraAttributesPractical::get_dof_blur_near_distance);
  227. ClassDB::bind_method(D_METHOD("set_dof_blur_near_transition", "distance"), &CameraAttributesPractical::set_dof_blur_near_transition);
  228. ClassDB::bind_method(D_METHOD("get_dof_blur_near_transition"), &CameraAttributesPractical::get_dof_blur_near_transition);
  229. ClassDB::bind_method(D_METHOD("set_dof_blur_amount", "amount"), &CameraAttributesPractical::set_dof_blur_amount);
  230. ClassDB::bind_method(D_METHOD("get_dof_blur_amount"), &CameraAttributesPractical::get_dof_blur_amount);
  231. ClassDB::bind_method(D_METHOD("set_auto_exposure_max_sensitivity", "max_sensitivity"), &CameraAttributesPractical::set_auto_exposure_max_sensitivity);
  232. ClassDB::bind_method(D_METHOD("get_auto_exposure_max_sensitivity"), &CameraAttributesPractical::get_auto_exposure_max_sensitivity);
  233. ClassDB::bind_method(D_METHOD("set_auto_exposure_min_sensitivity", "min_sensitivity"), &CameraAttributesPractical::set_auto_exposure_min_sensitivity);
  234. ClassDB::bind_method(D_METHOD("get_auto_exposure_min_sensitivity"), &CameraAttributesPractical::get_auto_exposure_min_sensitivity);
  235. ADD_GROUP("DOF Blur", "dof_blur_");
  236. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dof_blur_far_enabled"), "set_dof_blur_far_enabled", "is_dof_blur_far_enabled");
  237. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_far_distance", PROPERTY_HINT_RANGE, "0.01,8192,0.01,exp,suffix:m"), "set_dof_blur_far_distance", "get_dof_blur_far_distance");
  238. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_far_transition", PROPERTY_HINT_RANGE, "-1,8192,0.01"), "set_dof_blur_far_transition", "get_dof_blur_far_transition");
  239. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dof_blur_near_enabled"), "set_dof_blur_near_enabled", "is_dof_blur_near_enabled");
  240. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_near_distance", PROPERTY_HINT_RANGE, "0.01,8192,0.01,exp,suffix:m"), "set_dof_blur_near_distance", "get_dof_blur_near_distance");
  241. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_near_transition", PROPERTY_HINT_RANGE, "-1,8192,0.01"), "set_dof_blur_near_transition", "get_dof_blur_near_transition");
  242. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_amount", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_dof_blur_amount", "get_dof_blur_amount");
  243. ADD_GROUP("Auto Exposure", "auto_exposure_");
  244. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_min_sensitivity", PROPERTY_HINT_RANGE, "0,1600,0.01,or_greater,suffic:ISO"), "set_auto_exposure_min_sensitivity", "get_auto_exposure_min_sensitivity");
  245. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_max_sensitivity", PROPERTY_HINT_RANGE, "0,64000,0.1,or_greater,suffic:ISO"), "set_auto_exposure_max_sensitivity", "get_auto_exposure_max_sensitivity");
  246. }
  247. CameraAttributesPractical::CameraAttributesPractical() {
  248. _update_dof_blur();
  249. _update_exposure();
  250. set_auto_exposure_min_sensitivity(0.0);
  251. set_auto_exposure_max_sensitivity(800.0);
  252. notify_property_list_changed();
  253. }
  254. CameraAttributesPractical::~CameraAttributesPractical() {
  255. }
  256. //////////////////////////////////////////////////////
  257. /* CameraAttributesPhysical */
  258. void CameraAttributesPhysical::set_aperture(float p_aperture) {
  259. exposure_aperture = p_aperture;
  260. _update_exposure();
  261. _update_frustum();
  262. }
  263. float CameraAttributesPhysical::get_aperture() const {
  264. return exposure_aperture;
  265. }
  266. void CameraAttributesPhysical::set_shutter_speed(float p_shutter_speed) {
  267. exposure_shutter_speed = p_shutter_speed;
  268. _update_exposure();
  269. }
  270. float CameraAttributesPhysical::get_shutter_speed() const {
  271. return exposure_shutter_speed;
  272. }
  273. void CameraAttributesPhysical::set_focal_length(float p_focal_length) {
  274. frustum_focal_length = p_focal_length;
  275. _update_frustum();
  276. emit_changed();
  277. }
  278. float CameraAttributesPhysical::get_focal_length() const {
  279. return frustum_focal_length;
  280. }
  281. void CameraAttributesPhysical::set_focus_distance(float p_focus_distance) {
  282. frustum_focus_distance = p_focus_distance;
  283. _update_frustum();
  284. }
  285. float CameraAttributesPhysical::get_focus_distance() const {
  286. return frustum_focus_distance;
  287. }
  288. void CameraAttributesPhysical::set_near(real_t p_near) {
  289. frustum_near = p_near;
  290. _update_frustum();
  291. emit_changed();
  292. }
  293. real_t CameraAttributesPhysical::get_near() const {
  294. return frustum_near;
  295. }
  296. void CameraAttributesPhysical::set_far(real_t p_far) {
  297. frustum_far = p_far;
  298. _update_frustum();
  299. emit_changed();
  300. }
  301. real_t CameraAttributesPhysical::get_far() const {
  302. return frustum_far;
  303. }
  304. real_t CameraAttributesPhysical::get_fov() const {
  305. return frustum_fov;
  306. }
  307. void CameraAttributesPhysical::_update_frustum() {
  308. //https://en.wikipedia.org/wiki/Circle_of_confusion#Circle_of_confusion_diameter_limit_based_on_d/1500
  309. Vector2i sensor_size = Vector2i(36, 24); // Matches high-end DSLR, could be made variable if there is demand.
  310. float CoC = sensor_size.length() / 1500.0;
  311. frustum_fov = Math::rad_to_deg(2 * atan(sensor_size.height / (2 * frustum_focal_length)));
  312. // Based on https://en.wikipedia.org/wiki/Depth_of_field.
  313. float u = MAX(frustum_focus_distance * 1000.0, frustum_focal_length + 1.0); // Focus distance expressed in mm and clamped to at least 1 mm away from lens.
  314. float hyperfocal_length = frustum_focal_length + ((frustum_focal_length * frustum_focal_length) / (exposure_aperture * CoC));
  315. // This computes the start and end of the depth of field. Anything between these two points has a Circle of Confusino so small
  316. // that it is not picked up by the camera sensors.
  317. // To be properly physically-based, we would run the DoF shader at all depths. To be efficient, we are only running it where the CoC
  318. // will be visible, this introduces some value shifts in the near field that we have to compensate for below.
  319. float depth_near = ((hyperfocal_length * u) / (hyperfocal_length + (u - frustum_focal_length))) / 1000.0; // In meters.
  320. float depth_far = ((hyperfocal_length * u) / (hyperfocal_length - (u - frustum_focal_length))) / 1000.0; // In meters.
  321. float scale = (frustum_focal_length / (u - frustum_focal_length)) * (frustum_focal_length / exposure_aperture);
  322. bool use_far = (depth_far < frustum_far) && (depth_far > 0.0);
  323. bool use_near = depth_near > frustum_near;
  324. #ifdef DEBUG_ENABLED
  325. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  326. // Force disable DoF in editor builds to suppress warnings.
  327. use_far = false;
  328. use_near = false;
  329. }
  330. #endif
  331. RS::get_singleton()->camera_attributes_set_dof_blur(
  332. get_rid(),
  333. use_far,
  334. u / 1000.0, // Focus distance clampd to focal length expressed in meters.
  335. -1.0, // Negative to tell Bokeh effect to use physically-based scaling.
  336. use_near,
  337. u / 1000.0,
  338. -1.0,
  339. scale / 5.0); // Arbitrary scaling to get close to how much blur there should be.
  340. }
  341. float CameraAttributesPhysical::calculate_exposure_normalization() const {
  342. const float e = (exposure_aperture * exposure_aperture) * exposure_shutter_speed * (100.0 / exposure_sensitivity);
  343. return 1.0 / (e * 1.2);
  344. }
  345. void CameraAttributesPhysical::set_auto_exposure_min_exposure_value(float p_min) {
  346. auto_exposure_min = p_min;
  347. _update_auto_exposure();
  348. }
  349. float CameraAttributesPhysical::get_auto_exposure_min_exposure_value() const {
  350. return auto_exposure_min;
  351. }
  352. void CameraAttributesPhysical::set_auto_exposure_max_exposure_value(float p_max) {
  353. auto_exposure_max = p_max;
  354. _update_auto_exposure();
  355. }
  356. float CameraAttributesPhysical::get_auto_exposure_max_exposure_value() const {
  357. return auto_exposure_max;
  358. }
  359. void CameraAttributesPhysical::_update_auto_exposure() {
  360. RS::get_singleton()->camera_attributes_set_auto_exposure(
  361. get_rid(),
  362. auto_exposure_enabled,
  363. pow(2.0, auto_exposure_min) * (12.5 / exposure_sensitivity), // Convert from EV100 to Luminance
  364. pow(2.0, auto_exposure_max) * (12.5 / exposure_sensitivity), // Convert from EV100 to Luminance
  365. auto_exposure_speed,
  366. auto_exposure_scale);
  367. emit_changed();
  368. }
  369. void CameraAttributesPhysical::_validate_property(PropertyInfo &property) const {
  370. if (!GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units") && (property.name == "exposure_aperture" || property.name == "exposure_shutter_speed")) {
  371. property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  372. return;
  373. }
  374. }
  375. void CameraAttributesPhysical::_bind_methods() {
  376. ClassDB::bind_method(D_METHOD("set_aperture", "aperture"), &CameraAttributesPhysical::set_aperture);
  377. ClassDB::bind_method(D_METHOD("get_aperture"), &CameraAttributesPhysical::get_aperture);
  378. ClassDB::bind_method(D_METHOD("set_shutter_speed", "shutter_speed"), &CameraAttributesPhysical::set_shutter_speed);
  379. ClassDB::bind_method(D_METHOD("get_shutter_speed"), &CameraAttributesPhysical::get_shutter_speed);
  380. ClassDB::bind_method(D_METHOD("set_focal_length", "focal_length"), &CameraAttributesPhysical::set_focal_length);
  381. ClassDB::bind_method(D_METHOD("get_focal_length"), &CameraAttributesPhysical::get_focal_length);
  382. ClassDB::bind_method(D_METHOD("set_focus_distance", "focus_distance"), &CameraAttributesPhysical::set_focus_distance);
  383. ClassDB::bind_method(D_METHOD("get_focus_distance"), &CameraAttributesPhysical::get_focus_distance);
  384. ClassDB::bind_method(D_METHOD("set_near", "near"), &CameraAttributesPhysical::set_near);
  385. ClassDB::bind_method(D_METHOD("get_near"), &CameraAttributesPhysical::get_near);
  386. ClassDB::bind_method(D_METHOD("set_far", "far"), &CameraAttributesPhysical::set_far);
  387. ClassDB::bind_method(D_METHOD("get_far"), &CameraAttributesPhysical::get_far);
  388. ClassDB::bind_method(D_METHOD("get_fov"), &CameraAttributesPhysical::get_fov);
  389. ClassDB::bind_method(D_METHOD("set_auto_exposure_max_exposure_value", "exposure_value_max"), &CameraAttributesPhysical::set_auto_exposure_max_exposure_value);
  390. ClassDB::bind_method(D_METHOD("get_auto_exposure_max_exposure_value"), &CameraAttributesPhysical::get_auto_exposure_max_exposure_value);
  391. ClassDB::bind_method(D_METHOD("set_auto_exposure_min_exposure_value", "exposure_value_min"), &CameraAttributesPhysical::set_auto_exposure_min_exposure_value);
  392. ClassDB::bind_method(D_METHOD("get_auto_exposure_min_exposure_value"), &CameraAttributesPhysical::get_auto_exposure_min_exposure_value);
  393. ADD_GROUP("Frustum", "frustum_");
  394. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frustum_focus_distance", PROPERTY_HINT_RANGE, "0.01,4000.0,0.01,suffix:m"), "set_focus_distance", "get_focus_distance");
  395. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frustum_focal_length", PROPERTY_HINT_RANGE, "1.0,800.0,0.01,exp,suffix:mm"), "set_focal_length", "get_focal_length");
  396. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frustum_near", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater,exp,suffix:m"), "set_near", "get_near");
  397. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frustum_far", PROPERTY_HINT_RANGE, "0.01,4000,0.01,or_greater,exp,suffix:m"), "set_far", "get_far");
  398. ADD_GROUP("Exposure", "exposure_");
  399. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure_aperture", PROPERTY_HINT_RANGE, "0.5,64.0,0.01,exp,suffix:f-stop"), "set_aperture", "get_aperture");
  400. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure_shutter_speed", PROPERTY_HINT_RANGE, "0.1,8000.0,0.001,suffix:1/s"), "set_shutter_speed", "get_shutter_speed");
  401. ADD_GROUP("Auto Exposure", "auto_exposure_");
  402. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_min_exposure_value", PROPERTY_HINT_RANGE, "-16.0,16.0,0.01,or_greater,suffix:EV100"), "set_auto_exposure_min_exposure_value", "get_auto_exposure_min_exposure_value");
  403. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_max_exposure_value", PROPERTY_HINT_RANGE, "-16.0,16.0,0.01,or_greater,suffix:EV100"), "set_auto_exposure_max_exposure_value", "get_auto_exposure_max_exposure_value");
  404. }
  405. CameraAttributesPhysical::CameraAttributesPhysical() {
  406. _update_exposure();
  407. _update_frustum();
  408. set_auto_exposure_min_exposure_value(-8);
  409. set_auto_exposure_max_exposure_value(10); // Use a wide range by default to feel more like a real camera.
  410. notify_property_list_changed();
  411. }
  412. CameraAttributesPhysical::~CameraAttributesPhysical() {
  413. }