ARKit框架下的類有:
- ARAnchor
- ARCamera
- ARConfiguration
- ARError
- ARFaceAnchor
- ARFaceGeomorey
- ARFrame
- ARHitTestResult
- ARLightEstimate
- ARPlaneAnchor
- ARPointCloud
- ARSCNView
- ARSession
- ARSKView
ARAnchor
用于跟蹤真實或虛擬對象相對于相機的位置和方向蘸泻,addAnchor:方法將它們添加到AR會話中俏竞。
在世界跟蹤會話中啟用planeDetection選項時屡穗,ARKit還會自動添加錨點
屬性:
錨點唯一的identifier
@property (nonatomic, readonly) NSUUID *identifier;
在世界坐標中定義錨的旋轉(zhuǎn),平移和縮放的轉(zhuǎn)換矩陣
@property (nonatomic, readonly) matrix_float4x4 transform;
ARKit類(如ARFaceAnchor類)采用此協(xié)議,該類表示場景中的移動對象枣氧。
ARKit自動管理活動AR會話中的這些對象的表示泡态,確保現(xiàn)實世界對象的位置和方向(錨的轉(zhuǎn)換屬性)的變化反映在相應(yīng)的ARKit對象中袱结。
@protocol ARTrackable <NSObject>
指示當前變換對于現(xiàn)實世界對象的移動是否有效亮隙。
@property (nonatomic, readonly) BOOL isTracked;
ARPlaneAnchor
ARPlaneAnchor繼承自ARAnchor
平面校準,是個枚舉垢夹,只有一個值溢吻。這個值與重力平面垂直
@property (nonatomic, readonly) ARPlaneAnchorAlignment alignment;
平面在錨的坐標空間中的中心
@property (nonatomic, readonly) vector_float3 center;
平面在錨的坐標空間中的范圍
@property (nonatomic, readonly) vector_float3 extent;
ARConfiguration
這是一個抽象類,用與配置ARSession,配置的屬性有:
參考坐標系
@property (nonatomic, readwrite) ARWorldAlignment worldAlignment;
這是一個枚舉促王,確定坐標系如何與世界保持一致
默認是ARWorldAlignmentGravity
(0, -1, 0)
還有ARWorldAlignmentGravityAndHeading
和ARWorldAlignmentCamera
其中犀盟,ARWorldAlignmentGravityAndHeading
默認是(0, -1, 0),當North is True
的時候蝇狼,向量是(0, 0, -1)
光預估阅畴。默認是Enabled
@property (nonatomic, readwrite, getter=isLightEstimationEnabled) BOOL lightEstimationEnabled;
是否采集和提供音頻數(shù)據(jù)。默認禁止
@property (nonatomic, readwrite) BOOL providesAudioData;
設(shè)置是否支持配置迅耘,要求芯片是A9,版本是iOS11及以上
@property(class, nonatomic, readonly) BOOL isSupported;
ARConfiguration有三個子類:
ARWorldTrackingConfiguration
所有的AR配置都會建立設(shè)備居住的真實世界與虛擬三維坐標空間之間的對應(yīng)關(guān)系贱枣,我們可以在其中模擬內(nèi)容。當應(yīng)用程序?qū)⒃搩?nèi)容與實時相機圖像一起顯示時颤专,用戶會體驗到虛擬內(nèi)容是真實世界的一部分的錯覺纽哥。
在空間之間創(chuàng)建和維護這種對應(yīng)關(guān)系需要跟蹤設(shè)備的運動。 ARWorldTrackingConfiguration類以六個自由度(6DOF)跟蹤設(shè)備的運動:三個旋轉(zhuǎn)軸(滾動栖秕,俯仰和偏航)以及三個平移軸(在x春塌,y和z中的運動)。
這種跟蹤可以創(chuàng)造出逼真的AR體驗:即使用戶將設(shè)備傾斜到對象的上方或下方簇捍,或者移動設(shè)備以查看對象只壳,虛擬對象也可能與真實世界保持相同的位置物體的側(cè)面和背面。
如果啟用
planeDetection
設(shè)置,ARKit將分析場景以查找真實的平坦表面梯投。對于檢測到的每個平面命辖,ARKit會自動將ARPlaneAnchor對象添加到會話中
AROrientationTrackingConfiguration
AROrientationTrackingConfiguration
類以三個自由度(3DOF)跟蹤設(shè)備的運動:特別是三個旋轉(zhuǎn)軸(滾動,俯仰和偏航)分蓖。
這種基本的運動跟蹤級別可以創(chuàng)建有限的AR體驗:即使用戶將設(shè)備旋轉(zhuǎn)到位于該對象的上方尔艇,下方或旁邊,虛擬對象也可能成為真實世界的一部分么鹤。但是终娃,這種配置不能跟蹤設(shè)備的移動:非平凡地改變設(shè)備的位置會打破AR幻覺,導致虛擬內(nèi)容相對于真實世界出現(xiàn)漂移蒸甜。例如棠耕,用戶不能四處走動以查看虛擬對象的側(cè)面和背面。此外柠新,3DOF跟蹤不支持平面檢測或命中測試窍荧。
蘋果建議:
由于3DOF跟蹤限制了AR體驗,因此通常不應(yīng)直接使用AROrientationTrackingConfiguration
類恨憎。相反蕊退,使用子類ARWorldTrackingConfiguration
進行六自由度(6DOF)跟蹤郊楣,平面檢測和命中測試。使用3DOF跟蹤僅在6DOF跟蹤暫時不可用的情況下作為后備

