Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/mesh_instance_2d.cpp
9896 views
1
/**************************************************************************/
2
/* mesh_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 "mesh_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_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 MeshInstance2D::_navmesh_source_geometry_parsing_callback;
43
RID MeshInstance2D::_navmesh_source_geometry_parser;
44
45
void MeshInstance2D::_notification(int p_what) {
46
switch (p_what) {
47
case NOTIFICATION_DRAW: {
48
if (mesh.is_valid()) {
49
draw_mesh(mesh, texture);
50
}
51
} break;
52
}
53
}
54
55
void MeshInstance2D::_bind_methods() {
56
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshInstance2D::set_mesh);
57
ClassDB::bind_method(D_METHOD("get_mesh"), &MeshInstance2D::get_mesh);
58
59
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &MeshInstance2D::set_texture);
60
ClassDB::bind_method(D_METHOD("get_texture"), &MeshInstance2D::get_texture);
61
62
ADD_SIGNAL(MethodInfo("texture_changed"));
63
64
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
65
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
66
}
67
68
void MeshInstance2D::set_mesh(const Ref<Mesh> &p_mesh) {
69
if (mesh == p_mesh) {
70
return;
71
}
72
73
if (mesh.is_valid()) {
74
mesh->disconnect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
75
}
76
77
mesh = p_mesh;
78
79
if (mesh.is_valid()) {
80
// If mesh is a PrimitiveMesh, calling get_rid on it can trigger a changed callback
81
// so do this before connecting to the change signal.
82
mesh->get_rid();
83
mesh->connect_changed(callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
84
}
85
86
queue_redraw();
87
}
88
89
Ref<Mesh> MeshInstance2D::get_mesh() const {
90
return mesh;
91
}
92
93
void MeshInstance2D::set_texture(const Ref<Texture2D> &p_texture) {
94
if (p_texture == texture) {
95
return;
96
}
97
texture = p_texture;
98
queue_redraw();
99
emit_signal(SceneStringName(texture_changed));
100
}
101
102
Ref<Texture2D> MeshInstance2D::get_texture() const {
103
return texture;
104
}
105
106
#ifdef DEBUG_ENABLED
107
Rect2 MeshInstance2D::_edit_get_rect() const {
108
if (mesh.is_valid()) {
109
AABB aabb = mesh->get_aabb();
110
return Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
111
}
112
113
return Node2D::_edit_get_rect();
114
}
115
116
bool MeshInstance2D::_edit_use_rect() const {
117
return mesh.is_valid();
118
}
119
#endif // DEBUG_ENABLED
120
121
#ifndef NAVIGATION_2D_DISABLED
122
void MeshInstance2D::navmesh_parse_init() {
123
ERR_FAIL_NULL(NavigationServer2D::get_singleton());
124
if (!_navmesh_source_geometry_parser.is_valid()) {
125
_navmesh_source_geometry_parsing_callback = callable_mp_static(&MeshInstance2D::navmesh_parse_source_geometry);
126
_navmesh_source_geometry_parser = NavigationServer2D::get_singleton()->source_geometry_parser_create();
127
NavigationServer2D::get_singleton()->source_geometry_parser_set_callback(_navmesh_source_geometry_parser, _navmesh_source_geometry_parsing_callback);
128
}
129
}
130
131
void MeshInstance2D::navmesh_parse_source_geometry(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node) {
132
MeshInstance2D *mesh_instance = Object::cast_to<MeshInstance2D>(p_node);
133
134
if (mesh_instance == nullptr) {
135
return;
136
}
137
138
NavigationPolygon::ParsedGeometryType parsed_geometry_type = p_navigation_mesh->get_parsed_geometry_type();
139
140
if (!(parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_MESH_INSTANCES || parsed_geometry_type == NavigationPolygon::PARSED_GEOMETRY_BOTH)) {
141
return;
142
}
143
144
Ref<Mesh> mesh = mesh_instance->get_mesh();
145
if (mesh.is_null()) {
146
return;
147
}
148
149
const Transform2D mesh_instance_xform = p_source_geometry_data->root_node_transform * mesh_instance->get_global_transform();
150
151
using namespace Clipper2Lib;
152
153
PathsD subject_paths, dummy_clip_paths;
154
155
for (int i = 0; i < mesh->get_surface_count(); i++) {
156
if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
157
continue;
158
}
159
160
if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FLAG_USE_2D_VERTICES)) {
161
continue;
162
}
163
164
PathD subject_path;
165
166
int index_count = 0;
167
if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
168
index_count = mesh->surface_get_array_index_len(i);
169
} else {
170
index_count = mesh->surface_get_array_len(i);
171
}
172
173
ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));
174
175
Array a = mesh->surface_get_arrays(i);
176
177
Vector<Vector2> mesh_vertices = a[Mesh::ARRAY_VERTEX];
178
179
if (mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
180
Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX];
181
for (int vertex_index : mesh_indices) {
182
const Vector2 &vertex = mesh_vertices[vertex_index];
183
const PointD &point = PointD(vertex.x, vertex.y);
184
subject_path.push_back(point);
185
}
186
} else {
187
for (const Vector2 &vertex : mesh_vertices) {
188
const PointD &point = PointD(vertex.x, vertex.y);
189
subject_path.push_back(point);
190
}
191
}
192
subject_paths.push_back(subject_path);
193
}
194
195
PathsD path_solution;
196
197
path_solution = Union(subject_paths, dummy_clip_paths, FillRule::NonZero);
198
199
//path_solution = RamerDouglasPeucker(path_solution, 0.025);
200
201
Vector<Vector<Vector2>> polypaths;
202
203
for (const PathD &scaled_path : path_solution) {
204
Vector<Vector2> shape_outline;
205
for (const PointD &scaled_point : scaled_path) {
206
shape_outline.push_back(Point2(static_cast<real_t>(scaled_point.x), static_cast<real_t>(scaled_point.y)));
207
}
208
209
for (int i = 0; i < shape_outline.size(); i++) {
210
shape_outline.write[i] = mesh_instance_xform.xform(shape_outline[i]);
211
}
212
213
p_source_geometry_data->add_obstruction_outline(shape_outline);
214
}
215
}
216
#endif // NAVIGATION_2D_DISABLED
217
218
MeshInstance2D::MeshInstance2D() {
219
}
220
221