-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetVRMode.js
173 lines (137 loc) · 7.43 KB
/
setVRMode.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma strict
private var fallingLaunch : GameObject;
private var fallingLaunchComponent : FallingLaunch;
var GvrViewerMainObject : GameObject;
// should be a prefab tilted 90 degrees in X axis, so the user faces forward in headset:
var cameraVRParentPrefab : GameObject;
private var cameraVRParent : GameObject;
var cameraObj : GameObject;
private var VRViewerComponent : Component;
// var VRViewer : GvrViewer;
private var hasCentered : boolean = false;
var VRUICameraVRTransform : Transform;
var oceanCameraVRTransform : Transform;
var VRUICameraObj : GameObject;
var VRUICamera : Camera;
private var VRUICameraVRHead : GvrHead;
var fogColor : Color;
var fogColorVR : Color;
function Awake () {
if (!cameraObj) {
cameraObj = Camera.main.gameObject;
// cameraObj = transform.Find("Player-cameras/Camera").gameObject;
}
}
function Start () {
fallingLaunch = GameObject.Find("LaunchGameObject");
fallingLaunchComponent = fallingLaunch.GetComponent.<FallingLaunch>();
// oceanCameraVRTransform = cameraObj.transform.Find("Camera-for-ocean-VR");
// VRUICameraVRTransform = cameraObj.transform.Find("Camera-for-VR-UI");
// VR UI Camera lives two levels down from the Player GameObject,
// inside the main parent Camera:
oceanCameraVRTransform = cameraObj.transform.Find("Camera-for-ocean-VR");
VRUICameraVRTransform = cameraObj.transform.Find("Camera-for-VR-UI");
VRUICameraObj = VRUICameraVRTransform ? VRUICameraVRTransform.gameObject : null;
VRUICamera = VRUICameraObj.GetComponent.<Camera>();
// NOTE: Would be best to perform these lookups elsewhere, but
// due to JS/C# interactions and compilation order it's less feasible.
// http://answers.unity3d.com/questions/507580/bce0019-enabled-is-not-a-member-of-unityenginecomp-3.html
// Also better to use a type below, not a string, but it's necessary due to C# + JS:
// https://docs.unity3d.com/ScriptReference/Component.GetComponent.html
VRViewerComponent = GvrViewerMainObject.GetComponent("GvrViewer");
fogColor = RenderSettings.fogColor;
if (FallingLaunch.isVRMode && VRViewerComponent) {
// Clear is the Unity-default color:
if (fogColorVR.ToString() != Color.clear.ToString()) {
RenderSettings.fogColor = fogColorVR;
}
(VRViewerComponent as MonoBehaviour).enabled = true; // type coercion required to access 'enabled'
(VRViewerComponent as GvrViewer).VRModeEnabled = true;
// Center Cardboard view post-instantiation, but before calling setParent on
// the main camera to make it the child of the 90-degree-offset prefab,
// since we want to map 'forward' in Cardboard-land to 'down' in world space:
if (!hasCentered) {
var VRViewer = GvrViewerMainObject.GetComponent.<GvrViewer>();
VRViewer.Instance.Recenter();
hasCentered = true;
}
// Re-parent camera for 90deg tilt offset (so player can look forward in VR):
cameraVRParent =
Instantiate(cameraVRParentPrefab);
// Make the camera-VR-parent object (post-instantiation) a child of this (Player) gameObject transform,
// then make the Camera (cameraObj) a child of the camera-VR-parent object:
cameraVRParent.transform.SetParent(transform);
cameraObj.transform.SetParent(cameraVRParent.transform);
// oceanCameraVR in particular seems to sometimes to adopt skewed rotation.
// (related to whether Player is tilted when the main VR camera object is re-parented?)
// Consider manually forcing it and the UI camera to (0,0,0) post-reparenting, to be safe:
if (oceanCameraVRTransform) {
// Debug.Log("found oceanCameraVRTransform with rotation " + oceanCameraVRTransform.rotation);
oceanCameraVRTransform.localRotation = Quaternion.identity;
}
if (VRUICameraVRTransform) {
// Debug.Log("found VRUICameraVRTransform with rotation " + VRUICameraVRTransform.rotation);
VRUICameraVRTransform.localRotation = Quaternion.identity;
}
} else if (VRViewerComponent) {
(VRViewerComponent as GvrViewer).VRModeEnabled = false;
}
if (!FallingLaunch.isVRMode) {
GvrViewerMainObject.SetActive(false); // disable the GvrViewerMain gameObject to stop blank viewer from rendering
}
}
function Update () {
// Early return if not in VR mode:
if (!FallingLaunch.isVRMode) {
return;
} else if (FallingLaunch.isVRMode) {
MaintainVRUI();
if (VRViewerComponent) {
// See note in Start() about unfortunate-but-required type coercion:
if ( (VRViewerComponent as GvrViewer).BackButtonPressed ) {
// When Cardboard SDK close icon / back button tapped, return home:
FallingPlayer.isPausable = false;
fallingLaunchComponent.DisableVRMode();
FallingPlayer.UIscriptComponent.SaveCheckpointVR();
FallingPlayer.UIscriptComponent.LoadHomeNow();
}
}
}
}
function MaintainVRUI () {
// Existence check required for StereoControllerComponent in Update()
// since it takes 1+ seconds to set up everything via GVR plugin:
if (!VRUICameraVRHead && VRUICameraObj && VRUICamera) {
// Early return if we're not in VR mode and we've already disabled the VR UI camera:
if (!FallingLaunch.isVRMode && VRUICamera.enabled == false) {
return;
}
if (FallingLaunch.isVRMode) {
if (VRUICameraObj.GetComponent.<GvrHead>()) {
// This GvrHead is already nested within the Player object,
// and the UI is meant to be static relative to the player,
// so therefore shouldn't independently track rotation.
// We use getComponent.<GvrHead> instead of the Google plugin's Head getter,
// because the latter is not guaranteed to be located on this component.
// It could be a parent object's GvrHead, since the GvrHeads are applied at runtime
// by GVRViewer, which adds a StereoController to `Camera.allCameras`'s results array.
// Then StereoController executes `AddStereoRig`, which only adds a new GvrHead if a
// component's parent is not presumed to have one. Otherwise, the parent GvrHead is used.
// If we just set `trackRotation` on StereoController.Head directly, we might be disabling
// the parent (main) camera's ability to track with head movement, which is `no bueno.`
// Keep VR UI aligned with parent gameObject, in case of drift via GvrHead
// (currently, this gameObj doesn't get its own GvrHead, but the order of assignment
// is nondeterministic, constructed by looping through an array of Camera.allCameras).
// Thus, the below is defensive code in case it does ever get its own GvrHead.
if (Debug.isDebugBuild) {
Debug.Log("Found matching GvrHead for VRUICameraVRHead");
}
VRUICameraVRHead = VRUICameraObj.GetComponent.<GvrHead>();
VRUICameraVRHead.trackRotation = false;
}
} else if (!FallingLaunch.isVRMode && VRUICamera.enabled) {
// Disable VR-specific UI camera if we're not in VR mode:
VRUICamera.enabled = false;
}
}
}