Magic Leap: Raycasting (Eye Gaze)

One of the features of the ML1 device is the ability to track your horrible attention span in the form of Eye Gaze. It feels a little bit like this:

super weird and super awesome. aside, i don’t own anything from pirates of the caribbean so don’t sue me.

So I’ll show you a little something that follows your gaze.

Super simple stuff no problem.

Do all your Unity project set up etc. etc. Import Custom Assets etc. etc.

We only need world reconstruction in the privileges so check that under Publishing Settings tab.

In the hierarchy create a sphere and make the transform scale (0.1, 0.1, 0.1). I’d assign the material something green like cause it looks pretty

Drag the MLSpatialMapper prefab into your hierarchy.

The project now looks like this:

NOW WE CODE

Make a script and call it something like, EyeRaycast… or something.

Declare a public gameobject eyeobject (the thing that’ll follow your eye gaze), camera and private Vector3 headlook (to store the vector that points to where you’re looking).

public class EyeRaycast : MonoBehaviour {

    public GameObject eyeobject;            // follows eyegaze
    public Camera maincamera;
    private Vector3 headlook;               // where you're looking

    void Start ()
    {
        MLEyes.Start();
    }

In your Unity drag and drop the 3D thing you want to follow your gaze and your MainCamera into your… maincamera.

In the void Start()? You gotta start Magic Leaps eye api. MLEyes is the class that has all the eye tracking data for both left and right eyes.

Next is in the Update() function. This will be where we update the position of your eyegaze vector and the position vector of the eyeobject and we’ll first check to see if the MLEyes API has started. So If MLEyes has started, get the true position of your fixation point (subtrack the jitter of your head) and send out them rays.

if (MLEyes.IsStarted)
        {
            headlook = MLEyes.FixationPoint - maincamera.transform.position;

            RaycastHit _hit;
            if(Physics.Raycast(maincamera.transform.position, headlook, out _hit))
            {
              eyeobject.transform.position = _hit.point;
              eyeobject.transform.LookAt(_hit.normal + _hit.point);
            }
        }
    }

That transform.LookAt? That makes the GameObject eyeobject LOOK AT where the PERPENDICULAR vector, our hit.normal, is AND the hit.point. This makes the object face up relative to whatever surface it hits.

So if you’re using an object that has clearly defined faces, like an actual cursor:

Actual cursor

Actual cursor

The cursor is going to ‘glide’ across any surface it’s on.

And now you track how distracted you get!

Below is the full code.

note: using eye gaze as a major mechanic in the app you’re making can be exhausting to a user. you’ve been warned