Inheritance: MonoBehaviour
    // ConstrainSelfIntersection collides joints with the skeleton to keep the skeleton's hands and wrists from puncturing its body
    // A cylinder is created to represent the torso. Intersecting joints have their positions changed to push them outside the torso.
    public void Constrain(ref KinectWrapper.NuiSkeletonData skeleton)
    {
//        if (null == skeleton)
//        {
//            return;
//        }

		int shoulderCenterIndex = (int)KinectWrapper.NuiSkeletonPositionIndex.ShoulderCenter;
		int hipCenterIndex = (int)KinectWrapper.NuiSkeletonPositionIndex.HipCenter;

        if (skeleton.eSkeletonPositionTrackingState[shoulderCenterIndex] != KinectWrapper.NuiSkeletonPositionTrackingState.NotTracked &&
            skeleton.eSkeletonPositionTrackingState[hipCenterIndex] != KinectWrapper.NuiSkeletonPositionTrackingState.NotTracked)
        {
            Vector3 shoulderDiffLeft = KinectHelper.VectorBetween(ref skeleton, shoulderCenterIndex, (int)KinectWrapper.NuiSkeletonPositionIndex.ShoulderLeft);
            Vector3 shoulderDiffRight = KinectHelper.VectorBetween(ref skeleton, shoulderCenterIndex, (int)KinectWrapper.NuiSkeletonPositionIndex.ShoulderRight);
            float shoulderLengthLeft = shoulderDiffLeft.magnitude;
            float shoulderLengthRight = shoulderDiffRight.magnitude;

            // The distance between shoulders is averaged for the radius
            float cylinderRadius = (shoulderLengthLeft + shoulderLengthRight) * 0.5f;
    
            // Calculate the shoulder center and the hip center.  Extend them up and down respectively.
            Vector3 shoulderCenter = (Vector3)skeleton.SkeletonPositions[shoulderCenterIndex];
            Vector3 hipCenter = (Vector3)skeleton.SkeletonPositions[hipCenterIndex];
            Vector3 hipShoulder = hipCenter - shoulderCenter;
            hipShoulder.Normalize();

            shoulderCenter = shoulderCenter - (hipShoulder * (ShoulderExtend * cylinderRadius));
            hipCenter = hipCenter + (hipShoulder * (HipExtend * cylinderRadius));
    
            // Optionally increase radius to account for bulky avatars
            cylinderRadius *= RadiusMultiplier;
   
            // joints to collide
            int[] collisionIndices = 
			{ 
				(int)KinectWrapper.NuiSkeletonPositionIndex.WristLeft, 
				(int)KinectWrapper.NuiSkeletonPositionIndex.HandLeft, 
				(int)KinectWrapper.NuiSkeletonPositionIndex.WristRight, 
				(int)KinectWrapper.NuiSkeletonPositionIndex.HandRight 
			};
    
            foreach (int j in collisionIndices)
            {
                Vector3 collisionJoint = (Vector3)skeleton.SkeletonPositions[j];
                
                Vector4 distanceNormal = KinectHelper.DistanceToLineSegment(shoulderCenter, hipCenter, collisionJoint);

                Vector3 normal = new Vector3(distanceNormal.x, distanceNormal.y, distanceNormal.z);

                // if distance is within the cylinder then push the joint out and away from the cylinder
                if (distanceNormal.w < cylinderRadius)
                {
                    collisionJoint += normal * ((cylinderRadius - distanceNormal.w) * CollisionTolerance);

                    skeleton.SkeletonPositions[j] = (Vector4)collisionJoint;
                }
            }
        }
    }
Example #2
0
/********************************************************************************
*       DEVICE MANAGEMENT -> initialize, uninitialize, and update sensor
* ******************************************************************************/

    //called on application start
    private void Start()
    {
        NUIisReady = false;

        //initialize Kinect sensor
        NUIisReady = KinectWrapper.NuiContextInit(twoPlayer);

        //display messages
        if (NUIisReady)
        {
            Debug.Log("Sensor Initialized.");
        }
        else
        {
            Debug.Log("Could Not Initialize Sensor.");
        }

        if (scaleFactor == 0)
        {
            Debug.Log("WARNING: KUInterface.scaleFactor is set to zero. All joint positions will be the zero vector.");
        }

        //set up image memory
//        seqTex = new byte[IM_W * IM_H * 4];
//        cols = new Color32[IM_W * IM_H];
//        texture = new Texture2D(IM_W, IM_H);

//        seqDepth = new byte[IM_W * IM_H * 2];
//        dcols = new Color32[IM_W * IM_H];
//        depthImg = new Texture2D(IM_W, IM_H);
//        depth = new short[IM_W][];
//        for (int i = 0; i < depth.Length; i++) {
//            depth[i] = new short[IM_H];
//        }
    }
Example #3
0
    /// <summary>
    /// gets color texture image from Kinect RGB camera
    /// </summary>
    /// <returns>RGB image</returns>
//    public Texture2D GetTextureImage() {
//
//        return this.texture;
//    }


    /// <summary>
    /// gets depth data from Kinect depth camera
    /// </summary>
    /// <returns>2D array of pixel depths as bytes</returns>
    /// <remarks>depth[x=0][y=0] corresponds to top-left corner of image</remarks>
//    public short[][] GetDepthData() {
//
//        return this.depth;
//    }


    /// <summary>
    /// returns current Kinect camera angle from horizontal
    /// </summary>
    /// <returns>camera angle from horizontal</returns>
    public float GetCameraAngle()
    {
        float cameraAngle = 0;

        KinectWrapper.GetCameraAngle(ref cameraAngle);
        return(cameraAngle);
    }
Example #4
0
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture, 
		KinectWrapper.SkeletonJoint joint, Vector3 screenPos)
    {
        KinectManager manager = KinectManager.Instance;
        string sGestureText = gesture + " detected";
        if(GestureInfo != null)
        {
            GestureInfo.guiText.text = sGestureText;
        }

        if (gesture == KinectGestures.Gestures.SwipeLeft)
            swipeLeft = true;
        else if (gesture == KinectGestures.Gestures.SwipeRight)
            swipeRight = true;
        //else if(gesture == KinectGestures.Gestures.RaiseLeftHand)
        //    RaiseLeftHand = true;
        //else if(gesture == KinectGestures.Gestures.RaiseRightHand)
        //    RaiseRightHand = true;
        else if (gesture == KinectGestures.Gestures.Push)
            Push = true;
        else if (gesture == KinectGestures.Gestures.Psi)
            PSI = true;
        else if (gesture == KinectGestures.Gestures.Wave)
            Wave = true;
        return true;
    }
    void GetColorForRegsiteredColorStreamPixel(ref int index, ref KinectData sourceKinectData, ref Color32 result)
    {
        result = new Color32(0, 0, 0, 255);
        if (fullResolutionKinectData.RawColorStreamColors != null)
        {
            int x = index % sourceKinectData.Width;
            int y = index / sourceKinectData.Width;

            int cx, cy;
            int hr = KinectWrapper.NuiImageGetColorPixelCoordinatesFromDepthPixelAtResolution(
                KinectWrapper.Constants.ColorImageResolution,
                KinectWrapper.Constants.DepthImageResolution,
                ref KinectCoordinatesAdjustment,
                x, y, sourceKinectData.RawDepths[index],
                out cx, out cy);

            if (hr == 0)
            {
                int colorIndex = cy * sourceKinectData.Width + cx;
                if (colorIndex >= 0 && colorIndex < sourceKinectData.Size)
                {
                    result = sourceKinectData.RawColorStreamColors [colorIndex];
                }
            }
        }
        return;
    }
    // Initialize the filter with a set of TransformSmoothParameters.
    public void Init(KinectWrapper.NuiTransformSmoothParameters smoothingParameters)
    {
        this.smoothParameters = smoothingParameters;

        this.Reset();
        this.init = true;
    }
