| 1 | /**************************************************************************/ |
| 2 | /* light_3d.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 "core/config/project_settings.h" |
| 32 | |
| 33 | #include "light_3d.h" |
| 34 | |
| 35 | void Light3D::set_param(Param p_param, real_t p_value) { |
| 36 | ERR_FAIL_INDEX(p_param, PARAM_MAX); |
| 37 | param[p_param] = p_value; |
| 38 | |
| 39 | RS::get_singleton()->light_set_param(light, RS::LightParam(p_param), p_value); |
| 40 | |
| 41 | if (p_param == PARAM_SPOT_ANGLE || p_param == PARAM_RANGE) { |
| 42 | update_gizmos(); |
| 43 | |
| 44 | if (p_param == PARAM_SPOT_ANGLE) { |
| 45 | update_configuration_warnings(); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | real_t Light3D::get_param(Param p_param) const { |
| 51 | ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0); |
| 52 | return param[p_param]; |
| 53 | } |
| 54 | |
| 55 | void Light3D::set_shadow(bool p_enable) { |
| 56 | shadow = p_enable; |
| 57 | RS::get_singleton()->light_set_shadow(light, p_enable); |
| 58 | |
| 59 | notify_property_list_changed(); |
| 60 | update_configuration_warnings(); |
| 61 | } |
| 62 | |
| 63 | bool Light3D::has_shadow() const { |
| 64 | return shadow; |
| 65 | } |
| 66 | |
| 67 | void Light3D::set_negative(bool p_enable) { |
| 68 | negative = p_enable; |
| 69 | RS::get_singleton()->light_set_negative(light, p_enable); |
| 70 | } |
| 71 | |
| 72 | bool Light3D::is_negative() const { |
| 73 | return negative; |
| 74 | } |
| 75 | |
| 76 | void Light3D::set_enable_distance_fade(bool p_enable) { |
| 77 | distance_fade_enabled = p_enable; |
| 78 | RS::get_singleton()->light_set_distance_fade(light, distance_fade_enabled, distance_fade_begin, distance_fade_shadow, distance_fade_length); |
| 79 | notify_property_list_changed(); |
| 80 | } |
| 81 | |
| 82 | bool Light3D::is_distance_fade_enabled() const { |
| 83 | return distance_fade_enabled; |
| 84 | } |
| 85 | |
| 86 | void Light3D::set_distance_fade_begin(real_t p_distance) { |
| 87 | distance_fade_begin = p_distance; |
| 88 | RS::get_singleton()->light_set_distance_fade(light, distance_fade_enabled, distance_fade_begin, distance_fade_shadow, distance_fade_length); |
| 89 | } |
| 90 | |
| 91 | real_t Light3D::get_distance_fade_begin() const { |
| 92 | return distance_fade_begin; |
| 93 | } |
| 94 | |
| 95 | void Light3D::set_distance_fade_shadow(real_t p_distance) { |
| 96 | distance_fade_shadow = p_distance; |
| 97 | RS::get_singleton()->light_set_distance_fade(light, distance_fade_enabled, distance_fade_begin, distance_fade_shadow, distance_fade_length); |
| 98 | } |
| 99 | |
| 100 | real_t Light3D::get_distance_fade_shadow() const { |
| 101 | return distance_fade_shadow; |
| 102 | } |
| 103 | |
| 104 | void Light3D::set_distance_fade_length(real_t p_length) { |
| 105 | distance_fade_length = p_length; |
| 106 | RS::get_singleton()->light_set_distance_fade(light, distance_fade_enabled, distance_fade_begin, distance_fade_shadow, distance_fade_length); |
| 107 | } |
| 108 | |
| 109 | real_t Light3D::get_distance_fade_length() const { |
| 110 | return distance_fade_length; |
| 111 | } |
| 112 | |
| 113 | void Light3D::set_cull_mask(uint32_t p_cull_mask) { |
| 114 | cull_mask = p_cull_mask; |
| 115 | RS::get_singleton()->light_set_cull_mask(light, p_cull_mask); |
| 116 | } |
| 117 | |
| 118 | uint32_t Light3D::get_cull_mask() const { |
| 119 | return cull_mask; |
| 120 | } |
| 121 | |
| 122 | void Light3D::set_color(const Color &p_color) { |
| 123 | color = p_color; |
| 124 | |
| 125 | if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units" )) { |
| 126 | Color combined = color.srgb_to_linear(); |
| 127 | combined *= correlated_color.srgb_to_linear(); |
| 128 | RS::get_singleton()->light_set_color(light, combined.linear_to_srgb()); |
| 129 | } else { |
| 130 | RS::get_singleton()->light_set_color(light, color); |
| 131 | } |
| 132 | // The gizmo color depends on the light color, so update it. |
| 133 | update_gizmos(); |
| 134 | } |
| 135 | |
| 136 | Color Light3D::get_color() const { |
| 137 | return color; |
| 138 | } |
| 139 | |
| 140 | void Light3D::set_shadow_reverse_cull_face(bool p_enable) { |
| 141 | reverse_cull = p_enable; |
| 142 | RS::get_singleton()->light_set_reverse_cull_face_mode(light, reverse_cull); |
| 143 | } |
| 144 | |
| 145 | bool Light3D::get_shadow_reverse_cull_face() const { |
| 146 | return reverse_cull; |
| 147 | } |
| 148 | |
| 149 | AABB Light3D::get_aabb() const { |
| 150 | if (type == RenderingServer::LIGHT_DIRECTIONAL) { |
| 151 | return AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2)); |
| 152 | |
| 153 | } else if (type == RenderingServer::LIGHT_OMNI) { |
| 154 | return AABB(Vector3(-1, -1, -1) * param[PARAM_RANGE], Vector3(2, 2, 2) * param[PARAM_RANGE]); |
| 155 | |
| 156 | } else if (type == RenderingServer::LIGHT_SPOT) { |
| 157 | real_t cone_slant_height = param[PARAM_RANGE]; |
| 158 | real_t cone_angle_rad = Math::deg_to_rad(param[PARAM_SPOT_ANGLE]); |
| 159 | |
| 160 | if (cone_angle_rad > Math_PI / 2.0) { |
| 161 | // Just return the AABB of an omni light if the spot angle is above 90 degrees. |
| 162 | return AABB(Vector3(-1, -1, -1) * cone_slant_height, Vector3(2, 2, 2) * cone_slant_height); |
| 163 | } |
| 164 | |
| 165 | real_t size = Math::sin(cone_angle_rad) * cone_slant_height; |
| 166 | return AABB(Vector3(-size, -size, -cone_slant_height), Vector3(2 * size, 2 * size, cone_slant_height)); |
| 167 | } |
| 168 | |
| 169 | return AABB(); |
| 170 | } |
| 171 | |
| 172 | PackedStringArray Light3D::get_configuration_warnings() const { |
| 173 | PackedStringArray warnings = VisualInstance3D::get_configuration_warnings(); |
| 174 | |
| 175 | if (has_shadow() && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" ) { |
| 176 | warnings.push_back(RTR("Shadows are not supported when using the GL Compatibility backend yet. Support will be added in a future release." )); |
| 177 | } |
| 178 | |
| 179 | if (!get_scale().is_equal_approx(Vector3(1, 1, 1))) { |
| 180 | warnings.push_back(RTR("A light's scale does not affect the visual size of the light." )); |
| 181 | } |
| 182 | |
| 183 | return warnings; |
| 184 | } |
| 185 | |
| 186 | void Light3D::set_bake_mode(BakeMode p_mode) { |
| 187 | bake_mode = p_mode; |
| 188 | RS::get_singleton()->light_set_bake_mode(light, RS::LightBakeMode(p_mode)); |
| 189 | } |
| 190 | |
| 191 | Light3D::BakeMode Light3D::get_bake_mode() const { |
| 192 | return bake_mode; |
| 193 | } |
| 194 | |
| 195 | void Light3D::set_projector(const Ref<Texture2D> &p_texture) { |
| 196 | projector = p_texture; |
| 197 | RID tex_id = projector.is_valid() ? projector->get_rid() : RID(); |
| 198 | RS::get_singleton()->light_set_projector(light, tex_id); |
| 199 | update_configuration_warnings(); |
| 200 | } |
| 201 | |
| 202 | Ref<Texture2D> Light3D::get_projector() const { |
| 203 | return projector; |
| 204 | } |
| 205 | |
| 206 | void Light3D::owner_changed_notify() { |
| 207 | // For cases where owner changes _after_ entering tree (as example, editor editing). |
| 208 | _update_visibility(); |
| 209 | } |
| 210 | |
| 211 | // Temperature expressed in Kelvins. Valid range 1000 - 15000 |
| 212 | // First converts to CIE 1960 then to sRGB |
| 213 | // As explained in the Filament documentation: https://google.github.io/filament/Filament.md.html#lighting/directlighting/lightsparameterization |
| 214 | Color _color_from_temperature(float p_temperature) { |
| 215 | float T2 = p_temperature * p_temperature; |
| 216 | float u = (0.860117757f + 1.54118254e-4f * p_temperature + 1.28641212e-7f * T2) / |
| 217 | (1.0f + 8.42420235e-4f * p_temperature + 7.08145163e-7f * T2); |
| 218 | float v = (0.317398726f + 4.22806245e-5f * p_temperature + 4.20481691e-8f * T2) / |
| 219 | (1.0f - 2.89741816e-5f * p_temperature + 1.61456053e-7f * T2); |
| 220 | |
| 221 | // Convert to xyY space. |
| 222 | float d = 1.0f / (2.0f * u - 8.0f * v + 4.0f); |
| 223 | float x = 3.0f * u * d; |
| 224 | float y = 2.0f * v * d; |
| 225 | |
| 226 | // Convert to XYZ space |
| 227 | const float a = 1.0 / MAX(y, 1e-5f); |
| 228 | Vector3 xyz = Vector3(x * a, 1.0, (1.0f - x - y) * a); |
| 229 | |
| 230 | // Convert from XYZ to sRGB(linear) |
| 231 | Vector3 linear = Vector3(3.2404542f * xyz.x - 1.5371385f * xyz.y - 0.4985314f * xyz.z, |
| 232 | -0.9692660f * xyz.x + 1.8760108f * xyz.y + 0.0415560f * xyz.z, |
| 233 | 0.0556434f * xyz.x - 0.2040259f * xyz.y + 1.0572252f * xyz.z); |
| 234 | linear /= MAX(1e-5f, linear[linear.max_axis_index()]); |
| 235 | // Normalize, clamp, and convert to sRGB. |
| 236 | return Color(linear.x, linear.y, linear.z).clamp().linear_to_srgb(); |
| 237 | } |
| 238 | |
| 239 | void Light3D::set_temperature(const float p_temperature) { |
| 240 | temperature = p_temperature; |
| 241 | if (!GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units" )) { |
| 242 | return; |
| 243 | } |
| 244 | correlated_color = _color_from_temperature(temperature); |
| 245 | |
| 246 | Color combined = color.srgb_to_linear() * correlated_color.srgb_to_linear(); |
| 247 | |
| 248 | RS::get_singleton()->light_set_color(light, combined.linear_to_srgb()); |
| 249 | // The gizmo color depends on the light color, so update it. |
| 250 | update_gizmos(); |
| 251 | } |
| 252 | |
| 253 | Color Light3D::get_correlated_color() const { |
| 254 | return correlated_color; |
| 255 | } |
| 256 | |
| 257 | float Light3D::get_temperature() const { |
| 258 | return temperature; |
| 259 | } |
| 260 | |
| 261 | void Light3D::_update_visibility() { |
| 262 | if (!is_inside_tree()) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | bool editor_ok = true; |
| 267 | |
| 268 | #ifdef TOOLS_ENABLED |
| 269 | if (editor_only) { |
| 270 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 271 | editor_ok = false; |
| 272 | } else { |
| 273 | editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root())); |
| 274 | } |
| 275 | } |
| 276 | #else |
| 277 | if (editor_only) { |
| 278 | editor_ok = false; |
| 279 | } |
| 280 | #endif |
| 281 | |
| 282 | RS::get_singleton()->instance_set_visible(get_instance(), is_visible_in_tree() && editor_ok); |
| 283 | } |
| 284 | |
| 285 | void Light3D::_notification(int p_what) { |
| 286 | switch (p_what) { |
| 287 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 288 | update_configuration_warnings(); |
| 289 | } break; |
| 290 | case NOTIFICATION_VISIBILITY_CHANGED: |
| 291 | case NOTIFICATION_ENTER_TREE: { |
| 292 | _update_visibility(); |
| 293 | } break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void Light3D::set_editor_only(bool p_editor_only) { |
| 298 | editor_only = p_editor_only; |
| 299 | _update_visibility(); |
| 300 | } |
| 301 | |
| 302 | bool Light3D::is_editor_only() const { |
| 303 | return editor_only; |
| 304 | } |
| 305 | |
| 306 | void Light3D::_validate_property(PropertyInfo &p_property) const { |
| 307 | if (!shadow && (p_property.name == "shadow_bias" || p_property.name == "shadow_normal_bias" || p_property.name == "shadow_reverse_cull_face" || p_property.name == "shadow_transmittance_bias" || p_property.name == "shadow_opacity" || p_property.name == "shadow_blur" || p_property.name == "distance_fade_shadow" )) { |
| 308 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 309 | } |
| 310 | |
| 311 | if (get_light_type() != RS::LIGHT_DIRECTIONAL && (p_property.name == "light_angular_distance" || p_property.name == "light_intensity_lux" )) { |
| 312 | // Angular distance and Light Intensity Lux are only used in DirectionalLight3D. |
| 313 | p_property.usage = PROPERTY_USAGE_NONE; |
| 314 | } else if (get_light_type() == RS::LIGHT_DIRECTIONAL && p_property.name == "light_intensity_lumens" ) { |
| 315 | p_property.usage = PROPERTY_USAGE_NONE; |
| 316 | } |
| 317 | |
| 318 | if (!GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units" ) && (p_property.name == "light_intensity_lumens" || p_property.name == "light_intensity_lux" || p_property.name == "light_temperature" )) { |
| 319 | p_property.usage = PROPERTY_USAGE_NONE; |
| 320 | } |
| 321 | |
| 322 | if (!distance_fade_enabled && (p_property.name == "distance_fade_begin" || p_property.name == "distance_fade_shadow" || p_property.name == "distance_fade_length" )) { |
| 323 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void Light3D::_bind_methods() { |
| 328 | ClassDB::bind_method(D_METHOD("set_editor_only" , "editor_only" ), &Light3D::set_editor_only); |
| 329 | ClassDB::bind_method(D_METHOD("is_editor_only" ), &Light3D::is_editor_only); |
| 330 | |
| 331 | ClassDB::bind_method(D_METHOD("set_param" , "param" , "value" ), &Light3D::set_param); |
| 332 | ClassDB::bind_method(D_METHOD("get_param" , "param" ), &Light3D::get_param); |
| 333 | |
| 334 | ClassDB::bind_method(D_METHOD("set_shadow" , "enabled" ), &Light3D::set_shadow); |
| 335 | ClassDB::bind_method(D_METHOD("has_shadow" ), &Light3D::has_shadow); |
| 336 | |
| 337 | ClassDB::bind_method(D_METHOD("set_negative" , "enabled" ), &Light3D::set_negative); |
| 338 | ClassDB::bind_method(D_METHOD("is_negative" ), &Light3D::is_negative); |
| 339 | |
| 340 | ClassDB::bind_method(D_METHOD("set_cull_mask" , "cull_mask" ), &Light3D::set_cull_mask); |
| 341 | ClassDB::bind_method(D_METHOD("get_cull_mask" ), &Light3D::get_cull_mask); |
| 342 | |
| 343 | ClassDB::bind_method(D_METHOD("set_enable_distance_fade" , "enable" ), &Light3D::set_enable_distance_fade); |
| 344 | ClassDB::bind_method(D_METHOD("is_distance_fade_enabled" ), &Light3D::is_distance_fade_enabled); |
| 345 | |
| 346 | ClassDB::bind_method(D_METHOD("set_distance_fade_begin" , "distance" ), &Light3D::set_distance_fade_begin); |
| 347 | ClassDB::bind_method(D_METHOD("get_distance_fade_begin" ), &Light3D::get_distance_fade_begin); |
| 348 | |
| 349 | ClassDB::bind_method(D_METHOD("set_distance_fade_shadow" , "distance" ), &Light3D::set_distance_fade_shadow); |
| 350 | ClassDB::bind_method(D_METHOD("get_distance_fade_shadow" ), &Light3D::get_distance_fade_shadow); |
| 351 | |
| 352 | ClassDB::bind_method(D_METHOD("set_distance_fade_length" , "distance" ), &Light3D::set_distance_fade_length); |
| 353 | ClassDB::bind_method(D_METHOD("get_distance_fade_length" ), &Light3D::get_distance_fade_length); |
| 354 | |
| 355 | ClassDB::bind_method(D_METHOD("set_color" , "color" ), &Light3D::set_color); |
| 356 | ClassDB::bind_method(D_METHOD("get_color" ), &Light3D::get_color); |
| 357 | |
| 358 | ClassDB::bind_method(D_METHOD("set_shadow_reverse_cull_face" , "enable" ), &Light3D::set_shadow_reverse_cull_face); |
| 359 | ClassDB::bind_method(D_METHOD("get_shadow_reverse_cull_face" ), &Light3D::get_shadow_reverse_cull_face); |
| 360 | |
| 361 | ClassDB::bind_method(D_METHOD("set_bake_mode" , "bake_mode" ), &Light3D::set_bake_mode); |
| 362 | ClassDB::bind_method(D_METHOD("get_bake_mode" ), &Light3D::get_bake_mode); |
| 363 | |
| 364 | ClassDB::bind_method(D_METHOD("set_projector" , "projector" ), &Light3D::set_projector); |
| 365 | ClassDB::bind_method(D_METHOD("get_projector" ), &Light3D::get_projector); |
| 366 | |
| 367 | ClassDB::bind_method(D_METHOD("set_temperature" , "temperature" ), &Light3D::set_temperature); |
| 368 | ClassDB::bind_method(D_METHOD("get_temperature" ), &Light3D::get_temperature); |
| 369 | ClassDB::bind_method(D_METHOD("get_correlated_color" ), &Light3D::get_correlated_color); |
| 370 | |
| 371 | ADD_GROUP("Light" , "light_" ); |
| 372 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_intensity_lumens" , PROPERTY_HINT_RANGE, "0,100000.0,0.01,or_greater,suffix:lm" ), "set_param" , "get_param" , PARAM_INTENSITY); |
| 373 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_intensity_lux" , PROPERTY_HINT_RANGE, "0,150000.0,0.01,or_greater,suffix:lx" ), "set_param" , "get_param" , PARAM_INTENSITY); |
| 374 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "light_temperature" , PROPERTY_HINT_RANGE, "1000,15000.0,1.0,suffix:k" ), "set_temperature" , "get_temperature" ); |
| 375 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "light_color" , PROPERTY_HINT_COLOR_NO_ALPHA), "set_color" , "get_color" ); |
| 376 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_energy" , PROPERTY_HINT_RANGE, "0,16,0.001,or_greater" ), "set_param" , "get_param" , PARAM_ENERGY); |
| 377 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_indirect_energy" , PROPERTY_HINT_RANGE, "0,16,0.001,or_greater" ), "set_param" , "get_param" , PARAM_INDIRECT_ENERGY); |
| 378 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_volumetric_fog_energy" , PROPERTY_HINT_RANGE, "0,16,0.001,or_greater" ), "set_param" , "get_param" , PARAM_VOLUMETRIC_FOG_ENERGY); |
| 379 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_projector" , PROPERTY_HINT_RESOURCE_TYPE, "Texture2D" ), "set_projector" , "get_projector" ); |
| 380 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_size" , PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m" ), "set_param" , "get_param" , PARAM_SIZE); |
| 381 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_angular_distance" , PROPERTY_HINT_RANGE, "0,90,0.01,degrees" ), "set_param" , "get_param" , PARAM_SIZE); |
| 382 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "light_negative" ), "set_negative" , "is_negative" ); |
| 383 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "light_specular" , PROPERTY_HINT_RANGE, "0,16,0.001,or_greater" ), "set_param" , "get_param" , PARAM_SPECULAR); |
| 384 | ADD_PROPERTY(PropertyInfo(Variant::INT, "light_bake_mode" , PROPERTY_HINT_ENUM, "Disabled,Static (VoxelGI/SDFGI/LightmapGI),Dynamic (VoxelGI/SDFGI only)" ), "set_bake_mode" , "get_bake_mode" ); |
| 385 | ADD_PROPERTY(PropertyInfo(Variant::INT, "light_cull_mask" , PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask" , "get_cull_mask" ); |
| 386 | |
| 387 | ADD_GROUP("Shadow" , "shadow_" ); |
| 388 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_enabled" ), "set_shadow" , "has_shadow" ); |
| 389 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "shadow_bias" , PROPERTY_HINT_RANGE, "0,10,0.001" ), "set_param" , "get_param" , PARAM_SHADOW_BIAS); |
| 390 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "shadow_normal_bias" , PROPERTY_HINT_RANGE, "0,10,0.001" ), "set_param" , "get_param" , PARAM_SHADOW_NORMAL_BIAS); |
| 391 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_reverse_cull_face" ), "set_shadow_reverse_cull_face" , "get_shadow_reverse_cull_face" ); |
| 392 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "shadow_transmittance_bias" , PROPERTY_HINT_RANGE, "-16,16,0.001" ), "set_param" , "get_param" , PARAM_TRANSMITTANCE_BIAS); |
| 393 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "shadow_opacity" , PROPERTY_HINT_RANGE, "0,1,0.01" ), "set_param" , "get_param" , PARAM_SHADOW_OPACITY); |
| 394 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "shadow_blur" , PROPERTY_HINT_RANGE, "0,10,0.001" ), "set_param" , "get_param" , PARAM_SHADOW_BLUR); |
| 395 | |
| 396 | ADD_GROUP("Distance Fade" , "distance_fade_" ); |
| 397 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "distance_fade_enabled" ), "set_enable_distance_fade" , "is_distance_fade_enabled" ); |
| 398 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_begin" , PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m" ), "set_distance_fade_begin" , "get_distance_fade_begin" ); |
| 399 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_shadow" , PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m" ), "set_distance_fade_shadow" , "get_distance_fade_shadow" ); |
| 400 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "distance_fade_length" , PROPERTY_HINT_RANGE, "0.0,4096.0,0.01,or_greater,suffix:m" ), "set_distance_fade_length" , "get_distance_fade_length" ); |
| 401 | |
| 402 | ADD_GROUP("Editor" , "" ); |
| 403 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only" ), "set_editor_only" , "is_editor_only" ); |
| 404 | |
| 405 | ADD_GROUP("" , "" ); |
| 406 | |
| 407 | BIND_ENUM_CONSTANT(PARAM_ENERGY); |
| 408 | BIND_ENUM_CONSTANT(PARAM_INDIRECT_ENERGY); |
| 409 | BIND_ENUM_CONSTANT(PARAM_VOLUMETRIC_FOG_ENERGY); |
| 410 | BIND_ENUM_CONSTANT(PARAM_SPECULAR); |
| 411 | BIND_ENUM_CONSTANT(PARAM_RANGE); |
| 412 | BIND_ENUM_CONSTANT(PARAM_SIZE); |
| 413 | BIND_ENUM_CONSTANT(PARAM_ATTENUATION); |
| 414 | BIND_ENUM_CONSTANT(PARAM_SPOT_ANGLE); |
| 415 | BIND_ENUM_CONSTANT(PARAM_SPOT_ATTENUATION); |
| 416 | BIND_ENUM_CONSTANT(PARAM_SHADOW_MAX_DISTANCE); |
| 417 | BIND_ENUM_CONSTANT(PARAM_SHADOW_SPLIT_1_OFFSET); |
| 418 | BIND_ENUM_CONSTANT(PARAM_SHADOW_SPLIT_2_OFFSET); |
| 419 | BIND_ENUM_CONSTANT(PARAM_SHADOW_SPLIT_3_OFFSET); |
| 420 | BIND_ENUM_CONSTANT(PARAM_SHADOW_FADE_START); |
| 421 | BIND_ENUM_CONSTANT(PARAM_SHADOW_NORMAL_BIAS); |
| 422 | BIND_ENUM_CONSTANT(PARAM_SHADOW_BIAS); |
| 423 | BIND_ENUM_CONSTANT(PARAM_SHADOW_PANCAKE_SIZE); |
| 424 | BIND_ENUM_CONSTANT(PARAM_SHADOW_OPACITY); |
| 425 | BIND_ENUM_CONSTANT(PARAM_SHADOW_BLUR); |
| 426 | BIND_ENUM_CONSTANT(PARAM_TRANSMITTANCE_BIAS); |
| 427 | BIND_ENUM_CONSTANT(PARAM_INTENSITY); |
| 428 | BIND_ENUM_CONSTANT(PARAM_MAX); |
| 429 | |
| 430 | BIND_ENUM_CONSTANT(BAKE_DISABLED); |
| 431 | BIND_ENUM_CONSTANT(BAKE_STATIC); |
| 432 | BIND_ENUM_CONSTANT(BAKE_DYNAMIC); |
| 433 | } |
| 434 | |
| 435 | Light3D::Light3D(RenderingServer::LightType p_type) { |
| 436 | type = p_type; |
| 437 | switch (p_type) { |
| 438 | case RS::LIGHT_DIRECTIONAL: |
| 439 | light = RenderingServer::get_singleton()->directional_light_create(); |
| 440 | break; |
| 441 | case RS::LIGHT_OMNI: |
| 442 | light = RenderingServer::get_singleton()->omni_light_create(); |
| 443 | break; |
| 444 | case RS::LIGHT_SPOT: |
| 445 | light = RenderingServer::get_singleton()->spot_light_create(); |
| 446 | break; |
| 447 | default: { |
| 448 | }; |
| 449 | } |
| 450 | |
| 451 | RS::get_singleton()->instance_set_base(get_instance(), light); |
| 452 | |
| 453 | set_color(Color(1, 1, 1, 1)); |
| 454 | set_shadow(false); |
| 455 | set_negative(false); |
| 456 | set_cull_mask(0xFFFFFFFF); |
| 457 | |
| 458 | set_param(PARAM_ENERGY, 1); |
| 459 | set_param(PARAM_INDIRECT_ENERGY, 1); |
| 460 | set_param(PARAM_VOLUMETRIC_FOG_ENERGY, 1); |
| 461 | set_param(PARAM_SPECULAR, 0.5); |
| 462 | set_param(PARAM_RANGE, 5); |
| 463 | set_param(PARAM_SIZE, 0); |
| 464 | set_param(PARAM_ATTENUATION, 1); |
| 465 | set_param(PARAM_SPOT_ANGLE, 45); |
| 466 | set_param(PARAM_SPOT_ATTENUATION, 1); |
| 467 | set_param(PARAM_SHADOW_MAX_DISTANCE, 0); |
| 468 | set_param(PARAM_SHADOW_SPLIT_1_OFFSET, 0.1); |
| 469 | set_param(PARAM_SHADOW_SPLIT_2_OFFSET, 0.2); |
| 470 | set_param(PARAM_SHADOW_SPLIT_3_OFFSET, 0.5); |
| 471 | set_param(PARAM_SHADOW_FADE_START, 0.8); |
| 472 | set_param(PARAM_SHADOW_PANCAKE_SIZE, 20.0); |
| 473 | set_param(PARAM_SHADOW_OPACITY, 1.0); |
| 474 | set_param(PARAM_SHADOW_BLUR, 1.0); |
| 475 | set_param(PARAM_SHADOW_BIAS, 0.1); |
| 476 | set_param(PARAM_SHADOW_NORMAL_BIAS, 1.0); |
| 477 | set_param(PARAM_TRANSMITTANCE_BIAS, 0.05); |
| 478 | set_param(PARAM_SHADOW_FADE_START, 1); |
| 479 | // For OmniLight3D and SpotLight3D, specified in Lumens. |
| 480 | set_param(PARAM_INTENSITY, 1000.0); |
| 481 | set_temperature(6500.0); // Nearly white. |
| 482 | set_disable_scale(true); |
| 483 | } |
| 484 | |
| 485 | Light3D::Light3D() { |
| 486 | ERR_PRINT("Light3D should not be instantiated directly; use the DirectionalLight3D, OmniLight3D or SpotLight3D subtypes instead." ); |
| 487 | } |
| 488 | |
| 489 | Light3D::~Light3D() { |
| 490 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 491 | RS::get_singleton()->instance_set_base(get_instance(), RID()); |
| 492 | |
| 493 | if (light.is_valid()) { |
| 494 | RenderingServer::get_singleton()->free(light); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | ///////////////////////////////////////// |
| 499 | |
| 500 | void DirectionalLight3D::set_shadow_mode(ShadowMode p_mode) { |
| 501 | shadow_mode = p_mode; |
| 502 | RS::get_singleton()->light_directional_set_shadow_mode(light, RS::LightDirectionalShadowMode(p_mode)); |
| 503 | notify_property_list_changed(); |
| 504 | } |
| 505 | |
| 506 | DirectionalLight3D::ShadowMode DirectionalLight3D::get_shadow_mode() const { |
| 507 | return shadow_mode; |
| 508 | } |
| 509 | |
| 510 | void DirectionalLight3D::set_blend_splits(bool p_enable) { |
| 511 | blend_splits = p_enable; |
| 512 | RS::get_singleton()->light_directional_set_blend_splits(light, p_enable); |
| 513 | } |
| 514 | |
| 515 | bool DirectionalLight3D::is_blend_splits_enabled() const { |
| 516 | return blend_splits; |
| 517 | } |
| 518 | |
| 519 | void DirectionalLight3D::set_sky_mode(SkyMode p_mode) { |
| 520 | sky_mode = p_mode; |
| 521 | RS::get_singleton()->light_directional_set_sky_mode(light, RS::LightDirectionalSkyMode(p_mode)); |
| 522 | } |
| 523 | |
| 524 | DirectionalLight3D::SkyMode DirectionalLight3D::get_sky_mode() const { |
| 525 | return sky_mode; |
| 526 | } |
| 527 | |
| 528 | void DirectionalLight3D::_validate_property(PropertyInfo &p_property) const { |
| 529 | if (shadow_mode == SHADOW_ORTHOGONAL && (p_property.name == "directional_shadow_split_1" || p_property.name == "directional_shadow_blend_splits" )) { |
| 530 | // Split 2 and split blending are only used with the PSSM 2 Splits and PSSM 4 Splits shadow modes. |
| 531 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 532 | } |
| 533 | |
| 534 | if ((shadow_mode == SHADOW_ORTHOGONAL || shadow_mode == SHADOW_PARALLEL_2_SPLITS) && (p_property.name == "directional_shadow_split_2" || p_property.name == "directional_shadow_split_3" )) { |
| 535 | // Splits 3 and 4 are only used with the PSSM 4 Splits shadow mode. |
| 536 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 537 | } |
| 538 | |
| 539 | if (p_property.name == "light_size" || p_property.name == "light_projector" || p_property.name == "light_specular" ) { |
| 540 | // Not implemented in DirectionalLight3D (`light_size` is replaced by `light_angular_distance`). |
| 541 | p_property.usage = PROPERTY_USAGE_NONE; |
| 542 | } |
| 543 | |
| 544 | if (p_property.name == "distance_fade_enabled" || p_property.name == "distance_fade_begin" || p_property.name == "distance_fade_shadow" || p_property.name == "distance_fade_length" ) { |
| 545 | // Not relevant for DirectionalLight3D, as the light LOD system only pertains to point lights. |
| 546 | // For DirectionalLight3D, `directional_shadow_max_distance` can be used instead. |
| 547 | p_property.usage = PROPERTY_USAGE_NONE; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | void DirectionalLight3D::_bind_methods() { |
| 552 | ClassDB::bind_method(D_METHOD("set_shadow_mode" , "mode" ), &DirectionalLight3D::set_shadow_mode); |
| 553 | ClassDB::bind_method(D_METHOD("get_shadow_mode" ), &DirectionalLight3D::get_shadow_mode); |
| 554 | |
| 555 | ClassDB::bind_method(D_METHOD("set_blend_splits" , "enabled" ), &DirectionalLight3D::set_blend_splits); |
| 556 | ClassDB::bind_method(D_METHOD("is_blend_splits_enabled" ), &DirectionalLight3D::is_blend_splits_enabled); |
| 557 | |
| 558 | ClassDB::bind_method(D_METHOD("set_sky_mode" , "mode" ), &DirectionalLight3D::set_sky_mode); |
| 559 | ClassDB::bind_method(D_METHOD("get_sky_mode" ), &DirectionalLight3D::get_sky_mode); |
| 560 | |
| 561 | ADD_GROUP("Directional Shadow" , "directional_shadow_" ); |
| 562 | ADD_PROPERTY(PropertyInfo(Variant::INT, "directional_shadow_mode" , PROPERTY_HINT_ENUM, "Orthogonal (Fast),PSSM 2 Splits (Average),PSSM 4 Splits (Slow)" ), "set_shadow_mode" , "get_shadow_mode" ); |
| 563 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "directional_shadow_split_1" , PROPERTY_HINT_RANGE, "0,1,0.001" ), "set_param" , "get_param" , PARAM_SHADOW_SPLIT_1_OFFSET); |
| 564 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "directional_shadow_split_2" , PROPERTY_HINT_RANGE, "0,1,0.001" ), "set_param" , "get_param" , PARAM_SHADOW_SPLIT_2_OFFSET); |
| 565 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "directional_shadow_split_3" , PROPERTY_HINT_RANGE, "0,1,0.001" ), "set_param" , "get_param" , PARAM_SHADOW_SPLIT_3_OFFSET); |
| 566 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional_shadow_blend_splits" ), "set_blend_splits" , "is_blend_splits_enabled" ); |
| 567 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "directional_shadow_fade_start" , PROPERTY_HINT_RANGE, "0,1,0.001" ), "set_param" , "get_param" , PARAM_SHADOW_FADE_START); |
| 568 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "directional_shadow_max_distance" , PROPERTY_HINT_RANGE, "0,8192,0.1,or_greater,exp" ), "set_param" , "get_param" , PARAM_SHADOW_MAX_DISTANCE); |
| 569 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "directional_shadow_pancake_size" , PROPERTY_HINT_RANGE, "0,1024,0.1,or_greater,exp" ), "set_param" , "get_param" , PARAM_SHADOW_PANCAKE_SIZE); |
| 570 | |
| 571 | ADD_PROPERTY(PropertyInfo(Variant::INT, "sky_mode" , PROPERTY_HINT_ENUM, "Light and Sky,Light Only,Sky Only" ), "set_sky_mode" , "get_sky_mode" ); |
| 572 | |
| 573 | BIND_ENUM_CONSTANT(SHADOW_ORTHOGONAL); |
| 574 | BIND_ENUM_CONSTANT(SHADOW_PARALLEL_2_SPLITS); |
| 575 | BIND_ENUM_CONSTANT(SHADOW_PARALLEL_4_SPLITS); |
| 576 | |
| 577 | BIND_ENUM_CONSTANT(SKY_MODE_LIGHT_AND_SKY); |
| 578 | BIND_ENUM_CONSTANT(SKY_MODE_LIGHT_ONLY); |
| 579 | BIND_ENUM_CONSTANT(SKY_MODE_SKY_ONLY); |
| 580 | } |
| 581 | |
| 582 | DirectionalLight3D::DirectionalLight3D() : |
| 583 | Light3D(RenderingServer::LIGHT_DIRECTIONAL) { |
| 584 | set_param(PARAM_SHADOW_MAX_DISTANCE, 100); |
| 585 | set_param(PARAM_SHADOW_FADE_START, 0.8); |
| 586 | // Increase the default shadow normal bias to better suit most scenes. |
| 587 | set_param(PARAM_SHADOW_NORMAL_BIAS, 2.0); |
| 588 | set_param(PARAM_INTENSITY, 100000.0); // Specified in Lux, approximate mid-day sun. |
| 589 | set_shadow_mode(SHADOW_PARALLEL_4_SPLITS); |
| 590 | blend_splits = false; |
| 591 | set_sky_mode(SKY_MODE_LIGHT_AND_SKY); |
| 592 | } |
| 593 | |
| 594 | void OmniLight3D::set_shadow_mode(ShadowMode p_mode) { |
| 595 | shadow_mode = p_mode; |
| 596 | RS::get_singleton()->light_omni_set_shadow_mode(light, RS::LightOmniShadowMode(p_mode)); |
| 597 | } |
| 598 | |
| 599 | OmniLight3D::ShadowMode OmniLight3D::get_shadow_mode() const { |
| 600 | return shadow_mode; |
| 601 | } |
| 602 | |
| 603 | PackedStringArray OmniLight3D::get_configuration_warnings() const { |
| 604 | PackedStringArray warnings = Light3D::get_configuration_warnings(); |
| 605 | |
| 606 | if (!has_shadow() && get_projector().is_valid()) { |
| 607 | warnings.push_back(RTR("Projector texture only works with shadows active." )); |
| 608 | } |
| 609 | |
| 610 | if (get_projector().is_valid() && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" ) { |
| 611 | warnings.push_back(RTR("Projector textures are not supported when using the GL Compatibility backend yet. Support will be added in a future release." )); |
| 612 | } |
| 613 | |
| 614 | return warnings; |
| 615 | } |
| 616 | |
| 617 | void OmniLight3D::_bind_methods() { |
| 618 | ClassDB::bind_method(D_METHOD("set_shadow_mode" , "mode" ), &OmniLight3D::set_shadow_mode); |
| 619 | ClassDB::bind_method(D_METHOD("get_shadow_mode" ), &OmniLight3D::get_shadow_mode); |
| 620 | |
| 621 | ADD_GROUP("Omni" , "omni_" ); |
| 622 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "omni_range" , PROPERTY_HINT_RANGE, "0,4096,0.001,or_greater,exp" ), "set_param" , "get_param" , PARAM_RANGE); |
| 623 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "omni_attenuation" , PROPERTY_HINT_EXP_EASING, "attenuation" ), "set_param" , "get_param" , PARAM_ATTENUATION); |
| 624 | ADD_PROPERTY(PropertyInfo(Variant::INT, "omni_shadow_mode" , PROPERTY_HINT_ENUM, "Dual Paraboloid,Cube" ), "set_shadow_mode" , "get_shadow_mode" ); |
| 625 | |
| 626 | BIND_ENUM_CONSTANT(SHADOW_DUAL_PARABOLOID); |
| 627 | BIND_ENUM_CONSTANT(SHADOW_CUBE); |
| 628 | } |
| 629 | |
| 630 | OmniLight3D::OmniLight3D() : |
| 631 | Light3D(RenderingServer::LIGHT_OMNI) { |
| 632 | set_shadow_mode(SHADOW_CUBE); |
| 633 | } |
| 634 | |
| 635 | PackedStringArray SpotLight3D::get_configuration_warnings() const { |
| 636 | PackedStringArray warnings = Light3D::get_configuration_warnings(); |
| 637 | |
| 638 | if (has_shadow() && get_param(PARAM_SPOT_ANGLE) >= 90.0) { |
| 639 | warnings.push_back(RTR("A SpotLight3D with an angle wider than 90 degrees cannot cast shadows." )); |
| 640 | } |
| 641 | |
| 642 | if (!has_shadow() && get_projector().is_valid()) { |
| 643 | warnings.push_back(RTR("Projector texture only works with shadows active." )); |
| 644 | } |
| 645 | |
| 646 | if (get_projector().is_valid() && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" ) { |
| 647 | warnings.push_back(RTR("Projector textures are not supported when using the GL Compatibility backend yet. Support will be added in a future release." )); |
| 648 | } |
| 649 | |
| 650 | return warnings; |
| 651 | } |
| 652 | |
| 653 | void SpotLight3D::_bind_methods() { |
| 654 | ADD_GROUP("Spot" , "spot_" ); |
| 655 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "spot_range" , PROPERTY_HINT_RANGE, "0,4096,0.001,or_greater,exp,suffix:m" ), "set_param" , "get_param" , PARAM_RANGE); |
| 656 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "spot_attenuation" , PROPERTY_HINT_EXP_EASING, "attenuation" ), "set_param" , "get_param" , PARAM_ATTENUATION); |
| 657 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "spot_angle" , PROPERTY_HINT_RANGE, "0,180,0.01,degrees" ), "set_param" , "get_param" , PARAM_SPOT_ANGLE); |
| 658 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "spot_angle_attenuation" , PROPERTY_HINT_EXP_EASING, "attenuation" ), "set_param" , "get_param" , PARAM_SPOT_ATTENUATION); |
| 659 | } |
| 660 | |
| 661 | SpotLight3D::SpotLight3D() : |
| 662 | Light3D(RenderingServer::LIGHT_SPOT) { |
| 663 | // Decrease the default shadow bias to better suit most scenes. |
| 664 | set_param(PARAM_SHADOW_BIAS, 0.03); |
| 665 | } |
| 666 | |