using SC.XR.Unity; using SC.XR.Unity.Module_InputSystem; using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; using static API_GSXR_Slam; public class SoftwareTest : MonoBehaviour { [SerializeField] private TextMeshProUGUI state_36Dof; [SerializeField] private TextMeshProUGUI state_isSlamRunning; [SerializeField] private TextMeshProUGUI state_isSlamSlamInitialized; [SerializeField] private TextMeshProUGUI action_InitializedCallBack; [SerializeField] private TextMeshProUGUI state_isSlamDataLost; [SerializeField] private TextMeshProUGUI state_SlamPosition; [SerializeField] private TextMeshProUGUI state_SlamRotation; [SerializeField] private RawImage renderTexture0; [SerializeField] private RawImage renderTexture1; [SerializeField] private RawImage FishEyeCamera0; [SerializeField] private RawImage FishEyeCamera1; [SerializeField] private int frameRate; private TrackMode trackMode = TrackMode.Mode_6Dof; List renderTextures; private bool render_Running = false; private bool isRenderCreated = false; private int onFloorOrOnHead = 0; Texture2D textureTemp; bool isPreview = false; int imageWidth; int imageHeight; bool outBUdate = true; uint outCurrFrameIndex = 0; ulong outFrameExposureNano = 0; byte[] outLeftFrameData; byte[] outRightFrameData; TextureFormat textureFormat = TextureFormat.Alpha8; private const string project_name = "SoftwareTest:"; /// /// 设置渲染帧率 /// 添加Slam初始化完成时的回调 /// 添加按键按下回调 /// private void Start() { GSXR_Set_RenderFrame(frameRate); GSXR_Add_InitializedCallBack(Action_InitializedCallBack); GSXR_Add_InitializedCallBack(Init); API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateRegister(KeyDown); } /// /// 按下左手柄按钮触发事件 /// X键启动Slam /// Y键停止Slam /// LFunction重置Slam /// /// /// private void KeyDown(InputKeyCode keyCode, InputDevicePartBase part) { if (keyCode == InputKeyCode.LFunction && part.PartType == InputDevicePartType.KSLeft) { Slam_Reset(); } } /// /// 3/6Dof模式 /// Slam系统是否在运行 /// Slam系统是否初始化完成 /// 获取左右眼瞳距 /// Slam数据是否丢失 /// Slam头部数据 /// private void Update() { //if (SlamManager == null || !GSXR_Is_SlamRunning()) //{ // return; //} state_36Dof.text = "TrackMode:" + trackMode; state_isSlamRunning.text = "SlamIsRunning:" + GSXR_Is_SlamRunning(); state_isSlamSlamInitialized.text = "SlamIsSlamInitialized:" + GSXR_Is_SlamInitialized(); state_isSlamDataLost.text = "IsSlamDataLost:" + GSXR_Is_SlamDataLost; state_SlamPosition.text =Camera.main.transform.position.ToString("F3"); state_SlamRotation.text =Camera.main.transform.rotation.ToString("F3"); } private void LateUpdate() { if (isPreview) { ShowCamera(); } else { FishEyeCamera0.texture = null; FishEyeCamera1.texture = null; } } /// /// 取消Slam初始化完成时的回调 /// 取消按键按下回调 /// 释放render /// private void OnDestroy() { GSXR_Remove_InitializedCallBack(Action_InitializedCallBack); API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateUnRegister(KeyDown); if (isRenderCreated) { renderTextures[0].Release(); renderTextures[1].Release(); } GSXR_Remove_InitializedCallBack(Init); textureTemp = null; outCurrFrameIndex = 0; outFrameExposureNano = 0; outLeftFrameData = null; outRightFrameData = null; } /// /// 设置Slam初始化完成时的回调 /// private void Action_InitializedCallBack() { action_InitializedCallBack.text = "GSXR_Add_InitializedCallBack succeed."; Debug.Log(project_name + "GSXR_Add_InitializedCallBack succeed."); } /// /// 切换6Dof/3Dof /// public void Set_3DofOR6Dof() { trackMode = trackMode == TrackMode.Mode_6Dof ? TrackMode.Mode_3Dof : TrackMode.Mode_6Dof; GSXR_Set_TrackMode(trackMode); Debug.Log(project_name + "Set trackMode to" + trackMode + "."); } /// /// 获取左右眼渲染画面 /// public void Get_RenderTexure() { if (!isRenderCreated) { renderTextures = GSXR_Get_RenderTexure(); isRenderCreated = true; Debug.Log(project_name + "Get RenderTexure."); } if (render_Running) { renderTexture0.texture = null; renderTexture1.texture = null; Debug.Log(project_name + "Clear render0 and render1."); } else { renderTexture0.texture = renderTextures[0]; renderTexture1.texture = renderTextures[1]; Debug.Log(project_name + "Show render0 and render1."); } render_Running = !render_Running; } /// /// 重定位 /// public void RecenterTracking() { GSXR_RecenterTracking(); Debug.Log(project_name + "GSXR_RecenterTracking."); } /// /// 重置SLAM /// public void Slam_Reset() { GSXR_Reset_Slam(); Debug.Log(project_name + "GSXR_Reset_Slam."); } /// /// 以Head方式设置原点 /// public void Set_HeadOrigin() { GSXR_Set_HeadOrigin(new Vector3(1, 0, 0)); Debug.Log(project_name + "Set HeadOrigin by (1,0,0)."); } /// /// 以World方式设置原点 /// public void Set_WorldOrigin() { GSXR_Set_WorldOrigin(new Vector3(1, 0, 0)); Debug.Log(project_name + "Set WorldOrigin by (1,0,0)."); } public void Set_OnFloorOrOnHead() { if(onFloorOrOnHead==0) { GSXR_Set_OnFloorOrOnHead(0); Debug.Log(project_name + "GSXR_Set_OnFloorOrOnHead(0)."); onFloorOrOnHead++; } else { GSXR_Set_OnFloorOrOnHead(1); Debug.Log(project_name + "GSXR_Set_OnFloorOrOnHead(1)."); onFloorOrOnHead--; } } public void PreBtn() { isPreview = !isPreview; } void Init() { imageWidth = (int)API_Module_Device.Current.FishEyeResolution.x; imageHeight = (int)API_Module_Device.Current.FishEyeResolution.y; outBUdate = true; outCurrFrameIndex = 0; outFrameExposureNano = 0; outLeftFrameData = new byte[imageWidth * imageHeight]; outRightFrameData = new byte[imageWidth * imageHeight]; textureFormat = TextureFormat.Alpha8; textureTemp = new Texture2D(imageWidth, imageHeight, textureFormat, false); } public void ShowCamera() { if (Application.platform == RuntimePlatform.Android) { GSXR_Get_LatestFishEyeBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData); if (outBUdate) { FishEyeCamera0.texture = GetTexture(outLeftFrameData); FishEyeCamera0.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight); FishEyeCamera1.texture = GetTexture(outRightFrameData); FishEyeCamera1.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight); } else { Debug.Log("Error: Please Check Slamconfig prop: gUseXXXCamera = true"); } } } public Texture2D GetTexture(byte[] outFrameData) { textureTemp.LoadRawTextureData(outFrameData); textureTemp.Apply(); return textureTemp; } }