light_3d.cpp 29 KB

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