Example #7
0
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        if (gesture == KinectGestures.Gestures.SwipeLeft && Cart.scene2) {
            Debug.Log("Swipe Left Completed!");
            GameObject.FindObjectOfType<Shelf>().SwipeLeft();
        }

        if (gesture == KinectGestures.Gestures.SwipeRight && Cart.scene2) {
            //Debug.Log("Swipe Left Completed!");
            GameObject.FindObjectOfType<Cart>().SwipeRight();
            Debug.Log("Swipe Right Completed!");
        }

        string sGestureText = gesture + " detected";
        if(gesture == KinectGestures.Gestures.Click)
            sGestureText += string.Format(" at ({0:F1}, {1:F1})", screenPos.x, screenPos.y);

        if(GestureInfo != null)
            GestureInfo.GetComponent<GUIText>().text = sGestureText;

        progressDisplayed = false;

        return true;
    }
Example #8
0
    // draws the skeleton in the given texture
    private void DrawSkeleton(Texture2D aTexture, ref KinectWrapper.BodyData bodyData)
    {
        int jointsCount = KinectWrapper.Constants.JointCount;

        for (int i = 0; i < jointsCount; i++)
        {
            int parent = (int)KinectWrapper.GetParentJoint((KinectWrapper.JointType)i);

            if (bodyData.joint[i].trackingState == KinectWrapper.TrackingState.Tracked && bodyData.joint[parent].trackingState == KinectWrapper.TrackingState.Tracked)
            {
                Vector2 posParent = KinectWrapper.GetKinectPointDepthCoords(bodyData.joint[parent].kinectPos);
                Vector2 posJoint  = KinectWrapper.GetKinectPointDepthCoords(bodyData.joint[i].kinectPos);

//				posParent.y = KinectWrapper.Constants.ImageHeight - posParent.y - 1;
//				posJoint.y = KinectWrapper.Constants.ImageHeight - posJoint.y - 1;
//				posParent.x = KinectWrapper.Constants.ImageWidth - posParent.x - 1;
//				posJoint.x = KinectWrapper.Constants.ImageWidth - posJoint.x - 1;

                //Color lineColor = playerJointsTracked[i] && playerJointsTracked[parent] ? Color.red : Color.yellow;
                DrawLine(aTexture, (int)posParent.x, (int)posParent.y, (int)posJoint.x, (int)posJoint.y, Color.yellow);
            }
        }

        //aTexture.Apply();
    }
    // returns the depth map position for a 3d joint position
    public Vector2 GetDepthMapPosForJointPos(Vector3 posJoint)
    {
        Vector3 vDepthPos = KinectWrapper.MapSkeletonPointToDepthPoint(posJoint);
        Vector2 vMapPos   = new Vector2(vDepthPos.x, vDepthPos.y);

        return(vMapPos);
    }
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture,
								 KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        string sGestureText = gesture + " detected";
        if(GestureInfo != null)
        {
            GestureInfo.guiText.text = sGestureText;
        }
          	if(gesture == KinectGestures.Gestures.SwipeLeft)
          	{
            swipeLeft = true;
          	}
        if(gesture == KinectGestures.Gestures.SwipeRight)
        {
            swipeRight = true;
        }
        if(gesture == KinectGestures.Gestures.SwipeUp)
        {
          swipeUp = true;
        }
        if(gesture == KinectGestures.Gestures.SwipeDown)
        {
          swipeDown = true;
        }
        if(gesture == KinectGestures.Gestures.Push)
        {
          push = true;
        }
        if(gesture == KinectGestures.Gestures.Pull)
        {
          pull = true;
        }

          return true;
    }
Example #11
0
    // Update the user histogram
    void UpdateUserMap()
    {
        if (KinectWrapper.PollUserHistogramFrame(ref userHistogramImage, computeColorMap))
        {
            // draw user histogram
            usersLblTex.SetPixels32(userHistogramImage.pixels);

            // draw skeleton lines
            if (displaySkeletonLines)
            {
                for (int i = 0; i < alUserIds.Count; i++)
                {
                    Int64 liUserId = alUserIds[i];
                    int   index    = dictUserIdToIndex[liUserId];

                    if (index >= 0 && index < KinectWrapper.Constants.BodyCount)
                    {
                        DrawSkeleton(usersLblTex, ref bodyFrame.bodyData[index]);
                    }
                }
            }

            usersLblTex.Apply();
        }
    }
    // returns the local joint position of the specified user, relative to the parent joint, in meters
    public Vector3 GetJointLocalPosition(int joint)
    {
        int parent = KinectWrapper.GetSkeletonJointParent(joint);

        return(joint >= 0 && joint < player1JointsPos.Length ?
               (player1JointsPos[joint] - player1JointsPos[parent]) : Vector3.zero);
    }
Example #13
0
    void Update()
    {
        if (kinectInitialized)
        {
            if (KinectWrapper.PollSkeleton(ref bodyFrame, lastFrameTime))
            {
                lastFrameTime = bodyFrame.liRelativeTime;
                ProcessBodyFrameData();
            }

            if (computeColorMap)
            {
                if (KinectWrapper.PollColorFrame(ref colorImage))
                {
                    UpdateColorMap();
                }
            }

            if (computeUserMap)
            {
                if (KinectWrapper.PollDepthFrame(ref depthImage, ref bodyIndexImage, ref minDepth, ref maxDepth))
                {
                    UpdateUserMap();
                }
            }
        }
    }
Example #14
0
    // Start the Kinect2Server app
    private int StartKinectServer()
    {
        // start Kinect Server
        kinectServer = new KinectServer();
        kinectServer.RunKinectServer();

        float fTimeToWait = Time.realtimeSinceStartup + 10f;         // allow 10 seconds time-out
        int   iPing       = 0;

        while (Time.realtimeSinceStartup < fTimeToWait)
        {
            iPing = KinectWrapper.PingKinect2Server();
            if (iPing == 12345678)
            {
                break;
            }
        }

        if (iPing == 12345678)
        {
            iPing = 0;
        }

        return(iPing);
    }
