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