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