创建运行AR会话的应用程序,并使用平面检测使用SceneKit放置3D内容。
软件开发工具包
- iOS 11.3+
- Xcode 10.0+ Beta
概观
此示例应用程序运行ARKit世界跟踪会话,其内容显示在SceneKit视图中。为了演示平面检测,应用程序只需放置一个SCNPlane
对象来可视化每个检测到ARPlaneAnchor
对象。
配置并运行AR会话
ARSCNView
类是包括一个SceneKit视图ARSession
管理创建的增强现实(AR)的经验所需要的运动跟踪和图像处理对象。但是,要运行会话,您必须提供会话配置。
ARWorldTrackingConfiguration
提供高精度的运动跟踪,并支持您将虚拟内容与真实世界的表面相关联的功能。要启动AR会话,请使用所需的选项(例如平面检测)创建会话配置对象,然后在ARSCNView
实例的session
对象上调用该方法:run(_:options:)
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]
sceneView.session.run(configuration)
仅当将显示它的视图显示在屏幕上时才运行会话。
重要
如果您的应用需要ARKit作为其核心功能,请使用应用文件部分UIRequiredDeviceCapabilities中的
arkit
键,使您的应用仅在支持ARKit的设备上可用。如果AR是您应用的辅助功能,请使用该属性确定是否提供isSupported
基于AR的功能。
为检测到的平面放置3D内容
设置AR会话后,可以使用SceneKit在视图中放置虚拟内容。
启用平面检测后,ARKit会为每个检测到的平面添加和更新锚点。默认情况下,ARSCNView
类为每个锚点添加一个SCNNode
对象到SceneKit场景。您的视图的委托可以实现向场景添加内容renderer(_:didAdd:for:)
方法。当您将内容添加为与锚对应的节点的子节点时,类会自动移动该内容,因为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提供两种跟踪估计平面区域的方法。平面锚点geometry
描述了一个凸多边形,紧密包围了ARKit当前估计属于同一平面的所有点(使用ARSCNPlaneGeometry
时很容易可视化)。ARKit还在平面锚点中提供了更简单的extent
和center
估计,并且它们一起描述了矩形边界(使用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不断更新其对每个检测到的平面形状和范围的估计。为了显示每个平面的当前估计形状,此示例应用程序还实现了renderer(_:didUpdate:for:)
方法,更新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
}
}
也可以看看
世界追踪
发现支持概念,功能和最佳实践,以构建出色的AR体验。
class ARWorldTrackingConfiguration
使用后置摄像头的配置,跟踪设备的方向和位置,并检测真实世界的表面,以及已知的图像或对象。
有关在世界跟踪AR会话中检测到的真实世界平面的位置和方向的信息。
class AREnvironmentProbeAnchor
在世界跟踪AR会话中为特定空间区域提供环境照明信息的对象。
原文:https://developer.apple.com/documentation/arkit/building_your_first_ar_experience