light_3d.cpp 29 KB

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