navigation_mesh_source_geometry_data_3d.cpp 8.3 KB

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