123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- 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<RenderTexture> 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:";
- /// <summary>
- /// 设置渲染帧率
- /// 添加Slam初始化完成时的回调
- /// 添加按键按下回调
- /// </summary>
- private void Start()
- {
- GSXR_Set_RenderFrame(frameRate);
- GSXR_Add_InitializedCallBack(Action_InitializedCallBack);
- GSXR_Add_InitializedCallBack(Init);
- API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateRegister(KeyDown);
- }
- /// <summary>
- /// 按下左手柄按钮触发事件
- /// X键启动Slam
- /// Y键停止Slam
- /// LFunction重置Slam
- /// </summary>
- /// <param name="keyCode"></param>
- /// <param name="part"></param>
- private void KeyDown(InputKeyCode keyCode, InputDevicePartBase part)
- {
- if (keyCode == InputKeyCode.LFunction && part.PartType == InputDevicePartType.KSLeft)
- {
- Slam_Reset();
- }
- }
- /// <summary>
- /// 3/6Dof模式
- /// Slam系统是否在运行
- /// Slam系统是否初始化完成
- /// 获取左右眼瞳距
- /// Slam数据是否丢失
- /// Slam头部数据
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 取消Slam初始化完成时的回调
- /// 取消按键按下回调
- /// 释放render
- /// </summary>
- 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;
- }
- /// <summary>
- /// 设置Slam初始化完成时的回调
- /// </summary>
- private void Action_InitializedCallBack()
- {
- action_InitializedCallBack.text = "GSXR_Add_InitializedCallBack succeed.";
- Debug.Log(project_name + "GSXR_Add_InitializedCallBack succeed.");
- }
- /// <summary>
- /// 切换6Dof/3Dof
- /// </summary>
- 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 + ".");
- }
- /// <summary>
- /// 获取左右眼渲染画面
- /// </summary>
- 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;
- }
- /// <summary>
- /// 重定位
- /// </summary>
- public void RecenterTracking()
- {
- GSXR_RecenterTracking();
- Debug.Log(project_name + "GSXR_RecenterTracking.");
- }
- /// <summary>
- /// 重置SLAM
- /// </summary>
- public void Slam_Reset()
- {
- GSXR_Reset_Slam();
- Debug.Log(project_name + "GSXR_Reset_Slam.");
- }
- /// <summary>
- /// 以Head方式设置原点
- /// </summary>
- public void Set_HeadOrigin()
- {
- GSXR_Set_HeadOrigin(new Vector3(1, 0, 0));
- Debug.Log(project_name + "Set HeadOrigin by (1,0,0).");
- }
- /// <summary>
- /// 以World方式设置原点
- /// </summary>
- 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;
- }
- }
|