void updatePointable(Leap.Pointable pointable, GameObject fingerObject) { updateParent(fingerObject, pointable.Hand.Id); SetVisible(fingerObject, pointable.IsValid); SetCollidable(fingerObject, pointable.IsValid); if (pointable.IsValid) { Vector3 vFingerPos = pointable.TipPosition.ToUnityTranslated(); // perform a cast between where it is and where it wants to be. float fingerRadius = fingerObject.GetComponent <SphereCollider>().radius; RaycastHit target; // the movement vector of the finger. Vector3 movementDir = vFingerPos - fingerObject.transform.position; movementDir.Normalize(); Vector3 end = vFingerPos + movementDir * fingerRadius; if (Physics.Linecast(fingerObject.transform.position, end, out target, LayerMask.NameToLayer("Buttons"))) { fingerObject.transform.position = target.point + -movementDir * fingerRadius; } else { fingerObject.transform.position = vFingerPos; } Vector3 vFingerDir = pointable.Direction.ToUnity(); fingerObject.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, vFingerDir); } }
void updatePointable(Leap.Pointable pointable, GameObject pointableObject) { SetVisible(pointableObject, pointable.IsValid); SetCollidable(pointableObject, pointable.IsValid); if (pointable.IsValid) { Vector3 vPointableDir = pointable.Direction.ToUnity(); Vector3 vPointablePos = pointable.TipPosition.ToUnityTranslated(); pointableObject.transform.localPosition = vPointablePos; pointableObject.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, vPointableDir); } }
void updatePointable(Leap.Pointable pointable, GameObject fingerObject) { updateParent(fingerObject, pointable.Hand.Id); bool isFarEnough = true; if ((pointable.Hand.PalmPosition.ToUnityTranslated() - pointable.TipPosition.ToUnityTranslated()).magnitude < Magnitude) { isFarEnough = false; } SetVisible(fingerObject, pointable.IsValid && isFarEnough, false); SetCollidable(fingerObject, pointable.IsValid && isFarEnough); if (pointable.IsValid && isFarEnough) { Vector3 localFingerPos = pointable.TipPosition.ToUnityTranslated(); float fingerRadius = fingerObject.GetComponent <SphereCollider>().radius - 0.25f; RaycastHit target; // need to convert it to a vec4 for it to take translation into account when converting to world space. Vector4 localFingerPt = new Vector4(localFingerPos.x, localFingerPos.y, localFingerPos.z, 1.0f); Vector3 worldTargetPos = fingerObject.transform.parent.localToWorldMatrix * localFingerPt; Vector3 worldMovementDir = worldTargetPos - fingerObject.transform.position; worldMovementDir.Normalize(); Vector3 worldRayTarget = worldTargetPos + worldMovementDir * fingerRadius; if (Physics.Linecast(fingerObject.transform.position, worldRayTarget, out target)) { Vector4 targetPoint = new Vector4(target.point.x, target.point.y, target.point.z, 1.0f); Vector3 localTargetPoint = fingerObject.transform.parent.worldToLocalMatrix * targetPoint; Vector3 localMovementDir = localFingerPos - fingerObject.transform.localPosition; localMovementDir.Normalize(); fingerObject.transform.localPosition = localTargetPoint - localMovementDir * fingerRadius; } else { fingerObject.transform.localPosition = localFingerPos; } Vector3 vFingerDir = pointable.Direction.ToUnity(); fingerObject.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, vFingerDir); fingerObject.GetComponent <Rigidbody>().velocity = pointable.TipVelocity.ToUnityScaled(); fingerObject.GetComponent <LeapFinger>().m_hand = pointable.Hand; } }
private void updatePointable(Leap.Pointable pointable, GameObject pointableObject) { pointableObject.active = pointable.IsValid; if (pointable.IsValid) { Vector3 vPointableDir = pointable.Direction.ToUnity(); Vector3 vPointablePos = pointable.TipPosition.ToUnityTranslated(); Debug.DrawRay(vPointablePos, vPointableDir * 0.1f, Color.red); pointableObject.transform.position = vPointablePos; pointableObject.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, vPointableDir); } }
void updatePointable(Leap.Pointable pointable, GameObject fingerObject, bool visible) { updateParent(fingerObject, pointable.Hand.Id); SetVisible(fingerObject, pointable.IsValid && visible); SetCollidable(fingerObject, pointable.IsValid && visible); if (pointable.IsValid) { Vector3 vFingerDir = pointable.Direction.ToUnity(); Vector3 vFingerPos = pointable.TipPosition.ToUnityTranslated(); fingerObject.transform.localPosition = vFingerPos; fingerObject.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, vFingerDir); } }
void updatePointable(Leap.Pointable pointable, GameObject fingerObject) { updateParent(fingerObject, pointable.Hand.Id); SetVisible(fingerObject, pointable.IsValid); SetCollidable(fingerObject, pointable.IsValid); if (pointable.IsValid) { Vector3 localFingerPos = pointable.TipPosition.ToUnityTranslated(); fingerObject.transform.localPosition = localFingerPos; Vector3 vFingerDir = pointable.Direction.ToUnity(); fingerObject.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, vFingerDir); fingerObject.GetComponent <LeapFinger>().m_hand = pointable.Hand; fingerObject.rigidbody.velocity = pointable.TipVelocity.ToUnityScaled(); } }
void OnApplicationQuit() { leapPointable = null; leapFrame = null; if(leapController != null) { leapController.Dispose(); leapController = null; } leapInitialized = false; instance = null; }
void Update() { if(leapInitialized && leapController != null) { Leap.Frame frame = leapController.Frame(); if(frame.IsValid && (frame.Id != lastFrameID)) { leapFrame = frame; lastFrameID = leapFrame.Id; leapFrameCounter++; // fix unfinished leap gesture progress if(fCircleProgress > 0f && fCircleProgress < 1f) fCircleProgress = 0f; if(fSwipeProgress > 0f && fSwipeProgress < 1f) fSwipeProgress = 0f; if(fKeyTapProgress > 0f && fKeyTapProgress < 1f) fKeyTapProgress = 0f; if(fScreenTapProgress > 0f && fScreenTapProgress < 1f) fScreenTapProgress = 0f; // get a suitable pointable leapPointable = leapFrame.Pointable(leapPointableID); if(!leapPointable.IsValid) leapPointable = leapFrame.Pointables.Frontmost; Leap.Vector stabilizedPosition = Leap.Vector.Zero; Leap.Hand handPrim = leapFrame.Hands.Count > 0 ? leapFrame.Hands[leapFrame.Hands.Count - 1] : null; if(leapPointable != null && leapPointable.IsValid && leapPointable.Hand != null && leapPointable.Hand.IsValid && handPrim != null && leapPointable.Hand.Id == handPrim.Id) { leapPointableID = leapPointable.Id; leapPointableHandID = leapPointable.Hand != null && leapPointable.Hand.IsValid ? leapPointable.Hand.Id : 0; leapPointablePos = LeapToUnity(leapPointable.StabilizedTipPosition, true); leapPointableDir = LeapToUnity(leapPointable.Direction, false); //leapPointableQuat = Quaternion.LookRotation(leapPointableDir); leapPointableZone = leapPointable.TouchZone; //stabilizedPosition = leapPointable.StabilizedTipPosition; leapHand = leapPointable.Hand; leapHandID = leapHand.Id; } else { leapPointableID = 0; leapPointable = null; // get leap hand leapHand = leapFrame.Hand(leapHandID); if(leapHand == null || !leapHand.IsValid) { leapHandID = 0; if(leapFrame.Hands.Count > 0) { for(int i = leapFrame.Hands.Count - 1; i >= 0; i--) { leapHand = leapFrame.Hands[i]; if(leapHand.IsValid /**&& leapHand.Fingers.Count > 0*/) { leapHandID = leapHand.Id; break; } } } } } if(leapHandID != 0) { leapHandPos = LeapToUnity(leapHand.StabilizedPalmPosition, true); stabilizedPosition = leapHand.StabilizedPalmPosition; leapHandFingersCount = leapHand.Fingers.Count; } // estimate the cursor coordinates if(stabilizedPosition != Leap.Vector.Zero) { Leap.InteractionBox iBox = frame.InteractionBox; Leap.Vector normalizedPosition = iBox.NormalizePoint(stabilizedPosition); cursorNormalPos.x = normalizedPosition.x; cursorNormalPos.y = normalizedPosition.y; cursorScreenPos.x = cursorNormalPos.x * UnityEngine.Screen.width; cursorScreenPos.y = cursorNormalPos.y * UnityEngine.Screen.height; } // do fingers count filter if(leapHandID != fingersCountHandID) { fingersCountHandID = leapHandID; fingersCountPrev = -1; fingersCountPrevPrev = -1; handGripDetected = false; fingersCountFilter.Reset(); } if(leapHandID != 0) { fingersCountFiltered = leapHandFingersCount; fingersCountFilter.UpdateFilter(ref fingersCountFiltered); if((leapFrameCounter - handGripFrameCounter) >= FramesToSkip) { handGripFrameCounter = leapFrameCounter; int fingersCountNow = (int)(fingersCountFiltered + 0.5f); //int fingersCountNow = leapHandFingersCount; if(fingersCountPrev == fingersCountPrevPrev) { if(!handGripDetected) { if(fingersCountNow < fingersCountPrev) { Finger leftFinger = leapHand.Finger(leapHandLFingerId); Finger rightFinger = leapHand.Finger(leapHandRFingerId); bool bThumbOff = !LeftHandedUser ? leapHandLFingerId != 0 && (leftFinger == null || !leftFinger.IsValid) : leapHandRFingerId != 0 && (rightFinger == null || !rightFinger.IsValid); if(bThumbOff) { handGripDetected = true; handGripFingersCount = fingersCountPrev; } } else { leapHandLFingerId = leapHand != null && leapHand.Fingers.Count > 0 ? leapHand.Fingers.Leftmost.Id : 0; leapHandRFingerId = leapHand != null && leapHand.Fingers.Count > 0 ? leapHand.Fingers.Rightmost.Id : 0; } } else { if(fingersCountNow >= fingersCountPrev/**handGripFingersCount*/) { Finger leftFinger = leapHand.Finger(leapHandLFingerId); Finger rightFinger = leapHand.Finger(leapHandRFingerId); bool bThumbOn = !LeftHandedUser ? (leftFinger != null && leftFinger.IsValid) : (rightFinger != null && rightFinger.IsValid); if(bThumbOn || fingersCountNow >= handGripFingersCount) { handGripDetected = false; } } else if(leapHand == null || !leapHand.IsValid) { // stop pinching if the hand is lost handGripDetected = false; } } } fingersCountPrevPrev = fingersCountPrev; fingersCountPrev = fingersCountNow; } } if(Time.realtimeSinceStartup >= gestureTrackingAtTime) { GestureList gestures = frame.Gestures (); for (int i = 0; i < gestures.Count; i++) { Gesture gesture = gestures[i]; switch (gesture.Type) { case Gesture.GestureType.TYPECIRCLE: CircleGesture circle = new CircleGesture(gesture); if((leapFrameCounter - iCircleFrameCounter) >= FramesToSkip && iCircleGestureID != circle.Id && circle.State == Gesture.GestureState.STATESTOP) { iCircleFrameCounter = leapFrameCounter; iCircleGestureID = circle.Id; fCircleProgress = 1f; gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; } else if(circle.Progress < 1f) { fCircleProgress = circle.Progress; } break; case Gesture.GestureType.TYPESWIPE: SwipeGesture swipe = new SwipeGesture(gesture); if((leapFrameCounter - iSwipeFrameCounter) >= FramesToSkip && iSwipeGestureID != swipe.Id && swipe.State == Gesture.GestureState.STATESTOP) { iSwipeFrameCounter = leapFrameCounter; iSwipeGestureID = swipe.Id; fSwipeProgress = 1f; // swipe.Progress gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; leapSwipeDir = LeapToUnity(swipe.Direction, false); leapSwipeSpeed = LeapToUnity(swipe.Position - swipe.StartPosition, true); if(swipe.DurationSeconds != 0) leapSwipeSpeed /= swipe.DurationSeconds; else leapSwipeSpeed = Vector3.zero; } else if(swipe.State != Gesture.GestureState.STATESTOP) { fSwipeProgress = 0.5f; } break; case Gesture.GestureType.TYPEKEYTAP: KeyTapGesture keytap = new KeyTapGesture (gesture); if((leapFrameCounter - iKeyTapFrameCounter) >= FramesToSkip && iKeyTapGestureID != keytap.Id && keytap.State == Gesture.GestureState.STATESTOP) { iKeyTapFrameCounter = leapFrameCounter; iKeyTapGestureID = keytap.Id; fKeyTapProgress = 1f; gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; } else if(keytap.Progress < 1f) { fKeyTapProgress = keytap.Progress; } break; case Gesture.GestureType.TYPESCREENTAP: ScreenTapGesture screentap = new ScreenTapGesture (gesture); if((leapFrameCounter - iScreenTapFrameCounter) >= FramesToSkip && iScreenTapGestureID != screentap.Id && screentap.State == Gesture.GestureState.STATESTOP) { iScreenTapFrameCounter = leapFrameCounter; iScreenTapGestureID = screentap.Id; fScreenTapProgress = 1f; gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; } else if(screentap.Progress < 1f) { fScreenTapProgress = screentap.Progress; } break; default: Debug.LogError("Unknown gesture type."); break; } } // check for extra gestures int listGestureSize = extraGesturesData.Count; float timestampNow = Time.realtimeSinceStartup; for(int g = 0; g < listGestureSize; g++) { LeapExtraGestures.ExtraGestureData gestureData = extraGesturesData[g]; if(timestampNow >= gestureData.startTrackingAtTime) { LeapExtraGestures.CheckForGesture(ref gestureData, Time.realtimeSinceStartup, this); extraGesturesData[g] = gestureData; if(gestureData.complete) { gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; } } } } if(DebugCamera) { DoDisplayFingers(); } } } }
void Update() { if(leapInitialized && leapController != null) { Leap.Frame frame = leapController.Frame(); if(frame.IsValid && (frame.Id != lastFrameID)) { leapFrame = frame; lastFrameID = leapFrame.Id; leapFrameCounter++; // get the prime hand leapHand = leapFrame.Hand(leapHandID); if(leapHand == null || !leapHand.IsValid) { leapHand = GetFrontmostHand(); leapHandID = leapHand != null && leapHand.IsValid ? leapHand.Id : 0; } leapPointable = leapFrame.Pointable(leapPointableID); if(leapPointable == null || !leapPointable.IsValid) { leapPointable = leapHand.IsValid ? GetPointingFigner(leapHand) : leapFrame.Pointables.Frontmost; leapPointableID = leapPointable != null && leapPointable.IsValid ? leapPointable.Id : 0; leapPointableHandID = leapPointable != null && leapPointable.IsValid && leapPointable.Hand.IsValid ? leapPointable.Hand.Id : 0; } if(leapPointable != null && leapPointable.IsValid && leapPointable.Hand != null && leapPointable.Hand.IsValid && leapHand != null && leapHand.IsValid && leapPointable.Hand.Id == leapHand.Id) { leapPointablePos = LeapToUnity(leapPointable.StabilizedTipPosition, true); leapPointableDir = LeapToUnity(leapPointable.Direction, false); } else { leapPointable = null; leapPointableID = 0; leapPointableHandID = 0; } Leap.Vector stabilizedPosition = Leap.Vector.Zero; if(leapHandID != 0) { leapHandPos = LeapToUnity(leapHand.StabilizedPalmPosition, true); stabilizedPosition = leapHand.StabilizedPalmPosition; leapHandFingersCount = leapHand.Fingers.Count; bool bCurrentHandGrip = handGripDetected; handGripDetected = !isHandOpen(leapHand); handReleaseDetected = !handGripDetected; } else { leapHandFingersCount = 0; handGripDetected = false; handReleaseDetected = false; } if(stabilizedPosition.MagnitudeSquared != 0f) { Leap.InteractionBox iBox = frame.InteractionBox; Leap.Vector normalizedPosition = iBox.NormalizePoint(stabilizedPosition); cursorNormalPos.x = normalizedPosition.x; cursorNormalPos.y = normalizedPosition.y; cursorScreenPos.x = cursorNormalPos.x * UnityEngine.Screen.width; cursorScreenPos.y = cursorNormalPos.y * UnityEngine.Screen.height; } } } }
void Update() { if(leapInitialized && leapController != null) { Leap.Frame frame = leapController.Frame(); if(frame.IsValid && (frame.Id != lastFrameID)) { leapFrame = frame; lastFrameID = leapFrame.Id; leapFrameCounter++; // // fix unfinished leap gesture progress // if(fCircleProgress > 0f && fCircleProgress < 1f) // fCircleProgress = 0f; // if(fSwipeProgress > 0f && fSwipeProgress < 1f) // fSwipeProgress = 0f; // if(fKeyTapProgress > 0f && fKeyTapProgress < 1f) // fKeyTapProgress = 0f; // if(fScreenTapProgress > 0f && fScreenTapProgress < 1f) // fScreenTapProgress = 0f; // get the prime hand leapHand = leapFrame.Hand(leapHandID); if(leapHand == null || !leapHand.IsValid) { leapHand = GetFrontmostHand(); leapHandID = leapHand != null && leapHand.IsValid ? leapHand.Id : 0; } // get the prime pointable leapPointable = leapFrame.Pointable(leapPointableID); if(leapPointable == null || !leapPointable.IsValid) { leapPointable = leapHand.IsValid ? GetPointingFigner(leapHand) : leapFrame.Pointables.Frontmost; leapPointableID = leapPointable != null && leapPointable.IsValid ? leapPointable.Id : 0; leapPointableHandID = leapPointable != null && leapPointable.IsValid && leapPointable.Hand.IsValid ? leapPointable.Hand.Id : 0; } if(leapPointable != null && leapPointable.IsValid && leapPointable.Hand != null && leapPointable.Hand.IsValid && leapHand != null && leapHand.IsValid && leapPointable.Hand.Id == leapHand.Id) { leapPointablePos = LeapToUnity(leapPointable.StabilizedTipPosition, true); leapPointableDir = LeapToUnity(leapPointable.Direction, false); //leapPointableQuat = Quaternion.LookRotation(leapPointableDir); } else { leapPointable = null; leapPointableID = 0; leapPointableHandID = 0; } Leap.Vector stabilizedPosition = Leap.Vector.Zero; if(leapHandID != 0) { leapHandPos = LeapToUnity(leapHand.StabilizedPalmPosition, true); stabilizedPosition = leapHand.StabilizedPalmPosition; leapHandFingersCount = leapHand.Fingers.Count; bool bCurrentHandGrip = handGripDetected; handGripDetected = !isHandOpen(leapHand); handReleaseDetected = !handGripDetected; if(controlMouseCursor) { if(!bCurrentHandGrip && handGripDetected) { MouseControl.MouseDrag(); } else if(bCurrentHandGrip && handReleaseDetected) { MouseControl.MouseRelease(); } } } else { if(controlMouseCursor && handGripDetected) { MouseControl.MouseRelease(); } leapHandFingersCount = 0; handGripDetected = false; handReleaseDetected = false; } // estimate the cursor coordinates if(stabilizedPosition.MagnitudeSquared != 0f) { Leap.InteractionBox iBox = frame.InteractionBox; Leap.Vector normalizedPosition = iBox.NormalizePoint(stabilizedPosition); cursorNormalPos.x = normalizedPosition.x; cursorNormalPos.y = normalizedPosition.y; cursorScreenPos.x = cursorNormalPos.x * UnityEngine.Screen.width; cursorScreenPos.y = cursorNormalPos.y * UnityEngine.Screen.height; if(controlMouseCursor) { MouseControl.MouseMove(cursorNormalPos); } } // Gesture analysis GestureList gestures = frame.Gestures (); for (int i = 0; i < gestures.Count; i++) { Gesture gesture = gestures[i]; if(Time.realtimeSinceStartup < gestureTrackingAtTime) continue; switch (gesture.Type) { case Gesture.GestureType.TYPECIRCLE: CircleGesture circle = new CircleGesture(gesture); if(iCircleGestureID != circle.Id && circle.State == Gesture.GestureState.STATESTOP) { iCircleGestureID = circle.Id; fCircleProgress = 1f; gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; } else if(circle.Progress < 1f) { fCircleProgress = circle.Progress; } break; case Gesture.GestureType.TYPESWIPE: SwipeGesture swipe = new SwipeGesture(gesture); if(iSwipeGestureID != swipe.Id && swipe.State == Gesture.GestureState.STATESTOP) { iSwipeGestureID = swipe.Id; fSwipeProgress = 1f; // swipe.Progress gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; leapSwipeDir = LeapToUnity(swipe.Direction, false); leapSwipeSpeed = LeapToUnity(swipe.Position - swipe.StartPosition, true); if(swipe.DurationSeconds != 0) leapSwipeSpeed /= swipe.DurationSeconds; else leapSwipeSpeed = Vector3.zero; } else if(swipe.State != Gesture.GestureState.STATESTOP) { fSwipeProgress = 0.5f; } break; case Gesture.GestureType.TYPEKEYTAP: KeyTapGesture keytap = new KeyTapGesture (gesture); // if(iKeyTapGestureID != keytap.Id && // keytap.State == Gesture.GestureState.STATESTOP) { iKeyTapGestureID = keytap.Id; fKeyTapProgress = 1f; gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; } // else if(keytap.Progress < 1f) // { // fKeyTapProgress = keytap.Progress; // } break; case Gesture.GestureType.TYPESCREENTAP: ScreenTapGesture screentap = new ScreenTapGesture (gesture); // if(iScreenTapGestureID != screentap.Id && // screentap.State == Gesture.GestureState.STATESTOP) { iScreenTapGestureID = screentap.Id; fScreenTapProgress = 1f; gestureTrackingAtTime = Time.realtimeSinceStartup + MinTimeBetweenGestures; } // else if(screentap.Progress < 1f) // { // fScreenTapProgress = screentap.Progress; // } break; default: Debug.LogError("Unknown gesture type."); break; } } if(DebugCamera) { DoDisplayFingers(); } } } }