void LateUpdate()
	{
		
//		if (!gestureEnabled) 
//		{
//			handClosed = false;
//			return;
//		}

		rightFistStatusInSensor = ruisSkeletonManager.skeletons[skeletonWand.bodyTrackingDeviceID, skeletonWand.playerId].rightHandStatus;
		leftFistStatusInSensor  = ruisSkeletonManager.skeletons[skeletonWand.bodyTrackingDeviceID, skeletonWand.playerId].leftHandStatus;
		
		if(leftOrRightFist == fistSide.LeftFist) fistStatusInSensor = leftFistStatusInSensor;
		else fistStatusInSensor = rightFistStatusInSensor;
		
		if(!ruisSkeletonManager.isNewKinect2Frame) return; 
		
		currentHandStatus = handClosed;
		
		if(handClosed)
		{
			// If received closed signal, reset buffer
			if(fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.closed) 
			{
				fistOpenSignalTimestampBuffer = new float[fistOpenSignalLimit];
				lastClosedSignalTimestamp = Time.time;
			}
			// If last signal was open, check if array is full of recent enough signals
			else if(fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.open) 
			{
				fistOpenSignalTimestampBuffer[openBufferIndex] = Time.time;
				openBufferIndex = (openBufferIndex + 1) % fistOpenSignalLimit;
				foundValidSignals = 0;
				validTimeWindow = Time.time - fistOpenDuration;
				for(int i = 0; i < fistOpenSignalTimestampBuffer.Length; i++) 
				{
					if(fistOpenSignalTimestampBuffer[i] > validTimeWindow) foundValidSignals++;
				}
				// Trigger opening of hand
				if(foundValidSignals >= fistOpenSignalLimit && handClosed) 
				{ 
					fistOpenSignalTimestampBuffer = new float[fistOpenSignalLimit];
					fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
					handClosed = false; 
				}
			}	
		}
		else 
		{	
			// If received open signal, reset buffer
			if(fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.open) fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
			// If last signal was open, check if array is full of recent enough signals
			else if(fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.closed) 
			{
				fistClosedSignalTimestampBuffer[closedBufferIndex] = Time.time;
				closedBufferIndex = (closedBufferIndex + 1) % fistClosedSignalLimit;
				foundValidSignals = 0;
				validTimeWindow = Time.time - fistClosedDuration;
				for(int i = 0; i < fistClosedSignalTimestampBuffer.Length; i++) 
				{
					if(fistClosedSignalTimestampBuffer[i] > validTimeWindow) foundValidSignals++;
				}
				// Trigger opening of hand
				if(foundValidSignals >= fistClosedSignalLimit && !handClosed) 
				{ 
					fistOpenSignalTimestampBuffer = new float[fistOpenSignalLimit];
					fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
					handClosed = true; 
					lastClosedSignalTimestamp = Time.time;
				}
			}	
		}
		// If no close signal detected for certaint amount of time, assume open hand.
		if(Time.time - lastClosedSignalTimestamp > fistOpenSignalLimit && handClosed) 
		{
			lastClosedSignalTimestamp = Time.time;
			fistOpenSignalTimestampBuffer = new float[fistOpenSignalLimit];
			fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
			handClosed = false;		
		}
		
		newHandStatus = handClosed;
		
		if(currentHandStatus == false && newHandStatus == true) 
			gestureWasTriggered = !gestureWasTriggered;
		
	}
	private void handleFingersCurling(bool trackThumbs)
	{

		bool closeHand;
		int invert = 1;
		float rotationSpeed = 10.0f; // Per second
		Quaternion clenchedRotationThumbTM_corrected = Quaternion.identity;
		Quaternion clenchedRotationThumbIP_corrected = Quaternion.identity;
		
		leftHandStatus = (skeletonManager.skeletons [bodyTrackingDeviceID, playerId].leftHandStatus);
		rightHandStatus = (skeletonManager.skeletons [bodyTrackingDeviceID, playerId].rightHandStatus);
		
		if(leftHandStatus == RUISSkeletonManager.Skeleton.handState.unknown || leftHandStatus ==  RUISSkeletonManager.Skeleton.handState.pointing) 
		{
			leftHandStatus = lastLeftHandStatus;
		}
		
		if(rightHandStatus == RUISSkeletonManager.Skeleton.handState.unknown || rightHandStatus ==  RUISSkeletonManager.Skeleton.handState.pointing) 
		{
			rightHandStatus = lastRightHandStatus;
		}
		
		lastLeftHandStatus = leftHandStatus ;
		lastRightHandStatus = rightHandStatus;
		
		for (int i = 0; i < 2; i++)  
		{ // Hands
			if (i == 0) 
			{
				closeHand = (rightHandStatus  == RUISSkeletonManager.Skeleton.handState.closed);
				invert = -1;
			}
			else 
			{
				closeHand = (leftHandStatus == RUISSkeletonManager.Skeleton.handState.closed);	
				invert = 1;
			}
			// Thumb rotation correction: these depend on your animation rig
			switch(boneLengthAxis)
			{
			case RUISAxis.X:
				clenchedRotationThumbTM_corrected = Quaternion.Euler(clenchedRotationThumbTM.eulerAngles.x 
				                                                     * invert, clenchedRotationThumbTM.eulerAngles.y, clenchedRotationThumbTM.eulerAngles.z);
				clenchedRotationThumbIP_corrected = clenchedRotationThumbTM;
				break;
			case RUISAxis.Y:
				clenchedRotationThumbTM_corrected = clenchedRotationThumbTM;
				clenchedRotationThumbIP_corrected = Quaternion.Euler(clenchedRotationThumbIP.eulerAngles.x, 
				                                                     clenchedRotationThumbIP.eulerAngles.y, clenchedRotationThumbIP.eulerAngles.z * invert);
				break;
			case RUISAxis.Z:
				clenchedRotationThumbTM_corrected = Quaternion.Euler(clenchedRotationThumbTM.eulerAngles.x,
				                                                     clenchedRotationThumbTM.eulerAngles.y * invert, clenchedRotationThumbTM.eulerAngles.z);
				clenchedRotationThumbIP_corrected = clenchedRotationThumbTM;
				break;
			}

			for(int a = 0; a < 5; a++) 
			{ // Fingers
				if(!closeHand && !(a == 4 && trackThumbs)) 
				{
					if(fingerTransforms[i, a, 0])
						fingerTransforms[i, a, 0].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 0].localRotation, initialFingerRotations[i, a, 0], Time.deltaTime * rotationSpeed);
					if(fingerTransforms[i, a, 1])
						fingerTransforms[i, a, 1].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 1].localRotation, initialFingerRotations[i, a, 1], Time.deltaTime * rotationSpeed);
					if(fingerTransforms[i, a, 2])
						fingerTransforms[i, a, 2].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 2].localRotation, initialFingerRotations[i, a, 2], Time.deltaTime * rotationSpeed);
					}
				else 
				{
					if(a != 4) 
					{
						if(fingerTransforms[i, a, 0])
							fingerTransforms[i, a, 0].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 0].localRotation, clenchedRotationMCP, Time.deltaTime * rotationSpeed);
						if(fingerTransforms[i, a, 1])
							fingerTransforms[i, a, 1].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 1].localRotation, clenchedRotationPIP, Time.deltaTime * rotationSpeed);
						if(fingerTransforms[i, a, 2])
							fingerTransforms[i, a, 2].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 2].localRotation, clenchedRotationDIP, Time.deltaTime * rotationSpeed);
					}
					else if(!trackThumbs) 
					{ // Thumbs (if separate thumb  tracking is not enabled)
						if(fingerTransforms[i, a, 0])
							fingerTransforms[i, a, 0].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 0].localRotation, clenchedRotationThumbTM_corrected, Time.deltaTime*rotationSpeed);
						if(fingerTransforms[i, a, 1])
							fingerTransforms[i, a, 1].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 1].localRotation, clenchedRotationThumbMCP, Time.deltaTime * rotationSpeed);
						if(fingerTransforms[i, a, 2])
							fingerTransforms[i, a, 2].localRotation = Quaternion.Slerp(fingerTransforms[i, a, 2].localRotation, clenchedRotationThumbIP_corrected, Time.deltaTime * rotationSpeed);
					}	
				}	
			}
		}
	}
