forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteMappingManager.cs
More file actions
125 lines (108 loc) · 4.48 KB
/
RemoteMappingManager.cs
File metadata and controls
125 lines (108 loc) · 4.48 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_WSA || UNITY_STANDALONE_WIN
using UnityEngine.Windows.Speech;
#endif
namespace HoloToolkit.Unity.SpatialMapping
{
[RequireComponent(typeof(RemoteMeshTarget))]
public partial class RemoteMappingManager : Singleton<RemoteMappingManager>
{
[Tooltip("Key to press in editor to enable spatial mapping over the network.")]
public KeyCode RemoteMappingKey = KeyCode.N;
[Tooltip("Keyword for sending meshes from HoloLens to Unity over the network.")]
public string SendMeshesKeyword = "send meshes";
#if UNITY_EDITOR || UNITY_STANDALONE
/// <summary>
/// Receives meshes collected over the network.
/// </summary>
private RemoteMeshTarget remoteMeshTarget;
#endif
#if UNITY_WSA || UNITY_STANDALONE_WIN
/// <summary>
/// Used for voice commands.
/// </summary>
private KeywordRecognizer keywordRecognizer;
/// <summary>
/// Collection of supported keywords and their associated actions.
/// </summary>
private Dictionary<string, System.Action> keywordCollection;
#endif
// Use this for initialization.
private void Start()
{
#if UNITY_WSA || UNITY_STANDALONE_WIN
// Create our keyword collection.
keywordCollection = new Dictionary<string, System.Action> { { SendMeshesKeyword, SendMeshes } };
// Tell the KeywordRecognizer about our keywords.
keywordRecognizer = new KeywordRecognizer(keywordCollection.Keys.ToArray());
// Register a callback for the KeywordRecognizer and start recognizing.
keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
keywordRecognizer.Start();
#endif
#if UNITY_EDITOR || UNITY_STANDALONE
remoteMeshTarget = GetComponent<RemoteMeshTarget>();
if (remoteMeshTarget != null && SpatialMappingManager.Instance.Source == null)
{
// Use the network-based mapping source to receive meshes in the Unity editor.
SpatialMappingManager.Instance.SetSpatialMappingSource(remoteMeshTarget);
}
#endif
}
// Called every frame by the Unity engine.
private void Update()
{
#if UNITY_EDITOR || UNITY_STANDALONE
// Use the 'network' sourced mesh.
if (Input.GetKeyUp(RemoteMappingKey))
{
SpatialMappingManager.Instance.SetSpatialMappingSource(remoteMeshTarget);
}
#endif
}
#if UNITY_WSA || UNITY_STANDALONE_WIN
/// <summary>
/// Called by keywordRecognizer when a phrase we registered for is heard.
/// </summary>
/// <param name="args">Information about the recognition event.</param>
private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
{
System.Action keywordAction;
if (keywordCollection.TryGetValue(args.text, out keywordAction))
{
keywordAction.Invoke();
}
}
#endif
/// <summary>
/// Sends the spatial mapping surfaces from the HoloLens to a remote system running the Unity editor.
/// </summary>
private void SendMeshes()
{
#if !UNITY_EDITOR && UNITY_WSA
List<MeshFilter> MeshFilters = SpatialMappingManager.Instance.GetMeshFilters();
for (int index = 0; index < MeshFilters.Count; index++)
{
List<Mesh> meshesToSend = new List<Mesh>();
MeshFilter filter = MeshFilters[index];
Mesh source = filter.sharedMesh;
Mesh clone = new Mesh();
List<Vector3> verts = new List<Vector3>();
verts.AddRange(source.vertices);
for(int vertIndex=0; vertIndex < verts.Count; vertIndex++)
{
verts[vertIndex] = filter.transform.TransformPoint(verts[vertIndex]);
}
clone.SetVertices(verts);
clone.SetTriangles(source.triangles, 0);
meshesToSend.Add(clone);
byte[] serialized = SimpleMeshSerializer.Serialize(meshesToSend);
RemoteMeshSource.Instance.SendData(serialized);
}
#endif
}
}
}