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