Added VR libraries

This commit is contained in:
Chris Midkiff
2018-10-08 23:54:11 -04:00
parent d9eb2a9763
commit 7ce1036e39
1037 changed files with 195630 additions and 348 deletions

View File

@@ -0,0 +1,54 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
// Simple static class to abstract out several jni calls that need to be shared
// between different classes.
public static class GvrActivityHelper {
#if UNITY_IOS
public const string GVR_DLL_NAME = "__Internal";
#else
public const string GVR_DLL_NAME = "gvr";
#endif // UNITY_IOS
public const string PACKAGE_UNITY_PLAYER = "com.unity3d.player.UnityPlayer";
#if UNITY_ANDROID && !UNITY_EDITOR
/// Returns the Android Activity used by the Unity device player. The caller is
/// responsible for memory-managing the returned AndroidJavaObject.
public static AndroidJavaObject GetActivity() {
AndroidJavaClass jc = new AndroidJavaClass(PACKAGE_UNITY_PLAYER);
if (jc == null) {
Debug.LogErrorFormat("Failed to get class {0}", PACKAGE_UNITY_PLAYER);
return null;
}
AndroidJavaObject activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
if (activity == null) {
Debug.LogError("Failed to obtain current Android activity.");
return null;
}
return activity;
}
/// Returns the application context of the current Android Activity.
public static AndroidJavaObject GetApplicationContext(AndroidJavaObject activity) {
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
if (context == null) {
Debug.LogError("Failed to get application context from Activity.");
return null;
}
return context;
}
#endif // UNITY_ANDROID && !UNITY_EDITOR
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: a89c2f792b6ed4b3c8e3d82ed9b3c4cc
timeCreated: 1478903803
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using UnityEngine;
namespace Gvr.Internal {
/// Manages cursor lock state while developer is using editor head and controller emulation.
public class GvrCursorHelper {
// Whether MouseControllerProvider is currently tracking mouse movement.
private static bool cachedHeadEmulationActive;
// Whether GvrEditorEmulator is currently tracking mouse movement.
private static bool cachedControllerEmulationActive;
public static bool HeadEmulationActive {
set {
cachedHeadEmulationActive = value;
UpdateCursorLockState();
}
}
public static bool ControllerEmulationActive {
set {
cachedControllerEmulationActive = value;
UpdateCursorLockState();
}
}
private static void UpdateCursorLockState() {
bool active = cachedHeadEmulationActive || cachedControllerEmulationActive;
Cursor.lockState = active ? CursorLockMode.Locked : CursorLockMode.None;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b6ecee1e7b5ef4412ba89d59a49afd69
timeCreated: 1506962278
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,144 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using System;
using System.Runtime.InteropServices;
/// Main entry point Daydream specific APIs.
///
/// This class automatically instantiates an instance when this API is used for the first time.
/// For explicit control over when the instance is created and the Java references are setup
/// call the provided CreateAsync method, for example when no UI is being displayed to the user.
public class GvrDaydreamApi : IDisposable {
private const string METHOD_CREATE = "create";
private const string METHOD_LAUNCH_VR_HOMESCREEN = "launchVrHomescreen";
private const string METHOD_RUN_ON_UI_THREAD = "runOnUiThread";
private const string PACKAGE_DAYDREAM_API = "com.google.vr.ndk.base.DaydreamApi";
private static GvrDaydreamApi m_instance;
#if UNITY_ANDROID && !UNITY_EDITOR
private AndroidJavaObject m_daydreamApiObject;
private AndroidJavaClass m_daydreamApiClass = new AndroidJavaClass(PACKAGE_DAYDREAM_API);
public static AndroidJavaObject JavaInstance {
get {
EnsureCreated(null);
return m_instance.m_daydreamApiObject;
}
}
#endif // UNITY_ANDROID && !UNITY_EDITOR
public static bool IsCreated {
get {
#if !UNITY_ANDROID || UNITY_EDITOR
return (m_instance != null);
#else
return (m_instance != null) && (m_instance.m_daydreamApiObject != null);
#endif // !UNITY_ANDROID || UNITY_EDITOR
}
}
private static void EnsureCreated(Action<bool> callback) {
if (!IsCreated) {
CreateAsync(callback);
} else {
callback(true);
}
}
/// @cond
/// Call Dispose to free up memory used by this API.
public void Dispose() {
m_instance = null;
}
/// @endcond
[System.Obsolete("Create() without arguments is deprecated. Use CreateAsync(callback) instead.")]
public static void Create() {
CreateAsync(null);
}
/// Asynchronously instantiates a GvrDayreamApi.
///
/// The provided callback will be called with a bool argument indicating
/// whether instance creation was successful.
public static void CreateAsync(Action<bool> callback) {
if (m_instance == null) {
m_instance = new GvrDaydreamApi();
}
#if UNITY_ANDROID && !UNITY_EDITOR
if (m_instance.m_daydreamApiObject != null) {
return;
}
if (m_instance.m_daydreamApiClass == null) {
Debug.LogErrorFormat("Failed to get DaydreamApi class, {0}", PACKAGE_DAYDREAM_API);
return;
}
AndroidJavaObject activity = GvrActivityHelper.GetActivity();
if (activity == null) {
Debug.LogError("DaydreamApi.Create failed to get acitivty");
return;
}
AndroidJavaObject context = GvrActivityHelper.GetApplicationContext(activity);
if (context == null) {
Debug.LogError("DaydreamApi.Create failed to get application context from activity");
return;
}
activity.Call(METHOD_RUN_ON_UI_THREAD, new AndroidJavaRunnable(() => {
m_instance.m_daydreamApiObject =
m_instance.m_daydreamApiClass.CallStatic<AndroidJavaObject>(METHOD_CREATE, context);
bool success = m_instance.m_daydreamApiObject != null;
if (!success) {
Debug.LogErrorFormat("DaydreamApi.Create call to {0} failed to instantiate object",
METHOD_CREATE);
}
if (callback != null) {
callback(success);
}
})
);
#endif // UNITY_ANDROID && !UNITY_EDITOR
}
[System.Obsolete("LaunchVrHome() deprecated. Use LaunchVrHomeAsync(callback) instead.")]
public static void LaunchVrHome() {
LaunchVrHomeAsync(null);
}
/// Asynchronously launches VR Home.
/// Instantiates an instance of GvrDaydreamApi if necessary. If successful,
/// launches VR Home.
/// The provided callback will be called with a bool argument indicating
/// whether instance creation and launch of VR Home was successful.
public static void LaunchVrHomeAsync(Action<bool> callback) {
EnsureCreated((success) => {
if (success) {
#if UNITY_ANDROID && !UNITY_EDITOR
m_instance.m_daydreamApiObject.Call(METHOD_LAUNCH_VR_HOMESCREEN);
#else
Debug.LogWarning("Launching VR Home is only possible on Android devices.");
#endif // UNITY_ANDROID && !UNITY_EDITOR
}
if (callback != null) {
callback(success);
}
});
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c4d57836d4e064f449f3e2f03330e9ce
timeCreated: 1490648002
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,85 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
/// <summary>
/// Provides information about the Android Intent that started the current Activity.
/// </summary>
public static class GvrIntent {
private const string METHOD_GET_INTENT = "getIntent";
private const string METHOD_HASH_CODE = "hashCode";
private const string METHOD_INTENT_GET_DATA_STRING = "getDataString";
private const string METHOD_INTENT_GET_BOOLEAN_EXTRA = "getBooleanExtra";
private const string EXTRA_VR_LAUNCH = "android.intent.extra.VR_LAUNCH";
// Returns the string representation of the data URI on which this activity's intent is
// operating. See Intent.getDataString() in the Android documentation.
public static string GetData() {
#if UNITY_EDITOR || !UNITY_ANDROID
return null;
#else
AndroidJavaObject androidIntent = GetIntent();
if (androidIntent == null) {
Debug.Log("Intent on current activity was null");
return null;
}
return androidIntent.Call<string>(METHOD_INTENT_GET_DATA_STRING);
#endif // UNITY_EDITOR || !UNITY_ANDROID
}
// Returns true if the intent category contains "android.intent.extra.VR_LAUNCH".
public static bool IsLaunchedFromVr() {
#if UNITY_EDITOR || !UNITY_ANDROID
return false;
#else
AndroidJavaObject androidIntent = GetIntent();
if (androidIntent == null) {
Debug.Log("Intent on current activity was null");
return false;
}
return androidIntent.Call<bool>(METHOD_INTENT_GET_BOOLEAN_EXTRA, EXTRA_VR_LAUNCH, false);
#endif // UNITY_EDITOR || !UNITY_ANDROID
}
// Returns the hash code of the Java intent object. Useful for discerning whether
// you have a new intent on un-pause.
public static int GetIntentHashCode() {
#if UNITY_EDITOR || !UNITY_ANDROID
return 0;
#else
AndroidJavaObject androidIntent = GetIntent();
if (androidIntent == null) {
Debug.Log("Intent on current activity was null");
return 0;
}
return androidIntent.Call<int>(METHOD_HASH_CODE);
#endif // UNITY_EDITOR || !UNITY_ANDROID
}
#if !UNITY_EDITOR && UNITY_ANDROID
private static AndroidJavaObject GetIntent() {
AndroidJavaObject androidActivity = null;
try {
androidActivity = GvrActivityHelper.GetActivity();
} catch (AndroidJavaException e) {
Debug.LogError("Exception while connecting to the Activity: " + e);
return null;
}
return androidActivity.Call<AndroidJavaObject>(METHOD_GET_INTENT);
}
#endif // !UNITY_EDITOR && UNITY_ANDROID
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9961197a3979a4fa684159098ac5881f
timeCreated: 1473201821
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,96 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR;
#else
using XRSettings = UnityEngine.VR.VRSettings;
#endif // UNITY_2017_2_OR_NEWER
/// Helper functions to perform common math operations for Gvr.
public static class GvrMathHelpers {
public static Vector3 GetIntersectionPosition(Camera cam, RaycastResult raycastResult) {
// Check for camera
if (cam == null) {
return Vector3.zero;
}
float intersectionDistance = raycastResult.distance + cam.nearClipPlane;
Vector3 intersectionPosition = cam.transform.position + cam.transform.forward * intersectionDistance;
return intersectionPosition;
}
public static Vector2 NormalizedCartesianToSpherical(Vector3 cartCoords) {
cartCoords.Normalize();
if (cartCoords.x == 0) {
cartCoords.x = Mathf.Epsilon;
}
float polar = Mathf.Atan(cartCoords.z / cartCoords.x);
if (cartCoords.x < 0) {
polar += Mathf.PI;
}
float elevation = Mathf.Asin(cartCoords.y);
return new Vector2(polar, elevation);
}
public static float EaseOutCubic(float min, float max, float value) {
if (min > max) {
Debug.LogError("Invalid values passed to EaseOutCubic, max must be greater than min. " +
"min: " + min + ", max: " + max);
return value;
}
value = Mathf.Clamp01(value);
value -= 1.0f;
float delta = max - min;
float result = delta * (value * value * value + 1.0f) + min;
return result;
}
/// Converts a float array of length 16 into a column-major 4x4 matrix.
public static Matrix4x4 ConvertFloatArrayToMatrix(float[] floatArray) {
Matrix4x4 result = new Matrix4x4();
if (floatArray == null || floatArray.Length != 16) {
throw new System.ArgumentException("floatArray must not be null and have a length of 16.");
}
result[0, 0] = floatArray[0];
result[1, 0] = floatArray[1];
result[2, 0] = floatArray[2];
result[3, 0] = floatArray[3];
result[0, 1] = floatArray[4];
result[1, 1] = floatArray[5];
result[2, 1] = floatArray[6];
result[3, 1] = floatArray[7];
result[0, 2] = floatArray[8];
result[1, 2] = floatArray[9];
result[2, 2] = floatArray[10];
result[3, 2] = floatArray[11];
result[0, 3] = floatArray[12];
result[1, 3] = floatArray[13];
result[2, 3] = floatArray[14];
result[3, 3] = floatArray[15];
return result;
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2057fd0fa21f34bc2ba2dd2a4a2a38b5
timeCreated: 1493226132
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,35 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public static class GvrUIHelpers {
/// Finds the meters scale for the local coordinate system
/// of the root canvas that contains the canvasObject passed in.
public static float GetMetersToCanvasScale(Transform canvasObject) {
Canvas canvas = canvasObject.GetComponentInParent<Canvas>();
if (canvas == null) {
return 0.0f;
}
if (!canvas.isRootCanvas) {
canvas = canvas.rootCanvas;
}
float metersToCanvasScale = canvas.transform.localScale.x;
return metersToCanvasScale;
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 00f93e3df109d4a69834d44deb072725
timeCreated: 1483744183
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,115 @@
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using Gvr.Internal;
#if UNITY_2017_2_OR_NEWER
using UnityEngine.XR;
#else
using UnityEngine.VR;
using XRNode = UnityEngine.VR.VRNode;
using XRSettings = UnityEngine.VR.VRSettings;
#endif // UNITY_2017_2_OR_NEWER
/// Helper functions common to GVR VR applications.
public static class GvrVRHelpers {
public static Vector2 GetViewportCenter() {
int viewportWidth = Screen.width;
int viewportHeight = Screen.height;
if (XRSettings.enabled) {
viewportWidth = XRSettings.eyeTextureWidth;
viewportHeight = XRSettings.eyeTextureHeight;
}
return new Vector2(0.5f * viewportWidth, 0.5f * viewportHeight);
}
public static Vector3 GetHeadForward() {
return GetHeadRotation() * Vector3.forward;
}
public static Quaternion GetHeadRotation() {
#if UNITY_EDITOR
if (InstantPreview.Instance != null && InstantPreview.Instance.IsCurrentlyConnected) {
// In-editor; Instant Preview is active:
return Camera.main.transform.localRotation;
} else {
// In-editor; Instant Preview is not active:
if (GvrEditorEmulator.Instance == null) {
Debug.LogWarning("No GvrEditorEmulator instance was found in your scene. Please ensure that " +
"GvrEditorEmulator exists in your scene.");
return Quaternion.identity;
}
return GvrEditorEmulator.Instance.HeadRotation;
}
#else
// Not running in editor:
return InputTracking.GetLocalRotation(XRNode.Head);
#endif // UNITY_EDITOR
}
public static Vector3 GetHeadPosition() {
#if UNITY_EDITOR
if (GvrEditorEmulator.Instance == null) {
Debug.LogWarning("No GvrEditorEmulator instance was found in your scene. Please ensure that " +
"GvrEditorEmulator exists in your scene.");
return Vector3.zero;
}
return GvrEditorEmulator.Instance.HeadPosition;
#else
return InputTracking.GetLocalPosition(XRNode.Head);
#endif // UNITY_EDITOR
}
public static float GetRecommendedMaxLaserDistance(GvrBasePointer.RaycastMode mode) {
switch(mode) {
case GvrBasePointer.RaycastMode.Direct:
return 20.0f;
case GvrBasePointer.RaycastMode.Hybrid:
return 1.0f;
case GvrBasePointer.RaycastMode.Camera:
default:
return 0.75f;
}
}
public static float GetRayIntersection(GvrBasePointer.RaycastMode mode) {
switch (mode) {
case GvrBasePointer.RaycastMode.Direct:
return 0.0f;
case GvrBasePointer.RaycastMode.Hybrid:
return 0.0f;
case GvrBasePointer.RaycastMode.Camera:
default:
return 2.5f;
}
}
public static bool GetShrinkLaser(GvrBasePointer.RaycastMode mode) {
switch (mode) {
case GvrBasePointer.RaycastMode.Direct:
return false;
case GvrBasePointer.RaycastMode.Hybrid:
return true;
case GvrBasePointer.RaycastMode.Camera:
default:
return false;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d0dea43be37504c45ab74cf9fcfbf34a
timeCreated: 1493226132
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: