Skip to content

Commit 4d19ed3

Browse files
authored
Merge pull request #9714 from keveleigh/scene-content-alignment
Update scene content alignment and boundary system
2 parents 2b4a350 + 8902946 commit 4d19ed3

File tree

17 files changed

+103
-142
lines changed

17 files changed

+103
-142
lines changed

Assets/MRTK/Core/Definitions/MixedRealityToolkitConfigurationProfile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public SystemType InputSystemType
152152
/// </summary>
153153
public bool IsBoundarySystemEnabled
154154
{
155-
get { return boundarySystemType != null && boundarySystemType.Type != null && enableBoundarySystem && boundaryVisualizationProfile != null; }
155+
get { return BoundarySystemSystemType != null && BoundarySystemSystemType.Type != null && enableBoundarySystem && boundaryVisualizationProfile != null; }
156156
internal set { enableBoundarySystem = value; }
157157
}
158158

Assets/MRTK/Core/Services/BaseBoundarySystem.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private void ReadProfile()
4747
/// <summary>
4848
/// Whether any XR device is present.
4949
/// </summary>
50+
[System.Obsolete("This value is no longer used.")]
5051
protected virtual bool IsXRDevicePresent { get; } = true;
5152

5253
#region IMixedRealityService Implementation
@@ -58,30 +59,41 @@ private void ReadProfile()
5859
/// <inheritdoc/>
5960
public override string Name { get; protected set; } = "Mixed Reality Boundary System";
6061

61-
private bool boundarySystemInitialized = false;
62-
6362
/// <inheritdoc/>
6463
public override void Initialize()
6564
{
66-
base.Initialize();
65+
// Initialize this value earlier than other systems, so we can use it to block boundary events being raised too early
66+
IsInitialized = false;
6767

6868
// The profile needs to be read on initialization to ensure that re-initialization
6969
// after profile change reads the correct data.
7070
ReadProfile();
7171

72-
if (!Application.isPlaying || !IsXRDevicePresent) { return; }
72+
if (!Application.isPlaying || !DeviceUtility.IsPresent) { return; }
7373

7474
boundaryEventData = new BoundaryEventData(EventSystem.current);
7575

7676
SetTrackingSpace();
7777
CalculateBoundaryBounds();
7878

79-
boundarySystemInitialized = true;
79+
base.Initialize();
8080

8181
RefreshVisualization();
8282
RaiseBoundaryVisualizationChanged();
8383
}
8484

85+
#if UNITY_EDITOR
86+
public override void Update()
87+
{
88+
base.Update();
89+
90+
// If a device is attached late, initialize with the new state of the world
91+
if (!IsInitialized && DeviceUtility.IsPresent)
92+
{
93+
Initialize();
94+
}
95+
}
96+
#endif // UNITY_EDITOR
8597

