light_occluder_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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::_physics_interpolated_changed() {
  139. RenderingServer::get_singleton()->canvas_light_occluder_set_interpolated(occluder, is_physics_interpolated());
  140. }
  141. void LightOccluder2D::_notification(int p_what) {
  142. switch (p_what) {
  143. case NOTIFICATION_ENTER_CANVAS: {
  144. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
  145. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  146. RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  147. } break;
  148. case NOTIFICATION_TRANSFORM_CHANGED: {
  149. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  150. } break;
  151. case NOTIFICATION_VISIBILITY_CHANGED: {
  152. RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
  153. } break;
  154. case NOTIFICATION_DRAW: {
  155. if (Engine::get_singleton()->is_editor_hint()) {
  156. if (occluder_polygon.is_valid()) {
  157. Vector<Vector2> poly = occluder_polygon->get_polygon();
  158. if (poly.size()) {
  159. if (occluder_polygon->is_closed()) {
  160. Vector<Color> color;
  161. color.push_back(Color(0, 0, 0, 0.6));
  162. draw_polygon(Variant(poly), color);
  163. } else {
  164. int ps = poly.size();
  165. const Vector2 *r = poly.ptr();
  166. for (int i = 0; i < ps - 1; i++) {
  167. draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. } break;
  174. case NOTIFICATION_EXIT_CANVAS: {
  175. RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
  176. } break;
  177. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  178. if (is_visible_in_tree() && is_physics_interpolated()) {
  179. // Explicitly make sure the transform is up to date in RenderingServer before
  180. // resetting. This is necessary because NOTIFICATION_TRANSFORM_CHANGED
  181. // is normally deferred, and a client change to transform will not always be sent
  182. // before the reset, so we need to guarantee this.
  183. RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
  184. RS::get_singleton()->canvas_light_occluder_reset_physics_interpolation(occluder);
  185. }
  186. } break;
  187. }
  188. }
  189. #ifdef TOOLS_ENABLED
  190. Rect2 LightOccluder2D::_edit_get_rect() const {
  191. return occluder_polygon.is_valid() ? occluder_polygon->_edit_get_rect() : Rect2();
  192. }
  193. bool LightOccluder2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  194. return occluder_polygon.is_valid() ? occluder_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  195. }
  196. #endif
  197. void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
  198. #ifdef DEBUG_ENABLED
  199. if (occluder_polygon.is_valid()) {
  200. occluder_polygon->disconnect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
  201. }
  202. #endif
  203. occluder_polygon = p_polygon;
  204. if (occluder_polygon.is_valid()) {
  205. RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
  206. } else {
  207. RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
  208. }
  209. #ifdef DEBUG_ENABLED
  210. if (occluder_polygon.is_valid()) {
  211. occluder_polygon->connect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
  212. }
  213. queue_redraw();
  214. #endif
  215. }
  216. Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
  217. return occluder_polygon;
  218. }
  219. void LightOccluder2D::set_occluder_light_mask(int p_mask) {
  220. mask = p_mask;
  221. RS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, p_mask);
  222. }
  223. int LightOccluder2D::get_occluder_light_mask() const {
  224. return mask;
  225. }
  226. PackedStringArray LightOccluder2D::get_configuration_warnings() const {
  227. PackedStringArray warnings = Node2D::get_configuration_warnings();
  228. if (!occluder_polygon.is_valid()) {
  229. warnings.push_back(RTR("An occluder polygon must be set (or drawn) for this occluder to take effect."));
  230. }
  231. if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().size() == 0) {
  232. warnings.push_back(RTR("The occluder polygon for this occluder is empty. Please draw a polygon."));
  233. }
  234. return warnings;
  235. }
  236. void LightOccluder2D::set_as_sdf_collision(bool p_enable) {
  237. sdf_collision = p_enable;
  238. RS::get_singleton()->canvas_light_occluder_set_as_sdf_collision(occluder, sdf_collision);
  239. }
  240. bool LightOccluder2D::is_set_as_sdf_collision() const {
  241. return sdf_collision;
  242. }
  243. void LightOccluder2D::_bind_methods() {
  244. ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
  245. ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
  246. ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
  247. ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
  248. ClassDB::bind_method(D_METHOD("set_as_sdf_collision", "enable"), &LightOccluder2D::set_as_sdf_collision);
  249. ClassDB::bind_method(D_METHOD("is_set_as_sdf_collision"), &LightOccluder2D::is_set_as_sdf_collision);
  250. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
  251. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sdf_collision"), "set_as_sdf_collision", "is_set_as_sdf_collision");
  252. ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
  253. }
  254. LightOccluder2D::LightOccluder2D() {
  255. occluder = RS::get_singleton()->canvas_light_occluder_create();
  256. set_notify_transform(true);
  257. set_as_sdf_collision(true);
  258. }
  259. LightOccluder2D::~LightOccluder2D() {
  260. ERR_FAIL_NULL(RenderingServer::get_singleton());
  261. RS::get_singleton()->free(occluder);
  262. }