// Update is called once per frame
void Update()
{
if (manager == null)
{
manager = KinectManager.Instance;
}
// get the users texture
if (manager && manager.IsInitialized())
{
//foregroundTex = manager.GetUsersLblTex();
if (backgroundImage && (backgroundImage.sprite == null))
{
//backgroundImage.texture = manager.GetUsersClrTex();
backgroundImage.sprite = Sprite.Create(manager.GetUsersClrTex(), new Rect(0, 0, manager.GetUsersClrTex().width, manager.GetUsersClrTex().height), Vector2.zero);
backgroundImage.rectTransform.sizeDelta = new Vector2(manager.GetUsersClrTex().width, manager.GetUsersClrTex().height);
}
}
if (manager.IsUserDetected())
{
uint[] userId = new uint[2] {
manager.GetPlayer1ID(), manager.GetPlayer2ID()
};
// update colliders
int numColliders = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < numColliders; i++)
{
if (manager.IsJointTracked(userId[j], i))
{
Vector3 posJoint = manager.GetRawSkeletonJointPos(userId[j], i);
if (posJoint != Vector3.zero)
{
// convert the joint 3d position to depth 2d coordinates
Vector2 posDepth = manager.GetDepthMapPosForJointPos(posJoint);
float scaledX = posDepth.x * foregroundRect.width / KinectWrapper.Constants.DepthImageWidth;
float scaledY = posDepth.y * -foregroundRect.height / KinectWrapper.Constants.DepthImageHeight;
float screenX = foregroundOfs.x + scaledX;
float screenY = Camera.main.pixelHeight - (foregroundOfs.y + scaledY);
float zDistance = posJoint.z - Camera.main.transform.position.z;
Vector3 posScreen = new Vector3(screenX, screenY, zDistance);
Vector3 posCollider = Camera.main.ScreenToWorldPoint(posScreen);
jointColliders[i + j * numColliders].transform.position = posCollider;
}
}
}
}
}
}