NRRGBCamTexture.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using UnityEngine;
  13. /// <summary> Create a rgb camera texture. </summary>
  14. public class NRRGBCamTexture : CameraModelView
  15. {
  16. /// <summary> When the data of RGBCamera is updated, it will be called. </summary>
  17. public Action<CameraTextureFrame> OnUpdate;
  18. /// <summary> The current frame. </summary>
  19. public CameraTextureFrame CurrentFrame;
  20. /// <summary> The texture. </summary>
  21. private Texture2D m_Texture;
  22. /// <summary> Information describing the raw. </summary>
  23. private byte[] m_RawData;
  24. public byte[] RawData { get { return m_RawData; } }
  25. /// <summary> Default constructor. </summary>
  26. public NRRGBCamTexture() : base(CameraImageFormat.RGB_888)
  27. {
  28. this.m_Texture = CreateTexture();
  29. this.CurrentFrame.texture = this.m_Texture;
  30. }
  31. /// <summary> Creates the texture. </summary>
  32. /// <returns> The new texture. </returns>
  33. private Texture2D CreateTexture()
  34. {
  35. return new Texture2D(Width, Height, TextureFormat.RGB24, false);
  36. }
  37. /// <summary> Gets the texture. </summary>
  38. /// <returns> The texture. </returns>
  39. public Texture2D GetTexture()
  40. {
  41. if (m_Texture == null)
  42. {
  43. this.m_Texture = CreateTexture();
  44. this.CurrentFrame.texture = this.m_Texture;
  45. }
  46. return m_Texture;
  47. }
  48. /// <summary> Load raw texture data. </summary>
  49. /// <param name="rgbRawDataFrame"> .</param>
  50. protected override void OnRawDataUpdate(FrameRawData frame)
  51. {
  52. if (m_Texture == null)
  53. {
  54. this.m_Texture = CreateTexture();
  55. }
  56. int dataSize = frame.data.Length;
  57. if (m_RawData == null || m_RawData.Length != dataSize)
  58. {
  59. m_RawData = new byte[dataSize];
  60. }
  61. Array.Copy(frame.data, 0, m_RawData, 0, dataSize);
  62. m_Texture.LoadRawTextureData(m_RawData);
  63. m_Texture.Apply();
  64. CurrentFrame.timeStamp = frame.timeStamp;
  65. CurrentFrame.gain = frame.gain;
  66. CurrentFrame.exposureTime = frame.exposureTime;
  67. CurrentFrame.texture = m_Texture;
  68. OnUpdate?.Invoke(CurrentFrame);
  69. }
  70. /// <summary> On texture stopped. </summary>
  71. protected override void OnStopped()
  72. {
  73. GameObject.Destroy(m_Texture);
  74. this.m_Texture = null;
  75. m_RawData = null;
  76. this.CurrentFrame.texture = null;
  77. }
  78. }
  79. }