using UnityEngine;
using System.Collections;
using com.rfilkov.kinect;
namespace com.rfilkov.components
{
///
/// Portrait background is the component that sets the background image in the required screen resolution.
///
public class PortraitBackground : MonoBehaviour
{
[Tooltip("Index of the sensor, whose color image will be used as background image.")]
public int sensorIndex = 0;
[Tooltip("Target aspect ratio. If left at 0:0, it will determine the aspect ratio from the current screen resolution.")]
public Vector2 targetAspectRatio = Vector2.zero; // new Vector2 (9f, 16f);
private bool isInitialized = false;
private Rect pixelInsetRect;
private Rect backgroundRect;
private Rect inScreenRect;
private Rect shaderUvRect;
private static PortraitBackground instance = null;
private KinectManager kinectManager = null;
private KinectInterop.SensorData sensorData = null;
private UnityEngine.UI.RawImage rawImage = null;
private int staticImageW = 0, staticImageH = 0;
private int screenW = 0;
private int screenH = 0;
private int imageW = 0;
private int imageH = 0;
///
/// Gets the singleton PortraitBackground instance.
///
/// The PortraitBackground instance.
public static PortraitBackground Instance
{
get
{
return instance;
}
}
///
/// Determines whether the instance is initialized or not.
///
/// true if the instance is initialized; otherwise, false.
public bool IsInitialized()
{
return isInitialized;
}
///
/// Gets the background rectangle in pixels. This rectangle can be provided as an argument to the GetJointPosColorOverlay()-KM function.
///
/// The background rectangle, in pixels
public Rect GetBackgroundRect()
{
return backgroundRect;
}
///
/// Gets the in-screen rectangle in pixels.
///
/// The in-screen rectangle, in pixels.
public Rect GetInScreenRect()
{
return inScreenRect;
}
///
/// Gets the shader uv rectangle. Can be used by custom shaders that need the portrait image uv-offsets and sizes.
///
/// The shader uv rectangle.
public Rect GetShaderUvRect()
{
return shaderUvRect;
}
///
/// Gets the color-image scaled screen width. The scaled screen height is equal to the color-image height.
///
/// Color-image scaled screen width.
public float GetColorScaledScreenWidth()
{
KinectManager kinectManager = KinectManager.Instance;
float fScaledScreenW = kinectManager ? (float)Screen.width * (float)kinectManager.GetColorImageHeight(sensorIndex) / (float)Screen.height : 0f;
return fScaledScreenW;
}
////////////////////////////////////////////////////////////////////////
void Awake()
{
instance = this;
}
void Start()
{
rawImage = GetComponent();
if(rawImage != null && rawImage.texture != null)
{
//staticImageW = rawImage.texture.width;
//staticImageH = rawImage.texture.height;
//Debug.Log("staticImageW: " + staticImageW + ", staticImageH: " + staticImageH);
}
kinectManager = KinectManager.Instance;
if(kinectManager && kinectManager.IsInitialized())
{
sensorData = kinectManager.GetSensorData(sensorIndex);
int colorImageW = staticImageW > 0 ? staticImageW : sensorData.colorImageWidth; // kinectManager.GetColorImageWidth(sensorIndex);
int colorImageH = staticImageH > 0 ? staticImageH : sensorData.colorImageHeight; // kinectManager.GetColorImageHeight(sensorIndex);
UpdateBackgroundParams(colorImageW, colorImageH);
}
}
void Update()
{
if (kinectManager && kinectManager.IsInitialized())
{
int colorImageW = staticImageW > 0 ? staticImageW : sensorData.colorImageWidth; // kinectManager.GetColorImageWidth(sensorIndex);
int colorImageH = staticImageH > 0 ? staticImageH : sensorData.colorImageHeight; // kinectManager.GetColorImageHeight(sensorIndex);
if (screenW != Screen.width || screenH != Screen.height ||
imageW != colorImageW || imageH != colorImageH)
{
isInitialized = false;
UpdateBackgroundParams(colorImageW, colorImageH);
}
if (rawImage != null && sensorData != null && sensorData.sensorInterface != null)
{
Vector2 updatedAnchorPos = sensorData.sensorInterface.GetBackgroundImageAnchorPos(sensorData);
if (rawImage.rectTransform.anchoredPosition != updatedAnchorPos)
{
rawImage.rectTransform.anchoredPosition = updatedAnchorPos;
//Debug.Log("anchoredPosition: " + rawImage.rectTransform.anchoredPosition);
}
}
}
}
// updates the background parameters
private void UpdateBackgroundParams(int colorImageW, int colorImageH)
{
if (colorImageW > 0 && colorImageH > 0)
{
screenW = Screen.width;
screenH = Screen.height;
imageW = colorImageW;
imageH = colorImageH;
//Debug.Log("UpdateBackgroundParams() - scrW: " + screenW + ", scrH: " + screenH + ", imgW: " + imageW + ", imgH: " + imageH);
// determine the target screen aspect ratio
float screenAspectRatio = targetAspectRatio != Vector2.zero ? (targetAspectRatio.x / targetAspectRatio.y) :
((float)Screen.width / (float)Screen.height);
float fFactorDW = (float)colorImageW / (float)colorImageH -
//(float)colorImageH / (float)colorImageW;
screenAspectRatio;
float fDeltaWidth = (float)Screen.height * fFactorDW;
float dOffsetX = -fDeltaWidth / 2f;
float fFactorSW = (float)colorImageW / (float)colorImageH;
float fScreenWidth = (float)Screen.height * fFactorSW;
float fAbsOffsetX = fDeltaWidth / 2f;
pixelInsetRect = new Rect(dOffsetX, 0, fDeltaWidth, 0);
backgroundRect = new Rect(dOffsetX, 0, fScreenWidth, Screen.height);
inScreenRect = new Rect(fAbsOffsetX, 0, fScreenWidth - fDeltaWidth, Screen.height);
shaderUvRect = new Rect(fAbsOffsetX / fScreenWidth, 0, (fScreenWidth - fDeltaWidth) / fScreenWidth, 1);
//Debug.Log("Background rect: " + backgroundRect + ", shaderRect: " + shaderUvRect + "\nScreenW: " + Screen.width + ", ScreenH: " + Screen.height);
//GUITexture guiTexture = GetComponent();
//if(guiTexture)
//{
// guiTexture.pixelInset = pixelInsetRect;
//}
if (rawImage)
{
rawImage.uvRect = shaderUvRect;
}
isInitialized = true;
}
}
}
}