Example #15
0
 private void Awake()
 {
     KinectWrapper.NuiCameraElevationGetAngle(out angle);
     Debug.Log("Start up");
     Debug.Log("Current angle = " + angle);
     KinectWrapper.NuiCameraElevationSetAngle(0);
 }
    public void GestureInProgress(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              float progress, KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        //GestureInfo.guiText.text = string.Format("{0} Progress: {1:F1}%", gesture, (progress * 100));
        if(gesture == KinectGestures.Gestures.Click && progress > 0.3f)
        {
            string sGestureText = string.Format ("{0} {1:F1}% complete", gesture, progress * 100);
            if(GestureInfo != null)
                GestureInfo.GetComponent<GUIText>().text = sGestureText;

            progressDisplayed = true;
        }
        else if((gesture == KinectGestures.Gestures.ZoomOut || gesture == KinectGestures.Gestures.ZoomIn) && progress > 0.5f)
        {
            string sGestureText = string.Format ("{0} detected, zoom={1:F1}%", gesture, screenPos.z * 100);
            if(GestureInfo != null)
                GestureInfo.GetComponent<GUIText>().text = sGestureText;

            progressDisplayed = true;
        }
        else if(gesture == KinectGestures.Gestures.Wheel && progress > 0.5f)
        {
            string sGestureText = string.Format ("{0} detected, angle={1:F1} deg", gesture, screenPos.z);
            if(GestureInfo != null)
                GestureInfo.GetComponent<GUIText>().text = sGestureText;

            progressDisplayed = true;
        }
    }
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        string sGestureText = gesture + " detected";
        if (gesture == KinectGestures.Gestures.Click) {
            sGestureText += string.Format (" at ({0:F1}, {1:F1})", screenPos.x, screenPos.y);
            print ("CLICKED DOWN???!?!?!?!?!");	//reverse units
            print ("SCREEN POS: " + screenPos);
            clickedPos.x = screenPos.x;
            clickedPos.y = screenPos.y;
            GetComponent<LineGame> ().kinectClickedOn = true;
            GetComponent<LineGame> ().clickedPos = clickedPos;
            GetComponent<Main> ().kinectClickedOn = true;
            GetComponent<Main> ().clickedPos = clickedPos;
            GetComponent<GrowingTeamGame> ().kinectClickedOn = true;
            GetComponent<GrowingTeamGame> ().clickedPos = clickedPos;
            GetComponent<AquariumGame> ().kinectClickedOn = true;
            GetComponent<AquariumGame> ().clickedPos = clickedPos;
        }

        if(GestureInfo != null)
            GestureInfo.GetComponent<GUIText>().text = sGestureText;

        progressDisplayed = false;

        return true;
    }
Example #18
0
    public static bool PollDepth(IntPtr depthStreamHandle, bool isNearMode, ref short[] depthPlayerData)
    {
        IntPtr imageFramePtr = IntPtr.Zero;
        bool   newDepth      = false;

        if (isNearMode)
        {
            KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.EnableNearMode);
        }
        else
        {
            KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.None);
        }

        int hr = KinectWrapper.NuiImageStreamGetNextFrame(depthStreamHandle, 0, ref imageFramePtr);

        if (hr == 0)
        {
            newDepth = true;

            NuiImageFrame    imageFrame   = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
            INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

            NuiLockedRect lockedRectPtr = new NuiLockedRect();
            IntPtr        r             = IntPtr.Zero;

            frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
            depthPlayerData = ExtractDepthImage(lockedRectPtr);

            frameTexture.UnlockRect(0);
            hr = KinectWrapper.NuiImageStreamReleaseFrame(depthStreamHandle, imageFramePtr);
        }

        return(newDepth);
    }
Example #19
0
    /// <summary>
    /// MonoBehaviour function
    /// </summary>
    void Update()
    {
        // If we load another scene, kinect manager will not be destroyed, but all the other objects wiil be, so all the references will be lost
        if (VideoOnObject && KinectVideo == null)
        {
            KinectVideo = GameObject.Find(VideoObjectName);
        }

        if (KinectInitialized)
        {
            if (VideoOnObject || VideoOnGUI)
            {
                if (colorStreamHandle != IntPtr.Zero && KinectWrapper.PollColor(colorStreamHandle, ref colorImage))
                {
                    usersClrTex.SetPixels32(colorImage);
                    usersClrTex.Apply();

                    if (VideoOnObject && KinectVideo != null)
                    {
                        KinectVideo.GetComponent <Renderer>().material.mainTexture = usersClrTex;
                    }
                }
            }

            if (KinectWrapper.PollSkeleton(ref smoothParameters, ref skeletonFrame))
            {
                ProcessSkeleton();
            }
        }
    }
Example #20
0
    void Update()
    {
        if (KinectInitialized)
        {
            if (ComputeUserMap)
            {
                if (depthStreamHandle != IntPtr.Zero &&
                    KinectWrapper.PollDepth(depthStreamHandle,
                                            KinectWrapper.Constants.IsNearMode, ref usersDepthMap))
                {
                    UpdateUserMap();
                }
            }

            if (ComputeColorMap)
            {
                if (colorStreamHandle != IntPtr.Zero && KinectWrapper.PollColor(colorStreamHandle, ref usersColorMap, ref colorImage))
                {
                    UpdateColorMap();
                }
            }

            if (KinectWrapper.PollSkeleton(ref smoothParameters, ref skeletonFrame))
            {
                ProcessSkeleton();
            }
        }
    }
Example #21
0
    public Vector3 GetJointPosition(uint UserId, int joint)
    {
        KinectWrapper.GetJointTransformation(UserId, joint, ref jointTransform);
        KinectWrapper.SkeletonJointPosition pos = jointTransform.pos;

        return(new Vector3(pos.x * 0.001f, pos.y * 0.001f, pos.z * 0.001f));
    }
    // CopySkeleton copies the data from another skeleton.
    public static void CopySkeleton(ref KinectWrapper.NuiSkeletonData source, ref KinectWrapper.NuiSkeletonData destination)
    {
        //        if (null == source)
        //        {
        //            return;
        //        }
        //
        //        if (null == destination)
        //        {
        //            destination = new Skeleton();
        //        }

        destination.eTrackingState = source.eTrackingState;
        destination.dwTrackingID = source.dwTrackingID;
        destination.Position = source.Position;
        destination.dwQualityFlags = source.dwQualityFlags;

        int jointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;

        if(destination.SkeletonPositions == null)
            destination.SkeletonPositions = new Vector4[jointsCount];
        if(destination.eSkeletonPositionTrackingState == null)
            destination.eSkeletonPositionTrackingState = new KinectWrapper.NuiSkeletonPositionTrackingState[jointsCount];

        for(int jointIndex = 0; jointIndex < jointsCount; jointIndex++)
        {
            destination.SkeletonPositions[jointIndex] = source.SkeletonPositions[jointIndex];
            destination.eSkeletonPositionTrackingState[jointIndex] = source.eSkeletonPositionTrackingState[jointIndex];
        }
    }
