//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
namespace Rokid.XR.Core
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR;
using UnityEngine.XR.Management;
///
/// XR Loader for UXR Plugin.
/// Loads Display and Input Subsystems.
///
public class XRLoader : XRLoaderHelper
{
private static List _displaySubsystemDescriptors =
new List();
private static List _inputSubsystemDescriptors =
new List();
///
/// Pairs the native enum to set the graphics API being used.
///
private enum UXRGraphicsApi
{
kOpenGlEs2 = 1,
kOpenGlEs3 = 2,
kVulkan = 4,
kNone = -1,
}
///
/// Describes the possible orientation of the viewport.
///
private enum UXRViewportOrientation
{
kLandscapeLeft = 0,
kLandscapeRight = 1,
kPortrait = 2,
kPortraitUpsideDown = 3,
}
///
/// Gets a value indicating whether the subsystems are initialized or not.
///
///
///
/// True after a successful call to Initialize() without a posterior call to
/// Deinitialize().
///
//internal static bool _isInitialized { get; private set; }
public static bool _isInitialized { get; set; }
///
/// Gets a value indicating whether the subsystems are started or not.
///
///
///
/// True after a successful call to Start() without a posterior call to Stop().
///
internal static bool _isStarted { get; private set; }
///
/// Initialize the loader. This should initialize all subsystems to support the desired
/// runtime setup this loader represents.
///
///
/// Whether or not initialization succeeded.
public override bool Initialize()
{
UXRSDKInitialize();
CreateSubsystem(
_displaySubsystemDescriptors, "CardboardDisplay");
CreateSubsystem(
_inputSubsystemDescriptors, "CardboardInput");
// add for XRI
OnXRILoaderInitialize?.Invoke(this);
_isInitialized = true;
return true;
}
///
/// Ask loader to start all initialized subsystems.
///
///
/// Whether or not all subsystems were successfully started.
public override bool Start()
{
StartSubsystem();
StartSubsystem();
// add for XRI
OnXRILoaderStart?.Invoke(this);
_isStarted = true;
return true;
}
///
/// Ask loader to stop all initialized subsystems.
///
///
/// Whether or not all subsystems were successfully stopped.
public override bool Stop()
{
StopSubsystem();
StopSubsystem();
// add for XRI
OnXRILoaderStop?.Invoke(this);
_isStarted = false;
return true;
}
///
/// Ask loader to deinitialize all initialized subsystems.
///
///
/// Whether or not deinitialization succeeded.
public override bool Deinitialize()
{
DestroySubsystem();
DestroySubsystem();
// add for XRI
OnXRILoaderDeinitialize?.Invoke(this);
UXRSDKDeinitialize();
_isInitialized = false;
return true;
}
///
/// Sets the screen parameters in the XR scene.
///
///
///
/// The rectangle where the XR scene will be rendered.
///
public static void RecalculateRectangles(Rect renderingArea)
{
setScreenParams((int)renderingArea.width, (int)renderingArea.height);
Debug.Log("-uxr- ScreenParam h:" + Screen.height + ",renderingArea: "+ renderingArea.ToString());
Debug.Log("-uxr- displays len:" + Display.displays.Length + ",glass systemHeight: " + Display.displays[Display.displays.Length-1].systemHeight +
",renderingHeight: " + Display.displays[Display.displays.Length-1].renderingHeight);
}
///
/// Sets which viewport orientation is being used by Unity to the native implementation.
///
///
///
/// The required screen orientation.
///
public static void SetViewportOrientation(ScreenOrientation screenOrientation)//TODO
{
/*switch (screenOrientation)
{
case ScreenOrientation.LandscapeLeft:
CardboardUnity_setViewportOrientation(
UXRViewportOrientation.kLandscapeLeft);
break;
case ScreenOrientation.LandscapeRight:
CardboardUnity_setViewportOrientation(
UXRViewportOrientation.kLandscapeRight);
break;
case ScreenOrientation.Portrait:
CardboardUnity_setViewportOrientation(UXRViewportOrientation.kLandscapeLeft);
break;
case ScreenOrientation.PortraitUpsideDown:
CardboardUnity_setViewportOrientation(
UXRViewportOrientation.kPortraitUpsideDown);
break;
default:
Debug.LogWarning(
"The UXR Plugin does not support the selected screen orientation." +
"Setting landscape left as default.");
CardboardUnity_setViewportOrientation(
UXRViewportOrientation.kLandscapeLeft);
break;
}*/
}
///
/// Sets which Graphics API is being used by Unity to the native implementation.
///
private static void SetGraphicsApi()
{
switch (SystemInfo.graphicsDeviceType)
{
case GraphicsDeviceType.OpenGLES2:
CardboardUnity_setGraphicsApi(UXRGraphicsApi.kOpenGlEs2);
break;
case GraphicsDeviceType.OpenGLES3:
CardboardUnity_setGraphicsApi(UXRGraphicsApi.kOpenGlEs3);
break;
#if UNITY_ANDROID
case GraphicsDeviceType.Vulkan:
CardboardUnity_setGraphicsApi(UXRGraphicsApi.kVulkan);
break;
#endif
default:
Debug.LogErrorFormat(
"The UXR Plugin cannot be initialized given that the selected " +
"Graphics API ({0}) is not supported. Please use OpenGL ES 2.0, " +
"OpenGL ES 3.0.", SystemInfo.graphicsDeviceType);
break;
}
}
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
private static extern void setScreenParams(int viewport_width, int viewport_height);
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
private static extern void CardboardUnity_setGraphicsApi(UXRGraphicsApi graphics_api);
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
private static extern void CardboardUnity_setViewportOrientation(
UXRViewportOrientation viewport_orientation);
#if UNITY_ANDROID
[DllImport(ApiConstants.UXR_GFX_PLUGIN)]
private static extern void UXRUnity_initializeAndroid(IntPtr context);
#endif
///
/// For Android, initializes JavaVM and Android activity context.
/// Then,it sets the screen size in pixels.
///
private void UXRSDKInitialize()
{
#if UNITY_ANDROID
// TODO(b/169797155): Move this to UnityPluginLoad().
// Gets Unity context (Main Activity).
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var activity = unityPlayer.GetStatic("currentActivity");
var context = activity.Call("getApplicationContext");
// Initializes SDK.
UXRUnity_initializeAndroid(activity.GetRawObject());
#endif
//SetGraphicsApi();//TODO
SetViewportOrientation(Screen.orientation);
// Safe area is required to avoid rendering behind the notch. If the device does not
// have any notch, it will be equivalent to the full screen area.
RecalculateRectangles(Screen.safeArea);
}
///
/// the XR provider is deinitialized.
///
private void UXRSDKDeinitialize()
{
}
#region ForXRI
public static Action OnXRILoaderInitialize;
public static Action OnXRILoaderStart;
public static Action OnXRILoaderStop;
public static Action OnXRILoaderDeinitialize;
public void CreateCustomSubsystem(List descriptors, string id)
where TDescriptor : ISubsystemDescriptor
where TSubsystem : ISubsystem
{
CreateSubsystem(descriptors, id);
}
public void StartCustomSubsystem() where T : class, ISubsystem
{
StartSubsystem();
}
public void StopCustomSubsystem() where T : class, ISubsystem
{
StopSubsystem();
}
public void DestroyCustomSubsystem() where T : class, ISubsystem
{
DestroySubsystem();
}
#endregion
}
}