Building Your First AR Experience
構建第一個AR體驗
Create an app that runs an AR session and uses plane detection to place 3D content using SceneKit.
創建一個APP運行一個AR會話,使用平面檢測來放置3dD內容使用SceneKit場景
Overview 概述
This sample app runs an ARKit world tracking session with content displayed in a SceneKit view. To demonstrate plane detection, the app visualizes both the estimated shape of and a bounding rectangle for each detected ARPlaneAnchor object. On supported devices, ARKit can recognize many types of real-world surfaces, so the app also labels each detected plane with identifying text.
這個app運行一個AR世界跟蹤會話其中內容顯示在SceneKit view中。為了演示平面檢測,該應用程序為每個被檢測到的ARPlaneAnchor對象可視化了估計形狀和一個邊框。在支持的設備上,ARKit可以識別許多類型的真實世界表面,因此該應用程序還用識別文本為每個檢測到的平面貼上標簽。
Configure and Run the AR Session 配置和運行AR會話
The ARSCNView class is a SceneKit view that includes an ARSession object that manages the motion tracking and image processing required to create an augmented reality (AR) experience. However, to run a session you must provide a session configuration.
ARSCNView類是一個SceneKit視圖,它包含一個ARSession對象,用于管理創建增強現實(AR)體驗所需的運動跟蹤和圖像處理。但是,要運行會話,必須提供會話配置。
The ARWorldTrackingConfiguration class provides high-precision motion tracking and enables features to help you place virtual content in relation to real-world surfaces. To start an AR session, create a session configuration object with the options you want (such as plane detection), then call the runWithConfiguration:options: method on the session object of your ARSCNView instance:
ARWorldTrackingConfiguration類提供了高精度的運動跟蹤,并使功能能夠幫助您將虛擬內容與真實世界的表面相關聯。要啟動一個AR會話,使用您想要的選項(例如平面檢測)創建一個會話配置對象,然后在ARSCNView實例的會話對象上調用runWithConfiguration:options: method:
let configuration = ARWorldTrackingConfiguration()
/**
平面檢測
沒這句話不走 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor);代理
*/
configuration.planeDetection = [.horizontal, .vertical]
sceneView.session.run(configuration)
Run your session only when the view that will display it is onscreen.
只有當將顯示會話的視圖在屏幕上時,才運行會話。
Important
If your app requires ARKit for its core functionality, use the arkit key in the UIRequiredDeviceCapabilities section of your app’s Info.plist file to make your app available only on devices that support ARKit. If AR is a secondary feature of your app, use the isSupported property to determine whether to offer AR-based features.
重要
如果您的應用程序的核心功能需要ARKit,請使用應用程序info.plist文件UIRequiredDeviceCapabilities部分中的ARKit鍵,使您的應用程序僅在支持ARKit的設備上可用。如果AR是應用程序的次要特性,請使用isSupported屬性確定是否提供基于AR的特性。
Place 3D Content for Detected Planes 放置3D內容到檢測到的平面上
After you’ve set up your AR session, you can use SceneKit to place virtual content in the view.
When plane detection is enabled, ARKit adds and updates anchors for each detected plane. By default, the ARSCNView class adds an SCNNode object to the SceneKit scene for each anchor. Your view’s delegate can implement the renderer:didAddNode:forAnchor: method to add content to the scene. When you add content as a child of the node corresponding to the anchor, the ARSCNView class automatically moves that content as ARKit refines its estimate of the plane’s position.
設置AR會話之后,可以使用SceneKit在視圖中放置虛擬內容。
當啟用平面檢測時,ARKit為每個檢測到的平面添加和更新錨點。默認情況下,ARSCNView類為每個錨點向SceneKit場景添加一個SCNNode對象。視圖可實現代理方法renderer:didAddNode:forAnchor:來向場景添加內容。當您將內容作為與錨對應的節點的一個子節點添加時,ARSCNView類會自動移動該內容,因為ARKit會改進它對平面位置的估計。
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
// Place content only for anchors found by plane detection.
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
// Create a custom object to visualize the plane geometry and extent.
let plane = Plane(anchor: planeAnchor, in: sceneView)
// Add the visualization to the ARKit-managed node so that it tracks
// changes in the plane anchor as plane estimation continues.
node.addChildNode(plane)
}
ARKit offers two ways to track the area of an estimated plane. A plane anchor’s geometry describes a convex polygon tightly enclosing all points that ARKit currently estimates to be part of the same plane (easily visualized using ARSCNPlaneGeometry). ARKit also provides a simpler estimate in a plane anchor’s extent and center, which together describe a rectangular boundary (easily visualized using SCNPlane).
ARKit提供兩種跟蹤估計平面區域的方法。平面錨的幾何形狀描述了一個凸多邊形,它緊密地包圍了ARKit目前估計屬于同一平面的所有點(使用ARSCNPlaneGeometry可以很容易地可視化)。ARKit還提供了對平面錨的范圍和中心的更簡單的估計,它們共同描述了一個矩形邊界(使用SCNPlane很容易可視化)。
// Create a mesh to visualize the estimated shape of the plane.
guard let meshGeometry = ARSCNPlaneGeometry(device: sceneView.device!)
else { fatalError("Can't create plane geometry") }
meshGeometry.update(from: anchor.geometry)
meshNode = SCNNode(geometry: meshGeometry)
// Create a node to visualize the plane's bounding rectangle.
let extentPlane: SCNPlane = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))
extentNode = SCNNode(geometry: extentPlane)
extentNode.simdPosition = anchor.center
// `SCNPlane` is vertically oriented in its local coordinate space, so
// rotate it to match the orientation of `ARPlaneAnchor`.
extentNode.eulerAngles.x = -.pi / 2
ARKit continually updates its estimates of each detected plane’s shape and extent. To show the current estimated shape for each plane, this sample app also implements the renderer:didUpdateNode:forAnchor: method, updating the ARSCNPlaneGeometry and SCNPlane objects to reflect the latest information from ARKit.
ARKit不斷更新其對每個檢測到的平面形狀和范圍的估計。為了顯示每個平面的當前估計形狀,這個示例應用程序還實現了renderer:didUpdateNode:forAnchor:方法,更新了ARSCNPlaneGeometry和SCNPlane對象,以反映來自ARKit的最新信息。
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
// Update only anchors and nodes set up by `renderer(_:didAdd:for:)`.
guard let planeAnchor = anchor as? ARPlaneAnchor,
let plane = node.childNodes.first as? Plane
else { return }
// Update ARSCNPlaneGeometry to the anchor's new estimated shape.
if let planeGeometry = plane.meshNode.geometry as? ARSCNPlaneGeometry {
planeGeometry.update(from: planeAnchor.geometry)
}
// Update extent visualization to the anchor's new bounding rectangle.
if let extentGeometry = plane.extentNode.geometry as? SCNPlane {
extentGeometry.width = CGFloat(planeAnchor.extent.x)
extentGeometry.height = CGFloat(planeAnchor.extent.z)
plane.extentNode.simdPosition = planeAnchor.center
}
// Update the plane's classification and the text position
if #available(iOS 12.0, *),
let classificationNode = plane.classificationNode,
let classificationGeometry = classificationNode.geometry as? SCNText {
let currentClassification = planeAnchor.classification.description
if let oldClassification = classificationGeometry.string as? String, oldClassification != currentClassification {
classificationGeometry.string = currentClassification
classificationNode.centerAlign()
}
}
}
在iPhone XS、iPhone XS Max和iPhone XR上,ARKit還可以對檢測到的平面進行分類,報告該平面代表哪種常見的現實世界表面(例如,桌子、地板或墻壁)。在本例中,renderer:didUpdateNode:forAnchor:方法還顯示和更新一個文本標簽來顯示該信息。