Example #23
0
    public static bool PollColor(IntPtr colorStreamHandle, ref Color32[] colorImage)
    {
        IntPtr imageFramePtr = IntPtr.Zero;
        bool   newColor      = false;

        int hr = KinectWrapper.NuiImageStreamGetNextFrame(colorStreamHandle, 0, ref imageFramePtr);

        if (hr == 0)
        {
            newColor = true;

            NuiImageFrame    imageFrame   = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
            INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

            NuiLockedRect lockedRectPtr = new NuiLockedRect();
            IntPtr        r             = IntPtr.Zero;

            frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
            ExtractColorImage(lockedRectPtr, ref colorImage);

            frameTexture.UnlockRect(0);
            hr = KinectWrapper.NuiImageStreamReleaseFrame(colorStreamHandle, imageFramePtr);
        }

        return(newColor);
    }
Example #24
0
    public PoseAngle(KinectWrapper.NuiSkeletonPositionIndex centerJoint, KinectWrapper.NuiSkeletonPositionIndex angleJoint,
		double angle, double threshold)
    {
        CenterJoint = centerJoint;
        AngleJoint = angleJoint;
        Angle = angle;
        Threshold = threshold;
    }
Example #25
0
    public Quaternion GetJointOrientation(uint UserId, int joint, bool flip)
    {
        KinectWrapper.GetJointTransformation(UserId, joint, ref jointTransform);
        KinectWrapper.SkeletonJointOrientation ori = jointTransform.ori;
        Quaternion quat = ConvertMatrixToQuat(ori, joint, flip);

        return(quat);
    }
Example #26
0
 public void OnApplicationQuit()
 {
     if (KinectInitialized)
     {
         KinectWrapper.NuiShutdown();
         instance = null;
     }
 }
 // Make sure to kill the Kinect on quitting.
 void OnApplicationQuit()
 {
     if (KinectInitialized)
     {
         // Shutdown OpenNI
         KinectWrapper.NuiShutdown();
     }
 }
    // Update is called once per frame
    void Update()
    {
        // get 1st player
        uint playerID = KinectManager.Instance != null?KinectManager.Instance.GetPlayer1ID() : 0;

        if (playerID <= 0)
        {
            return;
        }

        // set the position in space
        Vector3 posPointMan = KinectManager.Instance.GetUserPosition(playerID);

        posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z;

        // store the initial position
        if (!initialPosInitialized)
        {
            posInitialOffset      = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
            initialPosInitialized = true;
        }

        transform.position = posInitialOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));

        // update the local positions of the bones
        int jointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;

        for (int i = 0; i < jointsCount; i++)
        {
            if (_bones[i] != null)
            {
                if (KinectManager.Instance.IsJointTracked(playerID, i))
                {
                    _bones[i].gameObject.SetActive(true);

                    int     joint    = MirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(i): i;
                    Vector3 posJoint = KinectManager.Instance.GetJointPosition(playerID, joint);
                    posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z;
                    Quaternion rotJoint = KinectManager.Instance.GetJointOrientation(playerID, joint, !MirroredMovement);

                    posJoint  -= posPointMan;
                    posJoint.z = -posJoint.z;

                    if (MirroredMovement)
                    {
                        posJoint.x = -posJoint.x;
                    }

                    _bones[i].transform.localPosition = posJoint;
                    _bones[i].transform.localRotation = rotJoint;
                }
                else
                {
                    _bones[i].gameObject.SetActive(false);
                }
            }
        }
    }
Example #29
0
 // Make sure to kill the Kinect on quitting.
 void OnApplicationQuit()
 {
     if (_kinectInitialized)
     {
         // Shutdown OpenNI
         KinectWrapper.NuiShutdown();
         Instance = null;
     }
 }
Example #30
0
    // Use this for initialization
    void Start()
    {
        //km = GetComponent<KinectManager>();
        km = camera.GetComponent <KinectManager>();
        //mrenderer = GetComponent<MeshRenderer>();
        meshrenderer = GetComponent <MeshRenderer>();
        meshfilter   = GetComponent <MeshFilter>();
        mesh         = new Mesh();
        Vector3 pos = new Vector3(0, 0, 0);

        meshfilter.mesh = mesh;

        depth_data    = km.GetRawDepthMap();
        canvas_height = KinectWrapper.GetDepthHeight() / 2;
        canvas_width  = KinectWrapper.GetDepthWidth() / 2;
        //numPoints = canvas_height * canvas_width;
        numPoints = canvas_height * canvas_width;


        meshrenderer.material = new Material(Shader.Find("Tim/PointShader"));
        //meshrenderer.SetWidth(0.02f, 0.02f); //thickness of line
        Color c_white = Color.white;
        Color c_red   = Color.red;


        for (int i = 0; i < canvas_width; i++)
        {
            for (int j = 0; j < canvas_height; j++)
            {
                vertices.Add(new Vector3(i * 0.01f, j * 0.01f, 0));
            }
        }

        points = new Vector3[numPoints];
        int[]   indicies = new int[numPoints];
        Color[] colors   = new Color[numPoints];
        for (int i = 0; i < points.Length; ++i)
        {
            points[i]   = new Vector3(UnityEngine.Random.Range(-10, 10), UnityEngine.Random.Range(-10, 10), UnityEngine.Random.Range(-10, 10));
            indicies[i] = i;
            colors[i]   = new Color(UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), 1.0f);
            //mesh.normals[i] = new Vector3(0, 0, 1);
        }

        mesh.vertices = points;
        mesh.colors   = colors;
        mesh.SetIndices(indicies, MeshTopology.Points, 0);


        //depth, width, height

        Debug.Log(numPoints);
        Debug.Log(canvas_width);
        Debug.Log(canvas_height);

        Debug.Log(depth_data.Length);
    }
Example #31
0
 // Make sure to kill the Kinect on quitting.
 void OnApplicationQuit()
 {
     if (OpenNIInitialized)
     {
         // Shutdown OpenNI
         KinectWrapper.Shutdown();
         instance = null;
     }
 }
Example #32
0
    // IsTrackedOrInferred checks whether the skeleton joint is tracked or inferred.
    public static bool IsTrackedOrInferred(KinectWrapper.NuiSkeletonData skeleton, int jointIndex)
    {
//            if (null == skeleton)
//            {
//                return false;
//            }

		return skeleton.eSkeletonPositionTrackingState[jointIndex] != KinectWrapper.NuiSkeletonPositionTrackingState.NotTracked;
    }
Example #33
0
    void Update()
    {
        if (!isReady)
        {
            return;
        }

        KinectWrapper.NuiUpdate();
    }
