light_occluder_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 DEBUG_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 // DEBUG_ENABLED
  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. update_configuration_warning();
  86. }
  87. Vector<Vector2> OccluderPolygon2D::get_polygon() const {
  88. return polygon;
  89. }
  90. void OccluderPolygon2D::set_closed(bool p_closed) {
  91. if (closed == p_closed) {
  92. return;
  93. }
  94. closed = p_closed;
  95. if (polygon.size()) {
  96. RS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, polygon, closed);
  97. }
  98. emit_changed();
  99. }
  100. bool OccluderPolygon2D::is_closed() const {
  101. return closed;
  102. }
  103. void OccluderPolygon2D::set_cull_mode(CullMode p_mode) {
  104. cull = p_mode;
  105. RS::get_singleton()->canvas_occluder_polygon_set_cull_mode(occ_polygon, RS::CanvasOccluderPolygonCullMode(p_mode));
  106. }
  107. OccluderPolygon2D::CullMode OccluderPolygon2D::get_cull_mode() const {
  108. return cull;
  109. }
  110. RID OccluderPolygon2D::get_rid() const {
  111. return occ_polygon;
  112. }
  113. void OccluderPolygon2D::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("set_closed", "closed"), &OccluderPolygon2D::set_closed);
  115. ClassDB::bind_method(D_METHOD("is_closed"), &OccluderPolygon2D::is_closed);
  116. ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &OccluderPolygon2D::set_cull_mode);
  117. ClassDB::bind_method(D_METHOD("get_cull_mode"), &OccluderPolygon2D::get_cull_mode);
  118. ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &OccluderPolygon2D::set_polygon);
  119. ClassDB::bind_method(D_METHOD("get_polygon"), &OccluderPolygon2D::get_polygon);
  120. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
  121. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Disabled,ClockWise,CounterClockWise"), "set_cull_mode", "get_cull_mode");
  122. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
  123. BIND_ENUM_CONSTANT(CULL_DISABLED);
  124. BIND_ENUM_CONSTANT(CULL_CLOCKWISE);
  125. BIND_ENUM_CONSTANT(CULL_COUNTER_CLOCKWISE);
  126. }
  127. OccluderPolygon2D::OccluderPolygon2D() {
  128. occ_polygon = RS::get_singleton()->canvas_occluder_polygon_create();
  129. }
  130. OccluderPolygon2D::~OccluderPolygon2D() {
  131. ERR_FAIL_NULL(RenderingServer::get_singleton());
  132. RS::get_singleton()->free(occ_polygon);
  133. }
  134. void LightOccluder2D::_poly_changed() {
  135. #ifdef DEBUG_ENABLED
  136. queue_redraw();
  137. #endif // DEBUG_ENABLED
  138. }
  139. void LightOccluder2D::_physics_interpolated_changed() {
  140. RenderingServer::get_singleton()->canvas_light_occluder_set_interpolated(occluder, is_physics_interpolated());
  141. }
  142. void LightOccluder2D::_notification(int p_what) {
  143. switch (p_what) {
  144. case NOTIFICATION_ENTER_CANVAS: {
  145. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
  146. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  147. RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  148. } break;
  149. case NOTIFICATION_TRANSFORM_CHANGED: {
  150. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  151. } break;
  152. case NOTIFICATION_VISIBILITY_CHANGED: {
  153. RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  154. } break;
  155. case NOTIFICATION_DRAW: {
  156. if (Engine::get_singleton()->is_editor_hint()) {
  157. if (occluder_polygon.is_valid()) {
  158. Vector<Vector2> poly = occluder_polygon->get_polygon();
  159. if (poly.size()) {
  160. if (occluder_polygon->is_closed()) {
  161. Vector<Color> color;
  162. color.push_back(Color(0, 0, 0, 0.6));
  163. draw_polygon(Variant(poly), color);
  164. } else {
  165. int ps = poly.size();
  166. const Vector2 *r = poly.ptr();
  167. for (int i = 0; i < ps - 1; i++) {
  168. draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. } break;
  175. case NOTIFICATION_EXIT_CANVAS: {
  176. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
  177. } break;
  178. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  179. if (is_visible_in_tree() && is_physics_interpolated()) {
  180. // Explicitly make sure the transform is up to date in RenderingServer before
  181. // resetting. This is necessary because NOTIFICATION_TRANSFORM_CHANGED
  182. // is normally deferred, and a client change to transform will not always be sent
  183. // before the reset, so we need to guarantee this.
  184. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  185. RS::get_singleton()->canvas_light_occluder_reset_physics_interpolation(occluder);
  186. }
  187. } break;
  188. }
  189. }
  190. #ifdef DEBUG_ENABLED
  191. Rect2 LightOccluder2D::_edit_get_rect() const {
  192. return occluder_polygon.is_valid() ? occluder_polygon->_edit_get_rect() : Rect2();
  193. }
  194. bool LightOccluder2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  195. return occluder_polygon.is_valid() ? occluder_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  196. }
  197. #endif // DEBUG_ENABLED
  198. void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
  199. #ifdef DEBUG_ENABLED
  200. if (occluder_polygon.is_valid()) {
  201. occluder_polygon->disconnect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
  202. }
  203. #endif // DEBUG_ENABLED
  204. occluder_polygon = p_polygon;
  205. if (occluder_polygon.is_valid()) {
  206. RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
  207. } else {
  208. RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
  209. }
  210. #ifdef DEBUG_ENABLED
  211. if (occluder_polygon.is_valid()) {
  212. occluder_polygon->connect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
  213. }
  214. queue_redraw();
  215. #endif // DEBUG_ENABLED
  216. }
  217. Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
  218. return occluder_polygon;
  219. }
  220. void LightOccluder2D::set_occluder_light_mask(int p_mask) {
  221. mask = p_mask;
  222. RS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, p_mask);
  223. }
  224. int LightOccluder2D::get_occluder_light_mask() const {
  225. return mask;
  226. }
  227. PackedStringArray LightOccluder2D::get_configuration_warnings() const {
  228. PackedStringArray warnings = Node2D::get_configuration_warnings();
  229. if (!occluder_polygon.is_valid()) {
  230. warnings.push_back(RTR("An occluder polygon must be set (or drawn) for this occluder to take effect."));
  231. }
  232. if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().size() == 0) {
  233. warnings.push_back(RTR("The occluder polygon for this occluder is empty. Please draw a polygon."));
  234. }
  235. return warnings;
  236. }
  237. void LightOccluder2D::set_as_sdf_collision(bool p_enable) {
  238. sdf_collision = p_enable;
  239. RS::get_singleton()->canvas_light_occluder_set_as_sdf_collision(occluder, sdf_collision);
  240. }
  241. bool LightOccluder2D::is_set_as_sdf_collision() const {
  242. return sdf_collision;
  243. }
  244. void LightOccluder2D::_bind_methods() {
  245. ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
  246. ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
  247. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
  248. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
  249. ClassDB::bind_method(D_METHOD("set_as_sdf_collision", "enable"), &LightOccluder2D::set_as_sdf_collision);
  250. ClassDB::bind_method(D_METHOD("is_set_as_sdf_collision"), &LightOccluder2D::is_set_as_sdf_collision);
  251. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
  252. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sdf_collision"), "set_as_sdf_collision", "is_set_as_sdf_collision");
  253. ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  254. }
  255. LightOccluder2D::LightOccluder2D() {
  256. occluder = RS::get_singleton()->canvas_light_occluder_create();
  257. set_notify_transform(true);
  258. set_as_sdf_collision(true);
  259. }
  260. LightOccluder2D::~LightOccluder2D() {
  261. ERR_FAIL_NULL(RenderingServer::get_singleton());
  262. RS::get_singleton()->free(occluder);
  263. }