forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialMappingSource.cs
More file actions
130 lines (109 loc) · 4.89 KB
/
SpatialMappingSource.cs
File metadata and controls
130 lines (109 loc) · 4.89 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using UnityEngine;
namespace HoloToolkit.Unity
{
public class SpatialMappingSource : MonoBehaviour
{
/// <summary>
/// Collection of surface objects that have been created for this spatial mapping source.
/// </summary>
protected List<GameObject> surfaceObjects = new List<GameObject>();
/// <summary>
/// Collection of mesh renderers that have been created for this spatial mapping source.
/// </summary>
protected List<MeshRenderer> surfaceObjectRenderers = new List<MeshRenderer>();
/// <summary>
/// Collection of mesh filters that have been created for this spatial mapping source.
/// </summary>
protected List<MeshFilter> surfaceObjectMeshFilters = new List<MeshFilter>();
/// <summary>
/// When a mesh is created we will need to create a game object with a minimum
/// set of components to contain the mesh. These are the required component types.
/// </summary>
protected Type[] componentsRequiredForSurfaceMesh =
{
typeof(MeshFilter),
typeof(MeshRenderer),
typeof(MeshCollider)
};
/// <summary>
/// Creates a new surface game object.
/// </summary>
/// <param name="mesh">The mesh to attach. Can be null.</param>
/// <param name="objectName">What to name this object.</param>
/// <param name="parentObject">What to parent this object to.</param>
/// <param name="material">What material to use to draw this object.</param>
/// <returns>The newly created game object.</returns>
protected GameObject AddSurfaceObject(Mesh mesh, string objectName, Transform parentObject)
{
GameObject surface = new GameObject(objectName, componentsRequiredForSurfaceMesh);
surface.transform.SetParent(parentObject);
surfaceObjects.Add(surface);
MeshFilter surfaceMeshFilter = surface.GetComponent<MeshFilter>();
surfaceMeshFilter.sharedMesh = mesh;
surfaceObjectMeshFilters.Add(surfaceMeshFilter);
MeshRenderer surfaceMeshRenderer = surface.GetComponent<MeshRenderer>();
surfaceMeshRenderer.sharedMaterial = SpatialMappingManager.Instance.SurfaceMaterial;
surfaceObjectRenderers.Add(surfaceMeshRenderer);
surface.layer = SpatialMappingManager.Instance.PhysicsLayer;
return surface;
}
/// <summary>
/// When we aren't using a surface object any more we need to clean
/// up the cached objects we made for the surface.
/// </summary>
/// <param name="surfaceObject">The surface we aren't using anymore.</param>
protected void RemoveSurfaceObject(GameObject surfaceObject)
{
surfaceObjects.Remove(surfaceObject);
MeshFilter filter = surfaceObject.GetComponent<MeshFilter>();
surfaceObjectMeshFilters.Remove(filter);
MeshRenderer surfaceMeshRenderer = surfaceObject.GetComponent<MeshRenderer>();
surfaceObjectRenderers.Remove(surfaceMeshRenderer);
Destroy(surfaceObject);
}
/// <summary>
/// Cleans up references to objects that we have created.
/// </summary>
protected void Cleanup()
{
// For renderers and filters, clearing the lists is sufficient,
// since renderers and filters are attached to the surface objects
// that we will call destroy on.
surfaceObjectRenderers.Clear();
surfaceObjectMeshFilters.Clear();
for (int index = 0; index < surfaceObjects.Count; index++)
{
Destroy(surfaceObjects[index]);
}
surfaceObjects.Clear();
}
/// <summary>
/// Gets all mesh filters that have a valid mesh.
/// </summary>
/// <returns>A list of filters, each with a mesh containing at least one triangle.</returns>
virtual public List<MeshFilter> GetMeshFilters()
{
List<MeshFilter> meshFilters = new List<MeshFilter>();
foreach (MeshFilter filter in surfaceObjectMeshFilters)
{
if (filter != null && filter.sharedMesh != null && filter.sharedMesh.vertexCount > 2)
{
meshFilters.Add(filter);
}
}
return meshFilters;
}
/// <summary>
/// Gets all mesh renderers that have been created.
/// </summary>
/// <returns></returns>
virtual public List<MeshRenderer> GetMeshRenderers()
{
return surfaceObjectRenderers;
}
}
}