Example #34
0
    // If a user successfully calibrates, assign him/her to player 1 or 2.
    void OnCalibrationSuccess(uint UserId)
    {
        Debug.Log(String.Format("[{0}] Calibration success", UserId));

        // If player 1 hasn't been calibrated, assign that UserID to it.
        if (!Player1Calibrated)
        {
            // Check to make sure we don't accidentally assign player 2 to player 1.
            if (UserId != Player2ID)
            {
                Player1Calibrated = true;
                Player1ID         = UserId;

                foreach (AvatarController controller in Player1Controllers)
                {
                    controller.SuccessfulCalibration(UserId);
                }

                // If we're not using 2 users, we're all calibrated.
                if (!TwoUsers)
                {
                    AllPlayersCalibrated = true;
                }
            }
        }
        // Otherwise, assign to player 2.
        else
        {
            if (UserId != Player1ID)
            {
                Player2Calibrated = true;
                Player2ID         = UserId;

                foreach (AvatarController controller in Player2Controllers)
                {
                    controller.SuccessfulCalibration(UserId);
                }

                // All users are calibrated!
                AllPlayersCalibrated = true;
            }
        }

        // If all users are calibrated, stop trying to find them.
        if (AllPlayersCalibrated)
        {
            Debug.Log("");

            if (CalibrationText != null)
            {
                CalibrationText.guiText.text = "";
            }

            KinectWrapper.StopLookingForUsers();
        }
    }