8698
/// <inheritdoc/>
8799
public override void Destroy()
@@ -526,7 +538,7 @@ private void PropertyAction(bool value, GameObject boundaryObject, System.Action
526538
{
527539
// If not done initializing, no need to raise the changed event or check the visualization.
528540
// These will both happen at the end of the initialization flow.
529-
if (!boundarySystemInitialized)
541+
if (!IsInitialized)
530542
{
531543
return;
532544
}

Assets/MRTK/Core/Services/MixedRealityToolkit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ private void InitializeServiceLocator()
470470
}
471471

472472
// If the Boundary system has been selected for initialization in the Active profile, enable it in the project
473-
if (ActiveProfile.IsBoundarySystemEnabled && !ActiveProfile.ExperienceSettingsProfile.IsNull())
473+
if (ActiveProfile.IsBoundarySystemEnabled && ActiveProfile.ExperienceSettingsProfile != null)
474474
{
475475
DebugUtilities.LogVerbose("Begin registration of the boundary system");
476476
object[] args = { ActiveProfile.BoundaryVisualizationProfile, ActiveProfile.ExperienceSettingsProfile.TargetExperienceScale };

Assets/MRTK/Core/Utilities/SceneContent/MixedRealitySceneContent.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,24 @@ namespace Microsoft.MixedReality.Toolkit
1919
/// </summary>
2020
public class MixedRealitySceneContent : MonoBehaviour
2121
{
22-
public enum AlignmentType
22+
private enum AlignmentType
2323
{
2424
AlignWithExperienceScale,
2525
AlignWithHeadHeight
2626
}
2727

2828
[SerializeField]
2929
[Tooltip("Select this if the container should be placed in front of the head on app launch in a room scale app.")]
30-
public AlignmentType alignmentType = AlignmentType.AlignWithExperienceScale;
31-
32-
private Vector3 contentPosition = Vector3.zero;
30+
private AlignmentType alignmentType = AlignmentType.AlignWithExperienceScale;
3331

32+
[SerializeField]
3433
[Tooltip("Optional container object reference. If null, this script will move the object it's attached to.")]
3534
private Transform containerObject = null;
3635

36+
private Vector3 contentPosition = Vector3.zero;
37+
private const uint MaxEditorFrameWaitCount = 5;
38+
private Coroutine initializeSceneContentWithDelay;
39+
3740
private void Awake()
3841
{
3942
if (containerObject == null)
@@ -42,14 +45,36 @@ private void Awake()
4245
}
4346

4447
// Init the content height on non-XR platforms
45-
StartCoroutine(InitializeSceneContentWithDelay());
48+
initializeSceneContentWithDelay = StartCoroutine(InitializeSceneContentWithDelay());
49+
}
50+
51+
private void OnDestroy()
52+
{
53+
if (initializeSceneContentWithDelay != null)
54+
{
55+
StopCoroutine(initializeSceneContentWithDelay);
56+
}
4657
}
4758

48-
// Not waiting a frame often caused the camera's position to be incorrect at this point. This seems like a Unity bug.
59+
// Not waiting often caused the camera's position to be incorrect at this point. This seems like a Unity bug.
60+
// Editor takes a little longer to init.
4961
private IEnumerator InitializeSceneContentWithDelay()
5062
{
51-
yield return null;
63+
if (Application.isEditor)
64+
{
65+
for (int i = 0; i < MaxEditorFrameWaitCount; i++)
66+
{
67+
yield return null;
68+
}
69+
}
70+
else
71+
{
72+
yield return null;
73+
}
74+
5275
InitializeSceneContent();
76+
77+
initializeSceneContentWithDelay = null;
5378
}
5479

5580

@@ -77,8 +102,8 @@ public void InitializeSceneContent()
77102
#if UNITY_2020_1_OR_NEWER
78103
XRSubsystemHelpers.InputSubsystem != null && XRSubsystemHelpers.InputSubsystem.GetTrackingOriginMode().HasFlag(TrackingOriginModeFlags.Floor);
79104
#elif UNITY_2019_1_OR_NEWER
80-
#pragma warning disable 0618
81105
(XRSubsystemHelpers.InputSubsystem != null && XRSubsystemHelpers.InputSubsystem.GetTrackingOriginMode().HasFlag(TrackingOriginModeFlags.Floor)) ||
106+
#pragma warning disable 0618
82107
(XRDevice.isPresent && XRDevice.GetTrackingSpaceType() == TrackingSpaceType.RoomScale);
83108
#pragma warning restore 0618
84109
#else
@@ -97,11 +122,6 @@ public void InitializeSceneContent()
97122

98123
containerObject.position = contentPosition;
99124
}
100-
else
101-
{
102-
contentPosition = Vector3.zero;
103-
containerObject.position = contentPosition;
104-
}
105125
}
106126

107127
if (alignmentType == AlignmentType.AlignWithHeadHeight)

