ShowQVRCamera.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SC.XR.Unity
  6. {
  7. public class ShowQVRCamera : MonoBehaviour
  8. {
  9. public RawImage _showLeftImage;
  10. public RawImage _showRightImage;
  11. bool isPreview = false;
  12. int imageWidth;
  13. int imageHeight;
  14. bool outBUdate = true;
  15. uint outCurrFrameIndex = 0;
  16. ulong outFrameExposureNano = 0;
  17. byte[] outLeftFrameData;
  18. byte[] outRightFrameData;
  19. TextureFormat textureFormat = TextureFormat.Alpha8;
  20. void Awake()
  21. {
  22. Init();
  23. }
  24. private void LateUpdate()
  25. {
  26. if (isPreview)
  27. {
  28. ShowCamera();
  29. }
  30. else
  31. {
  32. _showLeftImage.texture = null;
  33. _showRightImage.texture = null;
  34. }
  35. }
  36. public void PreBtn()
  37. {
  38. isPreview = !isPreview;
  39. Debug.Log("LGS:是否预览:" + isPreview);
  40. }
  41. public void Init()
  42. {
  43. imageWidth = (int)API_Module_Device.Current.GreyCameraResolution.x;
  44. imageHeight = (int)API_Module_Device.Current.GreyCameraResolution.y;
  45. outBUdate = true;
  46. outCurrFrameIndex = 0;
  47. outFrameExposureNano = 0;
  48. outLeftFrameData = new byte[imageWidth * imageHeight];
  49. outRightFrameData = new byte[imageWidth * imageHeight];
  50. textureFormat = TextureFormat.Alpha8;
  51. }
  52. public void ShowCamera()
  53. {
  54. // if (!API_GSXR_Slam.SlamManager.Initialized) return;
  55. if (Application.platform == RuntimePlatform.Android)
  56. {
  57. API_GSXR_Slam.GSXR_Get_LatestQVRCameraBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  58. Debug.Log("LGS:outBUdate=>" + outBUdate + " outCurrFrameIndex:" + outCurrFrameIndex + " outFrameExposureNano" + outFrameExposureNano);
  59. if (outBUdate)
  60. {
  61. _showLeftImage.texture = GetTexture(outLeftFrameData);
  62. _showLeftImage.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight);
  63. _showRightImage.texture = GetTexture(outRightFrameData);
  64. _showRightImage.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight);
  65. }
  66. else
  67. {
  68. Debug.Log("Error: Please Check Svrconfig prop: gUseQVRCamera = true");
  69. }
  70. }
  71. }
  72. public Texture2D GetTexture(byte[] outFrameData)
  73. {
  74. Texture2D textureTemp = new Texture2D(imageWidth, imageHeight, textureFormat, false);
  75. textureTemp.LoadRawTextureData(outFrameData);
  76. textureTemp.Apply();
  77. return textureTemp;
  78. }
  79. }
  80. }