1/**************************************************************************/
2/* register_types.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 "register_types.h"
32
33#include "scene/resources/mesh.h"
34
35#include "thirdparty/vhacd/public/VHACD.h"
36
37static Vector<Vector<Vector3>> convex_decompose(const real_t *p_vertices, int p_vertex_count, const uint32_t *p_triangles, int p_triangle_count, const Ref<MeshConvexDecompositionSettings> &p_settings, Vector<Vector<uint32_t>> *r_convex_indices) {
38 VHACD::IVHACD::Parameters params;
39 params.m_concavity = p_settings->get_max_concavity();
40 params.m_alpha = p_settings->get_symmetry_planes_clipping_bias();
41 params.m_beta = p_settings->get_revolution_axes_clipping_bias();
42 params.m_minVolumePerCH = p_settings->get_min_volume_per_convex_hull();
43 params.m_resolution = p_settings->get_resolution();
44 params.m_maxNumVerticesPerCH = p_settings->get_max_num_vertices_per_convex_hull();
45 params.m_planeDownsampling = p_settings->get_plane_downsampling();
46 params.m_convexhullDownsampling = p_settings->get_convex_hull_downsampling();
47 params.m_pca = p_settings->get_normalize_mesh();
48 params.m_mode = p_settings->get_mode();
49 params.m_convexhullApproximation = p_settings->get_convex_hull_approximation();
50 params.m_oclAcceleration = true;
51 params.m_maxConvexHulls = p_settings->get_max_convex_hulls();
52 params.m_projectHullVertices = p_settings->get_project_hull_vertices();
53
54 VHACD::IVHACD *decomposer = VHACD::CreateVHACD();
55 decomposer->Compute(p_vertices, p_vertex_count, p_triangles, p_triangle_count, params);
56
57 int hull_count = decomposer->GetNConvexHulls();
58
59 Vector<Vector<Vector3>> ret;
60 ret.resize(hull_count);
61
62 if (r_convex_indices) {
63 r_convex_indices->resize(hull_count);
64 }
65
66 for (int i = 0; i < hull_count; i++) {
67 VHACD::IVHACD::ConvexHull hull;
68 decomposer->GetConvexHull(i, hull);
69
70 Vector<Vector3> &points = ret.write[i];
71 points.resize(hull.m_nPoints);
72
73 Vector3 *w = points.ptrw();
74 for (uint32_t j = 0; j < hull.m_nPoints; ++j) {
75 for (int k = 0; k < 3; k++) {
76 w[j][k] = hull.m_points[j * 3 + k];
77 }
78 }
79
80 if (r_convex_indices) {
81 Vector<uint32_t> &indices = r_convex_indices->write[i];
82 indices.resize(hull.m_nTriangles * 3);
83
84 memcpy(indices.ptrw(), hull.m_triangles, hull.m_nTriangles * 3 * sizeof(uint32_t));
85 }
86 }
87
88 decomposer->Clean();
89 decomposer->Release();
90
91 return ret;
92}
93
94void initialize_vhacd_module(ModuleInitializationLevel p_level) {
95 if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
96 return;
97 }
98
99 Mesh::convex_decomposition_function = convex_decompose;
100}
101
102void uninitialize_vhacd_module(ModuleInitializationLevel p_level) {
103 if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
104 return;
105 }
106
107 Mesh::convex_decomposition_function = nullptr;
108}
109