Debian Codegraph @

scene/2d/multimesh_instance_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* multimesh_instance_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 "multimesh_instance_2d.h"
32
33 #ifndef NAVIGATION_2D_DISABLED
34 #include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h"
35 #include "scene/resources/2d/navigation_polygon.h"
36 #include "servers/navigation_2d/navigation_server_2d.h"
37
38 #include "thirdparty/clipper2/include/clipper2/clipper.h"
39 #include "thirdparty/misc/polypartition.h"
40 #endif // NAVIGATION_2D_DISABLED
41
42 Callable MultiMeshInstance2D::_navmesh_source_geometry_parsing_callback;
43 RID MultiMeshInstance2D::_navmesh_source_geometry_parser;
44
45 void MultiMeshInstance2D::_refresh_interpolated() {
46 if (is_inside_tree() && multimesh.is_valid()) {
47 bool interpolated = is_physics_interpolated_and_enabled();
48 multimesh->set_physics_interpolated(interpolated);
49 }
50 }
51
52 void MultiMeshInstance2D::_physics_interpolated_changed() {
53 CanvasItem::_physics_interpolated_changed();
54 _refresh_interpolated();
55 }
56
57 void MultiMeshInstance2D::_notification(int p_what) {
58 switch (p_what) {
59 case NOTIFICATION_ENTER_TREE: {
60 _refresh_interpolated();
61 break;
62 }
63 case NOTIFICATION_DRAW: {
64 if (multimesh.is_valid()) {
65 draw_multimesh(multimesh, texture);
66 }
67 } break;
68 }
69 }
70
71 void MultiMeshInstance2D::_bind_methods() {
72 ClassDB::bind_method(D_METHOD("set_multimesh", "multimesh"), &MultiMeshInstance2D::set_multimesh);
73 ClassDB::bind_method(D_METHOD("get_multimesh"), &MultiMeshInstance2D::get_multimesh);
74
75 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &MultiMeshInstance2D::set_texture);
76 ClassDB::bind_method(D_METHOD("get_texture"), &MultiMeshInstance2D::get_texture);
77
78 ADD_SIGNAL(MethodInfo("texture_changed"));
79
80 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multimesh", PROPERTY_HINT_RESOURCE_TYPE, "MultiMesh"), "set_multimesh", "get_multimesh");
81 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
82 }
83
84 void MultiMeshInstance2D::set_multimesh(const Ref<MultiMesh> &p_multimesh) {
85 // Cleanup previous connection if any.
86 if (multimesh.is_valid()) {
87 multimesh->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
88 }
89 multimesh = p_multimesh;
90
91 // Connect to the multimesh so the AABB can update when instance transforms are changed.
92 if (multimesh.is_valid()) {
93 multimesh->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
94 _refresh_interpolated();
95 }
96 queue_redraw();
97 }
98
99 Ref<MultiMesh> MultiMeshInstance2D::get_multimesh() const {
100 return multimesh;
101 }
102
103 void MultiMeshInstance2D::set_texture(const Ref<Texture2D> &p_texture) {
104 if (p_texture == texture) {
105 return;
106 }
107 texture = p_texture;
108 queue_redraw();
109 emit_signal(SceneStringName(texture_changed));
110 }
111
112 Ref<Texture2D> MultiMeshInstance2D::get_texture() const {
113 return texture;
114 }
115
116 #ifdef DEBUG_ENABLED
117 Rect2 MultiMeshInstance2D::_edit_get_rect() const {
118 if (multimesh.is_valid()) {
119 AABB aabb = multimesh->get_aabb();
120 return Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
121 }
122
123 return Node2D::_edit_get_rect();
124 }
125 #endif // DEBUG_ENABLED
126
127 #ifndef NAVIGATION_2D_DISABLED
128 void MultiMeshInstance2D::navmesh_parse_init() {
129 ERR_FAIL_NULL(NavigationServer2D::get_singleton());
130 if (!_navmesh_source_geometry_parser.is_valid()) {
131 _navmesh_source_geometry_parsing_callback = callable_mp_static(&MultiMeshInstance2D::navmesh_parse_source_geometry);
132 _navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
133 NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
134 }
135 }
136
137 void MultiMeshInstance2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
138 MultiMeshInstance2D *multimesh_instance = Object::cast_to<MultiMeshInstance2D>(p_node);
139
140 if (multimesh_instance == nullptr) {
141 return;
142 }
143
144 NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
145 if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {
146 return;
147 }
148
149 Ref<MultiMesh> multimesh = multimesh_instance->get_multimesh();
150 if (!(multimesh.is_valid() && multimesh->get_transform_format() == MultiMesh::TRANSFORM_2D)) {
151 return;
152 }
153
154 Ref<Mesh> mesh = multimesh->get_mesh();
155 if (mesh.is_null()) {
156 return;
157 }
158
159 using namespace Clipper2Lib;
160
161 PathsD mesh_subject_paths, dummy_clip_paths;
162
163 for (int i = 0; i < mesh->get_surface_count(); i++) {
164 if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
165 continue;
166 }
167
168 if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FLAG_USE_2D_VERTICES)) {
169 continue;
170 }
171
172 PathD subject_path;
173
174 int index_count = 0;
175 if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
176 index_count = mesh->surface_get_array_index_len(i);
177 } else {
178 index_count = mesh->surface_get_array_len(i);
179 }
180
181 ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));
182
183 Array a = mesh->surface_get_arrays(i);
184
185 Vector<Vector2> mesh_vertices = a[Mesh::ARRAY_VERTEX];
186
187 if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
188 Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX];
189 for (int vertex_index : mesh_indices) {
190 const Vector2 &vertex = mesh_vertices[vertex_index];
191 const PointD &point = PointD(vertex.x, vertex.y);
192 subject_path.push_back(point);
193 }
194 } else {
195 for (const Vector2 &vertex : mesh_vertices) {
196 const PointD &point = PointD(vertex.x, vertex.y);
197 subject_path.push_back(point);
198 }
199 }
200 mesh_subject_paths.push_back(subject_path);
201 }
202
203 PathsD mesh_path_solution = Union(mesh_subject_paths, dummy_clip_paths, FillRule::NonZero);
204
205 //path_solution = RamerDouglasPeucker(path_solution, 0.025);
206
207 int multimesh_instance_count = multimesh->get_visible_instance_count();
208 if (multimesh_instance_count == -1) {
209 multimesh_instance_count = multimesh->get_instance_count();
210 }
211
212 const Transform2D multimesh_instance_xform = p_source_geometry_data->root_node_transform * multimesh_instance->get_global_transform();
213
214 for (int i = 0; i < multimesh_instance_count; i++) {
215 const Transform2D multimesh_instance_mesh_instance_xform = multimesh_instance_xform * multimesh->get_instance_transform_2d(i);
216
217 for (const PathD &mesh_path : mesh_path_solution) {
218 Vector<Vector2> shape_outline;
219
220 for (const PointD &mesh_path_point : mesh_path) {
221 shape_outline.push_back(Point2(static_cast<real_t>(mesh_path_point.x), static_cast<real_t>(mesh_path_point.y)));
222 }
223
224 for (int j = 0; j < shape_outline.size(); j++) {
225 shape_outline.write[j] = multimesh_instance_mesh_instance_xform.xform(shape_outline[j]);
226 }
227 p_source_geometry_data->add_obstruction_outline(shape_outline);
228 }
229 }
230 }
231 #endif // NAVIGATION_2D_DISABLED
232
233 MultiMeshInstance2D::MultiMeshInstance2D() {
234 }
235
236 MultiMeshInstance2D::~MultiMeshInstance2D() {
237 }