/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
using UnityEngine;
using UnityEngine.UI;
namespace NRKernal.NRExamples
{
/// A controller for handling camera yuv captures.
[HelpURL("https://developer.nreal.ai/develop/unity/rgb-camera")]
public class CameraYUVCaptureController : MonoBehaviour
{
/// The capture image.
public RawImage CaptureImage;
/// Number of frames.
public Text FrameCount;
/// Gets or sets the yuv camera texture.
/// The yuv camera texture.
private NRRGBCamTextureYUV YuvCamTexture { get; set; }
void Start()
{
YuvCamTexture = new NRRGBCamTextureYUV();
BindYuvTexture(YuvCamTexture.GetTexture());
YuvCamTexture.Play();
}
/// Updates this object.
void Update()
{
if (YuvCamTexture == null)
{
return;
}
FrameCount.text = YuvCamTexture.FrameCount.ToString();
}
/// Plays this object.
public void Play()
{
if (YuvCamTexture == null)
{
YuvCamTexture = new NRRGBCamTextureYUV();
}
YuvCamTexture.Play();
// The origin texture will be destroyed after call "Stop",
// Rebind the texture.
BindYuvTexture(YuvCamTexture.GetTexture());
}
/// Bind yuv texture.
/// The frame.
private void BindYuvTexture(NRRGBCamTextureYUV.YUVTextureFrame frame)
{
CaptureImage.enabled = true;
CaptureImage.material.SetTexture("_MainTex", frame.textureY);
CaptureImage.material.SetTexture("_UTex", frame.textureU);
CaptureImage.material.SetTexture("_VTex", frame.textureV);
}
/// Pauses this object.
public void Pause()
{
YuvCamTexture?.Pause();
}
/// Stops this object.
public void Stop()
{
YuvCamTexture?.Stop();
YuvCamTexture = null;
CaptureImage.enabled = false;
}
/// Executes the 'destroy' action.
void OnDestroy()
{
YuvCamTexture?.Stop();
YuvCamTexture = null;
}
}
}