ทำงานด้วยมือโดยใช้ ARCore สำหรับ Jetpack XR

ARCore สำหรับ Jetpack XR สามารถให้ข้อมูลเกี่ยวกับมือที่ตรวจพบของผู้ใช้ รวมถึงให้ข้อมูลท่าทางของมือและข้อต่อที่เกี่ยวข้อง ข้อมูลมือนี้ใช้เพื่อแนบเอนทิตีและโมเดลกับมือของผู้ใช้ได้ เช่น เมนูเครื่องมือ

รับเซสชัน

เข้าถึงข้อมูลมือผ่าน Android XR Session ดูหัวข้อทำความเข้าใจวงจรของเซสชันเพื่อรับ Session

กำหนดค่าเซสชัน

ระบบไม่ได้เปิดใช้การติดตามการเคลื่อนไหวของมือโดยค่าเริ่มต้นในเซสชัน XR หากต้องการรับข้อมูลมือ ให้กําหนดค่าเซสชันดังนี้

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! */)
}

ดึงข้อมูลมือ

ข้อมูลมือจะแสดงแยกกันสำหรับมือซ้ายและมือขวา ใช้ stateของมือแต่ละข้างเพื่อเข้าถึงตำแหน่งท่าทางของข้อต่อแต่ละข้อ โดยทำดังนี้

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)
}

มือมีพร็อพเพอร์ตี้ต่อไปนี้

  • isActive: มีการติดตามมือหรือไม่
  • handJoints: แผนที่ของข้อต่อมือกับท่าทาง ท่าทางของข้อต่อมือจะระบุโดยมาตรฐาน OpenXR

ใช้ข้อมูลมือในแอป

ตำแหน่งข้อต่อของมือผู้ใช้สามารถใช้เพื่อยึดวัตถุ 3 มิติไว้กับมือของผู้ใช้ เช่น เพื่อแนบโมเดลกับฝ่ามือซ้าย

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 =
    session.scene.perceptionSpace.transformPoseTo(
        palmPose,
        session.scene.activitySpace,
    )
val newPosition = transformedPose.translation + transformedPose.down * 0.05f
palmEntity.setPose(Pose(newPosition, transformedPose.rotation))

หรือหากต้องการแนบโมเดลกับปลายนิ้วชี้ของมือขวา ให้ทำดังนี้

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 =
    session.scene.perceptionSpace.transformPoseTo(
        tipPose,
        session.scene.activitySpace,
    )
val position = transformedPose.translation + transformedPose.forward * 0.03f
val rotation = Quaternion.fromLookTowards(transformedPose.up, Vector3.Up)
indexFingerEntity.setPose(Pose(position, rotation))

ตรวจจับท่าทางพื้นฐานของมือ

ใช้ท่าทางของข้อต่อในมือเพื่อตรวจจับท่าทางมือพื้นฐาน ดูรูปแบบข้อต่อของมือเพื่อกำหนดช่วงของท่าทางข้อต่อที่ควรอยู่เพื่อบันทึกเป็นท่าทางหนึ่งๆ

เช่น หากต้องการตรวจจับการบีบด้วยนิ้วโป้งและนิ้วชี้ ให้ใช้ระยะห่างระหว่างข้อต่อปลายนิ้ว 2 ข้อดังนี้

val thumbTip = handState.handJoints[HandJointType.THUMB_TIP] ?: return false
val thumbTipPose = session.scene.perceptionSpace.transformPoseTo(thumbTip, session.scene.activitySpace)
val indexTip = handState.handJoints[HandJointType.INDEX_TIP] ?: return false
val indexTipPose = session.scene.perceptionSpace.transformPoseTo(indexTip, session.scene.activitySpace)
return Vector3.distance(thumbTipPose.translation, indexTipPose.translation) < 0.05

ตัวอย่างท่าทางสัมผัสที่ซับซ้อนมากขึ้นคือท่าทางสัมผัส "หยุด" ในท่าทางสัมผัสนี้ นิ้วแต่ละนิ้วควรเหยียดออก นั่นคือ ข้อต่อของนิ้วแต่ละนิ้วควรชี้ไปในทิศทางเดียวกันโดยประมาณ

val threshold = toRadians(angleInDegrees = 30f)
fun pointingInSameDirection(joint1: HandJointType, joint2: HandJointType): Boolean {
    val forward1 = handState.handJoints[joint1]?.forward ?: return false
    val forward2 = handState.handJoints[joint2]?.forward ?: return false
    return Vector3.angleBetween(forward1, forward2) < threshold
}
return pointingInSameDirection(HandJointType.INDEX_PROXIMAL, HandJointType.INDEX_TIP) &&
    pointingInSameDirection(HandJointType.MIDDLE_PROXIMAL, HandJointType.MIDDLE_TIP) &&
    pointingInSameDirection(HandJointType.RING_PROXIMAL, HandJointType.RING_TIP)

โปรดคำนึงถึงประเด็นต่อไปนี้เมื่อพัฒนาการตรวจจับที่กำหนดเองสำหรับท่าทางมือ

  • ผู้ใช้อาจตีความท่าทางต่างๆ แตกต่างกัน ตัวอย่างเช่น บางคนอาจใช้ท่าทาง "หยุด" โดยกางนิ้วออก ขณะที่บางคนอาจพบว่าการประสานนิ้วเข้าด้วยกันเป็นท่าทางที่เข้าใจง่ายกว่า
  • ท่าทางสัมผัสบางอย่างอาจทำให้ไม่สบายตัว ใช้ท่าทางสัมผัสที่เข้าใจง่ายและไม่ทำให้มือของผู้ใช้เมื่อย

กำหนดมือรองของผู้ใช้

ระบบ Android จะวางการไปยังส่วนต่างๆ ของระบบไว้ที่มือหลักของผู้ใช้ตามที่ผู้ใช้ระบุไว้ในค่ากำหนดของระบบ ใช้มืออีกข้างสำหรับท่าทางสัมผัสที่กำหนดเองเพื่อหลีกเลี่ยงความขัดแย้งกับท่าทางสัมผัสในการไปยังส่วนต่างๆ ของระบบ

val handedness = Hand.getHandedness(activity.contentResolver)
val secondaryHand = if (handedness == Hand.Handedness.LEFT) Hand.right(session) else Hand.left(session)
val handState = secondaryHand?.state ?: return
detectGesture(handState)