Example #35
0
    void Update()
    {
        if (KinectInitialized)
        {
            // If the players aren't all calibrated yet, draw the user map.
            if (!AllPlayersCalibrated || UserMapAfterCalibration)
            {
                if (depthStreamHandle != IntPtr.Zero &&
                    KinectWrapper.PollDepth(depthStreamHandle, NearMode, ref usersDepthMap))
                {
                    UpdateUserMap();
                }

                if (colorStreamHandle != IntPtr.Zero &&
                    DisplayColorMap && KinectWrapper.PollColor(colorStreamHandle, ref colorImage))
                {
                    UpdateColorMap();
                }
            }

            if (KinectWrapper.PollSkeleton(smoothParameters, ref skeletonFrame))
            {
                ProcessSkeleton();
            }

            // Update player 1's models if he/she is calibrated and the model is active.
            if (Player1Calibrated)
            {
                foreach (AvatarController controller in Player1Controllers)
                {
                    if (controller.Active)
                    {
                        controller.UpdateAvatar(Player1ID, NearMode);
                    }
                }
            }

            // Update player 2's models if he/she is calibrated and the model is active.
            if (Player2Calibrated)
            {
                foreach (AvatarController controller in Player2Controllers)
                {
                    if (controller.Active)
                    {
                        controller.UpdateAvatar(Player2ID, NearMode);
                    }
                }
            }
        }

        // Kill the program with ESC.
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
 void Update()
 {
     if (KinectInitialized)
     {
         if (KinectWrapper.PollSkeleton(ref smoothParameters, ref skeletonFrame))
         {
             ProcessSkeleton();
         }
     }
 }
Example #37
0
    /*****************************************
     * Tests
     **************************************/
    private void Tests()
    {
        int liTest = KinectWrapper.GetANumber();

        print(liTest);

        IntPtr mpString = KinectWrapper.GetAString();
        string lsTest   = Marshal.PtrToStringAnsi(mpString);

        print(lsTest);
    }
Example #38
0
 public bool SetCameraAngle(int angle)
 {
     if (Time.time - lastCameraAngleChange > 30)
     {
         lastCameraAngleChange = Time.time;
         return(KinectWrapper.SetCameraAngle(angle));
     }
     else
     {
         return(false);
     }
 }
Example #39
0
    public JointConstraint( KinectWrapper.Joints joint_a,
							KinectWrapper.Joints joint_b,
							Relations relation,
							Operators operation,
							Vector3 val )
    {
        this.joint_a = joint_a;
        this.joint_b = joint_b;
        this.relation = relation;
        this.operation = operation;
        this.val = val;
    }
Example #40
0
    public void GestureInProgress(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              float progress, KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        if (gesture == KinectGestures.Gestures.SwipeLeft)
        {
            //Debug.Log("SwipeLeft " + progress + " Screen Pos: "+ screenPos + " Gesture: " + gesture);
        }
        if (gesture == KinectGestures.Gestures.SwipeRight)
        {
            //Debug.Log("SwipeRight " + progress + " Screen Pos: "+ screenPos + " Gesture: " + gesture);
        }
    }
Example #41
0
    // Polls for new skeleton data
    public static bool PollSkeleton(ref BodyFrame bodyFrame, Int64 lastFrameTime)
    {
        bool newSkeleton = false;

        int hr = KinectWrapper.GetBodyFrameData(ref bodyFrame, true, true);

        if (hr == 0 && (bodyFrame.liRelativeTime > lastFrameTime))
        {
            newSkeleton = true;
        }

        return(newSkeleton);
    }
Example #42
0
/********************************************************************************
*           USER METHODS -> Call these methods from your scripts
* ******************************************************************************/

    /// <summary>
    /// main joint position get function
    /// </summary>
    /// <param name="player">player number (1,2)</param>
    /// <param name="joint">KinectWrapper.Joints enum</param>
    /// <returns>position of given joint for given player</returns>
    public Vector3 GetJointPos(int player, KinectWrapper.Joints joint)
    {
        KinectWrapper.SkeletonTransform trans = new KinectWrapper.SkeletonTransform();
        if (NUIisReady)
        {
            KinectWrapper.GetSkeletonTransform(player, (int)joint, ref trans);
            return(new Vector3(trans.x * scaleFactor, trans.y * scaleFactor, trans.z * scaleFactor));
        }
        else
        {
            return(Vector3.zero);
        }
    }
Example #43
0
    // LerpAndApply performs a Lerp and applies the Lerped vector to the skeleton joint.
    public static void LerpAndApply(ref KinectWrapper.NuiSkeletonData skeleton, int jointIndex, Vector3 newJointPos, float lerpValue, KinectWrapper.NuiSkeletonPositionTrackingState finalTrackingState)
    {
//            if (null == skeleton)
//            {
//                return;
//            }

		Vector3 jointPos = (Vector3)skeleton.SkeletonPositions[jointIndex];
        jointPos = Vector3.Lerp(jointPos, newJointPos, lerpValue);
	
		skeleton.SkeletonPositions[jointIndex] = (Vector4)jointPos;
		skeleton.eSkeletonPositionTrackingState[jointIndex] = finalTrackingState;
    }
    public bool GestureCancelled(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              KinectWrapper.NuiSkeletonPositionIndex joint)
    {
        if(progressDisplayed)
        {
            // clear the progress info
            if(GestureInfo != null)
                GestureInfo.GetComponent<GUIText>().text = String.Empty;

            progressDisplayed = false;
        }

        return true;
    }
Example #45
0
	public bool GestureCancelled (uint userId, int userIndex, KinectGestures.Gestures gesture, 
		KinectWrapper.SkeletonJoint joint)
	{
		if(progressDisplayed)
		{
			// clear the progress info
			if(GestureInfo != null)
				GestureInfo.guiText.text = String.Empty;
			
			progressDisplayed = false;
		}
		
		return true;
	}
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        string sGestureText = gesture + " detected";
        if(gesture == KinectGestures.Gestures.Click)
            sGestureText += string.Format(" at ({0:F1}, {1:F1})", screenPos.x, screenPos.y);

        if(GestureInfo != null)
            GestureInfo.GetComponent<GUIText>().text = sGestureText;

        progressDisplayed = false;

        return true;
    }
    // Update the filter with a new frame of data and smooth.
    public void UpdateFilter(ref KinectWrapper.NuiSkeletonData skeleton)
    {
//        if (null == skeleton)
//        {
//            return;
//        }

        if (skeleton.eTrackingState != KinectWrapper.NuiSkeletonTrackingState.SkeletonTracked)
        {
            return;
        }

        if (this.init == false)
        {
            this.Init();    // initialize with default parameters                
        }

        //Array jointTypeValues = Enum.GetValues(typeof(KinectWrapper.NuiSkeletonPositionIndex));

        KinectWrapper.NuiTransformSmoothParameters tempSmoothingParams = new KinectWrapper.NuiTransformSmoothParameters();

        // Check for divide by zero. Use an epsilon of a 10th of a millimeter
        this.smoothParameters.fJitterRadius = Math.Max(0.0001f, this.smoothParameters.fJitterRadius);

        tempSmoothingParams.fSmoothing = smoothParameters.fSmoothing;
        tempSmoothingParams.fCorrection = smoothParameters.fCorrection;
        tempSmoothingParams.fPrediction = smoothParameters.fPrediction;
		
		int jointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;
        for(int jointIndex = 0; jointIndex < jointsCount; jointIndex++)
        {
			//KinectWrapper.NuiSkeletonPositionIndex jt = (KinectWrapper.NuiSkeletonPositionIndex)jointTypeValues.GetValue(jointIndex);
			
            // If not tracked, we smooth a bit more by using a bigger jitter radius
            // Always filter feet highly as they are so noisy
            if (skeleton.eSkeletonPositionTrackingState[jointIndex] != KinectWrapper.NuiSkeletonPositionTrackingState.Tracked)
            {
                tempSmoothingParams.fJitterRadius = smoothParameters.fJitterRadius * 2.0f;
                tempSmoothingParams.fMaxDeviationRadius = smoothParameters.fMaxDeviationRadius * 2.0f;
            }
            else
            {
                tempSmoothingParams.fJitterRadius = smoothParameters.fJitterRadius;
                tempSmoothingParams.fMaxDeviationRadius = smoothParameters.fMaxDeviationRadius;
            }

            FilterJoint(ref skeleton, jointIndex, ref tempSmoothingParams);
        }
    }
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture, 
        KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        string sGestureText = gesture + " detected";
        if(GestureInfo != null)
        {
            GestureInfo.GetComponent<GUIText>().text = sGestureText;
        }

        if(gesture == KinectGestures.Gestures.SwipeLeft)
            swipeLeft = true;
        else if(gesture == KinectGestures.Gestures.SwipeRight)
            swipeRight = true;

        return true;
    }
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        Debug.Log ("A gesture!");
        switch (currentRoom) {
        case Scene.Setup:
            Debug.Log ("A gesture in setup no less.");
            currentRoom = Scene.Setup;
            setupGameController.ReceiveGesture (gesture);
            break;
        case Scene.Model_Room:
            currentRoom = Scene.Model_Room;
            gameController.ReceiveGesture (gesture);
            break;
        }
        return true;
    }
    public void GestureInProgress(uint userId, int userIndex, KinectGestures.Gestures gesture, 
	                              float progress, KinectWrapper.NuiSkeletonPositionIndex joint, Vector3 screenPos)
    {
        if (GestureIsContinuous(gesture) && progress > 0.3f) {
            if (!gestureInProgress[(int)gesture] || Time.time - lastGestureTime[(int)gesture] >= timeBetweenContinuousGestures) {
                gestureInProgress[(int)gesture] = true;
                lastGestureTime[(int)gesture] = Time.time;
                switch (currentRoom) {
                case Scene.Setup:
                    currentRoom = Scene.Setup;
                    setupGameController.ReceiveGesture (gesture);
                    break;
                case Scene.Model_Room:
                    currentRoom = Scene.Model_Room;
                    gameController.ReceiveGesture (gesture);
                    break;
                }
            }
        }
    }
    public bool GestureCompleted(uint userId, int userIndex, KinectGestures.Gestures gesture, 
		KinectWrapper.SkeletonJoint joint, Vector3 screenPos)
    {
        string sGestureText = gesture + " detected";
        if(GestureInfo != null)
        {
            GestureInfo.guiText.text = sGestureText;
        }

        if(gesture == KinectGestures.Gestures.SwipeLeft)
            swipeLeft = true;
        else if(gesture == KinectGestures.Gestures.SwipeRight)
            swipeRight = true;
        else if(gesture == KinectGestures.Gestures.ZoomOut)
            zoomOut = true;
        else if(gesture == KinectGestures.Gestures.ZoomIn)
            zoomIn = true;

        return true;
    }
    // Update the filter with a new frame of data and smooth.
    public void UpdateFilter(ref KinectWrapper.NuiSkeletonData skeleton)
    {
        if (skeleton.eTrackingState != KinectWrapper.NuiSkeletonTrackingState.SkeletonTracked)
        {
            return;
        }

        if (this.init == false)
        {
            this.Init();    // initialize with default parameters
        }

        // Check for divide by zero. Use an epsilon of a 10th of a millimeter
        smoothParameters.fJitterRadius = Math.Max(0.0001f, smoothParameters.fJitterRadius);

        int jointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;
        for(int jointIndex = 0; jointIndex < jointsCount; jointIndex++)
        {
            FilterJoint(ref skeleton, jointIndex, ref smoothParameters);
        }
    }
    // Assign UserId to player 1 or 2.
    void CalibrateUser(uint UserId, int UserIndex, ref KinectWrapper.NuiSkeletonData skeletonData)
    {
        // If player 1 hasn't been calibrated, assign that UserID to it.
        if(!Player1Calibrated)
        {
            // Check to make sure we don't accidentally assign player 2 to player 1.
            if (!allUsers.Contains(UserId))
            {
                if(CheckForCalibrationPose(UserId, ref Player1CalibrationPose, ref player1CalibrationData, ref skeletonData))
                {
                    Player1Calibrated = true;
                    Player1ID = UserId;
                    Player1Index = UserIndex;

                    allUsers.Add(UserId);

                    foreach(AvatarController controller in Player1Controllers)
                    {
                        controller.SuccessfulCalibration(UserId);
                    }

                    // add the gestures to detect, if any
                    foreach(KinectGestures.Gestures gesture in Player1Gestures)
                    {
                        DetectGesture(UserId, gesture);
                    }

                    // notify the gesture listeners about the new user
                    foreach(KinectGestures.GestureListenerInterface listener in gestureListeners)
                    {
                        listener.UserDetected(UserId, 0);
                    }

                    // reset skeleton filters
                    ResetFilters();

                    // If we're not using 2 users, we're all calibrated.
                    //if(!TwoUsers)
                    {
                        AllPlayersCalibrated = !TwoUsers ? allUsers.Count >= 1 : allUsers.Count >= 2; // true;
                    }
                }
            }
        }
        // Otherwise, assign to player 2.
        else if(TwoUsers && !Player2Calibrated)
        {
            if (!allUsers.Contains(UserId))
            {
                if(CheckForCalibrationPose(UserId, ref Player2CalibrationPose, ref player2CalibrationData, ref skeletonData))
                {
                    Player2Calibrated = true;
                    Player2ID = UserId;
                    Player2Index = UserIndex;

                    allUsers.Add(UserId);

                    foreach(AvatarController controller in Player2Controllers)
                    {
                        controller.SuccessfulCalibration(UserId);
                    }

                    // add the gestures to detect, if any
                    foreach(KinectGestures.Gestures gesture in Player2Gestures)
                    {
                        DetectGesture(UserId, gesture);
                    }

                    // notify the gesture listeners about the new user
                    foreach(KinectGestures.GestureListenerInterface listener in gestureListeners)
                    {
                        listener.UserDetected(UserId, 1);
                    }

                    // reset skeleton filters
                    ResetFilters();

                    // All users are calibrated!
                    AllPlayersCalibrated = !TwoUsers ? allUsers.Count >= 1 : allUsers.Count >= 2; // true;
                }
            }
        }

        // If all users are calibrated, stop trying to find them.
        if(AllPlayersCalibrated)
        {
            Debug.Log("All players calibrated.");

            if(CalibrationText != null)
            {
                CalibrationText.GetComponent<GUIText>().text = "";
            }
        }
    }
    // Update the filter for one joint.
    protected void FilterJoint(ref KinectWrapper.NuiSkeletonData skeleton, int jointIndex, ref KinectWrapper.NuiTransformSmoothParameters smoothingParameters)
    {
        float filteredState;
        float trend;
        float diffVal;

        float rawState = (float)skeleton.eSkeletonPositionTrackingState[jointIndex];
        float prevFilteredState = history[jointIndex].FilteredState;
        float prevTrend = history[jointIndex].Trend;
        float prevRawState = history[jointIndex].RawState;

        // If joint is invalid, reset the filter
        if (rawState == 0f)
        {
            history[jointIndex].FrameCount = 0;
        }

        // Initial start values
        if (history[jointIndex].FrameCount == 0)
        {
            filteredState = rawState;
            trend = 0f;
        }
        else if (this.history[jointIndex].FrameCount == 1)
        {
            filteredState = (rawState + prevRawState) * 0.5f;
            diffVal = filteredState - prevFilteredState;
            trend = (diffVal * smoothingParameters.fCorrection) + (prevTrend * (1.0f - smoothingParameters.fCorrection));
        }
        else
        {
        //            // First apply jitter filter
        //            diffVal = rawState - prevFilteredState;
        //
        //            if (diffVal <= smoothingParameters.fJitterRadius)
        //            {
        //                filteredState = (rawState * (diffVal / smoothingParameters.fJitterRadius)) + (prevFilteredState * (1.0f - (diffVal / smoothingParameters.fJitterRadius)));
        //            }
        //            else
        //            {
        //                filteredState = rawState;
        //            }

            filteredState = rawState;

            // Now the double exponential smoothing filter
            filteredState = (filteredState * (1.0f - smoothingParameters.fSmoothing)) + ((prevFilteredState + prevTrend) * smoothingParameters.fSmoothing);

            diffVal = filteredState - prevFilteredState;
            trend = (diffVal * smoothingParameters.fCorrection) + (prevTrend * (1.0f - smoothingParameters.fCorrection));
        }

        // Predict into the future to reduce latency
        float predictedState = filteredState + (trend * smoothingParameters.fPrediction);

        // Check that we are not too far away from raw data
        diffVal = predictedState - rawState;

        if (diffVal > smoothingParameters.fMaxDeviationRadius)
        {
            predictedState = (predictedState * (smoothingParameters.fMaxDeviationRadius / diffVal)) + (rawState * (1.0f - (smoothingParameters.fMaxDeviationRadius / diffVal)));
        }

        // Save the data from this frame
        history[jointIndex].RawState = rawState;
        history[jointIndex].FilteredState = filteredState;
        history[jointIndex].Trend = trend;
        history[jointIndex].FrameCount++;

        // Set the filtered data back into the joint
        skeleton.eSkeletonPositionTrackingState[jointIndex] = (KinectWrapper.NuiSkeletonPositionTrackingState)(predictedState + 0.5f);
    }
    // Apply the rotations tracked by kinect to the joints.
    void TransformBone(uint userId, KinectWrapper.SkeletonJoint joint, int boneIndex, bool flip)
    {
        Transform boneTransform = bones[boneIndex];
        if(boneTransform == null)
            return;

        // Grab the bone we're moving.
        int iJoint = (int)joint;
        if(iJoint < 0)
            return;

        // Get Kinect joint orientation
        Quaternion jointRotation = KinectManager.Instance.GetJointOrientation(userId, iJoint, flip);
        if(jointRotation == Quaternion.identity)
            return;

        // Apply the new rotation.
        Quaternion newRotation = jointRotation * initialRotations[boneIndex];

        //If an offset node is specified, combine the transform with its
        //orientation to essentially make the skeleton relative to the node
        if (offsetNode != null)
        {
            // Grab the total rotation by adding the Euler and offset's Euler.
            Vector3 totalRotation = newRotation.eulerAngles + offsetNode.transform.rotation.eulerAngles;
            // Grab our new rotation.
            newRotation = Quaternion.Euler(totalRotation);
        }

        // Smoothly transition to our new rotation.
        boneTransform.rotation = Quaternion.Slerp(boneTransform.rotation, newRotation, Time.deltaTime * SmoothFactor);
    }
    // Invoked when a gesture is in progress
    // The gesture must be added first, with KinectManager.Instance.DetectGesture()
    public void GestureInProgress(uint userId, KinectWrapper.Gestures gesture, float progress, 
		KinectWrapper.SkeletonJoint joint, Vector3 screenPos)
    {
        //GestureInfo.guiText.text = string.Format("{0} Progress: {1:F1}%", gesture, (progress * 100));
        if((gesture == KinectWrapper.Gestures.RightHandCursor || gesture == KinectWrapper.Gestures.LeftHandCursor) && progress > 0.5f)
        {
        //			if(HandCursor != null)
        //			{
        //				HandCursor.transform.position = Vector3.Lerp(HandCursor.transform.position, screenPos, 3 * Time.deltaTime);
        //			}
        //
        //			string sGestureText = string.Format("{0} - ({1:F1}, {2:F1} {3:F1})", gesture, screenPos.x, screenPos.y, progress * 100);
        //			Debug.Log(sGestureText);
        }
        else if(gesture == KinectWrapper.Gestures.Click && progress > 0.3f)
        {
            string sGestureText = string.Format ("{0} {1:F1}% complete", gesture, progress * 100);
            if(GestureInfo != null)
                GestureInfo.guiText.text = sGestureText;
            progressDisplayed = true;
        }
        else if((gesture == KinectWrapper.Gestures.ZoomOut || gesture == KinectWrapper.Gestures.ZoomIn) && progress > 0.5f)
        {
            string sGestureText = string.Format ("{0} detected, zoom={1:F1}%", gesture, screenPos.z * 100);
            if(GestureInfo != null)
                GestureInfo.guiText.text = sGestureText;
            progressDisplayed = true;
        }
        else if(gesture == KinectWrapper.Gestures.Wheel && progress > 0.5f)
        {
            string sGestureText = string.Format ("{0} detected, angle={1:F1} deg", gesture, screenPos.z);
            if(GestureInfo != null)
                GestureInfo.guiText.text = sGestureText;
            progressDisplayed = true;
        }
    }
    // Invoked when a gesture is complete.
    // Return true, if the gesture must be detected again, false otherwise
    public bool GestureComplete(uint userId, KinectWrapper.Gestures gesture,
		KinectWrapper.SkeletonJoint joint, Vector3 screenPos)
    {
        string sGestureText = gesture + " detected";
        if(gesture == KinectWrapper.Gestures.Click)
            sGestureText += string.Format(" at ({0:F1}, {1:F1})", screenPos.x, screenPos.y);

        if(GestureInfo != null)
            GestureInfo.guiText.text = sGestureText;
        progressDisplayed = false;

        return true;
    }
    // check if the calibration pose is complete for given user
    private bool CheckForCalibrationPose(uint userId, ref KinectGestures.Gestures calibrationGesture, 
		ref KinectGestures.GestureData gestureData, ref KinectWrapper.NuiSkeletonData skeletonData)
    {
        if(calibrationGesture == KinectGestures.Gestures.None)
            return true;

        // init gesture data if needed
        if(gestureData.userId != userId)
        {
            gestureData.userId = userId;
            gestureData.gesture = calibrationGesture;
            gestureData.state = 0;
            gestureData.joint = 0;
            gestureData.progress = 0f;
            gestureData.complete = false;
            gestureData.cancelled = false;
        }

        // get temporary joints' position
        int skeletonJointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;
        bool[] jointsTracked = new bool[skeletonJointsCount];
        Vector3[] jointsPos = new Vector3[skeletonJointsCount];

        int stateTracked = (int)KinectWrapper.NuiSkeletonPositionTrackingState.Tracked;
        int stateNotTracked = (int)KinectWrapper.NuiSkeletonPositionTrackingState.NotTracked;

        int [] mustBeTrackedJoints = {
            (int)KinectWrapper.NuiSkeletonPositionIndex.AnkleLeft,
            (int)KinectWrapper.NuiSkeletonPositionIndex.FootLeft,
            (int)KinectWrapper.NuiSkeletonPositionIndex.AnkleRight,
            (int)KinectWrapper.NuiSkeletonPositionIndex.FootRight,
        };

        for (int j = 0; j < skeletonJointsCount; j++)
        {
            jointsTracked[j] = Array.BinarySearch(mustBeTrackedJoints, j) >= 0 ? (int)skeletonData.eSkeletonPositionTrackingState[j] == stateTracked :
                (int)skeletonData.eSkeletonPositionTrackingState[j] != stateNotTracked;

            if(jointsTracked[j])
            {
                jointsPos[j] = kinectToWorld.MultiplyPoint3x4(skeletonData.SkeletonPositions[j]);
            }
        }

        // estimate the gesture progess
        KinectGestures.CheckForGesture(userId, ref gestureData, Time.realtimeSinceStartup,
            ref jointsPos, ref jointsTracked);

        // check if gesture is complete
        if(gestureData.complete)
        {
            gestureData.userId = 0;
            return true;
        }

        return false;
    }
    // draws the skeleton in the given texture
    private void DrawSkeleton(Texture2D aTexture, ref KinectWrapper.NuiSkeletonData skeletonData, ref bool[] playerJointsTracked)
    {
        int jointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;

        for(int i = 0; i < jointsCount; i++)
        {
            int parent = KinectWrapper.GetSkeletonJointParent(i);

            if(playerJointsTracked[i] && playerJointsTracked[parent])
            {
                Vector3 posParent = KinectWrapper.MapSkeletonPointToDepthPoint(skeletonData.SkeletonPositions[parent]);
                Vector3 posJoint = KinectWrapper.MapSkeletonPointToDepthPoint(skeletonData.SkeletonPositions[i]);

        //				posParent.y = KinectWrapper.Constants.ImageHeight - posParent.y - 1;
        //				posJoint.y = KinectWrapper.Constants.ImageHeight - posJoint.y - 1;
        //				posParent.x = KinectWrapper.Constants.ImageWidth - posParent.x - 1;
        //				posJoint.x = KinectWrapper.Constants.ImageWidth - posJoint.x - 1;

                //Color lineColor = playerJointsTracked[i] && playerJointsTracked[parent] ? Color.red : Color.yellow;
                DrawLine(aTexture, (int)posParent.x, (int)posParent.y, (int)posJoint.x, (int)posJoint.y, Color.yellow);
            }
        }
    }
