register_types.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "register_types.h"
  31. #include "scene/resources/mesh.h"
  32. #include "thirdparty/vhacd/public/VHACD.h"
  33. static Vector<Vector<Vector3>> convex_decompose(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, const Mesh::ConvexDecompositionSettings &p_settings, Vector<Vector<uint32_t>> *r_convex_indices) {
  34. VHACD::IVHACD::Parameters params;
  35. params.m_concavity = p_settings.max_concavity;
  36. params.m_alpha = p_settings.symmetry_planes_clipping_bias;
  37. params.m_beta = p_settings.revolution_axes_clipping_bias;
  38. params.m_minVolumePerCH = p_settings.min_volume_per_convex_hull;
  39. params.m_resolution = p_settings.resolution;
  40. params.m_maxNumVerticesPerCH = p_settings.max_num_vertices_per_convex_hull;
  41. params.m_planeDownsampling = p_settings.plane_downsampling;
  42. params.m_convexhullDownsampling = p_settings.convexhull_downsampling;
  43. params.m_pca = p_settings.normalize_mesh;
  44. params.m_mode = p_settings.mode;
  45. params.m_convexhullApproximation = p_settings.convexhull_approximation;
  46. params.m_oclAcceleration = true;
  47. params.m_maxConvexHulls = p_settings.max_convex_hulls;
  48. params.m_projectHullVertices = p_settings.project_hull_vertices;
  49. VHACD::IVHACD *decomposer = VHACD::CreateVHACD();
  50. decomposer->Compute(p_vertices, p_vertex_count, p_triangles, p_triangle_count, params);
  51. int hull_count = decomposer->GetNConvexHulls();
  52. Vector<Vector<Vector3>> ret;
  53. ret.resize(hull_count);
  54. if (r_convex_indices) {
  55. r_convex_indices->resize(hull_count);
  56. }
  57. for (int i = 0; i < hull_count; i++) {
  58. VHACD::IVHACD::ConvexHull hull;
  59. decomposer->GetConvexHull(i, hull);
  60. Vector<Vector3> &points = ret.write[i];
  61. points.resize(hull.m_nPoints);
  62. Vector3 *w = points.ptrw();
  63. for (uint32_t j = 0; j < hull.m_nPoints; ++j) {
  64. for (int k = 0; k < 3; k++) {
  65. w[j][k] = hull.m_points[j * 3 + k];
  66. }
  67. }
  68. if (r_convex_indices) {
  69. Vector<uint32_t> &indices = r_convex_indices->write[i];
  70. indices.resize(hull.m_nTriangles * 3);
  71. memcpy(indices.ptrw(), hull.m_triangles, hull.m_nTriangles * 3 * sizeof(uint32_t));
  72. }
  73. }
  74. decomposer->Clean();
  75. decomposer->Release();
  76. return ret;
  77. }
  78. void initialize_vhacd_module(ModuleInitializationLevel p_level) {
  79. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  80. return;
  81. }
  82. Mesh::convex_decomposition_function = convex_decompose;
  83. }
  84. void uninitialize_vhacd_module(ModuleInitializationLevel p_level) {
  85. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  86. return;
  87. }
  88. Mesh::convex_decomposition_function = nullptr;
  89. }