Skip to content

Instantly share code, notes, and snippets.

@RandomOutput
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save RandomOutput/7ed314972ff16efba585 to your computer and use it in GitHub Desktop.

Select an option

Save RandomOutput/7ed314972ff16efba585 to your computer and use it in GitHub Desktop.
Cursorbehavior.cs
using UnityEngine;
using System.Collections;
using Leap;
public class CursorBehavior : MonoBehaviour {
private Controller _leapController;
[SerializeField]
private Vector3 _leapCoordsMin;
[SerializeField]
private Vector3 _leapCoordsMax;
private Vector3 _screenSize;
private Camera _uiCamera;
/* Leap coordinates to new cursor coordinates
*
* 1. Leap Coordinates to Normalized Coordinates
* 2. Normalized Coordinates to Screen Coordinates
* 3. Screen Coordinates to World Coordinates via Unity Helper Function
*/
// Use this for initialization
void Start () {
_leapController = new Controller();
_uiCamera = GameObject.Find("UI_Camera").GetComponent<Camera>();
}
// Update is called once per frame
void Update () {
_screenSize = new Vector3(UnityEngine.Screen.width, UnityEngine.Screen.height, 0); //Get the latest screen size
Frame frame = _leapController.Frame(); // Get the latest Frame
if(frame.Hands.Count > 0) {
gameObject.renderer.enabled = true;
Vector3 handPosition = frame.Hands[0].PalmPosition.ToUnity();
Vector3 indexFingerPosition = getIndexFinger(frame.Hands[0]).TipPosition.ToUnity();
Vector3 normalizedPositon = CursorBehavior.leapToNormal ( indexFingerPosition, _leapCoordsMin, _leapCoordsMax );
Vector3 screenPosition = CursorBehavior.normalToScreen ( normalizedPositon, _screenSize );
Debug.Log ("handPosition" + handPosition);
Debug.Log ("normPosition" + normalizedPositon);
Debug.Log ("scrnPosition" + screenPosition);
Vector3 newPosition = _uiCamera.ScreenToWorldPoint ( screenPosition );
transform.position = newPosition;
}
else {
gameObject.renderer.enabled = false;
}
}
public static Finger getIndexFinger(Hand hand) {
Finger returnFinger = Finger.Invalid;
foreach(Finger finger in hand.Fingers) {
if(finger.Type() == Finger.FingerType.TYPE_INDEX) {
returnFinger = finger;
break;
}
}
return returnFinger;
}
public static Vector3 leapToNormal(Vector3 leapCoords, Vector3 min, Vector3 max) {
Vector3 normalPosition = new Vector3();
normalPosition.x = CursorBehavior.normalizeFloat( leapCoords.x, min.x, max.x );
normalPosition.y = CursorBehavior.normalizeFloat( leapCoords.y, min.y, max.y );
normalPosition.z = CursorBehavior.normalizeFloat( leapCoords.z, min.z, max.z );
return normalPosition;
}
public static Vector3 normalToScreen(Vector3 normalCoords, Vector3 _screenSize) {
Vector3 screenPosition = new Vector3();
screenPosition.x = normalCoords.x * _screenSize.x;
screenPosition.y = normalCoords.y * _screenSize.y;
screenPosition.z = 10;
return screenPosition;
}
public static float normalizeFloat(float val, float min, float max) {
return (val - min) / (max - min);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment