Monday, March 21, 2016

Unity 5.3 + Leap: A Look at Orion

A while ago I posted a demo about raising your hand to get a character's attention using the Leap motion.  I was asked recently to take a second look at this demo as the code in that example appears to no longer work using Leap Motion's Orion release. I had not downloaded Orion yet, so this seemed like a good excuse to do so.

After updating everything, I took a look at one of the featured examples, Blocks and my first impression was quite good. This was by far the best hand tracking I have experienced with the Leap.  Thumbs up for sure.

Next I started assembling my own scene to see if I could get the Orion hands to display. I created a new project, set the project build settings for VR (Edit > Project Settings > Player, then ensured that Virtual Reality Supported was checked), and created  a basic scene with plane, a cube, and a directional light.  After importing the  LeapMotion_CoreAsset_Orion_Beta_4.0.1 package, from the LeapMotion assets, I dragged the Prefab > LMHeadMountedRig into the scene.




I gave it a quick test run, but no hands. I quickly realized that I had not followed steps 4 and 5 of the setup guide  to properly add the hand models, both to the scene and to the Hand Pool script attached to the LeapHandController.








After adding the hand models, I gave it a test run and I had hands in the scene.




Now back to the original question, could I  replicate the hand waving demo I created for my earlier post using Orion. In that demo I had a single character idling, minding his own business who, when the user raises their hand,  waves back with a speech bubble appearing saying “Hello, there!”.

I next added a rigged character and the speech bubble.  For the character motion  I used the same animation and animation controller as in the previous demo.




Finally I imported the wavehello.cs script.  After importing the wavehello.cs script, the first error I needed to address was that the namespace name HandController could not be found.

From the short example posted in the setup guide, it appears that the HandController has been replaced by LeapProvider.

The note on that page, says
" In almost every case, you should get Frame objects from the scene’s LeapProvider object. The LeapProvider transforms the frame data into the proper frame of reference. If you get a frame from the Leap.Controller object, the data will still be in the Leap Motion frame of reference and will not match the hands displayed in the scene."

To that end,  the next step was to create a Frame object and  get the CurrentFrame.

Frame frame = provider.CurrentFrame;


Then to get the hands, I looked for each Hand object in the frame.

foreach (Hand hand in frame.Hands)

And just as I did before, I looked at the Y position for each Hand object, and compared it to the Y position of the CenterEyeObject. To simplify things a bit, I only looked at the right hand (hand.isRight) so that I don't need to account for the situation where one hand is up and one hand is down.

The other change from the old script here is that you no longer use the GetPalmPosition() method and instead simply use hand.PalmPosition.y. Here is the  new Update function:


void Update()
{

  // Get the current frame.
  Frame frame = provider.CurrentFrame;

  foreach (Hand hand in frame.Hands){
    if (hand.IsRight){

      if (hand.PalmPosition.y >= centerEyeAnchor.transform.position.y - 0.03f){
        anim.SetTrigger("Wave");
        changeMenuDisplay(speechbubble, 1);
        Debug.Log("Hand above");
      }else{

        anim.SetTrigger("Idle");
        changeMenuDisplay(speechbubble, 0);
        Debug.Log("Hand below");
      }

    }
  }
}



The new script is on GitHub.