light_3d.cpp 30 KB

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