
ARKit demo傳送門
持續(xù)優(yōu)化更新中...
ARKit 簡介
增強現(xiàn)實技術(shù)(Augmented Reality,簡稱 AR),是一種實時地計算攝影機影像的位置及角度并加上相應(yīng)圖像、視頻、3D模型的技術(shù),這種技術(shù)的目標(biāo)是在屏幕上把虛擬世界套在現(xiàn)實世界并進行互動。
- ARKit框架提供了二種AR技術(shù),分別基于三3D場景的SceneKit,以及基于2D場景的SpriktKit。
- 開發(fā)環(huán)境關(guān)鍵詞:A9 和 iOS 11。
ARKit的工作原理
簡單說,ARKit就是攝像頭拍攝的2D世界轉(zhuǎn)化成3D世界,并加入虛擬的3D模型。并由SceneKit(或者其他兩種框架)呈現(xiàn)出來(本文以SceneKit為例)。
- ARKit負(fù)責(zé):1.相機捕捉現(xiàn)實世界圖像 2.并計算出對應(yīng)的虛擬3D模型。
- SceneKit負(fù)責(zé):呈現(xiàn)3D世界,并添加3D模型。
ARKit框架
ARSession
An ARSession object coordinates the major processes that ARKit performs on your behalf to create an augmented reality experience. These processes include reading data from the device's motion sensing hardware, controlling the device's built-in camera, and performing image analysis on captured camera images.
ARSession主要用于協(xié)調(diào)在構(gòu)建增強現(xiàn)實體驗的過程中各個關(guān)鍵步驟,如讀取傳感器數(shù)據(jù),控制相機,分析圖片等。
ARConfiguration
ARWorldTrackingConfiguration:跟蹤設(shè)備的位置、方向、平面檢測和hit testing。
AROrientationTrackingConfiguration:僅用于方向跟蹤。
ARFaceTrackingConfiguration:前置攝像頭僅用于人臉跟蹤。
ARSCNView
The ARSCNView class provides the easiest way to create augmented reality experiences that blend virtual 3D content with a device camera view of the real world. When you run the view's provided ARSession object.
ARSCNView本身包含了一個ARSession對象,二者相互協(xié)作,最終呈現(xiàn)出3D虛擬世界。簡單理解,ARSCNView的主要作用就是呈現(xiàn)三維世界。
ARFrame
A running AR session continuously captures video frames from the device camera. For each frame, ARKit analyzes the image together with data from the device's motion sensing hardware to estimate the device's real-world position. ARKit delivers this tracking information and imaging parameters in the form of an ARFrame object.
配合ARSession使用(currentFrame屬性),ARSession不停的獲取視頻幀信息,并結(jié)合底層的傳感器信息,將處理好的跟蹤信息和圖像參數(shù)放到這個類中。
ARCamera
Information about the camera position and imaging characteristics for a captured video frame in an AR session.>配置ARFrame使用,存放攝像頭的位置等相關(guān)信息。
ARPlaneAnchor
To track the positions and orientations of real or virtual objects relative to the camera.ARKit also automatically adds anchors when you enable the planeDetection option in a world tracking session.
ARPlaneAnchor是ARAnchor的子類,筆者稱之為平地錨點。ARKit能夠自動識別平地,并且會默認(rèn)添加一個錨點到場景中,當(dāng)然要想看到真實世界中的平地效果,需要我們自己使用SCNNode來渲染這個錨點。
ARHitTestResult
Information about a real-world surface found by examining a point in the device camera view of an AR session.
ARHitTestResult:點擊回調(diào)結(jié)果,這個類主要用于虛擬增強現(xiàn)實技術(shù)(AR技術(shù))中現(xiàn)實世界與3D場景中虛擬物體的交互。 比如我們在相機中移動。拖拽3D虛擬物體,都可以通過這個類來獲取ARKit所捕捉的結(jié)果。由ARSCNView的hitTest: type:方法返回。
其他
1.還有ARLightEstimate、ARPlaneAnchor、ARError等,這里不做一一介紹,在實際項目中都會涉及。
2.在實際的開發(fā)中使用SceneKit的API應(yīng)用非常多,實現(xiàn)復(fù)雜、絢麗的AR效果熟練的掌握SceneKit也是必不可少的。您可以看這里SceneKit。
關(guān)于SceneKit的細(xì)節(jié),可以閱讀下面的文章:
Apple官方文檔
Objc中國 SceneKit>Raywenderlich SceneKit
3D with SceneKit
Introduction To SceneKit
ARKit類關(guān)系圖

AR相關(guān)常用SceneKit類介紹
SCNView
SCNView:A view for displaying 3D SceneKit content.
ARSCNView繼承于這個view,呈現(xiàn)3D世界。
SCNScene
An SCNScene object represents a three-dimensional scene and its contents.
SCNView中一個屬性scene正是這個類,SCNView提供對3D場景的控制能力,而這個Scene才是真正的3D場景。
SCNNode
A structural element of a scene graph, representing a position and transform in a 3D coordinate space, to which you can attach geometry, lights, cameras, or other displayable content.
每個Scene都有一個根節(jié)點(rootNode),接下來對于場景添加各種3D模式都是通過這個SCNNode進行的添加,相當(dāng)于UIView添加subview。節(jié)點代表一個個三維世界的實體。可以添加幾何約束、追光、相機等。
SCNHitTestResult
Detailed information about a result from searching for elements of a scene located at a specified point, or along a specified line segment (or ray).>類似于ARHitTestResult,但是二者沒有集成關(guān)系,但作用是一樣的,SCNHitTestResult是由SCNView 的
hitTest: options:返回的。
SCNLight
A light source that can be attached to a node to illuminate the scene.>光照是SceneKit中非常重要的屬性,給SCNNode添加光照,調(diào)整視覺感。(4種光照效果)
SCNVector3和SCNVector4
三維和四維的坐標(biāo),可以用于表示位置、進行矩陣變換等。
SceneKit結(jié)構(gòu)圖
SceneKit結(jié)構(gòu)圖.jpeg
