soundvision/UnityProject/Assets/AzureKinectExamples/KinectDemos/OverlayDemo/Scripts/SkeletonOverlayer.cs

190 lines
7.2 KiB
C#
Raw Normal View History

using UnityEngine;
using System.Collections;
using com.rfilkov.kinect;
namespace com.rfilkov.components
{
/// <summary>
/// SkeletonOverlayer overlays the the user's body joints and bones with spheres and lines.
/// </summary>
public class SkeletonOverlayer : MonoBehaviour
{
[Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")]
public int playerIndex = 0;
[Tooltip("Game object used to overlay the joints.")]
public GameObject jointPrefab;
[Tooltip("Line object used to overlay the bones.")]
public LineRenderer linePrefab;
//public float smoothFactor = 10f;
[Tooltip("Depth sensor index - 0 is the 1st one, 1 - the 2nd one, etc.")]
public int sensorIndex = 0;
[Tooltip("Camera that will be used to overlay the 3D-objects over the background.")]
public Camera foregroundCamera;
//public UnityEngine.UI.Text debugText;
private GameObject[] joints = null;
private LineRenderer[] lines = null;
private Quaternion initialRotation = Quaternion.identity;
// reference to KM
private KinectManager kinectManager = null;
void Start()
{
kinectManager = KinectManager.Instance;
if (kinectManager && kinectManager.IsInitialized())
{
int jointsCount = kinectManager.GetJointCount();
if (jointPrefab)
{
// array holding the skeleton joints
joints = new GameObject[jointsCount];
for (int i = 0; i < joints.Length; i++)
{
joints[i] = Instantiate(jointPrefab) as GameObject;
joints[i].transform.parent = transform;
joints[i].name = ((KinectInterop.JointType)i).ToString();
joints[i].SetActive(false);
}
}
// array holding the skeleton lines
lines = new LineRenderer[jointsCount];
}
// always mirrored
initialRotation = Quaternion.Euler(new Vector3(0f, 180f, 0f));
if (!foregroundCamera)
{
// by default - the main camera
foregroundCamera = Camera.main;
}
}
void Update()
{
if (kinectManager && kinectManager.IsInitialized() && foregroundCamera)
{
// get the background rectangle (use the portrait background, if available)
Rect backgroundRect = foregroundCamera.pixelRect;
PortraitBackground portraitBack = PortraitBackground.Instance;
if (portraitBack && portraitBack.enabled)
{
backgroundRect = portraitBack.GetBackgroundRect();
}
// overlay all joints in the skeleton
if (kinectManager.IsUserDetected(playerIndex))
{
ulong userId = kinectManager.GetUserIdByIndex(playerIndex);
int jointsCount = kinectManager.GetJointCount();
//Debug.Log("Displaying user " + playerIndex + ", ID: " + userId +
// ", body: " + kinectManager.GetBodyIndexByUserId(userId) + ", pos: " + kinectManager.GetJointKinectPosition(userId, 0));
for (int i = 0; i < jointsCount; i++)
{
int joint = i;
if (kinectManager.IsJointTracked(userId, joint))
{
Vector3 posJoint = kinectManager.GetJointPosColorOverlay(userId, joint, sensorIndex, foregroundCamera, backgroundRect);
//Vector3 posJoint = manager.GetJointPosition(userId, joint);
if (joints != null)
{
// overlay the joint
if (posJoint != Vector3.zero)
{
joints[i].SetActive(true);
joints[i].transform.position = posJoint;
Quaternion rotJoint = kinectManager.GetJointOrientation(userId, joint, false);
rotJoint = initialRotation * rotJoint;
joints[i].transform.rotation = rotJoint;
}
else
{
joints[i].SetActive(false);
}
}
if (lines[i] == null && linePrefab != null)
{
lines[i] = Instantiate(linePrefab) as LineRenderer;
lines[i].transform.parent = transform;
lines[i].gameObject.SetActive(false);
}
if (lines[i] != null)
{
// overlay the line to the parent joint
int jointParent = (int)kinectManager.GetParentJoint((KinectInterop.JointType)joint);
Vector3 posParent = kinectManager.GetJointPosColorOverlay(userId, jointParent, sensorIndex, foregroundCamera, backgroundRect);
if (posJoint != Vector3.zero && posParent != Vector3.zero)
{
lines[i].gameObject.SetActive(true);
//lines[i].SetVertexCount(2);
lines[i].SetPosition(0, posParent);
lines[i].SetPosition(1, posJoint);
}
else
{
lines[i].gameObject.SetActive(false);
}
}
}
else
{
if (joints[i] != null)
{
joints[i].SetActive(false);
}
if (lines[i] != null)
{
lines[i].gameObject.SetActive(false);
}
}
}
}
else
{
// disable the skeleton
int jointsCount = kinectManager.GetJointCount();
for (int i = 0; i < jointsCount; i++)
{
if (joints[i] != null)
{
joints[i].SetActive(false);
}
if (lines[i] != null)
{
lines[i].gameObject.SetActive(false);
}
}
}
}
}
}
}