using SC.XR.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API_GSXR_Slam
{
public static GSXRManager SlamManager
{
get
{
return GSXRManager.Instance;
}
}
public static GSXRPlugin plugin
{
get
{
return GSXRPlugin.Instance;
}
}
///API-No.1
///
/// 设置眼镜进入模式,运行过程中可修改
///
public static void GSXR_Set_TrackMode(TrackMode mode)
{
if (API_GSXR_Slam.SlamManager != null)
{
int trackingMode = API_GSXR_Slam.plugin.GetTrackingMode();
if (mode == TrackMode.Mode_6Dof)
{
trackingMode |= (int)GSXRPlugin.TrackingMode.kTrackingPosition;
}
else
{
trackingMode &= (int)(~(1 << 1));
}
API_GSXR_Slam.plugin.SetTrackingMode(trackingMode);
}
}
///API-No.2
///
/// Slam系统是否在运行
///
/// true表示在运行,false表示未运行(Pause时为false)
public static bool GSXR_Is_SlamRunning() {
if (API_GSXR_Slam.SlamManager != null) {
return API_GSXR_Slam.SlamManager.status.running;
}
return false;
}
///API-No.3
///
/// Slam系统是否初始化完成
///
///
public static bool GSXR_Is_SlamInitialized() {
if (API_GSXR_Slam.SlamManager != null) {
return API_GSXR_Slam.SlamManager.status.initialized;
}
return false;
}
///API-No.4
///
/// 设置Slam初始化完成时的回调
///
///
public static void GSXR_Add_InitializedCallBack(Action action)
{
GSXRManager.SlamInitializedCallBack += action;
}
///API-No.5
public static void GSXR_Remove_InitializedCallBack(Action action)
{
GSXRManager.SlamInitializedCallBack -= action;
}
///API-No.6
///
/// 设置渲染帧率,只能在Start中调用
///
/// 默认-1表示系统默认帧率,设置范围0-200
public static void GSXR_Set_RenderFrame(int frameRate = -1)
{
if (API_GSXR_Slam.SlamManager != null)
{
if (frameRate == -1)
{
API_GSXR_Slam.plugin.SetVSyncCount((int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k1));
QualitySettings.vSyncCount = (int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k1);//Vsync
}
else
{
API_GSXR_Slam.plugin.SetVSyncCount((int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k0));
QualitySettings.vSyncCount = (int)(API_GSXR_Slam.SlamManager.settings.vSyncCount = GSXRManager.SlamSettings.eVSyncCount.k0);//Don't sync
Application.targetFrameRate = (frameRate >= 0 && frameRate < 200) ? frameRate : 75;
}
}
}
///API-No.7
///
/// 获取左右眼摄像头
///
/// List[0]左眼 List[1]右眼,空表示系统未启动完成
public static List GSXR_Get_EyeCameras()
{
List cameraList = new List(2);
if (API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.status.running == true)
{
cameraList.Add(Camera.main);
cameraList.Add(API_GSXR_Slam.SlamManager.rightCamera);
}
return cameraList;
}
///API-No.8
///
/// 获取左右眼渲染的画面,为获取当前帧的渲染结果,当前帧结束时调用
///
/// List[0]左眼 List[1]右眼,空表示系统未启动完成
public static List GSXR_Get_RenderTexure()
{
List cameraList = GSXR_Get_EyeCameras();
List RTList = new List(2);
foreach (var item in cameraList)
{
RTList.Add(item.targetTexture);
}
return RTList;
}
///API-No.9
///
/// 获取头部物体,如果想获取头部的旋转移动等数据,在LateUpdate方法里调用
///
/// 空表示系统未启动完成
public static Transform GSXR_Get_Head()
{
return Camera.main.transform;
}
///API-No.10
///
/// 设置瞳距,Awake时调用,Start后调用无效
///
/// 瞳距的偏移量,单位米
public static void GSXR_Set_PD(float offset = 0)
{
if (API_GSXR_Slam.SlamManager != null) {
API_GSXR_Slam.plugin.GSXR_SetMeshOffset(0, offset / 2);
API_GSXR_Slam.plugin.GSXR_SetMeshOffset(2, offset / 2);
//Camera.mainOffsetPostion += offset / 2 * Vector3.left;
//API_GSXR_Slam.SlamManager.rightCameraOffsetPostion += offset / 2 * Vector3.right;
}
}
///API-No.11
///
/// 获取瞳距,
///
/// 右眼类型:2,左眼类型:0
public static float GSXR_Get_PD(int type)
{
if (API_GSXR_Slam.SlamManager != null)
{
return API_GSXR_Slam.plugin.GSXR_GetMeshOffset(type)+0.064f;
}
return -1;
}
///API-No.12
///
/// 重定位,若无效果,表示系统初始化未完成,且只有在眼镜上有效
///
public static void GSXR_RecenterTracking()
{
if (API_GSXR_Slam.SlamManager != null)
{
API_GSXR_Slam.SlamManager.RecenterTracking();
}
}
///API-No.13
///
/// StartSlam
///
public static void GSXR_Start_Slam()
{
if (API_GSXR_Slam.SlamManager != null)
{
API_GSXR_Slam.SlamManager.StartSlam();
}
}
///API-No.14
///
/// StopSlam
/// When a StartSlam is running (not completed), calling StopSlam will not work
///
public static void GSXR_Stop_Slam()
{
if (API_GSXR_Slam.SlamManager != null)
{
API_GSXR_Slam.SlamManager.StopSlam();
}
}
///API-No.15
///
/// ResetSlam
///
public static void GSXR_Reset_Slam()
{
if (API_GSXR_Slam.SlamManager != null)
{
API_GSXR_Slam.SlamManager.ResetSlam();
}
}
public static void GSXR_Reset_Slam_V2()
{
GSXR_ResaveMap("resetslam");
}
///API-No.16
///
/// IS Slam 6Dof DataLost
///
public static bool GSXR_Is_SlamDataLost
{
get
{
if (API_GSXR_Slam.SlamManager != null)
{
return API_GSXR_Slam.SlamManager.IsTrackingValid;
}
return true;
}
}
///API-No.17
///
/// Get FishEye Data
///
public static int GSXR_Get_LatestFishEyeBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData)
{
if (API_GSXR_Slam.plugin != null)
{
PermissionRequest.getInstance.GetPerssion(UnityEngine.Android.Permission.Camera);
return API_GSXR_Slam.plugin.GSXR_Get_LatestFishEyeBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
}
return 0;
}
static bool isOpen=false;
public static bool GSXR_Is_EnablePointCloudData()
{
return isOpen;
}
public static void GSXR_Set_PointCloudData(bool _isOpen)
{
isOpen = _isOpen;
}
public static int GSXR_Get_PointCloudData(ref int dataNum, ref ulong dataTimestamp, float[] dataArray)
{
if (isOpen == false) {
dataNum = 0;
return 0;
}
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.plugin.GSXR_Get_PointCloudData(ref dataNum, ref dataTimestamp, dataArray);
}
return 0;
}
public static int GSXR_Get_OfflineMapRelocState()
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.plugin.GSXR_Get_OfflineMapRelocState();
}
return 0;
}
public static int GSXR_ResaveMap(string path)
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.plugin.GSXR_ResaveMap(path);
}
return 0;
}
public static void GSXR_SaveMap()
{
if (API_GSXR_Slam.plugin != null) {
API_GSXR_Slam.plugin.GSXR_SaveMap();
}
}
public static int GSXR_Get_Gnss(ref double dt, float[] gnss)
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.plugin.GSXR_Get_Gnss(ref dt, gnss);
}
return 0;
}
public static int GSXR_Get_PanelNum()
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.plugin.GSXR_Get_PanelNum();
}
return -1;
}
public static int GSXR_Get_PanelInfo(float[] info)
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.plugin.GSXR_Get_PanelInfo(info);
}
return -1;
}
public static int GSXR_Set_HeadOrigin(Vector3 postion) {
if (API_GSXR_Slam.plugin != null) {
API_GSXR_Slam.SlamManager.transform.position = (API_GSXR_Slam.SlamManager.transform.position - Camera.main.transform.position) + postion;
return 1;
}
return -1;
}
public static int GSXR_Set_WorldOrigin(Vector3 postion) {
if (API_GSXR_Slam.plugin != null) {
API_GSXR_Slam.SlamManager.transform.position = Matrix4x4.TRS(-postion, Quaternion.identity, Vector3.one).MultiplyPoint(API_GSXR_Slam.SlamManager.transform.position);
return 1;
}
return -1;
}
#region Old KS API
///
/// Controller Vibrate
///
/// 0-LeftController 1-RightController
///
///
///
public static void GSXR_Set_ControllerVibrate(int index, bool isOn, GSXRManager.VibrateOnType vibrateOnType = GSXRManager.VibrateOnType.OneShot, float time = 0.1f) {
if (API_GSXR_Slam.plugin != null) {
API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, vibrateOnType, time);
}
}
///
/// GSXR_Get_ControllerVibrateStatus
///
/// 0-LeftController 1-RightController
///
public static bool GSXR_Get_ControllerVibrateStatus(int index)
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerVibrateStatus(index);
}
return false;
}
#endregion
#region NEW KS Controller API
///
/// Controller Vibrate
///
///
///
///
///
///
///
public static int GSXR_Set_ControllerVibrate(int index, bool isOn, float amplitude, float frequency, float time)
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, amplitude, frequency, time);
}
return -1;
}
[Obsolete("Please Use NEW KS Vibrate GSXR_Set_ControllerVibrate(int index, bool isOn, float amplitude, float frequency, float time)")]
public static int GSXR_Set_ControllerVibrate(int index, bool isOn, int amplitude, int frequency, int time)
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.SlamManager.GSXR_Set_ControllerVibrate(index, isOn, amplitude/15f, frequency/15f, time/10f);
}
return -1;
}
///
/// GSXR_Get_ControllerVibrateState,
/// return:"0" is false ,"1" is true
///
/// 0-LeftController 1-RightController
///
public static int GSXR_Get_ControllerVibrateState(int index)
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerVibrateState(index);
}
return -1;
}
#endregion
#region Other API
///
/// GSXR_Set_OnFloorOrOnHead
///
/// 0-Head, 1-Floor
public static void GSXR_Set_OnFloorOrOnHead(int type)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.SlamManager.GSXR_Set_OnFloorOrOnHead(type);
}
}
public static void GSXR_Bind_Controller(int index,int type)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.SlamManager.GSXR_Bind_Controller(index, type);
}
}
public static void GSXR_Unbind_Controller(int index, int type)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.SlamManager.GSXR_Unbind_Controller(index, type);
}
}
public static int GSXR_Get_ControllerBondState()
{
if (API_GSXR_Slam.plugin != null)
{
return API_GSXR_Slam.SlamManager.GSXR_Get_ControllerBondState();
}
return -2;
}
///
/// Whether to Enable the Controller binding status callback interface
///
public static void GSXR_Set_ControllerBondEventCallback(bool isEnable)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.SlamManager.GSXR_Set_ControllerBondEventCallback(isEnable);
}
}
///
/// Add Controller binding state callback event
///
///
public static void GSXR_Add_ControllerBondEventCallback(Action callback)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.SlamManager.GSXR_Add_ControllerBondEventCallback(callback);
}
}
///
/// Remove Controller binding state callback event
///
///
public static void GSXR_Remove_ControllerBondEventCallback(Action callback)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.SlamManager.GSXR_Remove_ControllerBondEventCallback(callback);
}
}
#endregion
#region SeeThrough API
///
/// GSXR_StartSeeThrough
///
public static void GSXR_StartSeeThrough()
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.plugin.GSXR_StartSeeThrough();
}
}
///
/// GSXR_StopSeeThrough
///
public static void GSXR_StopSeeThrough()
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.plugin.GSXR_StopSeeThrough();
}
}
public static void GSXR_Add_SeeThrough_Callback(Action callback)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.plugin.GSXR_Regist_SeeThrough_Callback(callback);
}
}
public static void GSXR_Remove_SeeThrough_Callback(Action callback)
{
if (API_GSXR_Slam.plugin != null)
{
API_GSXR_Slam.plugin.GSXR_UnRegist_SeeThrough_Callbak(callback);
}
}
#endregion
///
/// GSXR_Add_SlamPauseCallback
///
///
public static void GSXR_Add_SlamPauseCallback(GSXRManager.OnApplicationPauseDele callback)
{
GSXRManager.onApplicationPauseDele += callback;
}
///
///
///
///
public static void GSXR_Remove_SlamPauseCallback(GSXRManager.OnApplicationPauseDele callback)
{
GSXRManager.onApplicationPauseDele -= callback;
}
}