ARKit框架详细解析(三)—— 开启你的第一个AR体验之旅

版本记录

版本号 时间
V1.0 2017.09.27

前言

苹果最近新出的一个API就是ARKit,是在2017年6月6日,苹果发布iOS11系统所新增框架,它能够帮助我们以最简单快捷的方式实现AR技术功能。接下来几篇我们就详细的对ARKit框架进行详细的解析。AR相关代码已经上传到Github - 刀客传奇AR相关和Building-Your-First-AR-Experience,感兴趣的可以看上面几篇。
1. ARKit框架详细解析(一)—— 基本概览
2. ARKit框架详细解析(二)—— 关于增强现实和ARKit

Overview - 概览

上面GitHub中的工程示例,此示例应用程序运行ARKit世界跟踪会话,并显示在SceneKit视图中显示的内容。 为了演示平面检测,应用程序只需放置一个SCNPlane对象来可视化每个检测到的ARPlaneAnchor 对象。


Configure and Run the AR Session - 配置和运行AR会话

ARSCNView类是一个SceneKit视图,它包含一个ARSession 对象,用于管理创建增强现实(AR)体验所需的运动跟踪和图像处理。 但是,要运行会话,您必须提供会话配置。

ARKit框架详细解析(三)—— 开启你的第一个AR体验之旅_第1张图片

ARWorldTrackingConfiguration类提供高精度的运动跟踪功能,可以帮助您将虚拟内容放置在真实世界的表面上。 要启动AR会话,请使用所需的选项(如平面检测)创建会话配置对象,然后在ARSCNView实例的会话对象上调用runWithConfiguration:options:方法,具体如下所示:

let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal
sceneView.session.run(configuration)

只有当屏幕上显示的视图才会运行会话。

重要提示:如果您的应用程序需要ARKit的核心功能,请使用应用程序的Info.plist文件部分中的arkit键使您的应用程序仅在支持ARKit的设备上可用。 如果AR是应用程序的辅助功能,请使用isSupported 属性来确定是否提供基于AR的功能。


在检测到的平面中放置3D内容

设置AR会话后,可以使用SceneKit在视图中放置虚拟内容。

当启用平面检测时,ARKit会为每个检测到的平面添加和更新锚点。 默认情况下,ARSCNView类为每个锚点的SceneKit场景添加一个 SCNNode对象。 您的视图的委托可以实现渲染器:renderer:didAddNode:forAnchor:方法来向场景添加内容。

相关代码如下所示。

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 SceneKit plane to visualize the plane anchor using its position and extent.
       let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))
       let planeNode = SCNNode(geometry: plane)
       planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
       
       /*
        `SCNPlane` is vertically oriented in its local coordinate space, so
        rotate the plane to match the horizontal orientation of `ARPlaneAnchor`.
       */
       planeNode.eulerAngles.x = -.pi / 2
       
       // Make the plane visualization semitransparent to clearly show real-world placement.
       planeNode.opacity = 0.25
       
       /*
        Add the plane visualization to the ARKit-managed node so that it tracks
        changes in the plane anchor as plane estimation continues.
       */
       node.addChildNode(planeNode)
}

如果将内容添加为与锚点对应的节点的子节点,则ARSCNView类将自动移动该内容,因为ARKit会优化其对平面位置和范围的估计。 为了显示估计平面的完整范围,此示例应用程序还实现了renderer:didUpdateNode:forAnchor: 方法,更新SCNPlane 对象的大小以反映ARKit提供的估计值,下面看一下示例代码。

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
    // Update content only for plane anchors and nodes matching the setup created in `renderer(_:didAdd:for:)`.
    guard let planeAnchor = anchor as?  ARPlaneAnchor,
        let planeNode = node.childNodes.first,
        let plane = planeNode.geometry as? SCNPlane
        else { return }
    
    // Plane estimation may shift the center of a plane relative to its anchor's transform.
    planeNode.simdPosition = float3(planeAnchor.center.x, 0, planeAnchor.center.z)
    
    /*
     Plane estimation may extend the size of the plane, or combine previously detected
     planes into a larger one. In the latter case, `ARSCNView` automatically deletes the
     corresponding node for one plane, then calls this method to update the size of
     the remaining plane.
    */
    plane.width = CGFloat(planeAnchor.extent.x)
    plane.height = CGFloat(planeAnchor.extent.z)
}

后记

未完,待续~~~

ARKit框架详细解析(三)—— 开启你的第一个AR体验之旅_第2张图片

你可能感兴趣的:(ARKit框架详细解析(三)—— 开启你的第一个AR体验之旅)