ARCore for Jetpack XR can detect flat surfaces in the user's environment and provide information on them such as their pose, size, and orientation. This can help your app find surfaces like tables to place objects on.
Create an ARCore for Jetpack XR session
Access plane information through an ARCore for Jetpack XR session. See
Understand a Session's lifecycle
to obtain a Session
.
Configure the Session
Plane detection is not enabled by default on XR sessions. To enable plane tracking, configure the session:
val newConfig = session.config.copy( planeTracking = Config.PlaneTrackingMode.HorizontalAndVertical, ) when (val result = session.configure(newConfig)) { is SessionConfigureConfigurationNotSupported -> TODO(/* Some combinations of configurations are not valid. Handle this failure case. */) is SessionConfigurePermissionsNotGranted -> TODO(/* The required permissions in result.permissions have not been granted. */) is SessionConfigureSuccess -> TODO(/* Success! */) }
Retrieve the state of perceived planes
ARCore for Jetpack XR provides the state of planes through a
StateFlow
that emits the state of planes. Subscribing
to planes in a session notifies your app when planes are added, updated, or
removed.
Plane.subscribe(session).collect { planes -> // Planes have changed; update plane rendering }
A plane has the following properties:
label
: a semantic description of a givenPlane
. Could be aWall
,Floor
,Ceiling
, orTable
.centerPose
: The pose of the center of the detected plane.extents
: The dimensions of the detected plane, in meters.vertices
: A list of vertices of a convex polygon that approximates the plane.
Perform a hit-test against planes
A hit-test is a method of calculating the intersection of a ray with objects tracked by the session. A common application of a hit-test is to point at a table and place an object at that location. Conducting a hit-test results in a list of hit objects. In other words, a hit-test doesn't stop at the first object hit. However, often you may only be interested in the first object hit of a given type.
To perform a hit-test, use Interaction.hitTest()
with a
Ray
:
val results = androidx.xr.arcore.hitTest(session, ray) // When interested in the first Table hit: val tableHit = results.firstOrNull { val trackable = it.trackable trackable is Plane && trackable.state.value.label == Plane.Label.Table }