Skip to content

Instantly share code, notes, and snippets.

@jbfove
Created January 22, 2019 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbfove/029a2bb44a8037cffc45e622b7aee75e to your computer and use it in GitHub Desktop.
Save jbfove/029a2bb44a8037cffc45e622b7aee75e to your computer and use it in GitHub Desktop.
using Fove.Managed;
using System;
using UnityEngine;
namespace Fove.Unity
{
public static class FoveResearch
{
private static Texture2D m_sEyesTexture;
private static Texture2D m_sPositionTexture;
private static FVRHeadset.Research.BitmapImage m_sEyesImage;
private static FVRHeadset.Research.BitmapImage m_sPositionImage;
private static FVRHeadset.Research m_sResearch;
public static Texture2D EyesTexture
{
get
{
EnsureInitialization();
return m_sEyesTexture;
}
}
public static Texture2D PositionTexture
{
get
{
EnsureInitialization();
return m_sPositionTexture;
}
}
private static void AcceptAddIn(FVRHeadset headset)
{
m_sResearch = headset.GetResearchInterface();
}
private static void EnsureInitialization()
{
if (m_sResearch != null)
return;
FoveManager.RegisterAddIn(AcceptAddIn);
FoveManager.AddInUpdate += UpdateImages;
m_sEyesTexture = null;
m_sPositionTexture = null;
m_sResearch.RegisterResearchCapabilities(EFVR_ResearchCapabilities.EyeImage | EFVR_ResearchCapabilities.PositionImage);
}
private delegate EFVR_ErrorCode GetImageDelegate(out FVRHeadset.Research.BitmapImage img);
private static void TryUpdatingTexture(GetImageDelegate func, ref FVRHeadset.Research.BitmapImage bimg, ref Texture2D tex)
{
try {
FVRHeadset.Research.BitmapImage temp;
var err = func(out temp);
if (err == EFVR_ErrorCode.None)
{
if (bimg != null && temp.Timestamp <= bimg.Timestamp)
return;
bimg = temp;
if (bimg.Width == 0 || bimg.Height == 0)
{
return;
}
if (tex != null && (tex.width != bimg.Width || tex.height != bimg.Height))
{
Texture2D.Destroy(tex);
tex = null;
}
if (tex == null)
tex = new Texture2D(bimg.Width, bimg.Height, TextureFormat.RGB24, false);
tex.LoadRawTextureData(bimg.ImageData.data, (int)bimg.ImageData.length);
tex.Apply();
}
else
{
//... log?
}
}
catch (Exception e)
{
Debug.Log("Error trying to load eyes image bitmap: " + e);
}
}
private static void UpdateImages()
{
TryUpdatingTexture(m_sResearch.GetEyeImage, ref m_sEyesImage, ref m_sEyesTexture);
TryUpdatingTexture(m_sResearch.GetPositionImage, ref m_sPositionImage, ref m_sPositionTexture);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment