-
-
Notifications
You must be signed in to change notification settings - Fork 22k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 2D CSG boolean operations #99911
Open
smix8
wants to merge
1
commit into
godotengine:master
Choose a base branch
from
smix8:csg2d
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,733
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env python | ||
from misc.utility.scons_hints import * | ||
|
||
Import("env") | ||
Import("env_modules") | ||
|
||
env_csg_2d = env_modules.Clone() | ||
|
||
# Godot's own source files | ||
env_csg_2d.add_source_files(env.modules_sources, "*.cpp") | ||
if env.editor_build: | ||
env_csg_2d.add_source_files(env.modules_sources, "editor/*.cpp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
def can_build(env, platform): | ||
return True | ||
|
||
|
||
def configure(env): | ||
pass | ||
|
||
|
||
def get_doc_classes(): | ||
return [ | ||
"CSGCapsule2D", | ||
"CSGCircle2D", | ||
"CSGCombiner2D", | ||
"CSGMesh2D", | ||
"CSGPolygon2D", | ||
"CSGPrimitive2D", | ||
"CSGShape2D", | ||
"CSGRectangle2D", | ||
] | ||
|
||
|
||
def get_doc_path(): | ||
return "doc_classes" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/**************************************************************************/ | ||
/* csg_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. */ | ||
/**************************************************************************/ | ||
|
||
#include "csg_2d.h" | ||
|
||
void CSGBrush2D::copy_from(const CSGBrush2D &p_brush, const Transform2D &p_xform) { | ||
outlines.resize(p_brush.outlines.size()); | ||
|
||
for (uint32_t i = 0; i < outlines.size(); i++) { | ||
for (uint32_t j = 0; j < outlines[i].vertices.size(); j++) { | ||
outlines[i].vertices[j] = p_xform.xform(p_brush.outlines[i].vertices[j]); | ||
} | ||
} | ||
|
||
poly_paths.clear(); | ||
|
||
for (Clipper2Lib::PathsD::size_type i = 0; i < p_brush.poly_paths.size(); ++i) { | ||
const Clipper2Lib::PathD &path = p_brush.poly_paths[i]; | ||
|
||
Clipper2Lib::PathD poly_path; | ||
|
||
for (Clipper2Lib::PathsD::size_type j = 0; j < path.size(); ++j) { | ||
Point2 vertex = Point2(static_cast<real_t>(path[j].x), static_cast<real_t>(path[j].y)); | ||
vertex = p_xform.xform(vertex); | ||
poly_path.push_back(Clipper2Lib::PointD(vertex.x, vertex.y)); | ||
} | ||
poly_paths.push_back(poly_path); | ||
} | ||
|
||
_regen_outline_rects(); | ||
} | ||
|
||
void CSGBrush2D::build_from_outlines(const LocalVector<Outline> &p_outlines) { | ||
poly_paths.clear(); | ||
|
||
Clipper2Lib::ClipperD clipper_D; | ||
clipper_D.PreserveCollinear(false); | ||
|
||
for (const Outline &outline : p_outlines) { | ||
const LocalVector<Vector2> &vertices = outline.vertices; | ||
Clipper2Lib::PathD subject_path; | ||
subject_path.reserve(vertices.size()); | ||
for (const Vector2 &vertex : vertices) { | ||
Clipper2Lib::PointD point = Clipper2Lib::PointD(vertex.x, vertex.y); | ||
subject_path.emplace_back(point); | ||
} | ||
|
||
Clipper2Lib::PathsD subject_paths; | ||
subject_paths.push_back(subject_path); | ||
|
||
if (outline.is_hole) { | ||
clipper_D.AddClip(subject_paths); | ||
} else { | ||
clipper_D.AddSubject(subject_paths); | ||
} | ||
} | ||
|
||
clipper_D.Execute(Clipper2Lib::ClipType::Union, Clipper2Lib::FillRule::NonZero, poly_paths); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/**************************************************************************/ | ||
/* csg_2d.h */ | ||
/**************************************************************************/ | ||
/* 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. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef CSG_2D_H | ||
#define CSG_2D_H | ||
|
||
#include "core/math/rect2.h" | ||
#include "core/math/transform_2d.h" | ||
#include "core/templates/local_vector.h" | ||
|
||
#include "thirdparty/clipper2/include/clipper2/clipper.h" | ||
|
||
struct CSGBrush2D { | ||
struct Outline { | ||
LocalVector<Vector2> vertices; | ||
bool is_hole = false; | ||
Rect2 rect; | ||
}; | ||
|
||
LocalVector<Outline> outlines; | ||
|
||
inline void _regen_outline_rects() { | ||
for (uint32_t i = 0; i < outlines.size(); i++) { | ||
outlines[i].rect = Rect2(); | ||
if (outlines[i].vertices.size() > 0) { | ||
outlines[i].rect.position = outlines[i].vertices[0]; | ||
for (uint32_t j = 1; j < outlines[i].vertices.size(); j++) { | ||
outlines[i].rect.expand_to(outlines[i].vertices[j]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Clipper2Lib::PathsD poly_paths; | ||
|
||
void copy_from(const CSGBrush2D &p_brush, const Transform2D &p_xform); | ||
|
||
void build_from_outlines(const LocalVector<Outline> &p_outlines); | ||
}; | ||
|
||
#endif // CSG_2D_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,147 @@ | ||||||
/**************************************************************************/ | ||||||
/* csg_capsule_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. */ | ||||||
/**************************************************************************/ | ||||||
|
||||||
#include "csg_capsule_2d.h" | ||||||
|
||||||
#include "core/math/geometry_2d.h" | ||||||
#include "scene/resources/world_2d.h" | ||||||
#include "servers/physics_server_2d.h" | ||||||
|
||||||
#include "thirdparty/misc/polypartition.h" | ||||||
|
||||||
#ifdef TOOLS_ENABLED | ||||||
Rect2 CSGCapsule2D::_edit_get_rect() const { | ||||||
Rect2 rect = Rect2(Vector2(-radius, -height * 0.5 - radius), Vector2(radius, height * 0.5 + radius) * 2.0); | ||||||
rect.position -= rect.size * 0.3; | ||||||
rect.size += rect.size * 0.6; | ||||||
return rect; | ||||||
} | ||||||
|
||||||
bool CSGCapsule2D::_edit_use_rect() const { | ||||||
return true; | ||||||
} | ||||||
|
||||||
bool CSGCapsule2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { | ||||||
return Geometry2D::is_point_in_polygon(p_point, _get_vertices()); | ||||||
} | ||||||
#endif // TOOLS_ENABLED | ||||||
|
||||||
CSGBrush2D *CSGCapsule2D::_build_brush() { | ||||||
CSGBrush2D *new_brush = memnew(CSGBrush2D); | ||||||
|
||||||
LocalVector<CSGBrush2D::Outline> &outlines = new_brush->outlines; | ||||||
outlines.resize(1); | ||||||
CSGBrush2D::Outline &outline = outlines[0]; | ||||||
|
||||||
const Vector<Vector2> &cached_vertices = _get_vertices(); | ||||||
|
||||||
LocalVector<Vector2> &vertices = outline.vertices; | ||||||
|
||||||
vertices.resize(cached_vertices.size() + 1); | ||||||
|
||||||
for (int i = 0; i < cached_vertices.size(); i++) { | ||||||
vertices[i] = cached_vertices[i]; | ||||||
} | ||||||
vertices[cached_vertices.size()] = cached_vertices[0]; | ||||||
|
||||||
brush_outlines.resize(1); | ||||||
brush_outlines[0] = vertices; | ||||||
|
||||||
new_brush->build_from_outlines(outlines); | ||||||
|
||||||
return new_brush; | ||||||
} | ||||||
|
||||||
Vector<Vector2> CSGCapsule2D::_get_vertices() const { | ||||||
if (vertices_cache_dirty) { | ||||||
vertices_cache_dirty = false; | ||||||
|
||||||
vertices_cache.resize(radial_segments * 4 + 2); | ||||||
smix8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Vector2 *vertices_cache_ptrw = vertices_cache.ptrw(); | ||||||
int vertices_index = 0; | ||||||
|
||||||
const real_t turn_step = Math_TAU / float(radial_segments * 4); | ||||||
for (int i = 0; i < radial_segments * 4; i++) { | ||||||
Vector2 ofs = Vector2(0, (i > radial_segments && i <= radial_segments * 3) ? -height * 0.5 + radius : height * 0.5 - radius); | ||||||
|
||||||
vertices_cache_ptrw[vertices_index++] = Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius + ofs; | ||||||
if (i == radial_segments || i == radial_segments * 3) { | ||||||
vertices_cache_ptrw[vertices_index++] = Vector2(Math::sin(i * turn_step), Math::cos(i * turn_step)) * radius - ofs; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return vertices_cache; | ||||||
} | ||||||
|
||||||
void CSGCapsule2D::_bind_methods() { | ||||||
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CSGCapsule2D::set_radius); | ||||||
ClassDB::bind_method(D_METHOD("get_radius"), &CSGCapsule2D::get_radius); | ||||||
|
||||||
ClassDB::bind_method(D_METHOD("set_height", "height"), &CSGCapsule2D::set_height); | ||||||
ClassDB::bind_method(D_METHOD("get_height"), &CSGCapsule2D::get_height); | ||||||
|
||||||
ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &CSGCapsule2D::set_radial_segments); | ||||||
ClassDB::bind_method(D_METHOD("get_radial_segments"), &CSGCapsule2D::get_radial_segments); | ||||||
|
||||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_radius", "get_radius"); | ||||||
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.001,1000.0,0.001,or_greater,exp,suffix:m"), "set_height", "get_height"); | ||||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1"), "set_radial_segments", "get_radial_segments"); | ||||||
} | ||||||
|
||||||
void CSGCapsule2D::set_radius(const float p_radius) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
radius = p_radius; | ||||||
vertices_cache_dirty = true; | ||||||
_make_dirty(); | ||||||
} | ||||||
|
||||||
float CSGCapsule2D::get_radius() const { | ||||||
return radius; | ||||||
} | ||||||
|
||||||
void CSGCapsule2D::set_height(const float p_height) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
height = p_height; | ||||||
vertices_cache_dirty = true; | ||||||
_make_dirty(); | ||||||
} | ||||||
|
||||||
float CSGCapsule2D::get_height() const { | ||||||
return height; | ||||||
} | ||||||
|
||||||
void CSGCapsule2D::set_radial_segments(const int p_segments) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
radial_segments = p_segments > 0 ? p_segments : 1; | ||||||
vertices_cache_dirty = true; | ||||||
_make_dirty(); | ||||||
} | ||||||
|
||||||
int CSGCapsule2D::get_radial_segments() const { | ||||||
return radial_segments; | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already included