ARFaceTrackingConfiguration
面部追蹤配置根據(jù)設(shè)備的前置攝像頭來檢測用戶的臉部今瀑。運行此配置時,AR會話將檢測用戶的臉部(如果在前置攝像機圖像中可見)点把,并將錨點列表添加到表示臉部的ARFaceAnchor對象橘荠。每個臉部錨點提供關(guān)于臉部的位置和方向,其拓撲結(jié)構(gòu)和描述臉部表情的特征的信息
注意:
臉部追蹤僅適用于帶前置TrueDepth相機的iOS設(shè)備(iOS Device Compatibility Reference
)愉粤。在向用戶提供需要臉部跟蹤的任何功能之前砾医,使用ARFaceTrackingConfiguration
isSupported
屬性確定當前設(shè)備上是否有臉部跟蹤.
ARFaceTrackingConfiguration
類不提供方法或?qū)傩裕С謴乃?code>ARConfiguration繼承的所有屬性衣厘。另外,當啟lightEstimationEnabled
設(shè)置時压恒,人臉跟蹤配置將使用檢測到的人臉作為光線探測器影暴,并提供對方向或環(huán)境光照的估計
ARError
typedef NS_ERROR_ENUM(ARErrorDomain, ARErrorCode) {
/** Unsupported configuration. */
ARErrorCodeUnsupportedConfiguration = 100,
/** A sensor required to run the session is not available. */
ARErrorCodeSensorUnavailable = 101,
/** A sensor failed to provide the required input. */
ARErrorCodeSensorFailed = 102,
/** App does not have permission to use the camera. The user may change this in settings. */
ARErrorCodeCameraUnauthorized = 103,
/** World tracking has encountered a fatal error. */
ARErrorCodeWorldTrackingFailed = 200,
};
ARFrame
視頻圖像和位置跟蹤信息作為AR會話的一部分被捕獲。
正在運行的AR會話不斷捕捉設(shè)備攝像頭的視頻幀探赫。對于每一幀型宙,ARKit都會分析圖像以及來自設(shè)備運動感應(yīng)硬件的數(shù)據(jù),以估計設(shè)備的實際位置伦吠。 ARKit以ARFrame
對象的形式提供此跟蹤信息和成像參數(shù)
當前幀的時間戳id
@property (nonatomic, readonly) NSTimeInterval timestamp;
當前幀捕獲的圖像
@property (nonatomic, readonly) CVPixelBufferRef capturedImage;
捕捉到的每一幀的depth data【只有在臉部追蹤的時候妆兑,depth data才會捕捉】
@property (nonatomic, strong, readonly, nullable) AVDepthData *capturedDepthData;
depth data的時間戳id
@property (nonatomic, readonly) NSTimeInterval capturedDepthDataTimestamp;
相機提供幀的位置和方向信息
@property (nonatomic, copy, readonly) ARCamera *camera;
場景里一系列的錨點
@property (nonatomic, copy, readonly) NSArray<ARAnchor *> *anchors;
場景中燈光的光線估計值。當沒有時返回nil
@property (nonatomic, strong, nullable, readonly) ARLightEstimate *lightEstimate;
相對于框架原點的場景中的特征點毛仪。僅僅當設(shè)置是world tracking的時候才會提供
@property (nonatomic, strong, nullable, readonly) ARPointCloud *rawFeaturePoints;
其他重要的東東
在幀中搜索與捕獲圖像中的點相對應(yīng)的對象搁嗓。
捕獲圖像的坐標空間中的2D點可以指沿著線段的任何點在三維坐標空間。命中測試是沿著這條線段查找世界上的對象的過程
參數(shù)1:點捕獲圖像的圖像空間坐標系中的一個點箱靴。 值應(yīng)該從(0,0 左上角到(1,1)右下角
參數(shù)2:要搜索的結(jié)果的類型腺逛。
返回值:從最近到最遠排列的所有命中測試結(jié)果的數(shù)組。
- (NSArray<ARHitTestResult *> *)hitTest:(CGPoint)point types:(ARHitTestResultType)types;
其中搜索結(jié)果的類型
typedef NS_OPTIONS(NSUInteger, ARHitTestResultType) {
/** Result type from intersecting the nearest feature point. */
ARHitTestResultTypeFeaturePoint = (1 << 0),
/** Result type from intersecting a horizontal plane estimate, determined for the current frame. */
ARHitTestResultTypeEstimatedHorizontalPlane = (1 << 1),
/** Result type from intersecting with an existing plane anchor. */
ARHitTestResultTypeExistingPlane = (1 << 3),
/** Result type from intersecting with an existing plane anchor, taking into account the plane’s extent. */
ARHitTestResultTypeExistingPlaneUsingExtent = (1 << 4),
} NS_SWIFT_NAME(ARHitTestResult.ResultType);