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