This repository was archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathMeshClassificationFracking.cs
369 lines (313 loc) · 14.4 KB
/
MeshClassificationFracking.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
using UnityEngine.XR;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
#if UNITY_IOS
using UnityEngine.XR.ARKit;
#endif // UNITY_IOS && !UNITY_EDITOR
using Object = UnityEngine.Object;
namespace UnityEngine.XR.ARFoundation.Samples
{
public class MeshClassificationFracking : MonoBehaviour
{
/// <summary>
/// The number of mesh classifications detected.
/// </summary>
const int k_NumClassifications = 8;
/// <summary>
/// The mesh manager for the scene.
/// </summary>
public ARMeshManager m_MeshManager;
/// <summary>
/// The mesh prefab for the None classification.
/// </summary>
public MeshFilter m_NoneMeshPrefab;
/// <summary>
/// The mesh prefab for the Wall classification.
/// </summary>
public MeshFilter m_WallMeshPrefab;
/// <summary>
/// The mesh prefab for the Floor classification.
/// </summary>
public MeshFilter m_FloorMeshPrefab;
/// <summary>
/// The mesh prefab for the Ceiling classification.
/// </summary>
public MeshFilter m_CeilingMeshPrefab;
/// <summary>
/// The mesh prefab for the Table classification.
/// </summary>
public MeshFilter m_TableMeshPrefab;
/// <summary>
/// The mesh prefab for the Seat classification.
/// </summary>
public MeshFilter m_SeatMeshPrefab;
/// <summary>
/// The mesh prefab for the Window classification.
/// </summary>
public MeshFilter m_WindowMeshPrefab;
/// <summary>
/// The mesh prefab for the Door classification.
/// </summary>
public MeshFilter m_DoorMeshPrefab;
#if UNITY_IOS
/// <summary>
/// A mapping from tracking ID to instantiated mesh filters.
/// </summary>
readonly Dictionary<TrackableId, MeshFilter[]> m_MeshFrackingMap = new Dictionary<TrackableId, MeshFilter[]>();
/// <summary>
/// The delegate to call to breakup a mesh.
/// </summary>
Action<MeshFilter> m_BreakupMeshAction;
/// <summary>
/// The delegate to call to update a mesh.
/// </summary>
Action<MeshFilter> m_UpdateMeshAction;
/// <summary>
/// The delegate to call to remove a mesh.
/// </summary>
Action<MeshFilter> m_RemoveMeshAction;
/// <summary>
/// An array to store the triangle vertices of the base mesh.
/// </summary>
readonly List<int> m_BaseTriangles = new List<int>();
/// <summary>
/// An array to store the triangle vertices of the classified mesh.
/// </summary>
readonly List<int> m_ClassifiedTriangles = new List<int>();
readonly List<Material> m_SharedPrefabMats = new List<Material>();
const float k_ShowAlpha = 0.3f;
bool m_ShowingMeshes = false;
/// <summary>
/// On awake, set up the mesh filter delegates.
/// </summary>
void Awake()
{
m_BreakupMeshAction = new Action<MeshFilter>(BreakupMesh);
m_UpdateMeshAction = new Action<MeshFilter>(UpdateMesh);
m_RemoveMeshAction = new Action<MeshFilter>(RemoveMesh);
m_SharedPrefabMats.Add(m_NoneMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial);
m_SharedPrefabMats.Add(m_WallMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial);
m_SharedPrefabMats.Add(m_FloorMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial);
m_SharedPrefabMats.Add((m_CeilingMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial));
m_SharedPrefabMats.Add(m_TableMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial);
m_SharedPrefabMats.Add(m_SeatMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial);
m_SharedPrefabMats.Add(m_WindowMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial);
m_SharedPrefabMats.Add(m_DoorMeshPrefab.GetComponent<MeshRenderer>().sharedMaterial);
}
/// <summary>
/// On enable, subscribe to the meshes changed event.
/// </summary>
void OnEnable()
{
Debug.Assert(m_MeshManager != null, "mesh manager cannot be null");
m_MeshManager.meshesChanged += OnMeshesChanged;
}
/// <summary>
/// On disable, unsubscribe from the meshes changed event.
/// </summary>
void OnDisable()
{
Debug.Assert(m_MeshManager != null, "mesh manager cannot be null");
m_MeshManager.meshesChanged -= OnMeshesChanged;
}
/// <summary>
/// When the meshes change, update the scene meshes.
/// </summary>
void OnMeshesChanged(ARMeshesChangedEventArgs args)
{
if (args.added != null)
{
args.added.ForEach(m_BreakupMeshAction);
}
if (args.updated != null)
{
args.updated.ForEach(m_UpdateMeshAction);
}
if (args.removed != null)
{
args.removed.ForEach(m_RemoveMeshAction);
}
}
/// <summary>
/// Parse the trackable ID from the mesh filter name.
/// </summary>
/// <param name="meshFilterName">The mesh filter name containing the trackable ID.</param>
/// <returns>
/// The trackable ID parsed from the string.
/// </returns>
TrackableId ExtractTrackableId(string meshFilterName)
{
string[] nameSplit = meshFilterName.Split(' ');
return new TrackableId(nameSplit[1]);
}
/// <summary>
/// Given a base mesh, the face classifications for all faces in the mesh, and a single face classification to
/// extract, extract into a new mesh only the faces that have the selected face classification.
/// </summary>
/// <param name="baseMesh">The original base mesh.</param>
/// <param name="faceClassifications">The array of face classifications for each triangle in the
/// <paramref name="baseMesh"/></param>
/// <param name="selectedMeshClassification">A single classification to extract the faces from the
/// <paramref="baseMesh"/>into the <paramref name="classifiedMesh"/></param>
/// <param name="classifiedMesh">The output mesh to be updated with the extracted mesh.</param>
void ExtractClassifiedMesh(Mesh baseMesh, NativeArray<ARMeshClassification> faceClassifications, ARMeshClassification selectedMeshClassification, Mesh classifiedMesh)
{
// Count the number of faces matching the selected classification.
int classifiedFaceCount = 0;
for (int i = 0; i < faceClassifications.Length; ++i)
{
if (faceClassifications[i] == selectedMeshClassification)
{
++classifiedFaceCount;
}
}
// Clear the existing mesh.
classifiedMesh.Clear();
// If there were matching face classifications, build a new mesh from the base mesh.
if (classifiedFaceCount > 0)
{
baseMesh.GetTriangles(m_BaseTriangles, 0);
Debug.Assert(m_BaseTriangles.Count == (faceClassifications.Length * 3),
"unexpected mismatch between triangle count and face classification count");
m_ClassifiedTriangles.Clear();
m_ClassifiedTriangles.Capacity = classifiedFaceCount * 3;
for (int i = 0; i < faceClassifications.Length; ++i)
{
if (faceClassifications[i] == selectedMeshClassification)
{
int baseTriangleIndex = i * 3;
m_ClassifiedTriangles.Add(m_BaseTriangles[baseTriangleIndex + 0]);
m_ClassifiedTriangles.Add(m_BaseTriangles[baseTriangleIndex + 1]);
m_ClassifiedTriangles.Add(m_BaseTriangles[baseTriangleIndex + 2]);
}
}
classifiedMesh.vertices = baseMesh.vertices;
classifiedMesh.normals = baseMesh.normals;
classifiedMesh.SetTriangles(m_ClassifiedTriangles, 0);
}
}
/// <summary>
/// Break up a single mesh with multiple face classifications into submeshes, each with an unique and uniform mesh
/// classification.
/// </summary>
/// <param name="meshFilter">The mesh filter for the base mesh with multiple face classifications.</param>
void BreakupMesh(MeshFilter meshFilter)
{
XRMeshSubsystem meshSubsystem = m_MeshManager.subsystem as XRMeshSubsystem;
if (meshSubsystem == null)
{
return;
}
var meshId = ExtractTrackableId(meshFilter.name);
var faceClassifications = meshSubsystem.GetFaceClassifications(meshId, Allocator.Persistent);
if (!faceClassifications.IsCreated)
{
return;
}
using (faceClassifications)
{
if (faceClassifications.Length <= 0)
{
return;
}
var parent = meshFilter.transform.parent;
MeshFilter[] meshFilters = new MeshFilter[k_NumClassifications];
meshFilters[(int)ARMeshClassification.None] = (m_NoneMeshPrefab == null) ? null : Instantiate(m_NoneMeshPrefab, parent);
meshFilters[(int)ARMeshClassification.Wall] = (m_WallMeshPrefab == null) ? null : Instantiate(m_WallMeshPrefab, parent);
meshFilters[(int)ARMeshClassification.Floor] = (m_FloorMeshPrefab == null) ? null : Instantiate(m_FloorMeshPrefab, parent);
meshFilters[(int)ARMeshClassification.Ceiling] = (m_CeilingMeshPrefab == null) ? null : Instantiate(m_CeilingMeshPrefab, parent);
meshFilters[(int)ARMeshClassification.Table] = (m_TableMeshPrefab == null) ? null : Instantiate(m_TableMeshPrefab, parent);
meshFilters[(int)ARMeshClassification.Seat] = (m_SeatMeshPrefab == null) ? null : Instantiate(m_SeatMeshPrefab, parent);
meshFilters[(int)ARMeshClassification.Window] = (m_WindowMeshPrefab == null) ? null : Instantiate(m_WindowMeshPrefab, parent);
meshFilters[(int)ARMeshClassification.Door] = (m_DoorMeshPrefab == null) ? null : Instantiate(m_DoorMeshPrefab, parent);
m_MeshFrackingMap[meshId] = meshFilters;
var baseMesh = meshFilter.sharedMesh;
for (int i = 0; i < k_NumClassifications; ++i)
{
var classifiedMeshFilter = meshFilters[i];
if (classifiedMeshFilter != null)
{
var classifiedMesh = classifiedMeshFilter.mesh;
ExtractClassifiedMesh(baseMesh, faceClassifications, (ARMeshClassification)i, classifiedMesh);
meshFilters[i].mesh = classifiedMesh;
}
}
}
}
/// <summary>
/// Update the submeshes corresponding to the single mesh with multiple face classifications into submeshes.
/// </summary>
/// <param name="meshFilter">The mesh filter for the base mesh with multiple face classifications.</param>
void UpdateMesh(MeshFilter meshFilter)
{
XRMeshSubsystem meshSubsystem = m_MeshManager.subsystem as XRMeshSubsystem;
if (meshSubsystem == null)
{
return;
}
var meshId = ExtractTrackableId(meshFilter.name);
var faceClassifications = meshSubsystem.GetFaceClassifications(meshId, Allocator.Persistent);
if (!faceClassifications.IsCreated)
{
return;
}
using (faceClassifications)
{
if (faceClassifications.Length <= 0)
{
return;
}
var meshFilters = m_MeshFrackingMap[meshId];
var baseMesh = meshFilter.sharedMesh;
for (int i = 0; i < k_NumClassifications; ++i)
{
var classifiedMeshFilter = meshFilters[i];
if (classifiedMeshFilter != null)
{
var classifiedMesh = classifiedMeshFilter.mesh;
ExtractClassifiedMesh(baseMesh, faceClassifications, (ARMeshClassification)i, classifiedMesh);
meshFilters[i].mesh = classifiedMesh;
}
}
}
}
/// <summary>
/// Remove the submeshes corresponding to the single mesh.
/// </summary>
/// <param name="meshFilter">The mesh filter for the base mesh with multiple face classifications.</param>
void RemoveMesh(MeshFilter meshFilter)
{
var meshId = ExtractTrackableId(meshFilter.name);
var meshFilters = m_MeshFrackingMap[meshId];
for (int i = 0; i < k_NumClassifications; ++i)
{
var classifiedMeshFilter = meshFilters[i];
if (classifiedMeshFilter != null)
{
Object.Destroy(classifiedMeshFilter);
}
}
m_MeshFrackingMap.Remove(meshId);
}
public void ToggleVisability()
{
foreach (Material mat in m_SharedPrefabMats)
{
if (m_ShowingMeshes)
{
mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, 0);
}
else
{
mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, k_ShowAlpha);
}
}
m_ShowingMeshes = !m_ShowingMeshes;
}
#endif // UNITY_IOS
}
}