public final class Eye


A representation of a user's eye.

An Eye instance provides the state of the eye (shut or gazing), as well as a Pose indicating where the user is currently looking.

Summary

Nested types

public final class Eye.State

The representation of the current state of an Eye.

Public methods

final @NonNull StateFlow<@NonNull Eye.State>

A StateFlow that contains the latest State of an Eye.

static final Eye
left(@NonNull Session session)

Returns the left eye, if available.

static final Eye
right(@NonNull Session session)

Returns the right eye, if available.

Public methods

getState

Added in 1.0.0-alpha09
public final @NonNull StateFlow<@NonNull Eye.StategetState()

A StateFlow that contains the latest State of an Eye.

left

Added in 1.0.0-alpha09
public static final Eye left(@NonNull Session session)

Returns the left eye, if available.

import androidx.xr.arcore.ArDevice
import androidx.xr.arcore.Eye
import androidx.xr.arcore.hitTest
import androidx.xr.runtime.Session
import androidx.xr.runtime.TrackingState
import androidx.xr.runtime.math.Ray
import androidx.xr.scenecore.scene

// We need the ArDevice object to properly transform the eye pose coordinate space into
// something we can use.
val arDevice = ArDevice.getInstance(session)

// Obtain the left eye from the Session object.
Eye.left(session)?.let {
    // The left eye is available; launch a coroutine
    // to respond to changes in the eye's state.
    yourCoroutineScope.launch {
        it.state.collect { eyeState ->
            // Early out if the eye is not reported as tracking
            if (eyeState.trackingState != TrackingState.TRACKING) return@collect

            // We are only interested in the eye pose if it is open, so early out if it is
            // closed as well.
            if (!eyeState.isOpen) return@collect

            // The pose we get from the eye is the actual pose of the eye. We want to turn that
            // into something we can use for interacting with other objects in the scene (for
            // example, a hit test). To do that, we need to convert the eye pose into the
            // correct coordinate space, and then use it to create a `Ray` we can pass to the
            // `hitTest()` function.
            //
            // Unlike most other objects, which are reported in the perception coordinate space,
            // the eye pose is reported in the device coordinate space. So we'll first need to
            // convert the device pose into the activity space (via the perception space), and
            // then we can use that to convert the eye pose in to the activity space.
            val transformedPose =
                eyeState.pose.let { pose ->
                    val headPose =
                        session.scene.perceptionSpace.getScenePoseFromPerceptionPose(
                            arDevice.state.value.devicePose
                        )
                    headPose.transformPoseTo(pose, session.scene.activitySpace)
                }

            val gazeRay = Ray(transformedPose.translation, transformedPose.backward)

            val hits = hitTest(session, gazeRay)
            yourHitTestHandler(hits)
        }
    }
}

right

Added in 1.0.0-alpha09
public static final Eye right(@NonNull Session session)

Returns the right eye, if available.

import androidx.xr.arcore.ArDevice
import androidx.xr.arcore.Eye
import androidx.xr.arcore.hitTest
import androidx.xr.runtime.Session
import androidx.xr.runtime.TrackingState
import androidx.xr.runtime.math.Ray
import androidx.xr.scenecore.scene

// We need the ArDevice object to properly transform the eye pose coordinate space into
// something we can use.
val arDevice = ArDevice.getInstance(session)

// Obtain the right eye from the Session object.
Eye.right(session)?.let {
    // The left eye is available; launch a coroutine
    // to respond to changes in the eye's state.
    yourCoroutineScope.launch {
        it.state.collect { eyeState ->
            // Early out if the eye is not reported as tracking
            if (eyeState.trackingState != TrackingState.TRACKING) return@collect

            // We are only interested in the eye pose if it is open, so early out if it is
            // closed as well.
            if (!eyeState.isOpen) return@collect

            // The pose we get from the eye is the actual pose of the eye. We want to turn that
            // into something we can use for interacting with other objects in the scene (for
            // example, a hit test). To do that, we need to convert the eye pose into the
            // correct coordinate space, and then use it to create a `Ray` we can pass to the
            // `hitTest()` function.
            //
            // Unlike most other objects, which are reported in the perception coordinate space,
            // the eye pose is reported in the device coordinate space. So we'll first need to
            // convert the device pose into the activity space (via the perception space), and
            // then we can use that to convert the eye pose in to the activity space.
            val transformedPose =
                eyeState.pose.let { pose ->
                    val headPose =
                        session.scene.perceptionSpace.getScenePoseFromPerceptionPose(
                            arDevice.state.value.devicePose
                        )
                    headPose.transformPoseTo(pose, session.scene.activitySpace)
                }

            val gazeRay = Ray(transformedPose.translation, transformedPose.backward)

            val hits = hitTest(session, gazeRay)
            yourHitTestHandler(hits)
        }
    }
}