轉載請注明出處
Apple原文地址: https://developer.apple.com/documentation/arkit/building_a_basic_ar_experience
Building a Basic AR Experience 構建基本的 AR 場景
Configure an AR session and use SceneKit or SpriteKit to display AR content.
配置 AR Session,然后使用 SceneKit 或者 SpriteKit 來展示 AR 內容。
Overview
- When you use the ARSCNView
or ARSKView
class, ARKit automatically manages the basic requirements for creating an AR experience: Each view displays a live camera image as its backdrop and renders the 2D or 3D overlay content you provide to create the illusion of that content inhabiting the real world. To use one of these view classes, you configure it for the kind of AR experience you want to create, and choose positions and representations for your overlay content.
- 如果您使用了 ARSCNView或者 ARSKView類的話,那么 ARKit 就可自行滿足創建 AR 場景的基本要求:即每個視圖的背景用實時的相機圖像來展示,然后還可以渲染您提供的 2D 或者 3D 覆蓋物 (overlay),從而構建出「這些覆蓋物實際上是位于現實世界中」這樣一種錯覺。要使用這些視圖類的話,您可以根據您所想要創建的 AR 場景類型來進行配置,然后為覆蓋物選定位置和表示方式。
- To build your own view for an AR experience instead, see Displaying an AR Experience with Metal.
- 如果您需要構建自定義的視圖來展示 AR 場景的話,請參閱使用 Metal 來展示 AR 場景一節。
Note / 注意
- This article covers code found in Xcode project templates. For complete example code, create a new iOS application with the Augmented Reality template, and choose SceneKit or SpriteKit from the Content Technology popup menu.
- 本文所涉及的代碼均可以在 Xcode 項目模板當中找到。如果要獲取完整的示例代碼,請使用 “Augmented Reality” 模板來創建一個新的 iOS 應用,然后在彈出的 Content Technology 菜單當中選擇
SceneKit
或者SpriteKit
。
Configure and Run the AR Session / 配置 AR Session 并運行
- ARSCNView和 ARSKView類都是包含在 ARSession當中的,而 ARSession對象則用來管理設備動作追蹤和進行圖像處理的,也就是創建 AR 場景的必需品。但是,要運行 Session,您首先必須選擇一種 Session 配置。
The type of configuration object you choose determines the style and quality of AR experiences you can create:
您所選擇的配置對象的類型,決定了您所創建的 AR 場景的風格和質量:
- On iOS devices with an A9 processor or later, the ARWorldTrackingSessionConfiguration
subclass provides high-precision motion tracking and enables features to help you place virtual content in relation to real-world surfaces. - 在具備 A9 或者更高版本處理器的 iOS 設備當中,ARWorldTrackingSessionConfiguration子類提供了高精度的設備動作追蹤功能,可以幫助您將虛擬內容「放置」在現實世界中的某個表面上。
- On other devices supported by ARKit, the ARSessionConfiguration
base class provides basic motion tracking that permits slightly less immersive AR experiences. - 在 ARKit 所支持的其他設備當中,ARSessionConfiguration這個基礎類則提供了基本的動作追蹤功能,可以提供略弱的沉浸式 AR 體驗。
To start an AR session, create a session configuration object with the options you want, then call the run(_:options:)
method on the session
object of your ARSCNView
or ARSKView
instance:
要啟動 AR Session,那么首先要使用您所需要的配置,來創建 Session 配置對象,然后對 ARSCNView或者 ARSKView實例中的 session對象調用 run(_:options:)**方法:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingSessionConfiguration()
configuration.planeDetection = .horizontal
// Run the view's session
sceneView.session.run(configuration)
}
Important 重要
Run your session only when the view that will display it is onscreen.
只有當視圖即將展示在屏幕的時候,才能夠運行視圖 Session。
After you’ve set up your AR session, use SceneKit or SpriteKit to place virtual content in the view.
當您配置完 AR Session 之后,就可以使用 SceneKit 或者 SpriteKit ,來將虛擬內容放置到視圖當中了。