1/**************************************************************************/
2/* navigation_mesh_source_geometry_data_3d.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 "navigation_mesh_source_geometry_data_3d.h"
32
33void NavigationMeshSourceGeometryData3D::set_vertices(const Vector<float> &p_vertices) {
34 vertices = p_vertices;
35}
36
37void NavigationMeshSourceGeometryData3D::set_indices(const Vector<int> &p_indices) {
38 indices = p_indices;
39}
40
41void NavigationMeshSourceGeometryData3D::clear() {
42 vertices.clear();
43 indices.clear();
44}
45
46void NavigationMeshSourceGeometryData3D::_add_vertex(const Vector3 &p_vec3) {
47 vertices.push_back(p_vec3.x);
48 vertices.push_back(p_vec3.y);
49 vertices.push_back(p_vec3.z);
50}
51
52void NavigationMeshSourceGeometryData3D::_add_mesh(const Ref<Mesh> &p_mesh, const Transform3D &p_xform) {
53 int current_vertex_count;
54 for (int i = 0; i < p_mesh->get_surface_count(); i++) {
55 current_vertex_count = vertices.size() / 3;
56
57 if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
58 continue;
59 }
60
61 int index_count = 0;
62 if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
63 index_count = p_mesh->surface_get_array_index_len(i);
64 } else {
65 index_count = p_mesh->surface_get_array_len(i);
66 }
67
68 ERR_CONTINUE((index_count == 0 || (index_count % 3) != 0));
69
70 int face_count = index_count / 3;
71
72 Array a = p_mesh->surface_get_arrays(i);
73 ERR_CONTINUE(a.is_empty() || (a.size() != Mesh::ARRAY_MAX));
74
75 Vector<Vector3> mesh_vertices = a[Mesh::ARRAY_VERTEX];
76 ERR_CONTINUE(mesh_vertices.is_empty());
77 const Vector3 *vr = mesh_vertices.ptr();
78
79 if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) {
80 Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX];
81 ERR_CONTINUE(mesh_indices.is_empty() || (mesh_indices.size() != index_count));
82 const int *ir = mesh_indices.ptr();
83
84 for (int j = 0; j < mesh_vertices.size(); j++) {
85 _add_vertex(p_xform.xform(vr[j]));
86 }
87
88 for (int j = 0; j < face_count; j++) {
89 // CCW
90 indices.push_back(current_vertex_count + (ir[j * 3 + 0]));
91 indices.push_back(current_vertex_count + (ir[j * 3 + 2]));
92 indices.push_back(current_vertex_count + (ir[j * 3 + 1]));
93 }
94 } else {
95 ERR_CONTINUE(mesh_vertices.size() != index_count);
96 face_count = mesh_vertices.size() / 3;
97 for (int j = 0; j < face_count; j++) {
98 _add_vertex(p_xform.xform(vr[j * 3 + 0]));
99 _add_vertex(p_xform.xform(vr[j * 3 + 2]));
100 _add_vertex(p_xform.xform(vr[j * 3 + 1]));
101
102 indices.push_back(current_vertex_count + (j * 3 + 0));
103 indices.push_back(current_vertex_count + (j * 3 + 1));
104 indices.push_back(current_vertex_count + (j * 3 + 2));
105 }
106 }
107 }
108}
109
110void NavigationMeshSourceGeometryData3D::_add_mesh_array(const Array &p_mesh_array, const Transform3D &p_xform) {
111 ERR_FAIL_COND(p_mesh_array.size() != Mesh::ARRAY_MAX);
112
113 Vector<Vector3> mesh_vertices = p_mesh_array[Mesh::ARRAY_VERTEX];
114 ERR_FAIL_COND(mesh_vertices.is_empty());
115 const Vector3 *vr = mesh_vertices.ptr();
116
117 Vector<int> mesh_indices = p_mesh_array[Mesh::ARRAY_INDEX];
118 ERR_FAIL_COND(mesh_indices.is_empty());
119 const int *ir = mesh_indices.ptr();
120
121 const int face_count = mesh_indices.size() / 3;
122 const int current_vertex_count = vertices.size() / 3;
123
124 for (int j = 0; j < mesh_vertices.size(); j++) {
125 _add_vertex(p_xform.xform(vr[j]));
126 }
127
128 for (int j = 0; j < face_count; j++) {
129 // CCW
130 indices.push_back(current_vertex_count + (ir[j * 3 + 0]));
131 indices.push_back(current_vertex_count + (ir[j * 3 + 2]));
132 indices.push_back(current_vertex_count + (ir[j * 3 + 1]));
133 }
134}
135
136void NavigationMeshSourceGeometryData3D::_add_faces(const PackedVector3Array &p_faces, const Transform3D &p_xform) {
137 ERR_FAIL_COND(p_faces.is_empty());
138 ERR_FAIL_COND(p_faces.size() % 3 != 0);
139 int face_count = p_faces.size() / 3;
140 int current_vertex_count = vertices.size() / 3;
141
142 for (int j = 0; j < face_count; j++) {
143 _add_vertex(p_xform.xform(p_faces[j * 3 + 0]));
144 _add_vertex(p_xform.xform(p_faces[j * 3 + 1]));
145 _add_vertex(p_xform.xform(p_faces[j * 3 + 2]));
146
147 indices.push_back(current_vertex_count + (j * 3 + 0));
148 indices.push_back(current_vertex_count + (j * 3 + 2));
149 indices.push_back(current_vertex_count + (j * 3 + 1));
150 }
151}
152
153void NavigationMeshSourceGeometryData3D::add_mesh(const Ref<Mesh> &p_mesh, const Transform3D &p_xform) {
154 ERR_FAIL_COND(!p_mesh.is_valid());
155 _add_mesh(p_mesh, root_node_transform * p_xform);
156}
157
158void NavigationMeshSourceGeometryData3D::add_mesh_array(const Array &p_mesh_array, const Transform3D &p_xform) {
159 ERR_FAIL_COND(p_mesh_array.size() != Mesh::ARRAY_MAX);
160 _add_mesh_array(p_mesh_array, root_node_transform * p_xform);
161}
162
163void NavigationMeshSourceGeometryData3D::add_faces(const PackedVector3Array &p_faces, const Transform3D &p_xform) {
164 ERR_FAIL_COND(p_faces.size() % 3 != 0);
165 _add_faces(p_faces, root_node_transform * p_xform);
166}
167
168void NavigationMeshSourceGeometryData3D::_bind_methods() {
169 ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationMeshSourceGeometryData3D::set_vertices);
170 ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationMeshSourceGeometryData3D::get_vertices);
171
172 ClassDB::bind_method(D_METHOD("set_indices", "indices"), &NavigationMeshSourceGeometryData3D::set_indices);
173 ClassDB::bind_method(D_METHOD("get_indices"), &NavigationMeshSourceGeometryData3D::get_indices);
174
175 ClassDB::bind_method(D_METHOD("clear"), &NavigationMeshSourceGeometryData3D::clear);
176 ClassDB::bind_method(D_METHOD("has_data"), &NavigationMeshSourceGeometryData3D::has_data);
177
178 ClassDB::bind_method(D_METHOD("add_mesh", "mesh", "xform"), &NavigationMeshSourceGeometryData3D::add_mesh);
179 ClassDB::bind_method(D_METHOD("add_mesh_array", "mesh_array", "xform"), &NavigationMeshSourceGeometryData3D::add_mesh_array);
180 ClassDB::bind_method(D_METHOD("add_faces", "faces", "xform"), &NavigationMeshSourceGeometryData3D::add_faces);
181
182 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_vertices", "get_vertices");
183 ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "indices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_indices", "get_indices");
184}
185
186NavigationMeshSourceGeometryData3D::NavigationMeshSourceGeometryData3D() {
187}
188
189NavigationMeshSourceGeometryData3D::~NavigationMeshSourceGeometryData3D() {
190 clear();
191}
192