123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943 |
-
- namespace NRKernal
- {
- using System;
- using UnityEngine;
-
- public enum ControllerHandEnum
- {
-
- Right = 0,
-
- Left = 1
- }
-
- public enum RaycastModeEnum
- {
-
- Gaze,
-
- Laser
- }
-
- public enum InputSourceEnum
- {
-
- Hands,
-
- Controller
- }
-
- public enum ControllerVisualType
- {
-
- None = 0,
-
- NrealLight = 1,
-
- Phone = 2
- }
-
-
-
-
-
-
- [HelpURL("https://developer.nreal.ai/develop/unity/controller")]
- [ScriptOrder(NativeConstants.NRINPUT_ORDER)]
- public partial class NRInput : SingletonBehaviour<NRInput>
- {
-
- [Tooltip("If enable this, phone virtual controller would be shown in Unity Editor")]
- [SerializeField]
- private bool m_EmulateVirtualDisplayInEditor;
-
- [SerializeField]
- private Transform m_OverrideCameraCenter;
-
- [SerializeField]
- private ControllerAnchorsHelper m_AnchorHelper;
-
- [SerializeField]
- private RaycastModeEnum m_RaycastMode = RaycastModeEnum.Laser;
-
- [SerializeField]
- private InputSourceEnum m_InputSourceType = InputSourceEnum.Controller;
-
- [SerializeField]
- private float m_ClickInterval = 0.3f;
-
- [SerializeField]
- private float m_DragThreshold = 0.02f;
-
- private ControllerVisualManager m_VisualManager;
-
- private int m_LastControllerCount;
-
- private bool m_ReticleVisualActive = true;
-
- private bool m_LaserVisualActive = true;
-
- private bool m_ControllerVisualActive = true;
-
- private bool m_HapticVibrationEnabled = true;
-
- private bool m_RaycastersActive = true;
-
- private bool m_HasCheckedCameraCenter;
-
- private bool m_IsListeningToEditorValidateEvents = false;
-
- private InputSourceEnum m_EditorCachedInputSourceType = InputSourceEnum.Controller;
-
- private static bool m_IgnoreRecenterCallback = false;
-
- private static ControllerHandEnum m_DomainHand = ControllerHandEnum.Right;
-
- private static ControllerProviderBase m_ControllerProvider;
-
- private static ControllerState[] m_States = new ControllerState[MAX_CONTROLLER_STATE_COUNT]
- {
- new ControllerState(),
- new ControllerState()
- };
-
- public const int MAX_CONTROLLER_STATE_COUNT = 2;
-
- public static Action<ControllerHandEnum> OnDomainHandChanged;
-
- public static Action OnControllerConnected;
-
- public static Action OnControllerDisconnected;
-
- public static Action OnBeforeControllerRecenter;
-
- internal static Action OnControllerRecentering;
-
- public static Action OnControllerRecentered;
-
- public static Action OnControllerStatesUpdated;
-
-
-
- public static bool ReticleVisualActive { get { return Instance.m_ReticleVisualActive; } set { Instance.m_ReticleVisualActive = value; } }
-
-
- public static bool LaserVisualActive { get { return Instance.m_LaserVisualActive; } set { Instance.m_LaserVisualActive = value; } }
-
-
-
- public static bool ControllerVisualActive { get { return Instance.m_ControllerVisualActive; } set { Instance.m_ControllerVisualActive = value; } }
-
-
- public static bool HapticVibrationEnabled { get { return Instance.m_HapticVibrationEnabled; } set { Instance.m_HapticVibrationEnabled = value; } }
-
-
-
- public static bool RaycastersActive { get { return Instance.m_RaycastersActive; } set { Instance.m_RaycastersActive = value; } }
-
-
- public static bool EmulateVirtualDisplayInEditor { get { return Instance ? Instance.m_EmulateVirtualDisplayInEditor : false; } }
-
-
- public static ControllerAnchorsHelper AnchorsHelper { get { return Instance.m_AnchorHelper; } }
-
-
- public static ControllerHandEnum DomainHand { get { return m_DomainHand; } }
-
-
- public static RaycastModeEnum RaycastMode { get { return Instance.m_RaycastMode; } set { Instance.m_RaycastMode = value; } }
-
-
- public static InputSourceEnum CurrentInputSourceType { get { return Instance.m_InputSourceType; } }
-
-
- public static float ClickInterval { get { return Instance.m_ClickInterval; } set { Instance.m_ClickInterval = value; } }
-
-
- public static float DragThreshold { get { return Instance.m_DragThreshold; } set { Instance.m_DragThreshold = value; } }
-
-
- public static Transform CameraCenter { get { return Instance.GetCameraCenter(); } }
-
- public static NRHandsManager Hands = new NRHandsManager();
-
- private void Start()
- {
- if (isDirty)
- {
- return;
- }
- Init();
- }
-
- private void OnUpdate()
- {
- if (m_ControllerProvider == null)
- return;
- UpdateControllerProvider();
- }
-
- private void UpdateControllerProvider()
- {
- if (m_ControllerProvider.Inited)
- {
- m_ControllerProvider.Update();
- if (OnControllerStatesUpdated != null)
- {
- OnControllerStatesUpdated();
- }
- CheckControllerConnection();
- CheckControllerRecentered();
- CheckControllerButtonEvents();
- }
- else
- {
- m_ControllerProvider.Update();
- #if !UNITY_EDITOR
- if (m_ControllerProvider is NRControllerProvider)
- {
- m_DomainHand = ((NRControllerProvider)m_ControllerProvider).GetHandednessType();
- NRDebugger.Info("[NRInput] Set default domain hand:" + m_DomainHand);
- }
- #endif
- }
- }
-
- private void OnEnable()
- {
- if (isDirty)
- {
- return;
- }
- NRKernalUpdater.OnPostUpdate += OnUpdate;
- m_ControllerProvider?.OnResume();
- }
-
- private void OnDisable()
- {
- if (isDirty)
- {
- return;
- }
- NRKernalUpdater.OnPostUpdate -= OnUpdate;
- m_ControllerProvider?.OnPause();
- }
- #if UNITY_EDITOR
-
- private void OnValidate()
- {
- if (!m_IsListeningToEditorValidateEvents)
- return;
- if (m_EditorCachedInputSourceType != m_InputSourceType)
- {
- SetInputSource(m_InputSourceType);
- }
- }
- #endif
-
-
-
- public string GetVersion(int index)
- {
- if (m_ControllerProvider is NRControllerProvider)
- {
- return ((NRControllerProvider)m_ControllerProvider).GetVersion(index);
- }
- else
- {
- return "0.0.0";
- }
- }
-
- internal static void Destroy()
- {
- if (m_ControllerProvider != null)
- {
- m_ControllerProvider.OnDestroy();
- m_ControllerProvider = null;
- }
- }
-
-
-
-
- new void OnDestroy()
- {
- if (isDirty)
- {
- return;
- }
- base.OnDestroy();
- Destroy();
- }
-
- private void CheckControllerConnection()
- {
- int currentControllerCount = GetAvailableControllersCount();
- if (m_LastControllerCount < currentControllerCount)
- {
- if (OnControllerConnected != null)
- {
- OnControllerConnected();
- }
- }
- else if (m_LastControllerCount > currentControllerCount)
- {
- if (OnControllerDisconnected != null)
- {
- OnControllerDisconnected();
- }
- }
- m_LastControllerCount = currentControllerCount;
- }
-
- private void CheckControllerRecentered()
- {
- if (GetControllerState(DomainHand).recentered)
- {
- if (m_IgnoreRecenterCallback == false && OnBeforeControllerRecenter != null)
- {
- OnBeforeControllerRecenter();
- }
- if (OnControllerRecentering != null)
- {
- OnControllerRecentering();
- }
- if (m_IgnoreRecenterCallback == false && OnControllerRecentered != null)
- {
- OnControllerRecentered();
- }
- m_IgnoreRecenterCallback = false;
- }
- }
-
- private void CheckControllerButtonEvents()
- {
- int currentControllerCount = GetAvailableControllersCount();
- for (int i = 0; i < currentControllerCount; i++)
- {
- m_States[i].CheckButtonEvents();
- }
- }
-
-
- private void OnApplicationPause(bool paused)
- {
- if (m_ControllerProvider == null || !m_ControllerProvider.Inited)
- return;
- if (paused)
- {
- m_ControllerProvider.OnPause();
- }
- else
- {
- m_ControllerProvider.OnResume();
- m_IgnoreRecenterCallback = true;
- m_ControllerProvider.Recenter();
- }
- }
-
- private void Init()
- {
- NRDebugger.Info("[NRInput] Init");
- NRDevice.Instance.Init();
- m_VisualManager = gameObject.AddComponent<ControllerVisualManager>();
- m_VisualManager.Init(m_States);
- SwitchControllerProvider(ControllerProviderFactory.controllerProviderType);
- #if UNITY_EDITOR
- InitEmulator();
- m_IsListeningToEditorValidateEvents = true;
- #endif
- SetInputSourceSafely(m_InputSourceType);
- }
- #if UNITY_EDITOR
- private void InitEmulator()
- {
- if (!NREmulatorManager.Inited && !GameObject.Find("NREmulatorManager"))
- {
- NREmulatorManager.Inited = true;
- GameObject.Instantiate(Resources.Load("Prefabs/NREmulatorManager"));
- }
- if (!GameObject.Find("NREmulatorController"))
- {
- Instantiate(Resources.Load<GameObject>("Prefabs/NREmulatorController"));
- }
- }
- #endif
-
-
- private Transform GetCameraCenter()
- {
- if (m_OverrideCameraCenter == null)
- {
- m_HasCheckedCameraCenter = true;
- return NRSessionManager.Instance.CenterCameraAnchor;
- }
- else
- {
- if (!m_HasCheckedCameraCenter)
- {
- CheckCameraCenter();
- }
- return m_OverrideCameraCenter;
- }
- }
-
- private void CheckCameraCenter()
- {
- if (m_OverrideCameraCenter != null
- && NRSessionManager.Instance != null
- && NRSessionManager.Instance.NRSessionBehaviour != null)
- {
- var cameraRigTransform = NRSessionManager.Instance.NRSessionBehaviour.transform;
- if (m_OverrideCameraCenter.parent == cameraRigTransform)
- {
- m_OverrideCameraCenter = NRSessionManager.Instance.CenterCameraAnchor;
- }
- }
- m_HasCheckedCameraCenter = true;
- }
-
-
-
- private static int ConvertHandToIndex(ControllerHandEnum handEnum)
- {
- if (GetAvailableControllersCount() < 2)
- {
- return DomainHand == handEnum ? 0 : 1;
- }
- else
- {
- return (int)handEnum;
- }
- }
-
-
-
- private static ControllerState GetControllerState(ControllerHandEnum hand)
- {
- return m_States[ConvertHandToIndex(hand)];
- }
-
-
-
-
- private static void SetInputSourceSafely(InputSourceEnum inputSourceType)
- {
- var adaptInputSourceType = AdaptInputSource(inputSourceType);
- if (adaptInputSourceType != inputSourceType)
- {
- NRDebugger.Warning("[NRInput] AutoAdaptInputSource : {0} => {1}", inputSourceType, adaptInputSourceType);
- inputSourceType = adaptInputSourceType;
- }
- if (SetInputSource(inputSourceType))
- {
- return;
- }
- var fallbackInputSourceType = InputSourceEnum.Controller;
- NRDebugger.Info("[NRInput] Set Input Source To {0} Failed. Now Set Input Source To Fallback: {1}", inputSourceType, fallbackInputSourceType);
- SetInputSource(fallbackInputSourceType);
- }
-
-
- private static InputSourceEnum AdaptInputSource(InputSourceEnum inputSourceType)
- {
- if (inputSourceType == InputSourceEnum.Hands && !NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_HANDTRACKING))
- return InputSourceEnum.Controller;
- return inputSourceType;
- }
-
-
-
-
- internal static void SwitchControllerProvider(Type providerType)
- {
- if (m_ControllerProvider != null && m_ControllerProvider.GetType() == providerType)
- return;
- var nextControllerProvider = ControllerProviderFactory.GetOrCreateControllerProvider(providerType, m_States);
- if (nextControllerProvider == null)
- return;
- if (m_ControllerProvider != null)
- {
- m_ControllerProvider.OnPause();
- }
- m_ControllerProvider = nextControllerProvider;
- if (m_ControllerProvider != null)
- {
- m_ControllerProvider.OnResume();
- }
- }
-
-
- public static void SetDomainHandMode(ControllerHandEnum handEnum)
- {
- if (m_DomainHand == handEnum)
- return;
- m_DomainHand = handEnum;
- if (OnDomainHandChanged != null)
- {
- OnDomainHandChanged(m_DomainHand);
- }
- }
-
-
-
- public static bool SetInputSource(InputSourceEnum inputSourceType)
- {
- NRDebugger.Info("[NRInput] Set Input Source: " + inputSourceType);
- if (Instance == null)
- {
- return false;
- }
- bool success = true;
- switch (inputSourceType)
- {
- case InputSourceEnum.Hands:
- success = Hands.StartHandTracking();
- break;
- case InputSourceEnum.Controller:
- success = Hands.StopHandTracking();
- break;
- default:
- break;
- }
- if (success)
- {
- Instance.m_InputSourceType = inputSourceType;
- #if UNITY_EDITOR
- Instance.m_EditorCachedInputSourceType = inputSourceType;
- #endif
- }
- NRDebugger.Info("[NRInput] Input Source Set. Current Input Source = " + CurrentInputSourceType);
- return success;
- }
-
-
- public static int GetAvailableControllersCount()
- {
- if (m_ControllerProvider == null)
- {
- return 0;
- }
- return m_ControllerProvider.ControllerCount;
- }
-
-
- public static ControllerType GetControllerType()
- {
- return GetControllerState(DomainHand).controllerType;
- }
-
-
- public static ControllerConnectionState GetControllerConnectionState()
- {
- return GetControllerState(DomainHand).connectionState;
- }
-
-
-
- public static bool CheckControllerAvailable(ControllerHandEnum handEnum)
- {
- if (m_ControllerProvider is NRHandControllerProvider)
- {
- return Hands.GetHandState(handEnum == ControllerHandEnum.Right ? HandEnum.RightHand : HandEnum.LeftHand).pointerPoseValid;
- }
- int availableCount = GetAvailableControllersCount();
- if (availableCount == 2)
- {
- return true;
- }
- if (availableCount == 1)
- {
- return handEnum == DomainHand;
- }
- return false;
- }
-
-
-
- public static bool GetControllerAvailableFeature(ControllerAvailableFeature feature)
- {
- if (GetAvailableControllersCount() == 0)
- return false;
- return GetControllerState(m_DomainHand).IsFeatureAvailable(feature);
- }
-
-
-
- public static bool GetButton(ControllerButton button)
- {
- return GetButton(m_DomainHand, button);
- }
-
-
-
- public static bool GetButtonDown(ControllerButton button)
- {
- return GetButtonDown(m_DomainHand, button);
- }
-
-
-
- public static bool GetButtonUp(ControllerButton button)
- {
- return GetButtonUp(m_DomainHand, button);
- }
-
-
- public static bool IsTouching()
- {
- return IsTouching(m_DomainHand);
- }
-
-
-
-
- public static Vector2 GetTouch()
- {
- return GetTouch(m_DomainHand);
- }
-
-
- public static Vector2 GetDeltaTouch()
- {
- return GetDeltaTouch(m_DomainHand);
- }
-
-
-
- public static Vector3 GetPosition()
- {
- return GetPosition(m_DomainHand);
- }
-
-
- public static Quaternion GetRotation()
- {
- return GetRotation(m_DomainHand);
- }
-
-
- public static Vector3 GetGyro()
- {
- return GetGyro(m_DomainHand);
- }
-
-
- public static Vector3 GetAccel()
- {
- return GetAccel(m_DomainHand);
- }
-
-
- public static Vector3 GetMag()
- {
- return GetMag(m_DomainHand);
- }
-
-
- public static int GetControllerBattery()
- {
- return GetControllerBattery(DomainHand);
- }
-
-
-
-
- public static void TriggerHapticVibration(float durationSeconds = 0.1f, float frequency = 200f, float amplitude = 0.8f)
- {
- TriggerHapticVibration(m_DomainHand, durationSeconds, frequency, amplitude);
- }
-
-
-
-
-
-
- public static bool GetButton(ControllerHandEnum hand, ControllerButton button)
- {
- return GetControllerState(hand).GetButton(button);
- }
-
-
-
-
-
- public static bool GetButtonDown(ControllerHandEnum hand, ControllerButton button)
- {
- return GetControllerState(hand).GetButtonDown(button);
- }
-
-
-
-
-
- public static bool GetButtonUp(ControllerHandEnum hand, ControllerButton button)
- {
- return GetControllerState(hand).GetButtonUp(button);
- }
-
-
-
-
- public static bool IsTouching(ControllerHandEnum hand)
- {
- return GetControllerState(hand).isTouching;
- }
-
-
-
-
-
- public static Vector2 GetTouch(ControllerHandEnum hand)
- {
- return GetControllerState(hand).touchPos;
- }
-
-
-
-
- public static Vector2 GetDeltaTouch(ControllerHandEnum hand)
- {
- return GetControllerState(hand).deltaTouch;
- }
-
-
-
-
-
- public static Vector3 GetPosition(ControllerHandEnum hand)
- {
- return GetControllerState(hand).position;
- }
-
-
-
- public static Quaternion GetRotation(ControllerHandEnum hand)
- {
- return GetControllerState(hand).rotation;
- }
-
-
-
- public static Vector3 GetGyro(ControllerHandEnum hand)
- {
- return GetControllerState(hand).gyro;
- }
-
-
-
- public static Vector3 GetAccel(ControllerHandEnum hand)
- {
- return GetControllerState(hand).accel;
- }
-
-
-
- public static Vector3 GetMag(ControllerHandEnum hand)
- {
- return GetControllerState(hand).mag;
- }
-
-
-
-
- public static int GetControllerBattery(ControllerHandEnum hand)
- {
- return GetControllerState(hand).batteryLevel;
- }
-
-
-
-
-
- public static void TriggerHapticVibration(ControllerHandEnum hand, float durationSeconds = 0.1f, float frequency = 200f, float amplitude = 0.8f)
- {
- if (!HapticVibrationEnabled)
- return;
- if (GetAvailableControllersCount() == 0)
- return;
- m_ControllerProvider.TriggerHapticVibration(ConvertHandToIndex(hand), durationSeconds, frequency, amplitude);
- }
-
- public static void RecenterController()
- {
- if (GetAvailableControllersCount() == 0)
- return;
- m_IgnoreRecenterCallback = false;
- m_ControllerProvider.Recenter();
- }
-
-
-
-
- public static void AddDownListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).AddButtonListener(ButtonEventType.Down, button, action);
- }
-
-
-
-
- public static void RemoveDownListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).RemoveButtonListener(ButtonEventType.Down, button, action);
- }
-
-
-
-
- public static void AddPressingListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).AddButtonListener(ButtonEventType.Pressing, button, action);
- }
-
-
-
-
- public static void RemovePressingListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).RemoveButtonListener(ButtonEventType.Pressing, button, action);
- }
-
-
-
-
- public static void AddUpListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).AddButtonListener(ButtonEventType.Up, button, action);
- }
-
-
-
-
- public static void RemoveUpListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).RemoveButtonListener(ButtonEventType.Up, button, action);
- }
-
-
-
-
- public static void AddClickListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).AddButtonListener(ButtonEventType.Click, button, action);
- }
-
-
-
-
- public static void RemoveClickListener(ControllerHandEnum hand, ControllerButton button, Action action)
- {
- GetControllerState(hand).RemoveButtonListener(ButtonEventType.Click, button, action);
- }
- }
- }
|