ARKit文档翻译 -- World Tracking(1)

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)体验所需的运动跟踪和图像处理。但是,要运行会话,必须提供会话配置。

645fa6d5-2d0c-4a8b-9c7a-3cf10c0d780b.png

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:方法还显示和更新一个文本标签来显示该信息。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 193,968评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,682评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,254评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,074评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,964评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,055评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,484评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,170评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,433评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,512评论 2 308
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,296评论 1 325
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,184评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,545评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,150评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,437评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,630评论 2 335

推荐阅读更多精彩内容