/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using System;
using UnityEngine;
/// Create a rgb camera texture.
public class NRRGBCamTexture : CameraModelView
{
/// When the data of RGBCamera is updated, it will be called.
public Action OnUpdate;
/// The current frame.
public CameraTextureFrame CurrentFrame;
/// The texture.
private Texture2D m_Texture;
/// Information describing the raw.
private byte[] m_RawData;
public byte[] RawData { get { return m_RawData; } }
/// Default constructor.
public NRRGBCamTexture() : base(CameraImageFormat.RGB_888)
{
this.m_Texture = CreateTexture();
this.CurrentFrame.texture = this.m_Texture;
}
/// Creates the texture.
/// The new texture.
private Texture2D CreateTexture()
{
return new Texture2D(Width, Height, TextureFormat.RGB24, false);
}
/// Gets the texture.
/// The texture.
public Texture2D GetTexture()
{
if (m_Texture == null)
{
this.m_Texture = CreateTexture();
this.CurrentFrame.texture = this.m_Texture;
}
return m_Texture;
}
/// Load raw texture data.
/// .
protected override void OnRawDataUpdate(FrameRawData frame)
{
if (m_Texture == null)
{
this.m_Texture = CreateTexture();
}
int dataSize = frame.data.Length;
if (m_RawData == null || m_RawData.Length != dataSize)
{
m_RawData = new byte[dataSize];
}
Array.Copy(frame.data, 0, m_RawData, 0, dataSize);
m_Texture.LoadRawTextureData(m_RawData);
m_Texture.Apply();
CurrentFrame.timeStamp = frame.timeStamp;
CurrentFrame.gain = frame.gain;
CurrentFrame.exposureTime = frame.exposureTime;
CurrentFrame.texture = m_Texture;
OnUpdate?.Invoke(CurrentFrame);
}
/// On texture stopped.
protected override void OnStopped()
{
GameObject.Destroy(m_Texture);
this.m_Texture = null;
m_RawData = null;
this.CurrentFrame.texture = null;
}
}
}