// Update is called once per frame void Update() { float percent; Frame frame = controller.Frame(); Pointable pointable = frame.Pointables.Frontmost; //Vector direction = pointable.Direction; //float length = pointable.Length; //float width = pointable.Width; //Vector stabilizedPosition = pointable.StabilizedTipPosition; Vector position = pointable.TipPosition; //Vector speed = pointable.TipVelocity; //float touchDistance = pointable.TouchDistance; Pointable.Zone zone = pointable.TouchZone; Vector3 pointerPos = gameObject.transform.position + _leapManager.pointerPositionWorld + (gameObject.transform.rotation * Vector3.forward * 10.0f); Vector2 screenPosition = myCam.WorldToScreenPoint(pointerPos); Ray clicker = myCam.ScreenPointToRay(screenPosition); if (Physics.Raycast(clicker, out hit, float.MaxValue, hitMask)) { if (hit.collider.gameObject.name == "Restart") { if (lastHover != "Restart") { lastHover = "Restart"; timer = 0f; } else { timer += Time.deltaTime; if (timer >= 3f) { Application.LoadLevel("Map"); } else { percent = timer / 3f; cursor.renderer.material.color = Color.Lerp(startcolor, endcolor, percent); } } } else if (hit.collider.gameObject.name == "Menu") { if (lastHover != "Menu") { lastHover = "Menu"; timer = 0f; } else { timer += Time.deltaTime; if (Time.time + timer >= Time.time + 3f) { Application.LoadLevel("Startscreen"); } else { percent = timer / 3f; cursor.renderer.material.color = Color.Lerp(startcolor, endcolor, percent); } } } else if (hit.collider.gameObject.name == "Next") { if (lastHover != "Next") { lastHover = "Next"; timer = 0f; } else { timer += Time.deltaTime; if (Time.time + timer >= Time.time + 3f) { Application.LoadLevel("Level 2"); } else { percent = timer / 3f; cursor.renderer.material.color = Color.Lerp(startcolor, endcolor, percent); } } } else { timer = 0f; lastHover = ""; percent = timer / 3f; cursor.renderer.material.color = startcolor; } } else { cursor.renderer.material.color = startcolor; } }
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(); } } } }