Skip to content

Commit fe8724a

Browse files
authored
Merge pull request #297 from darax/master
fix SpatialUnderstanding doesn't work in emulator
2 parents 2192cc4 + 9931329 commit fe8724a

File tree

2 files changed

+52
-87
lines changed

2 files changed

+52
-87
lines changed

Assets/HoloToolkit/SpatialMapping/Scripts/SpatialMappingObserver.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public class SpatialMappingObserver : SpatialMappingSource
4343
/// </summary>
4444
public event SurfaceObserver.SurfaceChangedDelegate SurfaceChanged;
4545

46+
/// <summary>
47+
/// Event for hooking when the data for a surface is ready.
48+
/// </summary>
49+
public event SurfaceObserver.SurfaceDataReadyDelegate DataReady;
50+
4651
/// <summary>
4752
/// Our Surface Observer object for generating/updating Spatial Mapping data.
4853
/// </summary>
@@ -201,6 +206,11 @@ private void SurfaceObserver_OnDataReady(SurfaceData cookedData, bool outputWrit
201206
}
202207

203208
surfaceWorkOutstanding = false;
209+
SurfaceObserver.SurfaceDataReadyDelegate dataReady = DataReady;
210+
if (dataReady != null)
211+
{
212+
dataReady(cookedData, outputWritten, elapsedCookTimeSeconds);
213+
}
204214
}
205215

206216
private void CleanupSurface(GameObject surface)

Assets/HoloToolkit/SpatialUnderstanding/Scripts/SpatialUnderstandingSourceMesh.cs

Lines changed: 42 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using UnityEngine;
5-
using System.Collections;
65
using System;
7-
using System.Runtime.InteropServices;
86
using System.Collections.Generic;
97
using UnityEngine.VR.WSA;
108

@@ -32,27 +30,52 @@ private void Start()
3230
SpatialMappingObserver mappingObserver = SpatialMappingManager.Instance.Source as SpatialMappingObserver;
3331
if (mappingObserver != null)
3432
{
35-
mappingObserver.SurfaceChanged += OnSurfaceChanged;
33+
mappingObserver.DataReady += MappingObserver_DataReady;
34+
mappingObserver.SurfaceChanged += MappingObserver_SurfaceChanged;
3635
}
3736
}
3837

39-
private int FindMeshIndexInInputMeshList(int meshID)
38+
/// <summary>
39+
/// Called when a surface is going to be added, removed, or updated.
40+
/// We only care about removal so we can remove our internal copy of the surface mesh.
41+
/// </summary>
42+
/// <param name="surfaceId">The surface ID that is being added/removed/updated</param>
43+
/// <param name="changeType">Added | Removed | Updated</param>
44+
/// <param name="bounds">The world volume the mesh is in.</param>
45+
/// <param name="updateTime">When the mesh was updated.</param>
46+
private void MappingObserver_SurfaceChanged(SurfaceId surfaceId, SurfaceChange changeType, Bounds bounds, DateTime updateTime)
4047
{
41-
for (int i = 0; i < inputMeshList.Count; ++i)
48+
// We only need to worry about removing meshes from our list. Adding and updating is
49+
// done when the mesh data is actually ready.
50+
if (changeType == SurfaceChange.Removed)
4251
{
43-
if (inputMeshList[i].MeshID == meshID)
52+
int meshIndex = FindMeshIndexInInputMeshList(surfaceId.handle);
53+
if(meshIndex >= 0)
4454
{
45-
return i;
55+
inputMeshList.RemoveAt(meshIndex);
4656
}
4757
}
48-
return -1;
4958
}
5059

