ทำการทดสอบ Hit ในแอป Android

ทำ Hit-Test เพื่อหาตำแหน่งที่ถูกต้องของวัตถุ 3 มิติในฉากของคุณ การวางตำแหน่งที่ถูกต้องช่วยให้มั่นใจได้ว่าเนื้อหา AR จะแสดงผลในขนาดที่เหมาะสม (ชัดเจน)

ประเภทผลลัพธ์ Hit

การทดสอบ Hit สามารถให้ผลลัพธ์ Hit ที่แตกต่างกัน 4 ประเภทตามที่แสดงในตารางต่อไปนี้

ประเภทผลลัพธ์ Hit คำอธิบาย การวางแนว Use Case การเรียกใช้เมธอด
ความลึก (DepthPoint) ใช้ข้อมูลความลึกจากฉากทั้งหมดเพื่อกำหนดความลึกและการวางแนวที่ถูกต้องของจุด ตั้งฉากกับพื้นผิว 3 มิติ วางวัตถุเสมือนจริงบนพื้นผิวที่กำหนดเอง (ไม่ใช่แค่บนพื้นและผนัง) ต้องเปิดใช้ ArDepthMode เพื่อให้ดำเนินการได้

Frame.hitTest(…) ตรวจหา DepthPoint ในรายการส่งคืน
Plane กระทบพื้นผิวแนวนอนและ/หรือแนวตั้งเพื่อกำหนดความลึกและการวางแนวที่ถูกต้องของจุด ตั้งฉากกับพื้นผิว 3 มิติ วางวัตถุบนเครื่องบิน (พื้นหรือผนัง) โดยใช้รูปเรขาคณิตเต็มของเครื่องบิน ต้องใช้สเกลที่ถูกต้องทันที สำรองสำหรับการทดสอบ Hit ความลึก Frame.hitTest(…) ตรวจดู Plane ในรายการการคืนสินค้า
จุดของฟีเจอร์ (Point) อาศัยฟีเจอร์ภาพรอบๆ จุดที่ผู้ใช้แตะเพื่อกำหนดตำแหน่งและการวางแนวที่ถูกต้องของจุด ตั้งฉากกับพื้นผิว 3 มิติ วางวัตถุบนพื้นผิวที่กำหนดเอง (ไม่ใช่แค่บนพื้นและผนัง) Frame.hitTest(…) ตรวจดู Point ในรายการการคืนสินค้า
ตำแหน่งโฆษณาทันใจ (InstantPlacementPoint) ใช้พื้นที่หน้าจอในการวางเนื้อหา เริ่มแรกจะใช้ความลึกโดยประมาณที่แอปให้ไว้ ทำงานได้ในทันที แต่การโพสท่าและความลึกจริงจะเปลี่ยนไปเมื่อ ARCore กำหนดเรขาคณิตของฉากจริงได้ +Y ชี้ขึ้น ตรงข้ามกับแรงโน้มถ่วง วางวัตถุบนระนาบ (พื้นหรือผนัง) โดยใช้รูปทรงเรขาคณิตเต็มของระนาบ โดยจำเป็นต้องมีการวางตำแหน่งอย่างรวดเร็ว และประสบการณ์ที่ได้จะทนต่อความลึกและมาตราส่วนเริ่มต้นที่ไม่ทราบ Frame.hitTestInstantPlacement(float, float, float)

ทำการทดสอบ Hit มาตรฐาน

เรียกใช้ Frame.hitTest() เพื่อทำการทดสอบ Hit โดยใช้ยูทิลิตี TapHelper เพื่อรับ MotionEvent จากมุมมอง AR

Java

MotionEvent tap = tapHelper.poll();
if (tap == null) {
  return;
}

if (usingInstantPlacement) {
  // When using Instant Placement, the value in APPROXIMATE_DISTANCE_METERS will determine
  // how far away the anchor will be placed, relative to the camera's view.
  List<HitResult> hitResultList =
      frame.hitTestInstantPlacement(tap.getX(), tap.getY(), APPROXIMATE_DISTANCE_METERS);
  // Hit-test results using Instant Placement will only have one result of type
  // InstantPlacementResult.
} else {
  List<HitResult> hitResultList = frame.hitTest(tap);
  // TODO: Filter hitResultList to find a hit result of interest.
}

Kotlin

val tap = tapHelper.poll() ?: return
val hitResultList =
  if (usingInstantPlacement) {
    // When using Instant Placement, the value in APPROXIMATE_DISTANCE_METERS will determine
    // how far away the anchor will be placed, relative to the camera's view.
    frame.hitTestInstantPlacement(tap.x, tap.y, APPROXIMATE_DISTANCE_METERS)
    // Hit-test results using Instant Placement will only have one result of type
    // InstantPlacementResult.
  } else {
    frame.hitTest(tap)
  }

กรองผลลัพธ์ Hit ตามประเภทที่คุณสนใจ ตัวอย่างเช่น หากคุณต้องการมุ่งเน้นที่ DepthPoint:

Java

// Returned hit-test results are sorted by increasing distance from the camera or virtual ray's
// origin.
// The first hit result is often the most relevant when responding to user input.
for (HitResult hit : hitResultList) {
  Trackable trackable = hit.getTrackable();
  if (trackable instanceof DepthPoint) { // Replace with any type of trackable type
    // Do something with this hit result. For example, create an anchor at this point of
    // interest.
    Anchor anchor = hit.createAnchor();
    // TODO: Use this anchor in your AR experience.
    break;
  }
}

Kotlin

// Returned hit-test results are sorted by increasing distance from the camera or virtual ray's
// origin.
// The first hit result is often the most relevant when responding to user input.
val firstHitResult =
  hitResultList.firstOrNull { hit ->
    when (val trackable = hit.trackable!!) {
      is DepthPoint -> true // Replace with any type of trackable type
      else -> false
    }
  }
if (firstHitResult != null) {
  // Do something with this hit result. For example, create an anchor at this point of interest.
  val anchor = firstHitResult.createAnchor()
  // TODO: Use this anchor in your AR experience.
}

ทำการทดสอบ Hit โดยใช้แสงและทิศทางที่กำหนดเอง

โดยทั่วไป Hit-test จะถือว่าเป็นรังสีจากอุปกรณ์หรือกล้องของอุปกรณ์ แต่คุณสามารถใช้ Frame.hitTest(float[], int, float[], int) เพื่อทำการทดสอบ Hit โดยใช้รังสีที่กำหนดเองในพิกัดอวกาศของโลกแทนการใช้จุดกับพื้นที่หน้าจอ

สร้าง Anchor โดยใช้ผลลัพธ์ของ Hit

เมื่อได้รับผลลัพธ์ Hit แล้ว คุณจะใช้ท่าทางเป็นอินพุตเพื่อวางเนื้อหา AR ในฉากได้ ใช้ HitResult.createAnchor() เพื่อสร้าง Anchor ใหม่เพื่อให้แน่ใจว่าเนื้อหาแนบกับ Trackable เบื้องหลังของผลลัพธ์ Hit ตัวอย่างเช่น สมอจะยังคงติดอยู่กับระนาบที่ตรวจพบสำหรับผลลัพธ์การชนของเครื่องบิน เพื่อให้ดูเหมือนว่าเป็นส่วนหนึ่งของโลกจริง

สิ่งที่จะเกิดขึ้นหลังจากนี้