Skip to content

Commit 6add245

Browse files
author
Stephen Hodgson
authored
Merge pull request #6 from Microsoft/master
Merge MSFT_HoloToolkit-master
2 parents 3665ff5 + 9fdde95 commit 6add245

File tree

4 files changed

+280
-102
lines changed

4 files changed

+280
-102
lines changed

Assets/HoloToolkit/SpatialMapping/Scripts/TapToPlace.cs

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

44
using UnityEngine;
5-
using UnityEngine.VR.WSA;
6-
using UnityEngine.VR.WSA.Persistence;
75

86
namespace HoloToolkit.Unity
97
{
@@ -24,144 +22,84 @@ public partial class TapToPlace : MonoBehaviour
2422
public string SavedAnchorFriendlyName = "SavedAnchorFriendlyName";
2523

2624
/// <summary>
27-
/// Keeps track of anchors stored on local device.
25+
/// Manages persisted anchors.
2826
/// </summary>
29-
WorldAnchorStore anchorStore = null;
27+
private WorldAnchorManager anchorManager;
3028

3129
/// <summary>
32-
/// Locally saved wold anchor.
30+
/// Controls spatial mapping. In this script we access spatialMappingManager
31+
/// to control rendering and to access the physics layer mask.
3332
/// </summary>
34-
WorldAnchor savedAnchor;
35-
36-
bool placing = false;
37-
38-
void Start()
39-
{
40-
WorldAnchorStore.GetAsync(AnchorStoreReady);
41-
}
33+
private SpatialMappingManager spatialMappingManager;
4234

4335
/// <summary>
44-
/// Called when the local anchor store is ready.
36+
/// Keeps track of if the user is moving the object or not.
4537
/// </summary>
46-
/// <param name="store"></param>
47-
void AnchorStoreReady(WorldAnchorStore store)
48-
{
49-
anchorStore = store;
38+
private bool placing;
5039

51-
// Try to load a previously saved world anchor.
52-
savedAnchor = anchorStore.Load(SavedAnchorFriendlyName, gameObject);
53-
if (savedAnchor == null)
54-
{
55-
// Either world anchor was not saved / does not exist or has a different name.
56-
Debug.Log(gameObject.name + " : "+ "World anchor could not be loaded for this game object. Creating a new anchor.");
57-
58-
// Create anchor since one does not exist.
59-
CreateAnchor();
60-
}
61-
else
62-
{
63-
Debug.Log(gameObject.name + " : " + "World anchor loaded from anchor store and updated for this game object.");
64-
}
65-
}
66-
67-
// Called by GazeGestureManager when the user performs a tap gesture.
68-
void OnSelect()
40+
private void Start()
6941
{
70-
if (SpatialMappingManager.Instance != null)
42+
// Make sure we have all the components in the scene we need.
43+
anchorManager = WorldAnchorManager.Instance;
44+
if (anchorManager == null)
7145
{
72-
// On each tap gesture, toggle whether the user is in placing mode.
73-
placing = !placing;
74-
75-
// If the user is in placing mode, display the spatial mapping mesh.
76-
if (placing)
77-
{
78-
SpatialMappingManager.Instance.DrawVisualMeshes = true;
79-
80-
Debug.Log(gameObject.name + " : " + "Removing existing world anchor if any.");
81-
82-
// Remove existing world anchor when moving an object.
83-
DestroyImmediate(gameObject.GetComponent<WorldAnchor>());
84-
85-
// Delete existing world anchor from anchor store when moving an object.
86-
if (anchorStore != null)
87-
{
88-
anchorStore.Delete(SavedAnchorFriendlyName);
89-
}
90-
}
91-
// If the user is not in placing mode, hide the spatial mapping mesh.
92-
else
93-
{
94-
SpatialMappingManager.Instance.DrawVisualMeshes = false;
95-
96-
// Add world anchor when object placement is done.
97-
CreateAnchor();
98-
}
46+
Debug.LogError("This script expects that you have a WorldAnchorManager component in your scene.");
9947
}
100-
else
101-
{
102-
Debug.Log("TapToPlace requires spatial mapping. Try adding SpatialMapping prefab to project.");
103-
}
104-
}
105-
106-
private void CreateAnchor()
107-
{
108-
// NOTE: It's good practice to ensure your parent hierarchy or root game object does not already have a World Anchor.
109-
// You can handle this in a way that works best for your application.
110-
// For example: gameObject.transform.root.GetComponent<WorldAnchor>();
11148

112-
// Add the world anchor component when done moving an object.
113-
WorldAnchor anchor = gameObject.AddComponent<WorldAnchor>();
114-
if (anchor.isLocated)
49+
spatialMappingManager = SpatialMappingManager.Instance;
50+
if (spatialMappingManager == null)
11551
{
116-
SaveAnchor(anchor);
52+
Debug.LogError("This script expects that you have a SpatialMappingManager component in your scene.");
11753
}
118-
else
119-
{
120-
anchor.OnTrackingChanged += Anchor_OnTrackingChanged;
121-
}
122-
}
12354

124-
private void SaveAnchor(WorldAnchor anchor)
125-
{
126-
// Save the anchor to persist holograms across sessions.
127-
if (anchorStore.Save(SavedAnchorFriendlyName, anchor))
55+
if (anchorManager != null && spatialMappingManager != null)
12856
{
129-
Debug.Log(gameObject.name + " : " + "World anchor saved successfully.");
57+
anchorManager.AttachAnchor(this.gameObject, SavedAnchorFriendlyName);
13058
}
13159
else
13260
{
133-
Debug.LogError(gameObject.name + " : " + "World anchor save failed.");
61+
// If we don't have what we need to proceed, we may as well remove ourselves.
62+
Destroy(this);
13463
}
13564
}
13665

137-
private void Anchor_OnTrackingChanged(WorldAnchor self, bool located)
66+
// Called by GazeGestureManager when the user performs a tap gesture.
67+
public void OnSelect()
13868
{
139-
if (located)
69+
// On each tap gesture, toggle whether the user is in placing mode.
70+
placing = !placing;
71+
72+
// If the user is in placing mode, display the spatial mapping mesh.
73+
if (placing)
14074
{
141-
Debug.Log(gameObject.name + " : " + "World anchor located successfully.");
75+
spatialMappingManager.DrawVisualMeshes = true;
76+
77+
Debug.Log(gameObject.name + " : Removing existing world anchor if any.");
14278

143-
SaveAnchor(self);
144-
self.OnTrackingChanged -= Anchor_OnTrackingChanged;
79+
anchorManager.RemoveAnchor(gameObject);
14580
}
81+
// If the user is not in placing mode, hide the spatial mapping mesh.
14682
else
14783
{
148-
Debug.LogError(gameObject.name + " : " + "World anchor failed to locate.");
84+
spatialMappingManager.DrawVisualMeshes = false;
85+
// Add world anchor when object placement is done.
86+
anchorManager.AttachAnchor(gameObject, SavedAnchorFriendlyName);
14987
}
15088
}
15189

152-
void Update()
90+
private void Update()
15391
{
154-
// If the user is in placing mode,
155-
// update the placement to match the user's gaze.
156-
if (placing)
92+
// If the user is in placing mode,
93+
// update the placement to match the user's gaze.
94+
if (placing)
15795
{
15896
// Do a raycast into the world that will only hit the Spatial Mapping mesh.
15997
var headPosition = Camera.main.transform.position;
16098
var gazeDirection = Camera.main.transform.forward;
16199

162100
RaycastHit hitInfo;
163101
if (Physics.Raycast(headPosition, gazeDirection, out hitInfo,
164-
30.0f, SpatialMappingManager.Instance.LayerMask))
102+
30.0f, spatialMappingManager.LayerMask))
165103
{
166104
// Move this object to where the raycast
167105
// hit the Spatial Mapping mesh.

Assets/HoloToolkit/SpatialMapping/Tests/TapToPlace.unity

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ RenderSettings:
3737
m_ReflectionIntensity: 1
3838
m_CustomReflection: {fileID: 0}
3939
m_Sun: {fileID: 0}
40-
m_IndirectSpecularColor: {r: 0.44692582, g: 0.4967878, b: 0.5750859, a: 1}
40+
m_IndirectSpecularColor: {r: 0.44692558, g: 0.49678743, b: 0.57508624, a: 1}
4141
--- !u!157 &3
4242
LightmapSettings:
4343
m_ObjectHideFlags: 0
@@ -323,6 +323,7 @@ GameObject:
323323
- 4: {fileID: 2116021332}
324324
- 114: {fileID: 2116021331}
325325
- 114: {fileID: 2116021330}
326+
- 114: {fileID: 2116021333}
326327
m_Layer: 0
327328
m_Name: Managers
328329
m_TagString: Untagged
@@ -373,6 +374,17 @@ Transform:
373374
m_Children: []
374375
m_Father: {fileID: 0}
375376
m_RootOrder: 3
377+
--- !u!114 &2116021333
378+
MonoBehaviour:
379+
m_ObjectHideFlags: 0
380+
m_PrefabParentObject: {fileID: 0}
381+
m_PrefabInternal: {fileID: 0}
382+
m_GameObject: {fileID: 2116021329}
383+
m_Enabled: 1
384+
m_EditorHideFlags: 0
385+
m_Script: {fileID: 11500000, guid: f122ca4ae6b527e4798205becf9a0550, type: 3}
386+
m_Name:
387+
m_EditorClassIdentifier:
376388
--- !u!1 &2138978090
377389
GameObject:
378390
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)