51-
private int FindSurfaceIndexInList(int surfaceObjectID, List<SpatialMappingSource.SurfaceObject> surfaceObjects)
60+
/// <summary>
61+
/// Called by the surface observer when a mesh has had its data changed.
62+
/// </summary>
63+
/// <param name="bakedData">The data describing the surface.</param>
64+
/// <param name="outputWritten">If the data was successfully updated.</param>
65+
/// <param name="elapsedBakeTimeSeconds">How long it took to update.</param>
66+
private void MappingObserver_DataReady(SurfaceData bakedData, bool outputWritten, float elapsedBakeTimeSeconds)
5267
{
53-
for (int i = 0; i < surfaceObjects.Count; ++i)
68+
if (!outputWritten)
69+
return;
70+
71+
AddOrUpdateMeshInList(bakedData);
72+
}
73+
74+
private int FindMeshIndexInInputMeshList(int meshID)
75+
{
76+
for (int i = 0; i < inputMeshList.Count; ++i)
5477
{
55-
if (surfaceObjects[i].ID == surfaceObjectID)
78+
if (inputMeshList[i].MeshID == meshID)
5679
{
5780
return i;
5881
}
@@ -69,16 +92,14 @@ private int FindSurfaceIndexInList(int surfaceObjectID, List<SpatialMappingSourc
6992
/// <param name="surfaceObjects">The list of surfaceObjects</param>
7093
/// <param name="meshDataIndex">Index into the locally stored mesh data list</param>
7194
private void AddOrUpdateMeshInList(
72-
int surfaceId,
73-
int surfaceObjectIndex,
74-
List<SpatialMappingSource.SurfaceObject> surfaceObjects,
75-
int meshDataIndex = -1)
95+
SurfaceData bakedData)
7696
{
97+
SurfaceId surfaceId = bakedData.id;
98+
MeshFilter meshFilter = bakedData.outputMesh;
99+
int meshDataIndex = FindMeshIndexInInputMeshList(surfaceId.handle);
77100
SpatialUnderstandingDll.MeshData meshData = new SpatialUnderstandingDll.MeshData();
78-
int meshUpdateID = (meshDataIndex > 0) ? (inputMeshList[meshDataIndex].LastUpdateID + 1) : 0;
79-
80-
// Checks.
81-
MeshFilter meshFilter = surfaceObjects[surfaceObjectIndex].Filter;
101+
int meshUpdateID = (meshDataIndex >= 0) ? (inputMeshList[meshDataIndex].LastUpdateID + 1) : 1;
102+
82103
if ((meshFilter != null) &&
83104
(meshFilter.mesh != null) &&
84105
(meshFilter.mesh.triangles.Length > 0))
@@ -87,12 +108,12 @@ private void AddOrUpdateMeshInList(
87108
meshFilter.mesh.RecalculateNormals();
88109

89110
// Convert
90-
meshData.CopyFrom(meshFilter, surfaceId, meshUpdateID);
111+
meshData.CopyFrom(meshFilter, surfaceId.handle, meshUpdateID);
91112
}
92113
else
93114
{
94115
// No filter yet, add as an empty mesh (will be updated later in the update loop)
95-
meshData.CopyFrom(null, surfaceId, meshUpdateID);
116+
meshData.CopyFrom(null, surfaceId.handle, meshUpdateID);
96117
}
97118

98119
// And add it (unless an index of an update item is specified)
@@ -106,70 +127,6 @@ private void AddOrUpdateMeshInList(
106127
}
107128
}
108129

109-
private void OnSurfaceChanged(SurfaceId surfaceId, SurfaceChange changeType, Bounds bounds, DateTime updateTime)
110-
{
111-
// Find the surface
112-
List<SpatialMappingSource.SurfaceObject> surfaceObjects = SpatialMappingManager.Instance.GetSurfaceObjects();
113-
114-
// Find it (in both lists)
115-
int surfaceObjectIndex = FindSurfaceIndexInList(surfaceId.handle, surfaceObjects);
116-
int meshDataIndex = FindMeshIndexInInputMeshList(surfaceId.handle);
117-
118-
// Deal with the change
119-
switch (changeType)
120-
{
121-
case SurfaceChange.Added:
122-
{
123-
if (surfaceObjectIndex >= 0 && surfaceObjectIndex < surfaceObjects.Count)
124-
{
125-
AddOrUpdateMeshInList(surfaceId.handle, surfaceObjectIndex, surfaceObjects);
126-
}
127-
}
128-
break;
129-
case SurfaceChange.Removed:
130-
{
131-
if (meshDataIndex >= 0 && meshDataIndex < inputMeshList.Count)
132-
{
133-
inputMeshList.RemoveAt(meshDataIndex);
134-
}
135-
}
136-
break;
137-
case SurfaceChange.Updated:
138-
{
139-
if ((surfaceObjectIndex >= 0 && surfaceObjectIndex < surfaceObjects.Count) &&
140-
(meshDataIndex >= 0 && meshDataIndex < inputMeshList.Count))
141-
{
142-
AddOrUpdateMeshInList(surfaceId.handle, surfaceObjectIndex, surfaceObjects, meshDataIndex);
143-
}
144-
}
145-
break;
146-
}
147-
}
148-
149-
/// <summary>
150-
/// Update the internal mesh list from spatial mapping's surface object list.
151-
/// </summary>
152-
private void UpdateInputMeshList()
153-
{
154-
List<SpatialMappingSource.SurfaceObject> surfaceObjects = SpatialMappingManager.Instance.GetSurfaceObjects();
155-
156-
// If we have any meshes with zero indices, but with filters
157-
// that indicates that the filters are now ready to be processed
158-
for (int i = 0; i < inputMeshList.Count; ++i)
159-
{
160-
if ((inputMeshList[i].Indices == null) ||
161-
(inputMeshList[i].Indices.Length == 0))
162-
{
163-
int surfaceObjectIndex = FindSurfaceIndexInList(inputMeshList[i].MeshID, surfaceObjects);
164-
if ((surfaceObjectIndex > 0) &&
165-
(surfaceObjects[surfaceObjectIndex].UpdateID != inputMeshList[i].LastUpdateID))
166-
{
167-
AddOrUpdateMeshInList(inputMeshList[i].MeshID, surfaceObjectIndex, surfaceObjects, i);
168-
}
169-
}
170-
}
171-
}
172-
173130
/// <summary>
174131
/// Update the internal mesh list and provides an array pointer in
175132
/// the form the dll will accept.
@@ -179,8 +136,6 @@ private void UpdateInputMeshList()
179136
/// <returns></returns>
180137
public bool GetInputMeshList(out int meshCount, out IntPtr meshList)
181138
{
182-
// First, update our mesh data
183-
UpdateInputMeshList();
184139
if (inputMeshList.Count == 0)
185140
{
186141
meshCount = 0;

0 commit comments

Comments
 (0)