CameraYUVCaptureController.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. using UnityEngine;
  10. using UnityEngine.UI;
  11. namespace NRKernal.NRExamples
  12. {
  13. /// <summary> A controller for handling camera yuv captures. </summary>
  14. [HelpURL("https://developer.nreal.ai/develop/unity/rgb-camera")]
  15. public class CameraYUVCaptureController : MonoBehaviour
  16. {
  17. /// <summary> The capture image. </summary>
  18. public RawImage CaptureImage;
  19. /// <summary> Number of frames. </summary>
  20. public Text FrameCount;
  21. /// <summary> Gets or sets the yuv camera texture. </summary>
  22. /// <value> The yuv camera texture. </value>
  23. private NRRGBCamTextureYUV YuvCamTexture { get; set; }
  24. void Start()
  25. {
  26. YuvCamTexture = new NRRGBCamTextureYUV();
  27. BindYuvTexture(YuvCamTexture.GetTexture());
  28. YuvCamTexture.Play();
  29. }
  30. /// <summary> Updates this object. </summary>
  31. void Update()
  32. {
  33. if (YuvCamTexture == null)
  34. {
  35. return;
  36. }
  37. FrameCount.text = YuvCamTexture.FrameCount.ToString();
  38. }
  39. /// <summary> Plays this object. </summary>
  40. public void Play()
  41. {
  42. if (YuvCamTexture == null)
  43. {
  44. YuvCamTexture = new NRRGBCamTextureYUV();
  45. }
  46. YuvCamTexture.Play();
  47. // The origin texture will be destroyed after call "Stop",
  48. // Rebind the texture.
  49. BindYuvTexture(YuvCamTexture.GetTexture());
  50. }
  51. /// <summary> Bind yuv texture. </summary>
  52. /// <param name="frame"> The frame.</param>
  53. private void BindYuvTexture(NRRGBCamTextureYUV.YUVTextureFrame frame)
  54. {
  55. CaptureImage.enabled = true;
  56. CaptureImage.material.SetTexture("_MainTex", frame.textureY);
  57. CaptureImage.material.SetTexture("_UTex", frame.textureU);
  58. CaptureImage.material.SetTexture("_VTex", frame.textureV);
  59. }
  60. /// <summary> Pauses this object. </summary>
  61. public void Pause()
  62. {
  63. YuvCamTexture?.Pause();
  64. }
  65. /// <summary> Stops this object. </summary>
  66. public void Stop()
  67. {
  68. YuvCamTexture?.Stop();
  69. YuvCamTexture = null;
  70. CaptureImage.enabled = false;
  71. }
  72. /// <summary> Executes the 'destroy' action. </summary>
  73. void OnDestroy()
  74. {
  75. YuvCamTexture?.Stop();
  76. YuvCamTexture = null;
  77. }
  78. }
  79. }