forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneContentAdjuster.cs
108 lines (93 loc) · 4.22 KB
/
SceneContentAdjuster.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
#if UNITY_2017_2_OR_NEWER
using System.Collections;
using UnityEngine.XR;
#else
using UnityEngine.VR;
#endif
namespace HoloToolkit.Unity.Boundary
{
/// <summary>
/// Use this script on GameObjects you wish to be aligned in certain ways depending on the application space type.
/// For example, if you want to place an object at the height of the user's head in a room scale application, check alignWithHeadHeight.
/// In a stationary scale application, this is equivalent to placing the object at a height of 0.
/// You can also specify specific locations to place the object based on space type.
///
/// This script runs once, on GameObject creation.
///
/// See https://developer.microsoft.com/en-us/windows/mixed-reality/coordinate_systems_in_unity for more information.
/// <see cref="BoundaryManager"/> for TrackingSpaceType settings.
/// </summary>
public class SceneContentAdjuster : MonoBehaviour
{
private enum AlignmentType
{
AlignWithHeadHeight,
UsePresetPositions,
UsePresetXAndZWithHeadHeight
}
private int frameWaitHack = 0;
[SerializeField]
[Tooltip("Optional container object reference. If null, this script will move the object it's attached to.")]
private Transform containerObject;
[SerializeField]
[Tooltip("Select this if the container should be placed in front of the head on app launch in a room scale app.")]
private AlignmentType alignmentType = AlignmentType.AlignWithHeadHeight;
[SerializeField]
[Tooltip("Use this to set the desired position of the container in a stationary app. This will be ignored if AlignWithHeadHeight is set.")]
private Vector3 stationarySpaceTypePosition = Vector3.zero;
[SerializeField]
[Tooltip("Use this to set the desired position of the container in a room scale app. This will be ignored if AlignWithHeadHeight is set.")]
private Vector3 roomScaleSpaceTypePosition = Vector3.zero;
private Vector3 contentPosition = Vector3.zero;
private void Awake()
{
if (containerObject == null)
{
containerObject = transform;
}
#if UNITY_2017_2_OR_NEWER
// If no XR device is present, the editor will default to (0, 0, 0) and no adjustment is needed.
// This script runs on both opaque and transparent display devices, since the floor offset is based on
// TrackingSpaceType and not display type.
if (XRDevice.isPresent)
{
StartCoroutine(SetContentHeight());
return;
}
#endif
Destroy(this);
}
#if UNITY_2017_2_OR_NEWER
private IEnumerator SetContentHeight()
{
if (frameWaitHack < 1)
{
// Not waiting a frame often caused the camera's position to be incorrect at this point. This seems like a Unity bug.
frameWaitHack++;
yield return null;
}
if (alignmentType == AlignmentType.UsePresetPositions || alignmentType == AlignmentType.UsePresetXAndZWithHeadHeight)
{
if (XRDevice.GetTrackingSpaceType() == TrackingSpaceType.RoomScale)
{
containerObject.position = roomScaleSpaceTypePosition;
}
else if (XRDevice.GetTrackingSpaceType() == TrackingSpaceType.Stationary)
{
containerObject.position = stationarySpaceTypePosition;
}
}
if (alignmentType == AlignmentType.AlignWithHeadHeight || alignmentType == AlignmentType.UsePresetXAndZWithHeadHeight)
{
contentPosition.x = containerObject.position.x;
contentPosition.y = containerObject.position.y + CameraCache.Main.transform.position.y;
contentPosition.z = containerObject.position.z;
containerObject.position = contentPosition;
}
}
#endif
}
}