Skip to content

Commit 3043790

Browse files
authored
Merge pull request #2773 from Ecnassianer/body2Playspace
replacing "Body" with MixedRealityPlayspace
2 parents 96363d8 + a7de17d commit 3043790

File tree

9 files changed

+75
-36
lines changed

9 files changed

+75
-36
lines changed

Assets/MixedRealityToolkit-Examples/Demos/Boundary/Scripts/BoundaryVisualizationDemo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private void Awake()
4242
{
4343
markerParent = new GameObject();
4444
markerParent.name = "Boundary Demo Markers";
45-
markerParent.transform.parent = CameraCache.Main.transform.parent;
45+
markerParent.transform.parent = MixedRealityManager.Instance.MixedRealityPlayspace;
4646
}
4747

4848
private void Start()

Assets/MixedRealityToolkit-SDK/Features/Boundary/MixedRealityBoundaryManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public override void Destroy()
189189
private GameObject CreateBoundaryVisualizationParent()
190190
{
191191
GameObject visualizationParent = new GameObject("Boundary System Visualizations");
192-
visualizationParent.transform.parent = CameraCache.Main.transform.parent;
192+
visualizationParent.transform.parent = MixedRealityManager.Instance.MixedRealityPlayspace;
193193

194194
return visualizationParent;
195195
}
@@ -456,7 +456,7 @@ public bool Contains(Vector3 location, Boundary.Type boundaryType = Boundary.Typ
456456
}
457457

458458
// Handle the user teleporting (boundary moves with them).
459-
location = CameraCache.Main.transform.parent.InverseTransformPoint(location);
459+
location = MixedRealityManager.Instance.MixedRealityPlayspace.InverseTransformPoint(location);
460460

461461
if (FloorHeight.Value > location.y ||
462462
BoundaryHeight < location.y)
@@ -499,7 +499,7 @@ public bool TryGetRectangularBoundsParams(out Vector2 center, out float angle, o
499499
}
500500

501501
// Handle the user teleporting (boundary moves with them).
502-
Vector3 transformedCenter = CameraCache.Main.transform.parent.TransformPoint(
502+
Vector3 transformedCenter = MixedRealityManager.Instance.MixedRealityPlayspace.TransformPoint(
503503
new Vector3(rectangularBounds.Center.x, 0f, rectangularBounds.Center.y));
504504

505505
center = new Vector2(transformedCenter.x, transformedCenter.z);
@@ -530,9 +530,9 @@ public GameObject GetFloorVisualization()
530530
currentFloorObject.name = "Boundary System Floor";
531531
currentFloorObject.transform.localScale = new Vector3(floorScale.x, boundaryObjectThickness, floorScale.y);
532532
currentFloorObject.transform.Translate(new Vector3(
533-
CameraCache.Main.transform.parent.position.x,
533+
MixedRealityManager.Instance.MixedRealityPlayspace.position.x,
534534
FloorHeight.Value - (currentFloorObject.transform.localScale.y * 0.5f),
535-
CameraCache.Main.transform.parent.position.z));
535+
MixedRealityManager.Instance.MixedRealityPlayspace.position.z));
536536
currentFloorObject.GetComponent<Renderer>().sharedMaterial = MixedRealityManager.Instance.ActiveProfile.BoundaryVisualizationProfile.FloorMaterial;
537537

538538
return currentFloorObject;
@@ -606,9 +606,9 @@ public GameObject GetTrackedAreaVisualization()
606606
currentTrackedAreaObject.layer = ignoreRaycastLayerValue;
607607
currentTrackedAreaObject.AddComponent<LineRenderer>();
608608
currentTrackedAreaObject.transform.Translate(new Vector3(
609-
CameraCache.Main.transform.parent.position.x,
609+
MixedRealityManager.Instance.MixedRealityPlayspace.position.x,
610610
boundaryObjectRenderOffset,
611-
CameraCache.Main.transform.parent.position.z));
611+
MixedRealityManager.Instance.MixedRealityPlayspace.position.z));
612612

613613
// Configure the renderer properties.
614614
float lineWidth = 0.01f;

Assets/MixedRealityToolkit-SDK/Features/Input/MixedRealityInputManager.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ public override void Initialize()
9191

