Chapter 24: Detecting Images & 3D Objects
前言
之前的章節有做過辨識某個特定的2D圖片後,呈現相關AR內容。在新的ARKit 2.0中新增可辨識特定的3D物件另外可以對某個2D圖片進行追蹤。
大綱
Detecting 3D Objects
A brief overview of ARDock
Highlighting the detected object
A few notes about quality and accuracy
Image Tracking
Image detection和Image tracking的不同
Image detection is a static process that locates the reference image in the real world through the device’s camera.
Image tracking, on the other hand, is a dynamic process that begins immediately after an image is detected. It tracks the location of the reference image if it change
ARKit 2.0 提供新的 configuration -
ARImageTrackingConfiguration.
相比ARWorldTrackingConfiguration最大的優勢less expensive in terms of processing power — CPUs and batteries。
Detecting a card
這節內容之前章節已經有做過了。偵測提供的內建的特殊圖片。
// World tracking
let configuration = ARWorldTrackingConfiguration()
// 當ARKit偵測到這些圖片時,會透過ARSCNViewDelegate中的renderer(_:nodeFor:)回傳ARImageAnchor
let triggerImages = ARReferenceImage.referenceImages(inGroupNamed: "deck", bundle: nil)!
configuration.detectionImages = triggerImages
sceneView.session.run(configuration)
////////////////////////////
extension DeckViewController : ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
if let imageAnchor = anchor as? ARImageAnchor {
let overlayNode = createCardOverlayNode(for: imageAnchor)
return overlayNode
}
return nil
}
}
Highlighting a card
再偵測到撲克牌上覆蓋一個AR紅色透明平面。

extension DeckViewController {
func createCardOverlayNode(for anchor: ARImageAnchor) -> SCNNode {
let box = SCNBox(
width: anchor.referenceImage.physicalSize.width,
height: 0.0001,
length: anchor.referenceImage.physicalSize.height,
chamferRadius: 0)
if let material = box.firstMaterial {
material.diffuse.contents = UIColor.red
material.transparency = 0.3
}
return SCNNode(geometry: box)
}
A word on tracking configuration
New tracking configuration
ARImageTrackingConfiguration
. This is specifically designed to detect and track 2D images without the burden of tracking a full 3D world.It uses less CPU
It does it better: In world tracking, injected content is still tracked and displayed even if the detected image is no longer visible in the camera — you experienced that a few moments ago.
It’s more precise
It works in moving environments
Tracking a card, version 2.0
切換成新的configuration
- // World tracking
- let configuration = ARWorldTrackingConfiguration()
+ // Image tracking
+ let configuration = ARImageTrackingConfiguration()
// 當ARKit偵測到這些圖片時,會透過ARSCNViewDelegate中的renderer(_:nodeFor:)回傳ARImageAnchor
let triggerImages = ARReferenceImage.referenceImages(inGroupNamed: "deck", bundle: nil)!
- configuration.detectionImages = triggerImages
+ configuration.trackingImages = triggerImages
Creating a reference model
就像2D要提供reference image, 3D就要提供reference object, 那要如何建立reference object,Apple在WWDC 2018有提供一個範例可以讓我們產生想要的reference object。ARKit Scanner。
Preparing the ARKit session
// World tracking
let configuration = ARWorldTrackingConfiguration()
let models = ARReferenceObject.referenceObjects(inGroupNamed: "models", bundle: nil)!
configuration.detectionObjects = models
sceneView.session.run(configuration)
extension RubikViewController : ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
if let anchor = anchor as? ARObjectAnchor {
print("Found 3D object: \(anchor.referenceObject.name!)")
}
return nil
}
}
Last updated
Was this helpful?