Пример #3
0
    void LateUpdate()
    {
//		if (!gestureEnabled)
//		{
//			handClosed = false;
//			return;
//		}

        rightFistStatusInSensor = ruisSkeletonManager.skeletons[skeletonWand.bodyTrackingDeviceID, skeletonWand.playerId].rightHandStatus;
        leftFistStatusInSensor  = ruisSkeletonManager.skeletons[skeletonWand.bodyTrackingDeviceID, skeletonWand.playerId].leftHandStatus;

        if (leftOrRightFist == fistSide.LeftFist)
        {
            fistStatusInSensor = leftFistStatusInSensor;
        }
        else
        {
            fistStatusInSensor = rightFistStatusInSensor;
        }

        if (!ruisSkeletonManager.isNewKinect2Frame)
        {
            return;
        }

        currentHandStatus = handClosed;

        if (handClosed)
        {
            // If received closed signal, reset buffer
            if (fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.closed)
            {
                fistOpenSignalTimestampBuffer = new float[fistOpenSignalLimit];
                lastClosedSignalTimestamp     = Time.time;
            }
            // If last signal was open, check if array is full of recent enough signals
            else if (fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.open)
            {
                fistOpenSignalTimestampBuffer[openBufferIndex] = Time.time;
                openBufferIndex   = (openBufferIndex + 1) % fistOpenSignalLimit;
                foundValidSignals = 0;
                validTimeWindow   = Time.time - fistOpenDuration;
                for (int i = 0; i < fistOpenSignalTimestampBuffer.Length; i++)
                {
                    if (fistOpenSignalTimestampBuffer[i] > validTimeWindow)
                    {
                        foundValidSignals++;
                    }
                }
                // Trigger opening of hand
                if (foundValidSignals >= fistOpenSignalLimit && handClosed)
                {
                    fistOpenSignalTimestampBuffer   = new float[fistOpenSignalLimit];
                    fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
                    handClosed = false;
                }
            }
        }
        else
        {
            // If received open signal, reset buffer
            if (fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.open)
            {
                fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
            }
            // If last signal was open, check if array is full of recent enough signals
            else if (fistStatusInSensor == RUISSkeletonManager.Skeleton.handState.closed)
            {
                fistClosedSignalTimestampBuffer[closedBufferIndex] = Time.time;
                closedBufferIndex = (closedBufferIndex + 1) % fistClosedSignalLimit;
                foundValidSignals = 0;
                validTimeWindow   = Time.time - fistClosedDuration;
                for (int i = 0; i < fistClosedSignalTimestampBuffer.Length; i++)
                {
                    if (fistClosedSignalTimestampBuffer[i] > validTimeWindow)
                    {
                        foundValidSignals++;
                    }
                }
                // Trigger opening of hand
                if (foundValidSignals >= fistClosedSignalLimit && !handClosed)
                {
                    fistOpenSignalTimestampBuffer   = new float[fistOpenSignalLimit];
                    fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
                    handClosed = true;
                    lastClosedSignalTimestamp = Time.time;
                }
            }
        }
        // If no close signal detected for certaint amount of time, assume open hand.
        if (Time.time - lastClosedSignalTimestamp > fistOpenSignalLimit && handClosed)
        {
            lastClosedSignalTimestamp       = Time.time;
            fistOpenSignalTimestampBuffer   = new float[fistOpenSignalLimit];
            fistClosedSignalTimestampBuffer = new float[fistClosedSignalLimit];
            handClosed = false;
        }

        newHandStatus = handClosed;

        if (currentHandStatus == false && newHandStatus == true)
        {
            gestureWasTriggered = !gestureWasTriggered;
        }
    }