camera_attributes.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_CACHED(bool, "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 (!Engine::get_singleton()->is_editor_hint()) {
  84. return;
  85. }
  86. if (!GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/use_physical_light_units") && p_property.name == "exposure_sensitivity") {
  87. p_property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  88. return;
  89. }
  90. if (p_property.name.begins_with("auto_exposure_") && p_property.name != "auto_exposure_enabled" && !auto_exposure_enabled) {
  91. p_property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  92. return;
  93. }
  94. }
  95. void CameraAttributes::_bind_methods() {
  96. ClassDB::bind_method(D_METHOD("set_exposure_multiplier", "multiplier"), &CameraAttributes::set_exposure_multiplier);
  97. ClassDB::bind_method(D_METHOD("get_exposure_multiplier"), &CameraAttributes::get_exposure_multiplier);
  98. ClassDB::bind_method(D_METHOD("set_exposure_sensitivity", "sensitivity"), &CameraAttributes::set_exposure_sensitivity);
  99. ClassDB::bind_method(D_METHOD("get_exposure_sensitivity"), &CameraAttributes::get_exposure_sensitivity);
  100. ClassDB::bind_method(D_METHOD("set_auto_exposure_enabled", "enabled"), &CameraAttributes::set_auto_exposure_enabled);
  101. ClassDB::bind_method(D_METHOD("is_auto_exposure_enabled"), &CameraAttributes::is_auto_exposure_enabled);
  102. ClassDB::bind_method(D_METHOD("set_auto_exposure_speed", "exposure_speed"), &CameraAttributes::set_auto_exposure_speed);
  103. ClassDB::bind_method(D_METHOD("get_auto_exposure_speed"), &CameraAttributes::get_auto_exposure_speed);
  104. ClassDB::bind_method(D_METHOD("set_auto_exposure_scale", "exposure_grey"), &CameraAttributes::set_auto_exposure_scale);
  105. ClassDB::bind_method(D_METHOD("get_auto_exposure_scale"), &CameraAttributes::get_auto_exposure_scale);
  106. ADD_GROUP("Exposure", "exposure_");
  107. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure_sensitivity", PROPERTY_HINT_RANGE, "0.1,32000.0,0.1,suffix:ISO"), "set_exposure_sensitivity", "get_exposure_sensitivity");
  108. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure_multiplier", PROPERTY_HINT_RANGE, "0.0,8.0,0.001,or_greater"), "set_exposure_multiplier", "get_exposure_multiplier");
  109. ADD_GROUP("Auto Exposure", "auto_exposure_");
  110. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_exposure_enabled"), "set_auto_exposure_enabled", "is_auto_exposure_enabled");
  111. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_scale", PROPERTY_HINT_RANGE, "0.01,64,0.01"), "set_auto_exposure_scale", "get_auto_exposure_scale");
  112. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "auto_exposure_speed", PROPERTY_HINT_RANGE, "0.01,64,0.01"), "set_auto_exposure_speed", "get_auto_exposure_speed");
  113. }
  114. CameraAttributes::CameraAttributes() {
  115. camera_attributes = RS::get_singleton()->camera_attributes_create();
  116. }
  117. CameraAttributes::~CameraAttributes() {
  118. ERR_FAIL_NULL(RenderingServer::get_singleton());
  119. RS::get_singleton()->free(camera_attributes);
  120. }
  121. //////////////////////////////////////////////////////
  122. /* CameraAttributesPractical */
  123. void CameraAttributesPractical::set_dof_blur_far_enabled(bool p_enabled) {
  124. dof_blur_far_enabled = p_enabled;
  125. _update_dof_blur();
  126. notify_property_list_changed();
  127. }
  128. bool CameraAttributesPractical::is_dof_blur_far_enabled() const {
  129. return dof_blur_far_enabled;
  130. }
  131. void CameraAttributesPractical::set_dof_blur_far_distance(float p_distance) {
  132. dof_blur_far_distance = p_distance;
  133. _update_dof_blur();
  134. }
  135. float CameraAttributesPractical::get_dof_blur_far_distance() const {
  136. return dof_blur_far_distance;
  137. }
  138. void CameraAttributesPractical::set_dof_blur_far_transition(float p_distance) {
  139. dof_blur_far_transition = p_distance;
  140. _update_dof_blur();
  141. }
  142. float CameraAttributesPractical::get_dof_blur_far_transition() const {
  143. return dof_blur_far_transition;
  144. }
  145. void CameraAttributesPractical::set_dof_blur_near_enabled(bool p_enabled) {
  146. dof_blur_near_enabled = p_enabled;
  147. _update_dof_blur();
  148. notify_property_list_changed();
  149. }
  150. bool CameraAttributesPractical::is_dof_blur_near_enabled() const {
  151. return dof_blur_near_enabled;
  152. }
  153. void CameraAttributesPractical::set_dof_blur_near_distance(float p_distance) {
  154. dof_blur_near_distance = p_distance;
  155. _update_dof_blur();
  156. }
  157. float CameraAttributesPractical::get_dof_blur_near_distance() const {
  158. return dof_blur_near_distance;
  159. }
  160. void CameraAttributesPractical::set_dof_blur_near_transition(float p_distance) {
  161. dof_blur_near_transition = p_distance;
  162. _update_dof_blur();
  163. }
  164. float CameraAttributesPractical::get_dof_blur_near_transition() const {
  165. return dof_blur_near_transition;
  166. }
  167. void CameraAttributesPractical::set_dof_blur_amount(float p_amount) {
  168. dof_blur_amount = p_amount;
  169. _update_dof_blur();
  170. }
  171. float CameraAttributesPractical::get_dof_blur_amount() const {
  172. return dof_blur_amount;
  173. }
  174. void CameraAttributesPractical::_update_dof_blur() {
  175. RS::get_singleton()->camera_attributes_set_dof_blur(
  176. get_rid(),
  177. dof_blur_far_enabled,
  178. dof_blur_far_distance,
  179. dof_blur_far_transition,
  180. dof_blur_near_enabled,
  181. dof_blur_near_distance,
  182. dof_blur_near_transition,
  183. dof_blur_amount);
  184. }
  185. float CameraAttributesPractical::calculate_exposure_normalization() const {
  186. return exposure_sensitivity / 3072007.0; // Matches exposure normalization for default CameraAttributesPhysical at ISO 100.
  187. }
  188. void CameraAttributesPractical::set_auto_exposure_min_sensitivity(float p_min) {
  189. auto_exposure_min = p_min;
  190. _update_auto_exposure();
  191. }
  192. float CameraAttributesPractical::get_auto_exposure_min_sensitivity() const {
  193. return auto_exposure_min;
  194. }
  195. void CameraAttributesPractical::set_auto_exposure_max_sensitivity(float p_max) {
  196. auto_exposure_max = p_max;
  197. _update_auto_exposure();
  198. }
  199. float CameraAttributesPractical::get_auto_exposure_max_sensitivity() const {
  200. return auto_exposure_max;
  201. }
  202. void CameraAttributesPractical::_update_auto_exposure() {
  203. RS::get_singleton()->camera_attributes_set_auto_exposure(
  204. get_rid(),
  205. auto_exposure_enabled,
  206. auto_exposure_min * ((12.5 / 100.0) / exposure_sensitivity), // Convert from Sensitivity to Luminance
  207. auto_exposure_max * ((12.5 / 100.0) / exposure_sensitivity), // Convert from Sensitivity to Luminance
  208. auto_exposure_speed,
  209. auto_exposure_scale);
  210. emit_changed();
  211. }
  212. void CameraAttributesPractical::_validate_property(PropertyInfo &p_property) const {
  213. if (!Engine::get_singleton()->is_editor_hint()) {
  214. return;
  215. }
  216. if ((!dof_blur_far_enabled && (p_property.name == "dof_blur_far_distance" || p_property.name == "dof_blur_far_transition")) ||
  217. (!dof_blur_near_enabled && (p_property.name == "dof_blur_near_distance" || p_property.name == "dof_blur_near_transition"))) {
  218. p_property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  219. }
  220. }
  221. void CameraAttributesPractical::_bind_methods() {
  222. // DOF blur
  223. ClassDB::bind_method(D_METHOD("set_dof_blur_far_enabled", "enabled"), &CameraAttributesPractical::set_dof_blur_far_enabled);
  224. ClassDB::bind_method(D_METHOD("is_dof_blur_far_enabled"), &CameraAttributesPractical::is_dof_blur_far_enabled);
  225. ClassDB::bind_method(D_METHOD("set_dof_blur_far_distance", "distance"), &CameraAttributesPractical::set_dof_blur_far_distance);
  226. ClassDB::bind_method(D_METHOD("get_dof_blur_far_distance"), &CameraAttributesPractical::get_dof_blur_far_distance);
  227. ClassDB::bind_method(D_METHOD("set_dof_blur_far_transition", "distance"), &CameraAttributesPractical::set_dof_blur_far_transition);
  228. ClassDB::bind_method(D_METHOD("get_dof_blur_far_transition"), &CameraAttributesPractical::get_dof_blur_far_transition);
  229. ClassDB::bind_method(D_METHOD("set_dof_blur_near_enabled", "enabled"), &CameraAttributesPractical::set_dof_blur_near_enabled);
  230. ClassDB::bind_method(D_METHOD("is_dof_blur_near_enabled"), &CameraAttributesPractical::is_dof_blur_near_enabled);
  231. ClassDB::bind_method(D_METHOD("set_dof_blur_near_distance", "distance"), &CameraAttributesPractical::set_dof_blur_near_distance);
  232. ClassDB::bind_method(D_METHOD("get_dof_blur_near_distance"), &CameraAttributesPractical::get_dof_blur_near_distance);
  233. ClassDB::bind_method(D_METHOD("set_dof_blur_near_transition", "distance"), &CameraAttributesPractical::set_dof_blur_near_transition);
  234. ClassDB::bind_method(D_METHOD("get_dof_blur_near_transition"), &CameraAttributesPractical::get_dof_blur_near_transition);
  235. ClassDB::bind_method(D_METHOD("set_dof_blur_amount", "amount"), &CameraAttributesPractical::set_dof_blur_amount);
  236. ClassDB::bind_method(D_METHOD("get_dof_blur_amount"), &CameraAttributesPractical::get_dof_blur_amount);
  237. ClassDB::bind_method(D_METHOD("set_auto_exposure_max_sensitivity", "max_sensitivity"), &CameraAttributesPractical::set_auto_exposure_max_sensitivity);
  238. ClassDB::bind_method(D_METHOD("get_auto_exposure_max_sensitivity"), &CameraAttributesPractical::get_auto_exposure_max_sensitivity);
  239. ClassDB::bind_method(D_METHOD("set_auto_exposure_min_sensitivity", "min_sensitivity"), &CameraAttributesPractical::set_auto_exposure_min_sensitivity);
  240. ClassDB::bind_method(D_METHOD("get_auto_exposure_min_sensitivity"), &CameraAttributesPractical::get_auto_exposure_min_sensitivity);
  241. ADD_GROUP("DOF Blur", "dof_blur_");
  242. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dof_blur_far_enabled"), "set_dof_blur_far_enabled", "is_dof_blur_far_enabled");
  243. 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");
  244. 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");
  245. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "dof_blur_near_enabled"), "set_dof_blur_near_enabled", "is_dof_blur_near_enabled");
  246. 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");
  247. 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");
  248. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dof_blur_amount", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_dof_blur_amount", "get_dof_blur_amount");
  249. ADD_GROUP("Auto Exposure", "auto_exposure_");
  250. 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");
  251. 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");
  252. }
  253. CameraAttributesPractical::CameraAttributesPractical() {
  254. _update_dof_blur();
  255. _update_exposure();
  256. set_auto_exposure_min_sensitivity(0.0);
  257. set_auto_exposure_max_sensitivity(800.0);
  258. notify_property_list_changed();
  259. }
  260. CameraAttributesPractical::~CameraAttributesPractical() {
  261. }
  262. //////////////////////////////////////////////////////
  263. /* CameraAttributesPhysical */
  264. void CameraAttributesPhysical::set_aperture(float p_aperture) {
  265. exposure_aperture = p_aperture;
  266. _update_exposure();
  267. _update_frustum();
  268. }
  269. float CameraAttributesPhysical::get_aperture() const {
  270. return exposure_aperture;
  271. }
  272. void CameraAttributesPhysical::set_shutter_speed(float p_shutter_speed) {
  273. exposure_shutter_speed = p_shutter_speed;
  274. _update_exposure();
  275. }
  276. float CameraAttributesPhysical::get_shutter_speed() const {
  277. return exposure_shutter_speed;
  278. }
  279. void CameraAttributesPhysical::set_focal_length(float p_focal_length) {
  280. frustum_focal_length = p_focal_length;
  281. _update_frustum();
  282. emit_changed();
  283. }
  284. float CameraAttributesPhysical::get_focal_length() const {
  285. return frustum_focal_length;
  286. }
  287. void CameraAttributesPhysical::set_focus_distance(float p_focus_distance) {
  288. frustum_focus_distance = p_focus_distance;
  289. _update_frustum();
  290. }
  291. float CameraAttributesPhysical::get_focus_distance() const {
  292. return frustum_focus_distance;
  293. }
  294. void CameraAttributesPhysical::set_near(real_t p_near) {
  295. frustum_near = p_near;
  296. _update_frustum();
  297. emit_changed();
  298. }
  299. real_t CameraAttributesPhysical::get_near() const {
  300. return frustum_near;
  301. }
  302. void CameraAttributesPhysical::set_far(real_t p_far) {
  303. frustum_far = p_far;
  304. _update_frustum();
  305. emit_changed();
  306. }
  307. real_t CameraAttributesPhysical::get_far() const {
  308. return frustum_far;
  309. }
  310. real_t CameraAttributesPhysical::get_fov() const {
  311. return frustum_fov;
  312. }
  313. void CameraAttributesPhysical::_update_frustum() {
  314. //https://en.wikipedia.org/wiki/Circle_of_confusion#Circle_of_confusion_diameter_limit_based_on_d/1500
  315. Vector2i sensor_size = Vector2i(36, 24); // Matches high-end DSLR, could be made variable if there is demand.
  316. float CoC = sensor_size.length() / 1500.0;
  317. frustum_fov = Math::rad_to_deg(2 * std::atan(sensor_size.height / (2 * frustum_focal_length)));
  318. // Based on https://en.wikipedia.org/wiki/Depth_of_field.
  319. 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.
  320. float hyperfocal_length = frustum_focal_length + ((frustum_focal_length * frustum_focal_length) / (exposure_aperture * CoC));
  321. // This computes the start and end of the depth of field. Anything between these two points has a Circle of Confusino so small
  322. // that it is not picked up by the camera sensors.
  323. // 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
  324. // will be visible, this introduces some value shifts in the near field that we have to compensate for below.
  325. float depth_near = ((hyperfocal_length * u) / (hyperfocal_length + (u - frustum_focal_length))) / 1000.0; // In meters.
  326. float depth_far = ((hyperfocal_length * u) / (hyperfocal_length - (u - frustum_focal_length))) / 1000.0; // In meters.
  327. float scale = (frustum_focal_length / (u - frustum_focal_length)) * (frustum_focal_length / exposure_aperture);
  328. bool use_far = (depth_far < frustum_far) && (depth_far > 0.0);
  329. bool use_near = depth_near > frustum_near;
  330. #ifdef DEBUG_ENABLED
  331. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  332. // Force disable DoF in editor builds to suppress warnings.
  333. use_far = false;
  334. use_near = false;
  335. }
  336. #endif
  337. RS::get_singleton()->camera_attributes_set_dof_blur(
  338. get_rid(),
  339. use_far,
  340. u / 1000.0, // Focus distance clampd to focal length expressed in meters.
  341. -1.0, // Negative to tell Bokeh effect to use physically-based scaling.
  342. use_near,
  343. u / 1000.0,
  344. -1.0,
  345. scale / 5.0); // Arbitrary scaling to get close to how much blur there should be.
  346. }
  347. float CameraAttributesPhysical::calculate_exposure_normalization() const {
  348. const float e = (exposure_aperture * exposure_aperture) * exposure_shutter_speed * (100.0 / exposure_sensitivity);
  349. return 1.0 / (e * 1.2);
  350. }
  351. void CameraAttributesPhysical::set_auto_exposure_min_exposure_value(float p_min) {
  352. auto_exposure_min = p_min;
  353. _update_auto_exposure();
  354. }
  355. float CameraAttributesPhysical::get_auto_exposure_min_exposure_value() const {
  356. return auto_exposure_min;
  357. }
  358. void CameraAttributesPhysical::set_auto_exposure_max_exposure_value(float p_max) {
  359. auto_exposure_max = p_max;
  360. _update_auto_exposure();
  361. }
  362. float CameraAttributesPhysical::get_auto_exposure_max_exposure_value() const {
  363. return auto_exposure_max;
  364. }
  365. void CameraAttributesPhysical::_update_auto_exposure() {
  366. RS::get_singleton()->camera_attributes_set_auto_exposure(
  367. get_rid(),
  368. auto_exposure_enabled,
  369. std::pow(2.0, auto_exposure_min) * (12.5 / exposure_sensitivity), // Convert from EV100 to Luminance
  370. std::pow(2.0, auto_exposure_max) * (12.5 / exposure_sensitivity), // Convert from EV100 to Luminance
  371. auto_exposure_speed,
  372. auto_exposure_scale);
  373. emit_changed();
  374. }
  375. void CameraAttributesPhysical::_validate_property(PropertyInfo &property) const {
  376. if (!Engine::get_singleton()->is_editor_hint()) {
  377. return;
  378. }
  379. if (!GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/use_physical_light_units") && (property.name == "exposure_aperture" || property.name == "exposure_shutter_speed")) {
  380. property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  381. return;
  382. }
  383. }
  384. void CameraAttributesPhysical::_bind_methods() {
  385. ClassDB::bind_method(D_METHOD("set_aperture", "aperture"), &CameraAttributesPhysical::set_aperture);
  386. ClassDB::bind_method(D_METHOD("get_aperture"), &CameraAttributesPhysical::get_aperture);
  387. ClassDB::bind_method(D_METHOD("set_shutter_speed", "shutter_speed"), &CameraAttributesPhysical::set_shutter_speed);
  388. ClassDB::bind_method(D_METHOD("get_shutter_speed"), &CameraAttributesPhysical::get_shutter_speed);
  389. ClassDB::bind_method(D_METHOD("set_focal_length", "focal_length"), &CameraAttributesPhysical::set_focal_length);
  390. ClassDB::bind_method(D_METHOD("get_focal_length"), &CameraAttributesPhysical::get_focal_length);
  391. ClassDB::bind_method(D_METHOD("set_focus_distance", "focus_distance"), &CameraAttributesPhysical::set_focus_distance);
  392. ClassDB::bind_method(D_METHOD("get_focus_distance"), &CameraAttributesPhysical::get_focus_distance);
  393. ClassDB::bind_method(D_METHOD("set_near", "near"), &CameraAttributesPhysical::set_near);
  394. ClassDB::bind_method(D_METHOD("get_near"), &CameraAttributesPhysical::get_near);
  395. ClassDB::bind_method(D_METHOD("set_far", "far"), &CameraAttributesPhysical::set_far);
  396. ClassDB::bind_method(D_METHOD("get_far"), &CameraAttributesPhysical::get_far);
  397. ClassDB::bind_method(D_METHOD("get_fov"), &CameraAttributesPhysical::get_fov);
  398. ClassDB::bind_method(D_METHOD("set_auto_exposure_max_exposure_value", "exposure_value_max"), &CameraAttributesPhysical::set_auto_exposure_max_exposure_value);
  399. ClassDB::bind_method(D_METHOD("get_auto_exposure_max_exposure_value"), &CameraAttributesPhysical::get_auto_exposure_max_exposure_value);
  400. ClassDB::bind_method(D_METHOD("set_auto_exposure_min_exposure_value", "exposure_value_min"), &CameraAttributesPhysical::set_auto_exposure_min_exposure_value);
  401. ClassDB::bind_method(D_METHOD("get_auto_exposure_min_exposure_value"), &CameraAttributesPhysical::get_auto_exposure_min_exposure_value);
  402. ADD_GROUP("Frustum", "frustum_");
  403. 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");
  404. 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");
  405. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frustum_near", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater,exp,suffix:m"), "set_near", "get_near");
  406. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frustum_far", PROPERTY_HINT_RANGE, "0.01,4000,0.01,or_greater,exp,suffix:m"), "set_far", "get_far");
  407. ADD_GROUP("Exposure", "exposure_");
  408. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "exposure_aperture", PROPERTY_HINT_RANGE, "0.5,64.0,0.01,exp,suffix:f-stop"), "set_aperture", "get_aperture");
  409. 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");
  410. ADD_GROUP("Auto Exposure", "auto_exposure_");
  411. 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");
  412. 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");
  413. }
  414. CameraAttributesPhysical::CameraAttributesPhysical() {
  415. _update_exposure();
  416. _update_frustum();
  417. set_auto_exposure_min_exposure_value(-8);
  418. set_auto_exposure_max_exposure_value(10); // Use a wide range by default to feel more like a real camera.
  419. notify_property_list_changed();
  420. }
  421. CameraAttributesPhysical::~CameraAttributesPhysical() {
  422. }