ARCore สำหรับ Jetpack XR สามารถให้ข้อมูลเกี่ยวกับมือที่ตรวจพบของผู้ใช้ และให้ข้อมูลท่าทางสำหรับมือและข้อต่อที่เกี่ยวข้อง ข้อมูลมือนี้ สามารถใช้เพื่อแนบเอนทิตีและโมเดลกับมือของผู้ใช้ได้ เช่น เมนูเครื่องมือ
รับเซสชัน
เข้าถึงข้อมูลมือผ่าน Android XR Session
ดูทําความเข้าใจวงจรของเซสชันเพื่อรับ Session
กำหนดค่าเซสชัน
ระบบจะไม่ได้เปิดใช้การติดตามการเคลื่อนไหวของมือในเซสชัน XR โดยค่าเริ่มต้น หากต้องการรับข้อมูลมือ ให้
กำหนดค่าเซสชันและตั้งค่าโหมด HandTrackingMode.BOTH
ดังนี้
val newConfig = session.config.copy( handTracking = Config.HandTrackingMode.BOTH ) when (val result = session.configure(newConfig)) { is SessionConfigureConfigurationNotSupported -> TODO(/* Some combinations of configurations are not valid. Handle this failure case. */) is SessionConfigureSuccess -> TODO(/* Success! */) else -> TODO(/* A different unhandled exception was thrown. */) }
ดึงข้อมูลมือ
ข้อมูลมือจะใช้ได้กับมือซ้ายและมือขวาแยกกัน ใช้มือแต่ละข้าง
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) }
มือมีพร็อพเพอร์ตี้ต่อไปนี้
trackingState
: ไม่ว่าจะมีการติดตามมือหรือไม่ก็ตาม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.setEnabled(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.setEnabled(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.getPrimaryHandSide(activity.contentResolver) val secondaryHand = if (handedness == Hand.HandSide.LEFT) Hand.right(session) else Hand.left(session) val handState = secondaryHand?.state ?: return detectGesture(handState)
OpenXR™ และโลโก้ OpenXR เป็นเครื่องหมายการค้าของ The Khronos Group Inc. และจดทะเบียนเป็นเครื่องหมายการค้าในจีน สหภาพยุโรป ญี่ปุ่น และสหราชอาณาจักร