ShowFishEyeCamera.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 ShowFishEyeCamera : MonoBehaviour
  8. {
  9. public RawImage _showLeftImage;
  10. public RawImage _showRightImage;
  11. Texture2D textureTemp;
  12. bool isPreview = false;
  13. int imageWidth;
  14. int imageHeight;
  15. bool outBUdate = true;
  16. uint outCurrFrameIndex = 0;
  17. ulong outFrameExposureNano = 0;
  18. byte[] outLeftFrameData;
  19. byte[] outRightFrameData;
  20. TextureFormat textureFormat = TextureFormat.Alpha8;
  21. void Awake()
  22. {
  23. API_GSXR_Slam.GSXR_Add_InitializedCallBack(Init);
  24. }
  25. private void LateUpdate()
  26. {
  27. if (isPreview)
  28. {
  29. ShowCamera();
  30. }
  31. else
  32. {
  33. _showLeftImage.texture = null;
  34. _showRightImage.texture = null;
  35. }
  36. }
  37. void OnDestroy() {
  38. API_GSXR_Slam.GSXR_Remove_InitializedCallBack(Init);
  39. textureTemp = null;
  40. outCurrFrameIndex = 0;
  41. outFrameExposureNano = 0;
  42. outLeftFrameData = null;
  43. outRightFrameData = null;
  44. }
  45. public void PreBtn()
  46. {
  47. isPreview = !isPreview;
  48. Debug.Log("LGS:是否预览:" + isPreview);
  49. }
  50. void Init()
  51. {
  52. imageWidth = (int)API_Module_Device.Current.FishEyeResolution.x;
  53. imageHeight = (int)API_Module_Device.Current.FishEyeResolution.y;
  54. outBUdate = true;
  55. outCurrFrameIndex = 0;
  56. outFrameExposureNano = 0;
  57. outLeftFrameData = new byte[imageWidth * imageHeight];
  58. outRightFrameData = new byte[imageWidth * imageHeight];
  59. textureFormat = TextureFormat.Alpha8;
  60. textureTemp = new Texture2D(imageWidth, imageHeight, textureFormat, false);
  61. }
  62. public void ShowCamera()
  63. {
  64. // if (!API_GSXR_Slam.SlamManager.Initialized) return;
  65. if (Application.platform == RuntimePlatform.Android)
  66. {
  67. API_GSXR_Slam.GSXR_Get_LatestFishEyeBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  68. Debug.Log("LGS:outBUdate=>" + outBUdate + " outCurrFrameIndex:" + outCurrFrameIndex + " outFrameExposureNano" + outFrameExposureNano);
  69. if (outBUdate)
  70. {
  71. _showLeftImage.texture = GetTexture(outLeftFrameData);
  72. _showLeftImage.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight);
  73. _showRightImage.texture = GetTexture(outRightFrameData);
  74. _showRightImage.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight);
  75. }
  76. else
  77. {
  78. Debug.Log("Error: Please Check Slamconfig prop: gUseXXXCamera = true");
  79. }
  80. }
  81. }
  82. public Texture2D GetTexture(byte[] outFrameData)
  83. {
  84. textureTemp.LoadRawTextureData(outFrameData);
  85. textureTemp.Apply();
  86. return textureTemp;
  87. }
  88. }
  89. }