light_occluder_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**************************************************************************/
  2. /* light_occluder_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_occluder_2d.h"
  31. #include "core/config/engine.h"
  32. #include "core/math/geometry_2d.h"
  33. #define LINE_GRAB_WIDTH 8
  34. #ifdef TOOLS_ENABLED
  35. Rect2 OccluderPolygon2D::_edit_get_rect() const {
  36. if (rect_cache_dirty) {
  37. if (closed) {
  38. const Vector2 *r = polygon.ptr();
  39. item_rect = Rect2();
  40. for (int i = 0; i < polygon.size(); i++) {
  41. Vector2 pos = r[i];
  42. if (i == 0) {
  43. item_rect.position = pos;
  44. } else {
  45. item_rect.expand_to(pos);
  46. }
  47. }
  48. rect_cache_dirty = false;
  49. } else {
  50. if (polygon.size() == 0) {
  51. item_rect = Rect2();
  52. } else {
  53. Vector2 d = Vector2(LINE_GRAB_WIDTH, LINE_GRAB_WIDTH);
  54. item_rect = Rect2(polygon[0] - d, 2 * d);
  55. for (int i = 1; i < polygon.size(); i++) {
  56. item_rect.expand_to(polygon[i] - d);
  57. item_rect.expand_to(polygon[i] + d);
  58. }
  59. }
  60. }
  61. }
  62. return item_rect;
  63. }
  64. bool OccluderPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  65. if (closed) {
  66. return Geometry2D::is_point_in_polygon(p_point, Variant(polygon));
  67. } else {
  68. const real_t d = LINE_GRAB_WIDTH / 2 + p_tolerance;
  69. const Vector2 *points = polygon.ptr();
  70. for (int i = 0; i < polygon.size() - 1; i++) {
  71. Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, &points[i]);
  72. if (p.distance_to(p_point) <= d) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. }
  79. #endif
  80. void OccluderPolygon2D::set_polygon(const Vector<Vector2> &p_polygon) {
  81. polygon = p_polygon;
  82. rect_cache_dirty = true;
  83. RS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, p_polygon, closed);
  84. emit_changed();
  85. }
  86. Vector<Vector2> OccluderPolygon2D::get_polygon() const {
  87. return polygon;
  88. }
  89. void OccluderPolygon2D::set_closed(bool p_closed) {
  90. if (closed == p_closed) {
  91. return;
  92. }
  93. closed = p_closed;
  94. if (polygon.size()) {
  95. RS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, polygon, closed);
  96. }
  97. emit_changed();
  98. }
  99. bool OccluderPolygon2D::is_closed() const {
  100. return closed;
  101. }
  102. void OccluderPolygon2D::set_cull_mode(CullMode p_mode) {
  103. cull = p_mode;
  104. RS::get_singleton()->canvas_occluder_polygon_set_cull_mode(occ_polygon, RS::CanvasOccluderPolygonCullMode(p_mode));
  105. }
  106. OccluderPolygon2D::CullMode OccluderPolygon2D::get_cull_mode() const {
  107. return cull;
  108. }
  109. RID OccluderPolygon2D::get_rid() const {
  110. return occ_polygon;
  111. }
  112. void OccluderPolygon2D::_bind_methods() {
  113. ClassDB::bind_method(D_METHOD("set_closed", "closed"), &OccluderPolygon2D::set_closed);
  114. ClassDB::bind_method(D_METHOD("is_closed"), &OccluderPolygon2D::is_closed);
  115. ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &OccluderPolygon2D::set_cull_mode);
  116. ClassDB::bind_method(D_METHOD("get_cull_mode"), &OccluderPolygon2D::get_cull_mode);
  117. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &OccluderPolygon2D::set_polygon);
  118. ClassDB::bind_method(D_METHOD("get_polygon"), &OccluderPolygon2D::get_polygon);
  119. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
  120. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Disabled,ClockWise,CounterClockWise"), "set_cull_mode", "get_cull_mode");
  121. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  122. BIND_ENUM_CONSTANT(CULL_DISABLED);
  123. BIND_ENUM_CONSTANT(CULL_CLOCKWISE);
  124. BIND_ENUM_CONSTANT(CULL_COUNTER_CLOCKWISE);
  125. }
  126. OccluderPolygon2D::OccluderPolygon2D() {
  127. occ_polygon = RS::get_singleton()->canvas_occluder_polygon_create();
  128. }
  129. OccluderPolygon2D::~OccluderPolygon2D() {
  130. ERR_FAIL_NULL(RenderingServer::get_singleton());
  131. RS::get_singleton()->free(occ_polygon);
  132. }
  133. void LightOccluder2D::_poly_changed() {
  134. #ifdef DEBUG_ENABLED
  135. queue_redraw();
  136. #endif
  137. }
  138. void LightOccluder2D::_notification(int p_what) {
  139. switch (p_what) {
  140. case NOTIFICATION_ENTER_CANVAS: {
  141. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
  142. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  143. RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  144. } break;
  145. case NOTIFICATION_TRANSFORM_CHANGED: {
  146. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  147. } break;
  148. case NOTIFICATION_VISIBILITY_CHANGED: {
  149. RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  150. } break;
  151. case NOTIFICATION_DRAW: {
  152. if (Engine::get_singleton()->is_editor_hint()) {
  153. if (occluder_polygon.is_valid()) {
  154. Vector<Vector2> poly = occluder_polygon->get_polygon();
  155. if (poly.size()) {
  156. if (occluder_polygon->is_closed()) {
  157. Vector<Color> color;
  158. color.push_back(Color(0, 0, 0, 0.6));
  159. draw_polygon(Variant(poly), color);
  160. } else {
  161. int ps = poly.size();
  162. const Vector2 *r = poly.ptr();
  163. for (int i = 0; i < ps - 1; i++) {
  164. draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. } break;
  171. case NOTIFICATION_EXIT_CANVAS: {
  172. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
  173. } break;
  174. }
  175. }
  176. #ifdef TOOLS_ENABLED
  177. Rect2 LightOccluder2D::_edit_get_rect() const {
  178. return occluder_polygon.is_valid() ? occluder_polygon->_edit_get_rect() : Rect2();
  179. }
  180. bool LightOccluder2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  181. return occluder_polygon.is_valid() ? occluder_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  182. }
  183. #endif
  184. void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
  185. #ifdef DEBUG_ENABLED
  186. if (occluder_polygon.is_valid()) {
  187. occluder_polygon->disconnect("changed", callable_mp(this, &LightOccluder2D::_poly_changed));
  188. }
  189. #endif
  190. occluder_polygon = p_polygon;
  191. if (occluder_polygon.is_valid()) {
  192. RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
  193. } else {
  194. RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
  195. }
  196. #ifdef DEBUG_ENABLED
  197. if (occluder_polygon.is_valid()) {
  198. occluder_polygon->connect("changed", callable_mp(this, &LightOccluder2D::_poly_changed));
  199. }
  200. queue_redraw();
  201. #endif
  202. }
  203. Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
  204. return occluder_polygon;
  205. }
  206. void LightOccluder2D::set_occluder_light_mask(int p_mask) {
  207. mask = p_mask;
  208. RS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, p_mask);
  209. }
  210. int LightOccluder2D::get_occluder_light_mask() const {
  211. return mask;
  212. }
  213. PackedStringArray LightOccluder2D::get_configuration_warnings() const {
  214. PackedStringArray warnings = Node::get_configuration_warnings();
  215. if (!occluder_polygon.is_valid()) {
  216. warnings.push_back(RTR("An occluder polygon must be set (or drawn) for this occluder to take effect."));
  217. }
  218. if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().size() == 0) {
  219. warnings.push_back(RTR("The occluder polygon for this occluder is empty. Please draw a polygon."));
  220. }
  221. return warnings;
  222. }
  223. void LightOccluder2D::set_as_sdf_collision(bool p_enable) {
  224. sdf_collision = p_enable;
  225. RS::get_singleton()->canvas_light_occluder_set_as_sdf_collision(occluder, sdf_collision);
  226. }
  227. bool LightOccluder2D::is_set_as_sdf_collision() const {
  228. return sdf_collision;
  229. }
  230. void LightOccluder2D::_bind_methods() {
  231. ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
  232. ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
  233. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
  234. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
  235. ClassDB::bind_method(D_METHOD("set_as_sdf_collision", "enable"), &LightOccluder2D::set_as_sdf_collision);
  236. ClassDB::bind_method(D_METHOD("is_set_as_sdf_collision"), &LightOccluder2D::is_set_as_sdf_collision);
  237. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
  238. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sdf_collision"), "set_as_sdf_collision", "is_set_as_sdf_collision");
  239. ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  240. }
  241. LightOccluder2D::LightOccluder2D() {
  242. occluder = RS::get_singleton()->canvas_light_occluder_create();
  243. set_notify_transform(true);
  244. set_as_sdf_collision(true);
  245. }
  246. LightOccluder2D::~LightOccluder2D() {
  247. ERR_FAIL_NULL(RenderingServer::get_singleton());
  248. RS::get_singleton()->free(occluder);
  249. }