light_2d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**************************************************************************/
  2. /* light_2d.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 "light_2d.h"
  31. void Light2D::owner_changed_notify() {
  32. // For cases where owner changes _after_ entering tree (as example, editor editing).
  33. _update_light_visibility();
  34. }
  35. void Light2D::_update_light_visibility() {
  36. if (!is_inside_tree()) {
  37. return;
  38. }
  39. bool editor_ok = true;
  40. #ifdef TOOLS_ENABLED
  41. if (editor_only) {
  42. if (!Engine::get_singleton()->is_editor_hint()) {
  43. editor_ok = false;
  44. } else {
  45. editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
  46. }
  47. }
  48. #else
  49. if (editor_only) {
  50. editor_ok = false;
  51. }
  52. #endif
  53. RS::get_singleton()->canvas_light_set_enabled(canvas_light, enabled && is_visible_in_tree() && editor_ok);
  54. }
  55. void Light2D::set_enabled(bool p_enabled) {
  56. enabled = p_enabled;
  57. _update_light_visibility();
  58. }
  59. bool Light2D::is_enabled() const {
  60. return enabled;
  61. }
  62. void Light2D::set_editor_only(bool p_editor_only) {
  63. editor_only = p_editor_only;
  64. _update_light_visibility();
  65. }
  66. bool Light2D::is_editor_only() const {
  67. return editor_only;
  68. }
  69. void Light2D::set_color(const Color &p_color) {
  70. color = p_color;
  71. RS::get_singleton()->canvas_light_set_color(canvas_light, color);
  72. }
  73. Color Light2D::get_color() const {
  74. return color;
  75. }
  76. void Light2D::set_height(real_t p_height) {
  77. height = p_height;
  78. RS::get_singleton()->canvas_light_set_height(canvas_light, height);
  79. }
  80. real_t Light2D::get_height() const {
  81. return height;
  82. }
  83. void Light2D::set_energy(real_t p_energy) {
  84. energy = p_energy;
  85. RS::get_singleton()->canvas_light_set_energy(canvas_light, energy);
  86. }
  87. real_t Light2D::get_energy() const {
  88. return energy;
  89. }
  90. void Light2D::set_z_range_min(int p_min_z) {
  91. z_min = p_min_z;
  92. RS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
  93. }
  94. int Light2D::get_z_range_min() const {
  95. return z_min;
  96. }
  97. void Light2D::set_z_range_max(int p_max_z) {
  98. z_max = p_max_z;
  99. RS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
  100. }
  101. int Light2D::get_z_range_max() const {
  102. return z_max;
  103. }
  104. void Light2D::set_layer_range_min(int p_min_layer) {
  105. layer_min = p_min_layer;
  106. RS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
  107. }
  108. int Light2D::get_layer_range_min() const {
  109. return layer_min;
  110. }
  111. void Light2D::set_layer_range_max(int p_max_layer) {
  112. layer_max = p_max_layer;
  113. RS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
  114. }
  115. int Light2D::get_layer_range_max() const {
  116. return layer_max;
  117. }
  118. void Light2D::set_item_cull_mask(int p_mask) {
  119. item_mask = p_mask;
  120. RS::get_singleton()->canvas_light_set_item_cull_mask(canvas_light, item_mask);
  121. }
  122. int Light2D::get_item_cull_mask() const {
  123. return item_mask;
  124. }
  125. void Light2D::set_item_shadow_cull_mask(int p_mask) {
  126. item_shadow_mask = p_mask;
  127. RS::get_singleton()->canvas_light_set_item_shadow_cull_mask(canvas_light, item_shadow_mask);
  128. }
  129. int Light2D::get_item_shadow_cull_mask() const {
  130. return item_shadow_mask;
  131. }
  132. void Light2D::set_shadow_enabled(bool p_enabled) {
  133. shadow = p_enabled;
  134. RS::get_singleton()->canvas_light_set_shadow_enabled(canvas_light, shadow);
  135. notify_property_list_changed();
  136. }
  137. bool Light2D::is_shadow_enabled() const {
  138. return shadow;
  139. }
  140. void Light2D::set_shadow_filter(ShadowFilter p_filter) {
  141. ERR_FAIL_INDEX(p_filter, SHADOW_FILTER_MAX);
  142. shadow_filter = p_filter;
  143. RS::get_singleton()->canvas_light_set_shadow_filter(canvas_light, RS::CanvasLightShadowFilter(p_filter));
  144. notify_property_list_changed();
  145. }
  146. Light2D::ShadowFilter Light2D::get_shadow_filter() const {
  147. return shadow_filter;
  148. }
  149. void Light2D::set_shadow_color(const Color &p_shadow_color) {
  150. shadow_color = p_shadow_color;
  151. RS::get_singleton()->canvas_light_set_shadow_color(canvas_light, shadow_color);
  152. }
  153. Color Light2D::get_shadow_color() const {
  154. return shadow_color;
  155. }
  156. void Light2D::set_blend_mode(BlendMode p_mode) {
  157. blend_mode = p_mode;
  158. RS::get_singleton()->canvas_light_set_blend_mode(_get_light(), RS::CanvasLightBlendMode(p_mode));
  159. }
  160. Light2D::BlendMode Light2D::get_blend_mode() const {
  161. return blend_mode;
  162. }
  163. void Light2D::_notification(int p_what) {
  164. switch (p_what) {
  165. case NOTIFICATION_ENTER_TREE: {
  166. RS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, get_canvas());
  167. _update_light_visibility();
  168. } break;
  169. case NOTIFICATION_TRANSFORM_CHANGED: {
  170. RS::get_singleton()->canvas_light_set_transform(canvas_light, get_global_transform());
  171. } break;
  172. case NOTIFICATION_VISIBILITY_CHANGED: {
  173. _update_light_visibility();
  174. } break;
  175. case NOTIFICATION_EXIT_TREE: {
  176. RS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, RID());
  177. _update_light_visibility();
  178. } break;
  179. }
  180. }
  181. void Light2D::set_shadow_smooth(real_t p_amount) {
  182. shadow_smooth = p_amount;
  183. RS::get_singleton()->canvas_light_set_shadow_smooth(canvas_light, shadow_smooth);
  184. }
  185. real_t Light2D::get_shadow_smooth() const {
  186. return shadow_smooth;
  187. }
  188. void Light2D::_validate_property(PropertyInfo &p_property) const {
  189. if (!shadow && (p_property.name == "shadow_color" || p_property.name == "shadow_filter" || p_property.name == "shadow_filter_smooth" || p_property.name == "shadow_item_cull_mask")) {
  190. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  191. }
  192. if (shadow && p_property.name == "shadow_filter_smooth" && shadow_filter == SHADOW_FILTER_NONE) {
  193. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  194. }
  195. }
  196. void Light2D::_bind_methods() {
  197. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Light2D::set_enabled);
  198. ClassDB::bind_method(D_METHOD("is_enabled"), &Light2D::is_enabled);
  199. ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light2D::set_editor_only);
  200. ClassDB::bind_method(D_METHOD("is_editor_only"), &Light2D::is_editor_only);
  201. ClassDB::bind_method(D_METHOD("set_color", "color"), &Light2D::set_color);
  202. ClassDB::bind_method(D_METHOD("get_color"), &Light2D::get_color);
  203. ClassDB::bind_method(D_METHOD("set_energy", "energy"), &Light2D::set_energy);
  204. ClassDB::bind_method(D_METHOD("get_energy"), &Light2D::get_energy);
  205. ClassDB::bind_method(D_METHOD("set_z_range_min", "z"), &Light2D::set_z_range_min);
  206. ClassDB::bind_method(D_METHOD("get_z_range_min"), &Light2D::get_z_range_min);
  207. ClassDB::bind_method(D_METHOD("set_z_range_max", "z"), &Light2D::set_z_range_max);
  208. ClassDB::bind_method(D_METHOD("get_z_range_max"), &Light2D::get_z_range_max);
  209. ClassDB::bind_method(D_METHOD("set_layer_range_min", "layer"), &Light2D::set_layer_range_min);
  210. ClassDB::bind_method(D_METHOD("get_layer_range_min"), &Light2D::get_layer_range_min);
  211. ClassDB::bind_method(D_METHOD("set_layer_range_max", "layer"), &Light2D::set_layer_range_max);
  212. ClassDB::bind_method(D_METHOD("get_layer_range_max"), &Light2D::get_layer_range_max);
  213. ClassDB::bind_method(D_METHOD("set_item_cull_mask", "item_cull_mask"), &Light2D::set_item_cull_mask);
  214. ClassDB::bind_method(D_METHOD("get_item_cull_mask"), &Light2D::get_item_cull_mask);
  215. ClassDB::bind_method(D_METHOD("set_item_shadow_cull_mask", "item_shadow_cull_mask"), &Light2D::set_item_shadow_cull_mask);
  216. ClassDB::bind_method(D_METHOD("get_item_shadow_cull_mask"), &Light2D::get_item_shadow_cull_mask);
  217. ClassDB::bind_method(D_METHOD("set_shadow_enabled", "enabled"), &Light2D::set_shadow_enabled);
  218. ClassDB::bind_method(D_METHOD("is_shadow_enabled"), &Light2D::is_shadow_enabled);
  219. ClassDB::bind_method(D_METHOD("set_shadow_smooth", "smooth"), &Light2D::set_shadow_smooth);
  220. ClassDB::bind_method(D_METHOD("get_shadow_smooth"), &Light2D::get_shadow_smooth);
  221. ClassDB::bind_method(D_METHOD("set_shadow_filter", "filter"), &Light2D::set_shadow_filter);
  222. ClassDB::bind_method(D_METHOD("get_shadow_filter"), &Light2D::get_shadow_filter);
  223. ClassDB::bind_method(D_METHOD("set_shadow_color", "shadow_color"), &Light2D::set_shadow_color);
  224. ClassDB::bind_method(D_METHOD("get_shadow_color"), &Light2D::get_shadow_color);
  225. ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &Light2D::set_blend_mode);
  226. ClassDB::bind_method(D_METHOD("get_blend_mode"), &Light2D::get_blend_mode);
  227. ClassDB::bind_method(D_METHOD("set_height", "height"), &Light2D::set_height);
  228. ClassDB::bind_method(D_METHOD("get_height"), &Light2D::get_height);
  229. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  230. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
  231. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  232. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy", PROPERTY_HINT_RANGE, "0,16,0.01,or_greater"), "set_energy", "get_energy");
  233. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Add,Subtract,Mix"), "set_blend_mode", "get_blend_mode");
  234. ADD_GROUP("Range", "range_");
  235. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_min", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_min", "get_z_range_min");
  236. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_max", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_max", "get_z_range_max");
  237. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_min", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_min", "get_layer_range_min");
  238. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_max", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_max", "get_layer_range_max");
  239. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_cull_mask", "get_item_cull_mask");
  240. ADD_GROUP("Shadow", "shadow_");
  241. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_enabled"), "set_shadow_enabled", "is_shadow_enabled");
  242. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
  243. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_filter", PROPERTY_HINT_ENUM, "None (Fast),PCF5 (Average),PCF13 (Slow)"), "set_shadow_filter", "get_shadow_filter");
  244. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "shadow_filter_smooth", PROPERTY_HINT_RANGE, "0,64,0.1"), "set_shadow_smooth", "get_shadow_smooth");
  245. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_shadow_cull_mask", "get_item_shadow_cull_mask");
  246. BIND_ENUM_CONSTANT(SHADOW_FILTER_NONE);
  247. BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF5);
  248. BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF13);
  249. BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
  250. BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
  251. BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
  252. }
  253. Light2D::Light2D() {
  254. canvas_light = RenderingServer::get_singleton()->canvas_light_create();
  255. set_notify_transform(true);
  256. }
  257. Light2D::~Light2D() {
  258. ERR_FAIL_NULL(RenderingServer::get_singleton());
  259. RenderingServer::get_singleton()->free(canvas_light);
  260. }
  261. //////////////////////////////
  262. #ifdef TOOLS_ENABLED
  263. Dictionary PointLight2D::_edit_get_state() const {
  264. Dictionary state = Node2D::_edit_get_state();
  265. state["offset"] = get_texture_offset();
  266. return state;
  267. }
  268. void PointLight2D::_edit_set_state(const Dictionary &p_state) {
  269. Node2D::_edit_set_state(p_state);
  270. set_texture_offset(p_state["offset"]);
  271. }
  272. void PointLight2D::_edit_set_pivot(const Point2 &p_pivot) {
  273. set_position(get_transform().xform(p_pivot));
  274. set_texture_offset(get_texture_offset() - p_pivot);
  275. }
  276. Point2 PointLight2D::_edit_get_pivot() const {
  277. return Vector2();
  278. }
  279. bool PointLight2D::_edit_use_pivot() const {
  280. return true;
  281. }
  282. Rect2 PointLight2D::_edit_get_rect() const {
  283. if (texture.is_null()) {
  284. return Rect2();
  285. }
  286. Size2 s = texture->get_size() * _scale;
  287. return Rect2(texture_offset - s / 2.0, s);
  288. }
  289. bool PointLight2D::_edit_use_rect() const {
  290. return !texture.is_null();
  291. }
  292. #endif
  293. Rect2 PointLight2D::get_anchorable_rect() const {
  294. if (texture.is_null()) {
  295. return Rect2();
  296. }
  297. Size2 s = texture->get_size() * _scale;
  298. return Rect2(texture_offset - s / 2.0, s);
  299. }
  300. void PointLight2D::set_texture(const Ref<Texture2D> &p_texture) {
  301. texture = p_texture;
  302. if (texture.is_valid()) {
  303. RS::get_singleton()->canvas_light_set_texture(_get_light(), texture->get_rid());
  304. } else {
  305. RS::get_singleton()->canvas_light_set_texture(_get_light(), RID());
  306. }
  307. update_configuration_warnings();
  308. }
  309. Ref<Texture2D> PointLight2D::get_texture() const {
  310. return texture;
  311. }
  312. void PointLight2D::set_texture_offset(const Vector2 &p_offset) {
  313. texture_offset = p_offset;
  314. RS::get_singleton()->canvas_light_set_texture_offset(_get_light(), texture_offset);
  315. item_rect_changed();
  316. }
  317. Vector2 PointLight2D::get_texture_offset() const {
  318. return texture_offset;
  319. }
  320. PackedStringArray PointLight2D::get_configuration_warnings() const {
  321. PackedStringArray warnings = Node::get_configuration_warnings();
  322. if (!texture.is_valid()) {
  323. warnings.push_back(RTR("A texture with the shape of the light must be supplied to the \"Texture\" property."));
  324. }
  325. return warnings;
  326. }
  327. void PointLight2D::set_texture_scale(real_t p_scale) {
  328. _scale = p_scale;
  329. // Avoid having 0 scale values, can lead to errors in physics and rendering.
  330. if (_scale == 0) {
  331. _scale = CMP_EPSILON;
  332. }
  333. RS::get_singleton()->canvas_light_set_texture_scale(_get_light(), _scale);
  334. item_rect_changed();
  335. }
  336. real_t PointLight2D::get_texture_scale() const {
  337. return _scale;
  338. }
  339. void PointLight2D::_bind_methods() {
  340. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &PointLight2D::set_texture);
  341. ClassDB::bind_method(D_METHOD("get_texture"), &PointLight2D::get_texture);
  342. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &PointLight2D::set_texture_offset);
  343. ClassDB::bind_method(D_METHOD("get_texture_offset"), &PointLight2D::get_texture_offset);
  344. ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &PointLight2D::set_texture_scale);
  345. ClassDB::bind_method(D_METHOD("get_texture_scale"), &PointLight2D::get_texture_scale);
  346. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  347. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_texture_offset", "get_texture_offset");
  348. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texture_scale", PROPERTY_HINT_RANGE, "0.01,50,0.01"), "set_texture_scale", "get_texture_scale");
  349. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1024,1,or_greater,suffix:px"), "set_height", "get_height");
  350. }
  351. PointLight2D::PointLight2D() {
  352. RS::get_singleton()->canvas_light_set_mode(_get_light(), RS::CANVAS_LIGHT_MODE_POINT);
  353. set_hide_clip_children(true);
  354. }
  355. //////////
  356. void DirectionalLight2D::set_max_distance(real_t p_distance) {
  357. max_distance = p_distance;
  358. RS::get_singleton()->canvas_light_set_directional_distance(_get_light(), max_distance);
  359. }
  360. real_t DirectionalLight2D::get_max_distance() const {
  361. return max_distance;
  362. }
  363. void DirectionalLight2D::_bind_methods() {
  364. ClassDB::bind_method(D_METHOD("set_max_distance", "pixels"), &DirectionalLight2D::set_max_distance);
  365. ClassDB::bind_method(D_METHOD("get_max_distance"), &DirectionalLight2D::get_max_distance);
  366. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_height", "get_height");
  367. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "0,16384.0,1.0,or_greater,suffix:px"), "set_max_distance", "get_max_distance");
  368. }
  369. DirectionalLight2D::DirectionalLight2D() {
  370. RS::get_singleton()->canvas_light_set_mode(_get_light(), RS::CANVAS_LIGHT_MODE_DIRECTIONAL);
  371. set_max_distance(max_distance); // Update RenderingServer.
  372. set_hide_clip_children(true);
  373. }