2016-06-18 20:46:12 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* light_occluder_2d.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2015-03-02 11:54:10 +08:00
|
|
|
#include "light_occluder_2d.h"
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/engine.h"
|
2022-02-16 01:06:48 +08:00
|
|
|
#include "core/math/geometry_2d.h"
|
2017-08-19 07:02:56 +08:00
|
|
|
|
2018-12-05 12:41:29 +08:00
|
|
|
#define LINE_GRAB_WIDTH 8
|
2019-10-22 05:37:07 +08:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2018-12-05 12:41:29 +08:00
|
|
|
Rect2 OccluderPolygon2D::_edit_get_rect() const {
|
|
|
|
if (rect_cache_dirty) {
|
|
|
|
if (closed) {
|
2020-02-18 05:06:54 +08:00
|
|
|
const Vector2 *r = polygon.ptr();
|
2018-12-05 12:41:29 +08:00
|
|
|
item_rect = Rect2();
|
|
|
|
for (int i = 0; i < polygon.size(); i++) {
|
|
|
|
Vector2 pos = r[i];
|
2020-05-14 22:41:43 +08:00
|
|
|
if (i == 0) {
|
2018-12-05 12:41:29 +08:00
|
|
|
item_rect.position = pos;
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2018-12-05 12:41:29 +08:00
|
|
|
item_rect.expand_to(pos);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-12-05 12:41:29 +08:00
|
|
|
}
|
|
|
|
rect_cache_dirty = false;
|
|
|
|
} else {
|
|
|
|
if (polygon.size() == 0) {
|
|
|
|
item_rect = Rect2();
|
|
|
|
} else {
|
|
|
|
Vector2 d = Vector2(LINE_GRAB_WIDTH, LINE_GRAB_WIDTH);
|
|
|
|
item_rect = Rect2(polygon[0] - d, 2 * d);
|
|
|
|
for (int i = 1; i < polygon.size(); i++) {
|
|
|
|
item_rect.expand_to(polygon[i] - d);
|
|
|
|
item_rect.expand_to(polygon[i] + d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return item_rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OccluderPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
|
|
|
|
if (closed) {
|
2020-05-26 01:20:45 +08:00
|
|
|
return Geometry2D::is_point_in_polygon(p_point, Variant(polygon));
|
2018-12-05 12:41:29 +08:00
|
|
|
} else {
|
|
|
|
const real_t d = LINE_GRAB_WIDTH / 2 + p_tolerance;
|
2020-02-18 05:06:54 +08:00
|
|
|
const Vector2 *points = polygon.ptr();
|
2018-12-05 12:41:29 +08:00
|
|
|
for (int i = 0; i < polygon.size() - 1; i++) {
|
2020-05-26 01:20:45 +08:00
|
|
|
Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, &points[i]);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p.distance_to(p_point) <= d) {
|
2018-12-05 12:41:29 +08:00
|
|
|
return true;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-12-05 12:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-10-22 05:37:07 +08:00
|
|
|
#endif
|
2018-12-05 12:41:29 +08:00
|
|
|
|
2020-02-18 05:06:54 +08:00
|
|
|
void OccluderPolygon2D::set_polygon(const Vector<Vector2> &p_polygon) {
|
2015-03-02 11:54:10 +08:00
|
|
|
polygon = p_polygon;
|
2018-12-05 12:41:29 +08:00
|
|
|
rect_cache_dirty = true;
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, p_polygon, closed);
|
2015-03-02 11:54:10 +08:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2020-02-18 05:06:54 +08:00
|
|
|
Vector<Vector2> OccluderPolygon2D::get_polygon() const {
|
2015-03-02 11:54:10 +08:00
|
|
|
return polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OccluderPolygon2D::set_closed(bool p_closed) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (closed == p_closed) {
|
2015-03-02 11:54:10 +08:00
|
|
|
return;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-03-02 11:54:10 +08:00
|
|
|
closed = p_closed;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (polygon.size()) {
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_occluder_polygon_set_shape(occ_polygon, polygon, closed);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-03-02 11:54:10 +08:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OccluderPolygon2D::is_closed() const {
|
|
|
|
return closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OccluderPolygon2D::set_cull_mode(CullMode p_mode) {
|
|
|
|
cull = p_mode;
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_occluder_polygon_set_cull_mode(occ_polygon, RS::CanvasOccluderPolygonCullMode(p_mode));
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OccluderPolygon2D::CullMode OccluderPolygon2D::get_cull_mode() const {
|
|
|
|
return cull;
|
|
|
|
}
|
|
|
|
|
|
|
|
RID OccluderPolygon2D::get_rid() const {
|
|
|
|
return occ_polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OccluderPolygon2D::_bind_methods() {
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_closed", "closed"), &OccluderPolygon2D::set_closed);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_closed"), &OccluderPolygon2D::is_closed);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_cull_mode", "cull_mode"), &OccluderPolygon2D::set_cull_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cull_mode"), &OccluderPolygon2D::get_cull_mode);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &OccluderPolygon2D::set_polygon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_polygon"), &OccluderPolygon2D::get_polygon);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-02-12 08:11:37 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "is_closed");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mode", PROPERTY_HINT_ENUM, "Disabled,ClockWise,CounterClockWise"), "set_cull_mode", "get_cull_mode");
|
2020-02-18 05:06:54 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-08-20 23:45:01 +08:00
|
|
|
BIND_ENUM_CONSTANT(CULL_DISABLED);
|
|
|
|
BIND_ENUM_CONSTANT(CULL_CLOCKWISE);
|
|
|
|
BIND_ENUM_CONSTANT(CULL_COUNTER_CLOCKWISE);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OccluderPolygon2D::OccluderPolygon2D() {
|
2020-03-28 02:21:27 +08:00
|
|
|
occ_polygon = RS::get_singleton()->canvas_occluder_polygon_create();
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OccluderPolygon2D::~OccluderPolygon2D() {
|
2022-12-13 01:42:37 +08:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->free(occ_polygon);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LightOccluder2D::_poly_changed() {
|
2018-04-30 01:49:26 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2015-03-02 11:54:10 +08:00
|
|
|
#endif
|
2018-04-30 01:49:26 +08:00
|
|
|
}
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
void LightOccluder2D::_notification(int p_what) {
|
2022-02-16 01:06:48 +08:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_CANVAS: {
|
|
|
|
RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, get_canvas());
|
|
|
|
RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
|
|
|
|
RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
|
|
|
RS::get_singleton()->canvas_light_occluder_set_transform(occluder, get_global_transform());
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
RS::get_singleton()->canvas_light_occluder_set_enabled(occluder, is_visible_in_tree());
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
if (occluder_polygon.is_valid()) {
|
|
|
|
Vector<Vector2> poly = occluder_polygon->get_polygon();
|
|
|
|
|
|
|
|
if (poly.size()) {
|
|
|
|
if (occluder_polygon->is_closed()) {
|
|
|
|
Vector<Color> color;
|
|
|
|
color.push_back(Color(0, 0, 0, 0.6));
|
|
|
|
draw_polygon(Variant(poly), color);
|
|
|
|
} else {
|
|
|
|
int ps = poly.size();
|
|
|
|
const Vector2 *r = poly.ptr();
|
|
|
|
for (int i = 0; i < ps - 1; i++) {
|
|
|
|
draw_line(r[i], r[i + 1], Color(0, 0, 0, 0.6), 3);
|
|
|
|
}
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 01:06:48 +08:00
|
|
|
} break;
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2022-02-16 01:06:48 +08:00
|
|
|
case NOTIFICATION_EXIT_CANVAS: {
|
|
|
|
RS::get_singleton()->canvas_light_occluder_attach_to_canvas(occluder, RID());
|
|
|
|
} break;
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 05:37:07 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
2018-12-05 12:41:29 +08:00
|
|
|
Rect2 LightOccluder2D::_edit_get_rect() const {
|
|
|
|
return occluder_polygon.is_valid() ? occluder_polygon->_edit_get_rect() : Rect2();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LightOccluder2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
|
|
|
|
return occluder_polygon.is_valid() ? occluder_polygon->_edit_is_selected_on_click(p_point, p_tolerance) : false;
|
|
|
|
}
|
2019-10-22 05:37:07 +08:00
|
|
|
#endif
|
2018-12-05 12:41:29 +08:00
|
|
|
|
2015-03-02 11:54:10 +08:00
|
|
|
void LightOccluder2D::set_occluder_polygon(const Ref<OccluderPolygon2D> &p_polygon) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
2020-05-14 22:41:43 +08:00
|
|
|
if (occluder_polygon.is_valid()) {
|
2023-07-04 03:29:37 +08:00
|
|
|
occluder_polygon->disconnect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-03-02 11:54:10 +08:00
|
|
|
#endif
|
|
|
|
occluder_polygon = p_polygon;
|
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (occluder_polygon.is_valid()) {
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, occluder_polygon->get_rid());
|
2020-05-14 22:41:43 +08:00
|
|
|
} else {
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->canvas_light_occluder_set_polygon(occluder, RID());
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-03-02 11:54:10 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
2020-05-14 22:41:43 +08:00
|
|
|
if (occluder_polygon.is_valid()) {
|
2023-07-04 03:29:37 +08:00
|
|
|
occluder_polygon->connect_changed(callable_mp(this, &LightOccluder2D::_poly_changed));
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2022-08-14 05:21:24 +08:00
|
|
|
queue_redraw();
|
2015-03-02 11:54:10 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<OccluderPolygon2D> LightOccluder2D::get_occluder_polygon() const {
|
|
|
|
return occluder_polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LightOccluder2D::set_occluder_light_mask(int p_mask) {
|
|
|
|
mask = p_mask;
|
2020-12-05 08:32:11 +08:00
|
|
|
RS::get_singleton()->canvas_light_occluder_set_light_mask(occluder, p_mask);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int LightOccluder2D::get_occluder_light_mask() const {
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2022-09-19 23:43:15 +08:00
|
|
|
PackedStringArray LightOccluder2D::get_configuration_warnings() const {
|
|
|
|
PackedStringArray warnings = Node::get_configuration_warnings();
|
2020-05-15 04:59:27 +08:00
|
|
|
|
2016-05-18 05:27:15 +08:00
|
|
|
if (!occluder_polygon.is_valid()) {
|
2022-03-28 21:24:14 +08:00
|
|
|
warnings.push_back(RTR("An occluder polygon must be set (or drawn) for this occluder to take effect."));
|
2016-05-18 05:27:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (occluder_polygon.is_valid() && occluder_polygon->get_polygon().size() == 0) {
|
2022-03-28 21:24:14 +08:00
|
|
|
warnings.push_back(RTR("The occluder polygon for this occluder is empty. Please draw a polygon."));
|
2016-05-18 05:27:15 +08:00
|
|
|
}
|
|
|
|
|
2020-10-29 18:01:28 +08:00
|
|
|
return warnings;
|
2016-05-18 05:27:15 +08:00
|
|
|
}
|
|
|
|
|
2020-11-26 20:50:21 +08:00
|
|
|
void LightOccluder2D::set_as_sdf_collision(bool p_enable) {
|
|
|
|
sdf_collision = p_enable;
|
|
|
|
RS::get_singleton()->canvas_light_occluder_set_as_sdf_collision(occluder, sdf_collision);
|
|
|
|
}
|
|
|
|
bool LightOccluder2D::is_set_as_sdf_collision() const {
|
|
|
|
return sdf_collision;
|
|
|
|
}
|
|
|
|
|
2015-03-02 11:54:10 +08:00
|
|
|
void LightOccluder2D::_bind_methods() {
|
2017-08-09 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_occluder_polygon", "polygon"), &LightOccluder2D::set_occluder_polygon);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_occluder_polygon"), &LightOccluder2D::get_occluder_polygon);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2017-02-13 19:47:24 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_occluder_light_mask", "mask"), &LightOccluder2D::set_occluder_light_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_occluder_light_mask"), &LightOccluder2D::get_occluder_light_mask);
|
2015-03-02 11:54:10 +08:00
|
|
|
|
2020-11-26 20:50:21 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_as_sdf_collision", "enable"), &LightOccluder2D::set_as_sdf_collision);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_set_as_sdf_collision"), &LightOccluder2D::is_set_as_sdf_collision);
|
|
|
|
|
2017-02-12 08:11:37 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D"), "set_occluder_polygon", "get_occluder_polygon");
|
2020-11-26 20:50:21 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sdf_collision"), "set_as_sdf_collision", "is_set_as_sdf_collision");
|
2020-12-05 08:32:11 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask");
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LightOccluder2D::LightOccluder2D() {
|
2020-03-28 02:21:27 +08:00
|
|
|
occluder = RS::get_singleton()->canvas_light_occluder_create();
|
2020-11-26 20:50:21 +08:00
|
|
|
|
2017-01-13 07:35:46 +08:00
|
|
|
set_notify_transform(true);
|
2020-11-26 20:50:21 +08:00
|
|
|
set_as_sdf_collision(true);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LightOccluder2D::~LightOccluder2D() {
|
2022-12-13 01:42:37 +08:00
|
|
|
ERR_FAIL_NULL(RenderingServer::get_singleton());
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
RS::get_singleton()->free(occluder);
|
2015-03-02 11:54:10 +08:00
|
|
|
}
|