9292
private void InitializeInternal()
9393
{
94-
if (CameraCache.Main.transform.parent == null)
95-
{
96-
var cameraParent = new GameObject("Body");
97-
CameraCache.Main.transform.SetParent(cameraParent.transform);
98-
}
99-
10094
focusProvider = CameraCache.Main.gameObject.EnsureComponent<FocusProvider>();
10195
gazeProvider = CameraCache.Main.gameObject.EnsureComponent<GazeProvider>();
10296

Assets/MixedRealityToolkit-SDK/Features/Teleportation/MixedRealityTeleportManager.cs

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

44
using Microsoft.MixedReality.Toolkit.Core.EventDatum.Teleport;
@@ -40,16 +40,6 @@ public override void Initialize()
4040

4141
private void InitializeInternal()
4242
{
43-
if (CameraCache.Main.transform.parent == null)
44-
{
45-
var cameraParent = new GameObject("Body");
46-
CameraCache.Main.transform.SetParent(cameraParent.transform);
47-
}
48-
49-
// Make sure the camera is at the scene origin.
50-
CameraCache.Main.transform.parent.transform.position = Vector3.zero;
51-
CameraCache.Main.transform.localPosition = Vector3.zero;
52-
5343
#if UNITY_EDITOR
5444
if (!UnityEditor.EditorApplication.isPlaying)
5545
{
@@ -247,7 +237,7 @@ private void ProcessTeleportationRequest(TeleportEventData eventData)
247237
{
248238
isProcessingTeleportRequest = true;
249239

250-
var cameraParent = CameraCache.Main.transform.parent;
240+
var cameraParent = MixedRealityManager.Instance.MixedRealityPlayspace;
251241

252242
targetRotation = Vector3.zero;
253243
targetRotation.y = eventData.Pointer.PointerOrientation;

Assets/MixedRealityToolkit-SDK/Features/UX/Scripts/Pointers/TeleportPointer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public override void OnPositionInputChanged(InputEventData<Vector2> eventData)
303303
{
304304
canMove = false;
305305
// Rotate the camera by the rotation amount. If our angle is positive then rotate in the positive direction, otherwise in the opposite direction.
306-
CameraCache.Main.transform.parent.RotateAround(CameraCache.Main.transform.position, Vector3.up, angle >= 0.0f ? rotationAmount : -rotationAmount);
306+
MixedRealityManager.Instance.MixedRealityPlayspace.RotateAround(CameraCache.Main.transform.position, Vector3.up, angle >= 0.0f ? rotationAmount : -rotationAmount);
307307
}
308308
else // We may be trying to strafe backwards.
309309
{
@@ -317,10 +317,10 @@ public override void OnPositionInputChanged(InputEventData<Vector2> eventData)
317317
if (offsetStrafeAngle > 0 && offsetStrafeAngle < backStrafeActivationAngle)
318318
{
319319
canMove = false;
320-
var height = CameraCache.Main.transform.parent.position.y;
321-
var newPosition = -CameraCache.Main.transform.forward * strafeAmount + CameraCache.Main.transform.parent.position;
320+
var height = MixedRealityManager.Instance.MixedRealityPlayspace.position.y;
321+
var newPosition = -CameraCache.Main.transform.forward * strafeAmount + MixedRealityManager.Instance.MixedRealityPlayspace.position;
322322
newPosition.y = height;
323-
CameraCache.Main.transform.parent.position = newPosition;
323+
MixedRealityManager.Instance.MixedRealityPlayspace.position = newPosition;
324324
}
325325
}
326326
}

Assets/MixedRealityToolkit/_Core/Devices/BaseController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private void TryRenderControllerModel(Type controllerType)
185185
// If we've got a controller model prefab, then place it in the scene.
186186
if (controllerModel != null)
187187
{
188-
var controllerObject = UnityEngine.Object.Instantiate(controllerModel, CameraCache.Main.transform.parent);
188+
var controllerObject = UnityEngine.Object.Instantiate(controllerModel, MixedRealityManager.Instance.MixedRealityPlayspace);
189189
controllerObject.name = $"{ControllerHandedness}_{controllerObject.name}";
190190
var poseSynchronizer = controllerObject.GetComponent<IMixedRealityControllerPoseSynchronizer>();
191191

Assets/MixedRealityToolkit/_Core/Devices/BaseDeviceManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected virtual IMixedRealityPointer[] RequestPointers(SystemType controllerTy
8282
{
8383
var pointerObject = Object.Instantiate(pointerProfile.PointerPrefab);
8484
var pointer = pointerObject.GetComponent<IMixedRealityPointer>();
85-
pointerObject.transform.SetParent(CameraCache.Main.transform.parent);
85+
pointerObject.transform.SetParent(MixedRealityManager.Instance.MixedRealityPlayspace);
8686

8787
if (pointer != null)
8888
{

Assets/MixedRealityToolkit/_Core/Devices/MixedReality/WindowsMixedRealityController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ private void UpdateGripData(InteractionSourceState interactionSourceState, Mixed
243243
interactionSourceState.sourcePose.TryGetPosition(out currentGripPosition, InteractionSourceNode.Grip);
244244
interactionSourceState.sourcePose.TryGetRotation(out currentGripRotation, InteractionSourceNode.Grip);
245245

246-
if (CameraCache.Main.transform.parent != null)
246+
if (MixedRealityManager.Instance.MixedRealityPlayspace != null)
247247
{
248-
currentGripPose.Position = CameraCache.Main.transform.parent.TransformPoint(currentGripPosition);
249-
currentGripPose.Rotation = Quaternion.Euler(CameraCache.Main.transform.parent.TransformDirection(currentGripRotation.eulerAngles));
248+
currentGripPose.Position = MixedRealityManager.Instance.MixedRealityPlayspace.TransformPoint(currentGripPosition);
249+
currentGripPose.Rotation = Quaternion.Euler(MixedRealityManager.Instance.MixedRealityPlayspace.TransformDirection(currentGripRotation.eulerAngles));
250250
}
251251
else
252252
{

Assets/MixedRealityToolkit/_Core/Managers/MixedRealityManager.cs

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

44
using Microsoft.MixedReality.Toolkit.Core.Definitions;
@@ -147,6 +147,7 @@ private void Initialize()
147147
}
148148
}
149149
#endif
150+
EnsureMixedRealityRequirements();
150151

151152
if (ActiveProfile.IsCameraProfileEnabled)
152153
{
@@ -256,6 +257,13 @@ private void Initialize()
256257
isMixedRealityManagerInitializing = false;
257258
}
258259

260+
private void EnsureMixedRealityRequirements()
261+
{
262+
// There's lots of documented cases that if the camera doesn't start at 0,0,0, things break with the WMR SDK specifically.
263+
// We'll enforce that here, then tracking can update it to the appropriate position later.
264+
CameraCache.Main.transform.position = Vector3.zero;
265+
}
266+
259267
#region MonoBehaviour Implementation
260268

261269
private static MixedRealityManager instance;
@@ -368,6 +376,53 @@ private void InitializeInternal()
368376
}
369377
}
370378

379+
private Transform mixedRealityPlayspace;
380+
381+
/// <summary>
382+
/// Returns the MixedRealityPlayspace for the local player
383+
/// </summary>
384+
public Transform MixedRealityPlayspace
385+
{
386+
get {
387+
AssertIsInitialized();
388+
if (mixedRealityPlayspace)
389+
{
390+
return mixedRealityPlayspace;
391+
}
392+
else
393+
{
394+
string MixedRealityPlayspaceName = "MixedRealityPlayspace";
395+
if (CameraCache.Main.transform.parent == null)
396+
{
397+
mixedRealityPlayspace = new GameObject(MixedRealityPlayspaceName).transform;
398+
CameraCache.Main.transform.SetParent(mixedRealityPlayspace);
399+
}
400+
else
401+
{
402+
if (CameraCache.Main.transform.parent.name != MixedRealityPlayspaceName)
403+
{
404+
// Since the scene is set up with a different camera parent, its likely
405+
// that there's an expectation that that parent is going to be used for
406+
// something else. We print a warning to call out the fact that we're
407+
// co-opting this object for use with teleporting and such, since that
408+
// might cause conflicts with the parent's intended purpose.
409+
Debug.LogWarning("The Mixed Reality Manager expected the camera's parent to be named " + MixedRealityPlayspaceName + ". The existing parent will be renamed and used instead.");
410+
CameraCache.Main.transform.parent.name = MixedRealityPlayspaceName; // If we rename it, we make it clearer that why it's being teleported around at runtime.
411+
}
412+
}
413+
414+
// It's very important that the MixedRealityPlayspace align with the tracked space,
415+
// otherwise reality-locked things like playspace boundaries won't be aligned properly.
416+
// For now, we'll just assume that when the playspace is first initialized, the
417+
// tracked space origin overlaps with the world space origin. If a platform ever does
418+
// something else (i.e, placing the lower left hand corner of the tracked space at world
419+
// space 0,0,0), we should compensate for that here.
420+
421+
return mixedRealityPlayspace;
422+
}
423+
}
424+
}
425+
371426
private void ApplicationOnQuitting()
372427
{
373428
DisableAllManagers();

0 commit comments

Comments
 (0)