Skip to content
This repository was archived by the owner on Nov 16, 2024. It is now read-only.

Commit 3c3d189

Browse files
Fix persistence for multiple independent alignment subspaces. (#55)
Added tests for independent alignment persistence.
1 parent 71ea12c commit 3c3d189

File tree

11 files changed

+1602
-95
lines changed

11 files changed

+1602
-95
lines changed

Assets/MRTK/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealityArticulatedHand.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,21 @@ private void UpdateHandData(InteractionSourceState interactionSourceState)
286286
handMeshVertices[i] = WindowsMixedRealityUtilities.SystemVector3ToUnity(vertexAndNormals[i].Position);
287287
handMeshNormals[i] = WindowsMixedRealityUtilities.SystemVector3ToUnity(vertexAndNormals[i].Normal);
288288
}
289+
290+
/// Hands should follow the Playspace to accommodate teleporting, so fold in the Playspace transform.
291+
Vector3 unityPosition = WindowsMixedRealityUtilities.SystemVector3ToUnity(translation);
292+
unityPosition = MixedRealityPlayspace.TransformPoint(unityPosition);
293+
Quaternion unityRotation = WindowsMixedRealityUtilities.SystemQuaternionToUnity(rotation);
294+
unityRotation = MixedRealityPlayspace.Rotation * unityRotation;
289295

290296
HandMeshInfo handMeshInfo = new HandMeshInfo
291297
{
292298
vertices = handMeshVertices,
293299
normals = handMeshNormals,
294300
triangles = handMeshTriangleIndices,
295301
uvs = handMeshUVs,
296-
position = WindowsMixedRealityUtilities.SystemVector3ToUnity(translation),
297-
rotation = WindowsMixedRealityUtilities.SystemQuaternionToUnity(rotation)
302+
position = unityPosition,
303+
rotation = unityRotation
298304
};
299305

300306
CoreServices.InputSystem?.RaiseHandMeshUpdated(InputSource, ControllerHandedness, handMeshInfo);

Assets/WorldLocking.Examples/Scripts/AlignSubtree.cs renamed to Assets/WorldLocking.Core/Scripts/AlignSubtree.cs

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
using Microsoft.MixedReality.WorldLocking.Core;
54
using System.Collections;
65
using System.Collections.Generic;
76
using System.IO;
87
using UnityEngine;
98

10-
namespace Microsoft.MixedReality.WorldLocking.Examples
9+
namespace Microsoft.MixedReality.WorldLocking.Core
1110
{
1211
/// <summary>
1312
/// Script to use an independent AlignmentManager to align a specific subtree, independent of the rest of the scene.
@@ -30,6 +29,20 @@ namespace Microsoft.MixedReality.WorldLocking.Examples
3029
public class AlignSubtree : MonoBehaviour
3130
{
3231
#region Inspector fields
32+
33+
[SerializeField]
34+
[Tooltip("Collect all SpacePins from this subtree to manage.")]
35+
private bool collectFromTree = true;
36+
37+
/// <summary>
38+
/// Collect all SpacePins from this subtree to manage.
39+
/// </summary>
40+
public bool CollectFromTree { get { return collectFromTree; } set { collectFromTree = value; } }
41+
42+
[SerializeField]
43+
[Tooltip("Explicit list of Space Pins to manage.")]
44+
private List<SpacePin> ownedPins = new List<SpacePin>();
45+
3346
[SerializeField]
3447
[Tooltip("File name for saving to and loading from. Defaults to gameObject's name. Use forward slash '/' for subfolders.")]
3548
private string saveFileName = "";
@@ -85,6 +98,8 @@ public string SaveFileName
8598
/// </summary>
8699
private AlignmentManager alignmentManager = null;
87100

101+
private bool needLoad = true;
102+
88103
#endregion Internal members
89104

90105
#region Public APIs
@@ -114,6 +129,71 @@ public bool Load()
114129
}
115130
return false;
116131
}
132+
133+
/// <summary>
134+
/// Explicitly add a pin to the owned pins list.
135+
/// </summary>
136+
/// <param name="pin">THe pin to add.</param>
137+
/// <returns>True if added, false if it was already there.</returns>
138+
public bool AddOwnedPin(SpacePin pin)
139+
{
140+
if (!ownedPins.Contains(pin))
141+
{
142+
ownedPins.Add(pin);
143+
return true;
144+
}
145+
return false;
146+
}
147+
148+
/// <summary>
149+
/// Remove a specific pin from the owned pins list.
150+
/// </summary>
151+
/// <param name="pin">The pin to remove.</param>
152+
/// <returns>True if removed, else false (probably not found).</returns>
153+
public bool RemoveOwnedPin(SpacePin pin)
154+
{
155+
return ownedPins.Remove(pin);
156+
}
157+
158+
/// <summary>
159+
/// Clear the entire list of owned space pins.
160+
/// </summary>
161+
/// <remarks>
162+
/// This removes all pins in the list, whether added dynamically or added in the inspector.
163+
/// </remarks>
164+
public void ClearOwnedPins()
165+
{
166+
ownedPins.Clear();
167+
}
168+
169+
/// <summary>
170+
/// This should be called whenever pins are added to the owned list.
171+
/// </summary>
172+
/// <remarks>
173+
/// It's only necessary to call this when adding pins to the owned list dynamically
174+
/// from script. It is called from OnEnable for all pins added in the inspector or
175+
/// collected from the scene graph subtree.
176+
/// </remarks>
177+
public void ClaimPinOwnership()
178+
{
179+
CheckInternalWiring();
180+
if (CollectFromTree)
181+
{
182+
var spacePins = GetComponentsInChildren<SpacePin>();
183+
foreach (var pin in spacePins)
184+
{
185+
AddOwnedPin(pin);
186+
}
187+
}
188+
foreach (var pin in ownedPins)
189+
{
190+
pin.AlignmentManager = alignmentManager;
191+
}
192+
if (AutoSave)
193+
{
194+
needLoad = true;
195+
}
196+
}
117197
#endregion Public APIs
118198

119199
#region Internal AlignmentManager management
@@ -158,6 +238,8 @@ private void Update()
158238
{
159239
Debug.Assert(alignmentManager != null);
160240

241+
CheckLoad();
242+
161243
var wltMgr = WorldLockingManager.GetInstance();
162244
Debug.Assert(alignmentManager != wltMgr.AlignmentManager);
163245

@@ -169,23 +251,23 @@ private void Update()
169251
subTree.SetGlobalPose(lockedFromPinned);
170252
}
171253

254+
private void CheckLoad()
255+
{
256+
if (needLoad)
257+
{
258+
needLoad = false;
259+
Load();
260+
}
261+
}
262+
172263
/// <summary>
173264
/// Check that all internal wiring is complete. Assign our independent alignmentManager
174265
/// to all space pins beneath us.
175266
/// Load state from previous session if available and so configured.
176267
/// </summary>
177268
private void OnEnable()
178269
{
179-
CheckInternalWiring();
180-
var spacePins = GetComponentsInChildren<SpacePin>();
181-
foreach (var pin in spacePins)
182-
{
183-
pin.AlignmentManager = alignmentManager;
184-
}
185-
if (AutoSave)
186-
{
187-
Load();
188-
}
270+
ClaimPinOwnership();
189271
}
190272

191273
/// <summary>

Assets/WorldLocking.Examples/Scripts/AlignSubtree.cs.meta renamed to Assets/WorldLocking.Core/Scripts/AlignSubtree.cs.meta

File renamed without changes.

0 commit comments

Comments
 (0)