light_2d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*************************************************************************/
  2. /* light_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "core/engine.h"
  32. #include "servers/visual_server.h"
  33. Dictionary Light2D::_edit_get_state() const {
  34. Dictionary state = Node2D::_edit_get_state();
  35. state["offset"] = get_texture_offset();
  36. return state;
  37. }
  38. void Light2D::_edit_set_state(const Dictionary &p_state) {
  39. Node2D::_edit_set_state(p_state);
  40. set_texture_offset(p_state["offset"]);
  41. }
  42. void Light2D::_edit_set_pivot(const Point2 &p_pivot) {
  43. set_position(get_transform().xform(p_pivot));
  44. set_texture_offset(get_texture_offset() - p_pivot);
  45. }
  46. Point2 Light2D::_edit_get_pivot() const {
  47. return Vector2();
  48. }
  49. bool Light2D::_edit_use_pivot() const {
  50. return true;
  51. }
  52. Rect2 Light2D::_edit_get_rect() const {
  53. if (texture.is_null())
  54. return Rect2();
  55. Size2 s = texture->get_size() * _scale;
  56. return Rect2(texture_offset - s / 2.0, s);
  57. }
  58. bool Light2D::_edit_use_rect() const {
  59. return !texture.is_null();
  60. }
  61. Rect2 Light2D::get_anchorable_rect() const {
  62. if (texture.is_null())
  63. return Rect2();
  64. Size2 s = texture->get_size() * _scale;
  65. return Rect2(texture_offset - s / 2.0, s);
  66. }
  67. void Light2D::_update_light_visibility() {
  68. if (!is_inside_tree())
  69. return;
  70. bool editor_ok = true;
  71. #ifdef TOOLS_ENABLED
  72. if (editor_only) {
  73. if (!Engine::get_singleton()->is_editor_hint()) {
  74. editor_ok = false;
  75. } else {
  76. editor_ok = (get_tree()->get_edited_scene_root() && (this == get_tree()->get_edited_scene_root() || get_owner() == get_tree()->get_edited_scene_root()));
  77. }
  78. }
  79. #else
  80. if (editor_only) {
  81. editor_ok = false;
  82. }
  83. #endif
  84. VS::get_singleton()->canvas_light_set_enabled(canvas_light, enabled && is_visible_in_tree() && editor_ok);
  85. }
  86. void Light2D::set_enabled(bool p_enabled) {
  87. enabled = p_enabled;
  88. _update_light_visibility();
  89. }
  90. bool Light2D::is_enabled() const {
  91. return enabled;
  92. }
  93. void Light2D::set_editor_only(bool p_editor_only) {
  94. editor_only = p_editor_only;
  95. _update_light_visibility();
  96. }
  97. bool Light2D::is_editor_only() const {
  98. return editor_only;
  99. }
  100. void Light2D::set_texture(const Ref<Texture> &p_texture) {
  101. texture = p_texture;
  102. if (texture.is_valid())
  103. VS::get_singleton()->canvas_light_set_texture(canvas_light, texture->get_rid());
  104. else
  105. VS::get_singleton()->canvas_light_set_texture(canvas_light, RID());
  106. update_configuration_warning();
  107. }
  108. Ref<Texture> Light2D::get_texture() const {
  109. return texture;
  110. }
  111. void Light2D::set_texture_offset(const Vector2 &p_offset) {
  112. texture_offset = p_offset;
  113. VS::get_singleton()->canvas_light_set_texture_offset(canvas_light, texture_offset);
  114. item_rect_changed();
  115. _change_notify("offset");
  116. }
  117. Vector2 Light2D::get_texture_offset() const {
  118. return texture_offset;
  119. }
  120. void Light2D::set_color(const Color &p_color) {
  121. color = p_color;
  122. VS::get_singleton()->canvas_light_set_color(canvas_light, color);
  123. }
  124. Color Light2D::get_color() const {
  125. return color;
  126. }
  127. void Light2D::set_height(float p_height) {
  128. height = p_height;
  129. VS::get_singleton()->canvas_light_set_height(canvas_light, height);
  130. }
  131. float Light2D::get_height() const {
  132. return height;
  133. }
  134. void Light2D::set_energy(float p_energy) {
  135. energy = p_energy;
  136. VS::get_singleton()->canvas_light_set_energy(canvas_light, energy);
  137. }
  138. float Light2D::get_energy() const {
  139. return energy;
  140. }
  141. void Light2D::set_texture_scale(float p_scale) {
  142. _scale = p_scale;
  143. VS::get_singleton()->canvas_light_set_scale(canvas_light, _scale);
  144. item_rect_changed();
  145. }
  146. float Light2D::get_texture_scale() const {
  147. return _scale;
  148. }
  149. void Light2D::set_z_range_min(int p_min_z) {
  150. z_min = p_min_z;
  151. VS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
  152. }
  153. int Light2D::get_z_range_min() const {
  154. return z_min;
  155. }
  156. void Light2D::set_z_range_max(int p_max_z) {
  157. z_max = p_max_z;
  158. VS::get_singleton()->canvas_light_set_z_range(canvas_light, z_min, z_max);
  159. }
  160. int Light2D::get_z_range_max() const {
  161. return z_max;
  162. }
  163. void Light2D::set_layer_range_min(int p_min_layer) {
  164. layer_min = p_min_layer;
  165. VS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
  166. }
  167. int Light2D::get_layer_range_min() const {
  168. return layer_min;
  169. }
  170. void Light2D::set_layer_range_max(int p_max_layer) {
  171. layer_max = p_max_layer;
  172. VS::get_singleton()->canvas_light_set_layer_range(canvas_light, layer_min, layer_max);
  173. }
  174. int Light2D::get_layer_range_max() const {
  175. return layer_max;
  176. }
  177. void Light2D::set_item_cull_mask(int p_mask) {
  178. item_mask = p_mask;
  179. VS::get_singleton()->canvas_light_set_item_cull_mask(canvas_light, item_mask);
  180. }
  181. int Light2D::get_item_cull_mask() const {
  182. return item_mask;
  183. }
  184. void Light2D::set_item_shadow_cull_mask(int p_mask) {
  185. item_shadow_mask = p_mask;
  186. VS::get_singleton()->canvas_light_set_item_shadow_cull_mask(canvas_light, item_shadow_mask);
  187. }
  188. int Light2D::get_item_shadow_cull_mask() const {
  189. return item_shadow_mask;
  190. }
  191. void Light2D::set_mode(Mode p_mode) {
  192. mode = p_mode;
  193. VS::get_singleton()->canvas_light_set_mode(canvas_light, VS::CanvasLightMode(p_mode));
  194. }
  195. Light2D::Mode Light2D::get_mode() const {
  196. return mode;
  197. }
  198. void Light2D::set_shadow_enabled(bool p_enabled) {
  199. shadow = p_enabled;
  200. VS::get_singleton()->canvas_light_set_shadow_enabled(canvas_light, shadow);
  201. }
  202. bool Light2D::is_shadow_enabled() const {
  203. return shadow;
  204. }
  205. void Light2D::set_shadow_buffer_size(int p_size) {
  206. shadow_buffer_size = p_size;
  207. VS::get_singleton()->canvas_light_set_shadow_buffer_size(canvas_light, shadow_buffer_size);
  208. }
  209. int Light2D::get_shadow_buffer_size() const {
  210. return shadow_buffer_size;
  211. }
  212. void Light2D::set_shadow_gradient_length(float p_multiplier) {
  213. shadow_gradient_length = p_multiplier;
  214. VS::get_singleton()->canvas_light_set_shadow_gradient_length(canvas_light, p_multiplier);
  215. }
  216. float Light2D::get_shadow_gradient_length() const {
  217. return shadow_gradient_length;
  218. }
  219. void Light2D::set_shadow_filter(ShadowFilter p_filter) {
  220. shadow_filter = p_filter;
  221. VS::get_singleton()->canvas_light_set_shadow_filter(canvas_light, VS::CanvasLightShadowFilter(p_filter));
  222. }
  223. Light2D::ShadowFilter Light2D::get_shadow_filter() const {
  224. return shadow_filter;
  225. }
  226. void Light2D::set_shadow_color(const Color &p_shadow_color) {
  227. shadow_color = p_shadow_color;
  228. VS::get_singleton()->canvas_light_set_shadow_color(canvas_light, shadow_color);
  229. }
  230. Color Light2D::get_shadow_color() const {
  231. return shadow_color;
  232. }
  233. void Light2D::_notification(int p_what) {
  234. if (p_what == NOTIFICATION_ENTER_TREE) {
  235. VS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, get_canvas());
  236. _update_light_visibility();
  237. }
  238. if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  239. VS::get_singleton()->canvas_light_set_transform(canvas_light, get_global_transform());
  240. }
  241. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  242. _update_light_visibility();
  243. }
  244. if (p_what == NOTIFICATION_EXIT_TREE) {
  245. VS::get_singleton()->canvas_light_attach_to_canvas(canvas_light, RID());
  246. _update_light_visibility();
  247. }
  248. }
  249. String Light2D::get_configuration_warning() const {
  250. if (!texture.is_valid()) {
  251. return TTR("A texture with the shape of the light must be supplied to the 'texture' property.");
  252. }
  253. return String();
  254. }
  255. void Light2D::set_shadow_smooth(float p_amount) {
  256. shadow_smooth = p_amount;
  257. VS::get_singleton()->canvas_light_set_shadow_smooth(canvas_light, shadow_smooth);
  258. }
  259. float Light2D::get_shadow_smooth() const {
  260. return shadow_smooth;
  261. }
  262. void Light2D::_bind_methods() {
  263. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &Light2D::set_enabled);
  264. ClassDB::bind_method(D_METHOD("is_enabled"), &Light2D::is_enabled);
  265. ClassDB::bind_method(D_METHOD("set_editor_only", "editor_only"), &Light2D::set_editor_only);
  266. ClassDB::bind_method(D_METHOD("is_editor_only"), &Light2D::is_editor_only);
  267. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Light2D::set_texture);
  268. ClassDB::bind_method(D_METHOD("get_texture"), &Light2D::get_texture);
  269. ClassDB::bind_method(D_METHOD("set_texture_offset", "texture_offset"), &Light2D::set_texture_offset);
  270. ClassDB::bind_method(D_METHOD("get_texture_offset"), &Light2D::get_texture_offset);
  271. ClassDB::bind_method(D_METHOD("set_color", "color"), &Light2D::set_color);
  272. ClassDB::bind_method(D_METHOD("get_color"), &Light2D::get_color);
  273. ClassDB::bind_method(D_METHOD("set_height", "height"), &Light2D::set_height);
  274. ClassDB::bind_method(D_METHOD("get_height"), &Light2D::get_height);
  275. ClassDB::bind_method(D_METHOD("set_energy", "energy"), &Light2D::set_energy);
  276. ClassDB::bind_method(D_METHOD("get_energy"), &Light2D::get_energy);
  277. ClassDB::bind_method(D_METHOD("set_texture_scale", "texture_scale"), &Light2D::set_texture_scale);
  278. ClassDB::bind_method(D_METHOD("get_texture_scale"), &Light2D::get_texture_scale);
  279. ClassDB::bind_method(D_METHOD("set_z_range_min", "z"), &Light2D::set_z_range_min);
  280. ClassDB::bind_method(D_METHOD("get_z_range_min"), &Light2D::get_z_range_min);
  281. ClassDB::bind_method(D_METHOD("set_z_range_max", "z"), &Light2D::set_z_range_max);
  282. ClassDB::bind_method(D_METHOD("get_z_range_max"), &Light2D::get_z_range_max);
  283. ClassDB::bind_method(D_METHOD("set_layer_range_min", "layer"), &Light2D::set_layer_range_min);
  284. ClassDB::bind_method(D_METHOD("get_layer_range_min"), &Light2D::get_layer_range_min);
  285. ClassDB::bind_method(D_METHOD("set_layer_range_max", "layer"), &Light2D::set_layer_range_max);
  286. ClassDB::bind_method(D_METHOD("get_layer_range_max"), &Light2D::get_layer_range_max);
  287. ClassDB::bind_method(D_METHOD("set_item_cull_mask", "item_cull_mask"), &Light2D::set_item_cull_mask);
  288. ClassDB::bind_method(D_METHOD("get_item_cull_mask"), &Light2D::get_item_cull_mask);
  289. ClassDB::bind_method(D_METHOD("set_item_shadow_cull_mask", "item_shadow_cull_mask"), &Light2D::set_item_shadow_cull_mask);
  290. ClassDB::bind_method(D_METHOD("get_item_shadow_cull_mask"), &Light2D::get_item_shadow_cull_mask);
  291. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Light2D::set_mode);
  292. ClassDB::bind_method(D_METHOD("get_mode"), &Light2D::get_mode);
  293. ClassDB::bind_method(D_METHOD("set_shadow_enabled", "enabled"), &Light2D::set_shadow_enabled);
  294. ClassDB::bind_method(D_METHOD("is_shadow_enabled"), &Light2D::is_shadow_enabled);
  295. ClassDB::bind_method(D_METHOD("set_shadow_buffer_size", "size"), &Light2D::set_shadow_buffer_size);
  296. ClassDB::bind_method(D_METHOD("get_shadow_buffer_size"), &Light2D::get_shadow_buffer_size);
  297. ClassDB::bind_method(D_METHOD("set_shadow_smooth", "smooth"), &Light2D::set_shadow_smooth);
  298. ClassDB::bind_method(D_METHOD("get_shadow_smooth"), &Light2D::get_shadow_smooth);
  299. ClassDB::bind_method(D_METHOD("set_shadow_gradient_length", "multiplier"), &Light2D::set_shadow_gradient_length);
  300. ClassDB::bind_method(D_METHOD("get_shadow_gradient_length"), &Light2D::get_shadow_gradient_length);
  301. ClassDB::bind_method(D_METHOD("set_shadow_filter", "filter"), &Light2D::set_shadow_filter);
  302. ClassDB::bind_method(D_METHOD("get_shadow_filter"), &Light2D::get_shadow_filter);
  303. ClassDB::bind_method(D_METHOD("set_shadow_color", "shadow_color"), &Light2D::set_shadow_color);
  304. ClassDB::bind_method(D_METHOD("get_shadow_color"), &Light2D::get_shadow_color);
  305. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  306. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
  307. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "StreamTexture,ImageTexture"), "set_texture", "get_texture");
  308. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_texture_offset", "get_texture_offset");
  309. ADD_PROPERTY(PropertyInfo(Variant::REAL, "texture_scale", PROPERTY_HINT_RANGE, "0.01,50,0.01"), "set_texture_scale", "get_texture_scale");
  310. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  311. ADD_PROPERTY(PropertyInfo(Variant::REAL, "energy", PROPERTY_HINT_RANGE, "0.01,100,0.01"), "set_energy", "get_energy");
  312. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Add,Sub,Mix,Mask"), "set_mode", "get_mode");
  313. ADD_GROUP("Range", "range_");
  314. ADD_PROPERTY(PropertyInfo(Variant::REAL, "range_height", PROPERTY_HINT_RANGE, "-100,100,0.1"), "set_height", "get_height");
  315. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_min", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_min", "get_z_range_min");
  316. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_z_max", PROPERTY_HINT_RANGE, itos(VS::CANVAS_ITEM_Z_MIN) + "," + itos(VS::CANVAS_ITEM_Z_MAX) + ",1"), "set_z_range_max", "get_z_range_max");
  317. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_min", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_min", "get_layer_range_min");
  318. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_layer_max", PROPERTY_HINT_RANGE, "-512,512,1"), "set_layer_range_max", "get_layer_range_max");
  319. ADD_PROPERTY(PropertyInfo(Variant::INT, "range_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_cull_mask", "get_item_cull_mask");
  320. ADD_GROUP("Shadow", "shadow_");
  321. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shadow_enabled"), "set_shadow_enabled", "is_shadow_enabled");
  322. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
  323. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_buffer_size", PROPERTY_HINT_RANGE, "32,16384,1"), "set_shadow_buffer_size", "get_shadow_buffer_size");
  324. ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_gradient_length", PROPERTY_HINT_RANGE, "0,4096,0.1"), "set_shadow_gradient_length", "get_shadow_gradient_length");
  325. ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_filter", PROPERTY_HINT_ENUM, "None,PCF3,PCF5,PCF7,PCF9,PCF13"), "set_shadow_filter", "get_shadow_filter");
  326. ADD_PROPERTY(PropertyInfo(Variant::REAL, "shadow_filter_smooth", PROPERTY_HINT_RANGE, "0,64,0.1"), "set_shadow_smooth", "get_shadow_smooth");
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_item_cull_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_item_shadow_cull_mask", "get_item_shadow_cull_mask");
  328. BIND_ENUM_CONSTANT(MODE_ADD);
  329. BIND_ENUM_CONSTANT(MODE_SUB);
  330. BIND_ENUM_CONSTANT(MODE_MIX);
  331. BIND_ENUM_CONSTANT(MODE_MASK);
  332. BIND_ENUM_CONSTANT(SHADOW_FILTER_NONE);
  333. BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF3);
  334. BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF5);
  335. BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF7);
  336. BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF9);
  337. BIND_ENUM_CONSTANT(SHADOW_FILTER_PCF13);
  338. }
  339. Light2D::Light2D() {
  340. canvas_light = VisualServer::get_singleton()->canvas_light_create();
  341. enabled = true;
  342. editor_only = false;
  343. shadow = false;
  344. color = Color(1, 1, 1);
  345. height = 0;
  346. _scale = 1.0;
  347. z_min = -1024;
  348. z_max = 1024;
  349. layer_min = 0;
  350. layer_max = 0;
  351. item_mask = 1;
  352. item_shadow_mask = 1;
  353. mode = MODE_ADD;
  354. shadow_buffer_size = 2048;
  355. shadow_gradient_length = 0;
  356. energy = 1.0;
  357. shadow_color = Color(0, 0, 0, 0);
  358. shadow_filter = SHADOW_FILTER_NONE;
  359. shadow_smooth = 0;
  360. set_notify_transform(true);
  361. }
  362. Light2D::~Light2D() {
  363. VisualServer::get_singleton()->free(canvas_light);
  364. }