Example #60
0
    // Apply the rotations tracked by kinect to a special joint
    protected void TransformSpecialBone(uint userId, KinectWrapper.NuiSkeletonPositionIndex joint, KinectWrapper.NuiSkeletonPositionIndex jointParent, int boneIndex, Vector3 baseDir, bool flip)
    {
        Transform boneTransform = bones[boneIndex];
        if(boneTransform == null || kinectManager == null)
            return;

        if(!kinectManager.IsJointTracked(userId, (int)joint) ||
           !kinectManager.IsJointTracked(userId, (int)jointParent))
        {
            return;
        }

        Vector3 jointDir = kinectManager.GetDirectionBetweenJoints(userId, (int)jointParent, (int)joint, false, true);
        Quaternion jointRotation = jointDir != Vector3.zero ? Quaternion.FromToRotation(baseDir, jointDir) : Quaternion.identity;

        //		if(!flip)
        //		{
        //			Vector3 mirroredAngles = jointRotation.eulerAngles;
        //			mirroredAngles.y = -mirroredAngles.y;
        //			mirroredAngles.z = -mirroredAngles.z;
        //
        //			jointRotation = Quaternion.Euler(mirroredAngles);
        //		}

        if(jointRotation != Quaternion.identity)
        {
            // Smoothly transition to the new rotation
            Quaternion newRotation = Kinect2AvatarRot(jointRotation, boneIndex);

            if(smoothFactor != 0f)
                boneTransform.rotation = Quaternion.Slerp(boneTransform.rotation, newRotation, smoothFactor * Time.deltaTime);
            else
                boneTransform.rotation = newRotation;
        }
    }