Tanoda
VHACD.cs
Go to the documentation of this file.
1using System;
2using System.Diagnostics;
3using System.Linq;
4using System.Runtime.InteropServices;
5using System.Threading;
6using UnityEngine;
7using Debug = UnityEngine.Debug;
8
9public class VHACD : MonoBehaviour
10{
11 [Serializable]
12 public unsafe struct Parameters
13 {
14 public void Init()
15 {
16 m_resolution = 100000;
17 m_concavity = 0.001;
20 m_alpha = 0.05;
21 m_beta = 0.05;
22 m_pca = 0;
23 m_mode = 0; // 0: voxel-based (recommended), 1: tetrahedron-based
25 m_minVolumePerCH = 0.0001;
26 m_callback = null;
27 m_logger = null;
30 m_maxConvexHulls = 1024;
32 true; // This will project the output convex hull vertices onto the original source mesh to increase the floating point accuracy of the results
33 }
34
35 public void InitFast()
36 {
37 m_resolution = 10000;
38 m_concavity = 0.015;
41 m_alpha = 0.05;
42 m_beta = 0.05;
43 m_pca = 0;
44 m_mode = 0; // 0: voxel-based (recommended), 1: tetrahedron-based
46 m_minVolumePerCH = 0.0001;
47 m_callback = null;
48 m_logger = null;
51 m_maxConvexHulls = 128;
53 true; // This will project the output convex hull vertices onto the original source mesh to increase the floating point accuracy of the results
54 }
55
56 [Tooltip("maximum concavity")] [Range(0, 1)]
57 public double m_concavity;
58
59 [Tooltip("controls the bias toward clipping along symmetry planes")] [Range(0, 1)]
60 public double m_alpha;
61
62 [Tooltip("controls the bias toward clipping along revolution axes")] [Range(0, 1)]
63 public double m_beta;
64
65 [Tooltip("controls the adaptive sampling of the generated convex-hulls")] [Range(0, 0.01f)]
66 public double m_minVolumePerCH;
67
68 public void* m_callback;
69 public void* m_logger;
70
71 [Tooltip("maximum number of voxels generated during the voxelization stage")] [Range(10000, 64000000)]
72 public uint m_resolution;
73
74 [Tooltip("controls the maximum number of triangles per convex-hull")] [Range(4, 1024)]
76
77 [Tooltip("controls the granularity of the search for the \"best\" clipping plane")] [Range(1, 16)]
79
80 [Tooltip(
81 "controls the precision of the convex-hull generation process during the clipping plane selection stage")]
82 [Range(1, 16)]
84
85 [Tooltip("enable/disable normalizing the mesh before applying the convex decomposition")] [Range(0, 1)]
86 public uint m_pca;
87
88 [Tooltip("0: voxel-based (recommended), 1: tetrahedron-based")] [Range(0, 1)]
89 public uint m_mode;
90
91 [Range(0, 1)] public uint m_convexhullApproximation;
92
93 [Range(0, 1)] public uint m_oclAcceleration;
94
95 public uint m_maxConvexHulls;
96
97 [Tooltip(
98 "This will project the output convex hull vertices onto the original source mesh to increase the floating point accuracy of the results")]
100 }
101
102 unsafe struct ConvexHull
103 {
104 public double* m_points;
105 public uint* m_triangles;
106 public uint m_nPoints;
107 public uint m_nTriangles;
108 public double m_volume;
109 public fixed double m_center[3];
110 }
111
112 [DllImport("libvhacd")]
113 static extern unsafe void* CreateVHACD();
114
115 [DllImport("libvhacd")]
116 static extern unsafe void DestroyVHACD(void* pVHACD);
117
118 [DllImport("libvhacd")]
119 static extern unsafe bool ComputeFloat(
120 void* pVHACD,
121 float* points,
122 uint countPoints,
123 uint* triangles,
124 uint countTriangles,
125 Parameters* parameters);
126
127 [DllImport("libvhacd")]
128 static extern unsafe bool ComputeDouble(
129 void* pVHACD,
130 double* points,
131 uint countPoints,
132 uint* triangles,
133 uint countTriangles,
134 Parameters* parameters);
135
136 [DllImport("libvhacd")]
137 static extern unsafe uint GetNConvexHulls(void* pVHACD);
138
139 [DllImport("libvhacd")]
140 static extern unsafe void GetConvexHull(
141 void* pVHACD,
142 uint index,
143 ConvexHull* ch);
144
146
147 public VHACD()
148 {
150 }
151
152 [ContextMenu("Generate Convex Meshes")]
153 public unsafe void GenerateConvexMeshes()
154 {
155 var sp = new Stopwatch();
156 sp.Start();
157 Mesh mesh = null;
158 ExecuteOnMainThread.instance.RunOnMainThread.Enqueue(() => { mesh = GetComponent<MeshFilter>().sharedMesh; });
159 while (mesh == null) Thread.Sleep(10);
160 var vhacd = CreateVHACD();
161 var parameters = m_parameters;
162
163 Vector3[] verts = null;
164 int[] tris = null;
165 ExecuteOnMainThread.instance.RunOnMainThread.Enqueue(() =>
166 {
167 verts = mesh.vertices;
168 tris = mesh.triangles;
169 });
170
171 while (verts == null || tris == null) Thread.Sleep(10);
172 fixed (Vector3* pVerts = verts)
173 fixed (int* pTris = tris)
174 {
175 ComputeFloat(
176 vhacd,
177 (float*) pVerts, (uint) verts.Length,
178 (uint*) pTris, (uint) tris.Length / 3,
179 &parameters);
180 }
181
182 sp.Stop();
183 ExecuteOnMainThread.instance.RunOnMainThread.Enqueue(() =>
184 {
185 var numHulls = GetNConvexHulls(vhacd);
186 foreach (var index in Enumerable.Range(0, (int) numHulls))
187 {
188 ConvexHull hull;
189 GetConvexHull(vhacd, (uint) index, &hull);
190
191 var hullMesh = new Mesh();
192 var hullVerts = new Vector3[hull.m_nPoints];
193 fixed (Vector3* pHullVerts = hullVerts)
194 {
195 var pComponents = hull.m_points;
196 var pVerts = pHullVerts;
197
198 for (var pointCount = hull.m_nPoints; pointCount != 0; --pointCount)
199 {
200 pVerts->x = (float) pComponents[0];
201 pVerts->y = (float) pComponents[1];
202 pVerts->z = (float) pComponents[2];
203
204 pVerts += 1;
205 pComponents += 3;
206 }
207 }
208
209 hullMesh.SetVertices(hullVerts);
210
211 var indices = new int[hull.m_nTriangles * 3];
212 Marshal.Copy((IntPtr) hull.m_triangles, indices, 0, indices.Length);
213 hullMesh.SetTriangles(indices, 0);
214
215 var col = gameObject.AddComponent<MeshCollider>();
216 col.convex = true;
217 col.sharedMesh = hullMesh;
218 }
219
220 DestroyVHACD(vhacd);
221 Debug.Log(gameObject.name + ": " + sp.Elapsed);
222 });
223 }
224}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Definition: VHACD.cs:10
unsafe void GenerateConvexMeshes()
Definition: VHACD.cs:153
VHACD()
Definition: VHACD.cs:147
Parameters m_parameters
Definition: VHACD.cs:145
void InitFast()
Definition: VHACD.cs:35
double m_alpha
Definition: VHACD.cs:60
uint m_maxConvexHulls
Definition: VHACD.cs:95
uint m_pca
Definition: VHACD.cs:86
uint m_convexhullApproximation
Definition: VHACD.cs:91
double m_beta
Definition: VHACD.cs:63
uint m_planeDownsampling
Definition: VHACD.cs:78
uint m_oclAcceleration
Definition: VHACD.cs:93
uint m_maxNumVerticesPerCH
Definition: VHACD.cs:75
uint m_mode
Definition: VHACD.cs:89
double m_minVolumePerCH
Definition: VHACD.cs:66
uint m_resolution
Definition: VHACD.cs:72
void * m_logger
Definition: VHACD.cs:69
double m_concavity
Definition: VHACD.cs:57
void Init()
Definition: VHACD.cs:14
uint m_convexhullDownsampling
Definition: VHACD.cs:83
void * m_callback
Definition: VHACD.cs:68
bool m_projectHullVertices
Definition: VHACD.cs:99