Làm việc bằng tay bằng ARCore cho Jetpack XR

ARCore cho Jetpack XR có thể cung cấp thông tin về bàn tay được phát hiện của người dùng, đồng thời cung cấp thông tin về tư thế cho bàn tay và các khớp liên quan. Dữ liệu về bàn tay này có thể được dùng để đính kèm các thực thể và mô hình vào bàn tay của người dùng, ví dụ: trình đơn công cụ:

Lấy một phiên

Truy cập thông tin về bàn tay thông qua Session Android XR. Hãy xem phần Tìm hiểu vòng đời của Phiên để lấy Session.

Định cấu hình phiên

Theo mặc định, tính năng theo dõi cử chỉ tay không được bật trên các phiên XR. Để nhận dữ liệu về bàn tay, hãy định cấu hình phiên:

val newConfig = session.config.copy(
    handTracking = Config.HandTrackingMode.Enabled
)
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! */)
}

Truy xuất dữ liệu về bàn tay

Dữ liệu về tay có sẵn riêng cho tay trái và tay phải. Sử dụng state của mỗi tay để truy cập vào các vị trí tạo dáng cho từng khớp:

Hand.left(session)?.state?.collect { handState -> // or Hand.right(session)
    // Hand state has been updated.
    // Use the state of hand joints to update an entity's position.
    renderPlanetAtHandPalm(handState)
}

Kim có các thuộc tính sau:

  • isActive: liệu tay có đang được theo dõi hay không.
  • handJoints: bản đồ các khớp tay đến tư thế. Tư thế khớp tay được chỉ định theo các tiêu chuẩn OpenXR.

Sử dụng dữ liệu về cử chỉ tay trong ứng dụng

Bạn có thể sử dụng vị trí của các khớp tay của người dùng để neo các đối tượng 3D vào tay của người dùng, chẳng hạn như để đính kèm một mô hình vào lòng bàn tay trái:

val palmPose = leftHandState.handJoints[HandJointType.PALM] ?: return

// the down direction points in the same direction as the palm
val angle = Vector3.angleBetween(palmPose.rotation * Vector3.Down, Vector3.Up)
palmEntity.setHidden(angle > Math.toRadians(40.0))

val transformedPose =
    scenecoreSession.perceptionSpace.transformPoseTo(
        palmPose,
        scenecoreSession.activitySpace,
    )
val newPosition = transformedPose.translation + transformedPose.down * 0.05f
palmEntity.setPose(Pose(newPosition, transformedPose.rotation))

Hoặc để đính kèm một mô hình vào đầu ngón trỏ của tay phải:

val tipPose = rightHandState.handJoints[HandJointType.INDEX_TIP] ?: return

// the forward direction points towards the finger tip.
val angle = Vector3.angleBetween(tipPose.rotation * Vector3.Forward, Vector3.Up)
indexFingerEntity.setHidden(angle > Math.toRadians(40.0))

val transformedPose =
    scenecoreSession.perceptionSpace.transformPoseTo(
        tipPose,
        scenecoreSession.activitySpace,
    )
val position = transformedPose.translation + transformedPose.forward * 0.03f
val rotation = Quaternion.fromLookTowards(transformedPose.up, Vector3.Up)
indexFingerEntity.setPose(Pose(position, rotation))