ดูวิธีใช้ Instant Placement API ในแอปของคุณเอง
สิ่งที่ต้องดำเนินการก่อน
โปรดทําความเข้าใจแนวคิด AR ขั้นพื้นฐาน และวิธีกําหนดค่าเซสชัน ARCore ก่อนดําเนินการต่อ
คู่มือนี้จะถือว่าคุณได้ติดตั้งและกําหนดค่า Unity ไว้แล้ว หากไม่ โปรดดู คู่มือเริ่มใช้งานฉบับย่อสําหรับ Android ของ Unity เพื่อดูขั้นตอนการติดตั้งและการตั้งค่า
กําหนดค่าเซสชันใหม่ด้วยตําแหน่งทันที
กําหนดค่าแอปด้วยการกําหนดค่าเซสชันใหม่และตั้งค่าโหมดตําแหน่งโฆษณาทันที
- ใน Unity ให้สร้างการกําหนดค่าเซสชันใหม่สําหรับแอป ARCore (ชิ้นงาน > สร้าง > GoogleARCore > เซสชันการกําหนดค่า)
เลือกโหมดตําแหน่งโฆษณาด่วนในการกําหนดค่าเซสชัน
ค่าเริ่มต้นสําหรับ โหมดตําแหน่งโฆษณาด่วน คือปิดใช้ ปัจจุบันตําแหน่งด่วนรองรับเฉพาะโหมด Y ในเครื่อง
เลือกอุปกรณ์ ARCore ในลําดับชั้นของโปรเจ็กต์
ดับเบิลคลิกช่องการกําหนดค่าเซสชัน แล้วเลือก
SessionConfig
ที่สร้างขึ้นก่อนหน้านี้
วางวัตถุ
ในเซสชัน ARCore ใหม่ ให้ทดสอบ Hit โดยใช้ Frame.RaycastInstantPlacements จากนั้นสร้าง Anchor ใหม่โดยใช้ InstantPlacementsPoint ส่งมาจาก Trackable
ของผลการค้นหา Hit
var hitList = new List<TrackableHit>();
TrackableHit hitRaycast;
bool hitResult = Frame.RaycastInstantPlacement(
position.x, position.y, approximateDistance, out hitRaycast);
if (hitResult) {
// Place a pin object at the hit.
var pinObject = Instantiate(PinPrefab, hitRaycast.Pose.position, hitRaycast.Pose.rotation);
// Cast the trackable to an InstantPlacementPoint if you want to
// retrieve the real time pose and tracking method. Otherwise, you
// can use it like any other Trackable.
InstantPlacementPoint instantPlacementPoint = hitRaycast.Trackable as InstantPlacementPoint;
// Create an Anchor using the InstantPlacementPoint.
Anchor anchor = instantPlacementPoint.CreateAnchor(hitRaycast.Pose);
// Automatically sync the virtual object’s pose with the Anchor’s
// pose in each frame.
pinObject.transform.parent = anchor.transform;
}
ตําแหน่งด่วนรองรับการติดตามพื้นที่หน้าจอ เปลี่ยนไปใช้การติดตามเต็มรูปแบบโดยอัตโนมัติเมื่อจุดวางตําแหน่งทันทีได้ปักหมุดอยู่ในโลกจริง เรียกดูท่าทางปัจจุบันด้วย InstantPlacementPoint.Pose
ดูวิธีการติดตามปัจจุบันด้วย InstantPlacementPoint.TrackingMethod
แม้ว่า ARCore จะทําการทดสอบ Hit ตําแหน่งกับพื้นผิวของการวางแนวใดๆ ได้ แต่ผลลัพธ์ Hit จะแสดงท่าทางพร้อมกับ +Y เสมอเพื่อป้องกันทิศทางของแรงโน้มน้าว ในการทดสอบแนวนอน การทดสอบ Hit จะแสดงผลตําแหน่งที่ถูกต้องได้เร็วกว่ามาก
ตรวจสอบวิธีการติดตามตําแหน่งทันที
หาก ARCore คุณมีตําแหน่ง 3 มิติที่ถูกต้อง InstantPlacementPointTrackingMethod
จะเป็น FullTracking
ไม่เช่นนั้น จะเริ่มด้วย ScreenspaceWithApproximateDistance
และเปลี่ยนไปใช้ FullTracking
เมื่อ ARCore แสดงท่าทาง 3 มิติอย่างถูกต้อง วิธีการติดตามจะเปลี่ยนกลับไปเป็น ScreenspaceWithApproximateDistance
เมื่อวิธีการติดตามเป็น FullTracking
ทําให้การเปลี่ยนวิธีการติดตามราบรื่น
ระบบจะโพสท่าโดยอัตโนมัติเมื่อวิธีการติดตามของ InstantPlacementPoint
เปลี่ยนจากระยะทางโดยประมาณเป็นการติดตามเต็มรูปแบบ
ในกรณีนี้ ออบเจ็กต์จะข้าม
เมื่อวิธีการติดตามเปลี่ยนจาก ScreenspaceWithApproximateDistance
ในเฟรมหนึ่ง เป็น FullTracking
ในเฟรมถัดไป ท่าทางจะข้ามจากตําแหน่งเริ่มต้นโดยอิงตามระยะทางโดยประมาณไปยังตําแหน่งใหม่ในระยะทางที่แม่นยํา
การเปลี่ยนแปลงท่าทางนี้อย่างทันท่วงทีจะเปลี่ยนขนาดที่ชัดเจนของออบเจ็กต์ใดก็ได้ที่ตรึงไปยัง InstantPlacementPoint
กล่าวคือ วัตถุจะปรากฏขึ้นอย่างกะทันหันหรือเล็กกว่าในเฟรมก่อนหน้าอย่างกะทันหัน
โค้ดต่อไปนี้แสดงวิธีหลีกเลี่ยงการข้ามภาพโดยการตรวจสอบท่าทางเมื่อเวลาผ่านไป จากเฟรมล่าสุดที่รายงาน ScreenspaceWithApproximateDistance
ถึงตําแหน่งใหม่จากการรายงานเฟรม FullTracking
หากต้องการหลีกเลี่ยงการเปลี่ยนแปลงท่าทางที่มองเห็นได้อย่างฉับพลัน ให้แทรกแซงท่าทางปัจจุบันของออบเจ็กต์เสมือนและ InstantPlacementPoint.Pose
// Interpolate the pose for a specific duration. Start from the first frame that
// the tracking method changes. End after the transitionTime has
// passed transitionDuration.
if (previousTrackingMethod ==
InstantPlacementPointTrackingMethod.ScreenspaceWithApproximateDistance &&
instantPlacementPoint.TrackingMethod ==
InstantPlacementPointTrackingMethod.FullTracking) {
startTransition = true;
previousTrackingMethod = InstantPlacementPointTrackingMethod.FullTracking;
}
if (startTransition) {
transitionTime += Time.deltaTime;
float ratio = Mathf.Min(transitionTime, transitionDuration) / transitionDuration;
// Use a custom function to interpolate two poses based on the ratio. Here,
// LerpPose(Pose, Pose, float) is a generic interpolation function containing
// a current value, a target value, and an interpolation ratio.
var newPose = LerpPose(
new Pose(pinObject.transform.position, pinObject.transform.rotation),
instantPlacementPoint.Pose, ratio);
pinObject.transform.position = newPose.position;
pinObject.transform.rotation = newPose.rotation;
if (transitionTime >= transitionDuration) {
startTransition = false;
// Set the parent transform to be the Anchor created on the
// InstantPlacementPoint, so that it will automatically update the pose
// in future frames.
Anchor anchor = instantPlacementPoint.CreateAnchor(Pose.identity);
pinObject.transform.parent = anchor.transform;
}
}