AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
RemoteMappingManager.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using System.Linq;
6 using UnityEngine;
7 
8 #if UNITY_WSA || UNITY_STANDALONE_WIN
9 using UnityEngine.Windows.Speech;
10 #endif
11 
12 namespace HoloToolkit.Unity.SpatialMapping
13 {
14  [RequireComponent(typeof(RemoteMeshTarget))]
15  public partial class RemoteMappingManager : Singleton<RemoteMappingManager>
16  {
17  [Tooltip("Key to press in editor to enable spatial mapping over the network.")]
18  public KeyCode RemoteMappingKey = KeyCode.N;
19 
20  [Tooltip("Keyword for sending meshes from HoloLens to Unity over the network.")]
21  public string SendMeshesKeyword = "send meshes";
22 
23 #if UNITY_EDITOR || UNITY_STANDALONE
24  private RemoteMeshTarget remoteMeshTarget;
28 #endif
29 
30 #if UNITY_WSA || UNITY_STANDALONE_WIN
31  private KeywordRecognizer keywordRecognizer;
35 
39  private Dictionary<string, System.Action> keywordCollection;
40 #endif
41 
42  // Use this for initialization.
43  private void Start()
44  {
45 #if UNITY_WSA || UNITY_STANDALONE_WIN
46  // Create our keyword collection.
47  keywordCollection = new Dictionary<string, System.Action> { { SendMeshesKeyword, SendMeshes } };
48 
49  // Tell the KeywordRecognizer about our keywords.
50  keywordRecognizer = new KeywordRecognizer(keywordCollection.Keys.ToArray());
51  // Register a callback for the KeywordRecognizer and start recognizing.
52  keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
53  keywordRecognizer.Start();
54 #endif
55 
56 #if UNITY_EDITOR || UNITY_STANDALONE
57  remoteMeshTarget = GetComponent<RemoteMeshTarget>();
58 
59  if (remoteMeshTarget != null && SpatialMappingManager.Instance.Source == null)
60  {
61  // Use the network-based mapping source to receive meshes in the Unity editor.
62  SpatialMappingManager.Instance.SetSpatialMappingSource(remoteMeshTarget);
63  }
64 #endif
65  }
66 
67  // Called every frame by the Unity engine.
68  private void Update()
69  {
70 #if UNITY_EDITOR || UNITY_STANDALONE
71  // Use the 'network' sourced mesh.
72  if (Input.GetKeyUp(RemoteMappingKey))
73  {
74  SpatialMappingManager.Instance.SetSpatialMappingSource(remoteMeshTarget);
75  }
76 #endif
77  }
78 
79 #if UNITY_WSA || UNITY_STANDALONE_WIN
80  private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
85  {
86  System.Action keywordAction;
87 
88  if (keywordCollection.TryGetValue(args.text, out keywordAction))
89  {
90  keywordAction.Invoke();
91  }
92  }
93 #endif
94 
98  private void SendMeshes()
99  {
100 #if !UNITY_EDITOR && UNITY_WSA
101  List<MeshFilter> MeshFilters = SpatialMappingManager.Instance.GetMeshFilters();
102  for (int index = 0; index < MeshFilters.Count; index++)
103  {
104  List<Mesh> meshesToSend = new List<Mesh>();
105  MeshFilter filter = MeshFilters[index];
106  Mesh source = filter.sharedMesh;
107  Mesh clone = new Mesh();
108  List<Vector3> verts = new List<Vector3>();
109  verts.AddRange(source.vertices);
110 
111  for(int vertIndex=0; vertIndex < verts.Count; vertIndex++)
112  {
113  verts[vertIndex] = filter.transform.TransformPoint(verts[vertIndex]);
114  }
115 
116  clone.SetVertices(verts);
117  clone.SetTriangles(source.triangles, 0);
118  meshesToSend.Add(clone);
119  byte[] serialized = SimpleMeshSerializer.Serialize(meshesToSend);
120  RemoteMeshSource.Instance.SendData(serialized);
121  }
122 #endif
123  }
124  }
125 }
SimpleMeshSerializer converts a UnityEngine.Mesh object to and from an array of bytes. This class saves minimal mesh data (vertices and triangle indices) in the following format: File header: vertex count (32 bit integer), triangle count (32 bit integer) Vertex list: vertex.x, vertex.y, vertex.z (all 32 bit float) Triangle index list: 32 bit integers
static byte [] Serialize(IEnumerable< Mesh > meshes)
Serializes a list of Mesh objects into a byte array.
RemoteMeshTarget will listen for meshes being sent from a remote system (HoloLens). It is intended to run in the Unity Editor with exactly one HoloLens device sending data.
RemoteMeshSource will try to send meshes from the HoloLens to a remote system that is running the Uni...
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
The SpatialMappingManager class allows applications to use a SurfaceObserver or a stored Spatial Mapp...
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14