Assets/MRTK/Core/Utilities/XRSettingsUtilities.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace Microsoft.MixedReality.Toolkit.Utilities
1515
/// </summary>
1616
public static class XRSettingsUtilities
1717
{
18-
#if UNITY_2019_3_OR_NEWER
18+
#if UNITY_2019_3_OR_NEWER && !UNITY_2020_2_OR_NEWER
1919
private static bool? legacyXRAvailable = null;
20-
#endif // UNITY_2019_3_OR_NEWER
20+
#endif // UNITY_2019_3_OR_NEWER && !UNITY_2020_2_OR_NEWER
2121

2222
/// <summary>
2323
/// Checks if an XR SDK plug-in is installed that disables legacy VR. Returns false if so.
@@ -26,7 +26,9 @@ public static bool LegacyXRAvailable
2626
{
2727
get
2828
{
29-
#if UNITY_2019_3_OR_NEWER
29+
#if UNITY_2020_2_OR_NEWER
30+
return false;
31+
#elif UNITY_2019_3_OR_NEWER
3032
if (!legacyXRAvailable.HasValue)
3133
{
3234
legacyXRAvailable = true;
@@ -47,7 +49,7 @@ public static bool LegacyXRAvailable
4749
return legacyXRAvailable.HasValue && legacyXRAvailable.Value;
4850
#else
4951
return true;
50-
#endif // UNITY_2019_3_OR_NEWER
52+
#endif // UNITY_2020_2_OR_NEWER
5153
}
5254
}
5355
}

Assets/MRTK/Examples/Demos/HandTracking/Scenes/HandInteractionExamples.unity

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25520,7 +25520,8 @@ MonoBehaviour:
2552025520
m_Script: {fileID: 11500000, guid: c65c9dd2f312b8d41b8849d58e1053fa, type: 3}
2552125521
m_Name:
2552225522
m_EditorClassIdentifier:
25523-
alignmentType: 0
25523+
alignmentType: 1
25524+
containerObject: {fileID: 0}
2552425525
--- !u!114 &1700502666 stripped
2552525526
MonoBehaviour:
2552625527
m_CorrespondingSourceObject: {fileID: 4448574576992466704, guid: ffbe4bef620a0a542bb77fe2f765c905,

Assets/MRTK/Providers/XRSDK/XRSDKBoundarySystem.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ public XRSDKBoundarySystem(
3030
private static readonly List<XRInputSubsystem> XRInputSubsystems = new List<XRInputSubsystem>();
3131
#endif // UNITY_2019_3_OR_NEWER
3232

33-
/// <inheritdoc/>
34-
protected override bool IsXRDevicePresent
35-
{
36-
get
37-
{
38-
List<InputDevice> devices = new List<InputDevice>();
39-
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.HeadMounted, devices);
40-
return devices.Count > 0;
41-
}
42-
}
43-
4433
#region IMixedRealityService Implementation
4534

4635
/// <inheritdoc/>
@@ -51,31 +40,27 @@ protected override bool IsXRDevicePresent
5140
/// <inheritdoc/>
5241
protected override List<Vector3> GetBoundaryGeometry()
5342
{
54-
// Boundaries are supported for Room Scale experiences only.
55-
if (XRSubsystemHelpers.InputSubsystem?.GetTrackingOriginMode() != TrackingOriginModeFlags.Floor)
56-
{
57-
return null;
58-
}
59-
6043
// Get the boundary geometry.
6144
var boundaryGeometry = new List<Vector3>(0);
6245

63-
if (!XRSubsystemHelpers.InputSubsystem.TryGetBoundaryPoints(boundaryGeometry) || boundaryGeometry.Count == 0)
46+
if (XRSubsystemHelpers.InputSubsystem?.GetTrackingOriginMode() != TrackingOriginModeFlags.Floor
47+
|| !XRSubsystemHelpers.InputSubsystem.TryGetBoundaryPoints(boundaryGeometry)
48+
|| boundaryGeometry.Count == 0)
6449
{
6550
#if UNITY_2019_3_OR_NEWER
6651
// If the "main" input subsystem doesn't have an available boundary, check the rest of them
6752
SubsystemManager.GetInstances(XRInputSubsystems);
6853
foreach (XRInputSubsystem xrInputSubsystem in XRInputSubsystems)
6954
{
70-
if (xrInputSubsystem.running &&
71-
xrInputSubsystem.TryGetBoundaryPoints(boundaryGeometry) &&
72-
boundaryGeometry.Count > 0)
55+
if (xrInputSubsystem.running
56+
&& xrInputSubsystem.GetTrackingOriginMode() == TrackingOriginModeFlags.Floor
57+
&& xrInputSubsystem.TryGetBoundaryPoints(boundaryGeometry)
58+
&& boundaryGeometry.Count > 0)
7359
{
74-
return boundaryGeometry;
60+
break;
7561
}
7662
}
7763
#endif // UNITY_2019_3_OR_NEWER
78-
return null;
7964
}
8065

8166
return boundaryGeometry;
@@ -109,8 +94,19 @@ protected override void SetTrackingSpace()
10994
break;
11095
}
11196

112-
if (XRSubsystemHelpers.InputSubsystem != null && !XRSubsystemHelpers.InputSubsystem.TrySetTrackingOriginMode(trackingOriginMode))
97+
if (XRSubsystemHelpers.InputSubsystem == null || !XRSubsystemHelpers.InputSubsystem.TrySetTrackingOriginMode(trackingOriginMode))
11398
{
99+
#if UNITY_2019_3_OR_NEWER
100+
// If the "main" input subsystem can't set the origin mode, check the rest of them
101+
SubsystemManager.GetInstances(XRInputSubsystems);
102+
foreach (XRInputSubsystem xrInputSubsystem in XRInputSubsystems)
103+
{
104+
if (xrInputSubsystem.running && xrInputSubsystem.TrySetTrackingOriginMode(trackingOriginMode))
105+
{
106+
return;
107+
}
108+
}
109+
#endif // UNITY_2019_3_OR_NEWER
114110
Debug.LogWarning("Tracking origin unable to be set.");
115111
}
116112
}

Assets/MRTK/SDK/Profiles/HoloLens2/DefaultHoloLens2ConfigurationProfile.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ MonoBehaviour:
2525
inputSystemProfile: {fileID: 11400000, guid: bec5ceabcd10992439d51532332137f9, type: 2}
2626
inputSystemType:
2727
reference: Microsoft.MixedReality.Toolkit.Input.MixedRealityInputSystem, Microsoft.MixedReality.Toolkit.Services.InputSystem
28-
enableBoundarySystem: 0
28+
enableBoundarySystem: 1
2929
boundarySystemType:
3030
reference: Microsoft.MixedReality.Toolkit.Boundary.MixedRealityBoundarySystem,
3131
Microsoft.MixedReality.Toolkit.Services.BoundarySystem

Assets/MRTK/Services/BoundarySystem/XR2018/MixedRealityBoundarySystem.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ public MixedRealityBoundarySystem(
2424
MixedRealityBoundaryVisualizationProfile profile,
2525
ExperienceScale scale) : base(profile, scale) { }
2626

27-
/// <inheritdoc/>
28-
protected override bool IsXRDevicePresent => XRDevice.isPresent;
29-
3027
#region IMixedRealityService Implementation
3128

3229
/// <inheritdoc/>

Assets/MRTK/Tests/PlayModeTests/BaseCursorTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace Microsoft.MixedReality.Toolkit.Tests
2323
{
2424
// Tests to verify that cursor state is updated correctly
25-
public class BaseCursorTests
25+
public class BaseCursorTests : BasePlayModeTests
2626
{
2727
// Keeping this low by default so the test runs fast. Increase it to be able to see hand movements in the editor.
2828
private const int numFramesPerMove = 1;
@@ -41,10 +41,10 @@ public class BaseCursorTests
4141
/// for examples.
4242
/// See also comments at <see cref="TestUtilities.PlayspaceToArbitraryPose"/>.
4343
/// </remarks>
44-
[UnitySetUp]
45-
public IEnumerator SetUp()
44+
public override IEnumerator Setup()
4645
{
47-
PlayModeTestUtilities.Setup();
46+
yield return base.Setup();
47+
4848
TestUtilities.PlayspaceToArbitraryPose();
4949

5050
// Target frame rate is set to 50 to match the physics
@@ -69,12 +69,10 @@ public IEnumerator SetUp()
6969
yield return null;
7070
}
7171

72-
[UnityTearDown]
73-
public IEnumerator TearDown()
72+
public override IEnumerator TearDown()
7473
{
7574
Object.Destroy(cube);
76-
TestUtilities.ShutdownMixedRealityToolkit();
77-
yield return null;
75+
yield return base.TearDown();
7876
}
7977

8078
private void VerifyCursorStateFromPointers(IEnumerable<IMixedRealityPointer> pointers, CursorStateEnum state)

0 commit comments

Comments
 (0)