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