轉(zhuǎn)載請(qǐng)注明出處:http://www.lxweimin.com/p/3c7ee58d40f4
<h2 id="1.1">1.1-ARKit框架簡介
-
再上一小節(jié)中,相信大家已經(jīng)對(duì)
<ARKit>
框架的使用及原理有了一個(gè)全局的認(rèn)識(shí),為了能夠更加深入的研究學(xué)習(xí)ARKit
技術(shù),所以筆者本小節(jié)主要介紹<ARKit>
框架中所有的API- 本篇主要翻譯自蘋果官方文檔,適當(dāng)加上一些筆者的見解
ARKit框架類圖
<h2 id="1.2">1.2-ARAnchor</h2>
-
ARAnchor表示一個(gè)物體在3D空間的位置和方向(ARAnchor通常稱為物體的3D錨點(diǎn),有點(diǎn)像UIKit框架中CALayer的Anchor)
-
ARFrame表示的也是物體的位置和方向,但是ARFrame通常表示的是AR相機(jī)的位置和方向以及追蹤相機(jī)的時(shí)間,還可以捕捉相機(jī)的幀圖片
- 也就是說ARFrame用于捕捉相機(jī)的移動(dòng),其他虛擬物體用ARAnchor
-
ARFrame表示的也是物體的位置和方向,但是ARFrame通常表示的是AR相機(jī)的位置和方向以及追蹤相機(jī)的時(shí)間,還可以捕捉相機(jī)的幀圖片
@interface ARAnchor : NSObject <NSCopying>
/**
標(biāo)識(shí)符
*/
@property (nonatomic, readonly) NSUUID *identifier;
/**
錨點(diǎn)的旋轉(zhuǎn)變換矩陣,定義了錨點(diǎn)的旋轉(zhuǎn)、位置、縮放。是一個(gè)4x4的矩陣(讀者可以自行科普什么叫4x4矩陣)
*/
@property (nonatomic, readonly) matrix_float4x4 transform;
/**
構(gòu)造方法,一般我們無需構(gòu)造。因?yàn)樘砑右粋€(gè)3D物體時(shí)ARKit會(huì)有代理告知我們物體的錨點(diǎn)
*/
- (instancetype)initWithTransform:(matrix_float4x4)transform;
@end
<h2 id="1.3">1.3-ARCamera</h2>
- *AR相機(jī),該類API較多,并且理解比較困難,將會(huì)在后續(xù)小節(jié)單獨(dú)介紹
- 該類非常重要,且API較多,將在后續(xù)小節(jié)介紹
<h2 id="1.4">1.4-ARError</h2>
- ARError是一個(gè)描述ARKit錯(cuò)誤的類,這個(gè)錯(cuò)誤來源于幾個(gè)方面,例如設(shè)備不支持,或者當(dāng)相機(jī)常駐后臺(tái)時(shí)ARSession會(huì)斷開等問題
//作用域,一般會(huì)表示是哪一個(gè)類出現(xiàn)問題
NSString *const ARErrorDomain;
//錯(cuò)誤碼描述 100:不支持會(huì)話追蹤配置,主線由于A9芯片以下的機(jī)型會(huì)報(bào)錯(cuò) 101:失活狀態(tài) 102:傳感器故障 200:追蹤失敗
typedef NS_ERROR_ENUM(ARErrorDomain, ARErrorCode) {
/** Unsupported session 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,
/** World tracking has encountered a fatal error. */
ARErrorCodeWorldTrackingFailed = 200,
};
<h2 id="1.5">1.5-ARFrame</h2>
- ARFrame主要是追蹤相機(jī)當(dāng)前的狀態(tài),這個(gè)狀態(tài)不僅僅只是位置,還有圖像幀及時(shí)間等參數(shù)
@interface ARFrame : NSObject <NSCopying>
/**
時(shí)間戳.
*/
@property (nonatomic, readonly) NSTimeInterval timestamp;
/**
緩沖區(qū)圖像幀
*/
@property (nonatomic, readonly) CVPixelBufferRef capturedImage;
/**
相機(jī)(表示這個(gè)ARFrame是哪一個(gè)相機(jī)的,iPhone7plus有兩個(gè)攝像機(jī))
*/
@property (nonatomic, copy, readonly) ARCamera *camera;
/**
返回當(dāng)前相機(jī)捕捉到的錨點(diǎn)數(shù)據(jù)(當(dāng)一個(gè)3D虛擬模型加入到ARKit中時(shí),錨點(diǎn)值得就是這個(gè)模型在AR中的位置)
*/
@property (nonatomic, copy, readonly) NSArray<ARAnchor *> *anchors;
/**
燈光,詳情可見本章節(jié)ARLightEstimate類介紹(指的是燈光強(qiáng)度 一般是0-2000,系統(tǒng)默認(rèn)1000)
*/
@property (nonatomic, copy, nullable, readonly) ARLightEstimate *lightEstimate;
/**
特征點(diǎn)(應(yīng)該是捕捉平地或者人臉的,比較蘋果有自帶的人臉識(shí)別功能)
*/
@property (nonatomic, nullable, readonly) ARPointCloud *rawFeaturePoints;
/**
根據(jù)2D坐標(biāo)點(diǎn)搜索3D模型,這個(gè)方法通常用于,當(dāng)我們?cè)谑謾C(jī)屏幕點(diǎn)擊某一個(gè)點(diǎn)的時(shí)候,可以捕捉到這一個(gè)點(diǎn)所在的3D模型的位置,至于為什么是一個(gè)數(shù)組非常好理解。手機(jī)屏幕一個(gè)是長方形,這是一個(gè)二維空間。而相機(jī)捕捉到的是一個(gè)由這個(gè)二維空間射出去的長方體,我們點(diǎn)擊屏幕一個(gè)點(diǎn)可以理解為在這個(gè)長方體的邊緣射出一條線,這一條線上可能會(huì)有多個(gè)3D物體模型
point:2D坐標(biāo)點(diǎn)(手機(jī)屏幕某一點(diǎn))
ARHitTestResultType:捕捉類型 點(diǎn)還是面
(NSArray<ARHitTestResult *> *):追蹤結(jié)果數(shù)組 詳情見本章節(jié)ARHitTestResult類介紹
*/
- (NSArray<ARHitTestResult *> *)hitTest:(CGPoint)point types:(ARHitTestResultType)types;
/**
相機(jī)窗口的的坐標(biāo)變換(可用于相機(jī)橫豎屏的旋轉(zhuǎn)適配)
*/
- (CGAffineTransform)displayTransformWithViewportSize:(CGSize)viewportSize orientation:(UIInterfaceOrientation)orientation;
@end
<h2 id="1.6">1.6-ARHitTestResult</h2>
- ARHitTestResult:點(diǎn)擊回調(diào)結(jié)果,這個(gè)類主要用于虛擬增強(qiáng)現(xiàn)實(shí)技術(shù)(AR技術(shù))中現(xiàn)實(shí)世界與3D場景中虛擬物體的交互。 比如我們?cè)谙鄼C(jī)中移動(dòng)。拖拽3D虛擬物體,都可以通過這個(gè)類來獲取ARKit所捕捉的結(jié)果
//捕捉類型枚舉
typedef NS_OPTIONS(NSUInteger, ARHitTestResultType) {
/** 點(diǎn). */
ARHitTestResultTypeFeaturePoint = (1 << 0),
/** 水平面 y為0. */
ARHitTestResultTypeEstimatedHorizontalPlane = (1 << 1),
/** 已結(jié)存在的平面. */
ARHitTestResultTypeExistingPlane = (1 << 3),
/** 已結(jié)存在的錨點(diǎn)和平面. */
ARHitTestResultTypeExistingPlaneUsingExtent = (1 << 4),
} NS_SWIFT_NAME(ARHitTestResult.ResultType);
/**
捕捉類型
*/
@property (nonatomic, readonly) ARHitTestResultType type;
/**
3D虛擬物體與相機(jī)的距離(單位:米)
*/
@property (nonatomic, readonly) CGFloat distance;
/**
本地坐標(biāo)矩陣(世界坐標(biāo)指的是相機(jī)為場景原點(diǎn)的坐標(biāo),而每一個(gè)3D物體自身有一個(gè)場景,本地坐標(biāo)就是相對(duì)于這個(gè)場景的坐標(biāo))類似于frame和bounds的區(qū)別
*/
@property (nonatomic, readonly) matrix_float4x4 localTransform;
/**
世界坐標(biāo)矩陣
*/
@property (nonatomic, readonly) matrix_float4x4 worldTransform;
/**
錨點(diǎn)(3D虛擬物體,在虛擬世界有一個(gè)位置,這個(gè)位置參數(shù)是SceneKit中的SCNVector3:三維矢量),而錨點(diǎn)anchor是這個(gè)物體在AR現(xiàn)實(shí)場景中的位置,是一個(gè)4x4的矩陣
*/
@property (nonatomic, strong, nullable, readonly) ARAnchor *anchor;
@end
<h2 id="1.7">1.7-ARLightEstimate</h2>
- ARLightEstimate是一個(gè)燈光效果,它可以讓你的AR場景看起來更加的好
@interface ARLightEstimate : NSObject <NSCopying>
/**
燈光強(qiáng)度 范圍0-2000 默認(rèn)1000
*/
@property (nonatomic, readonly) CGFloat ambientIntensity;
@end
<h2 id="1.8">1.8-ARPlaneAnchor</h2>
-
ARPlaneAnchor是ARAnchor的子類,筆者稱之為平地錨點(diǎn)。ARKit能夠自動(dòng)識(shí)別平地,并且會(huì)默認(rèn)添加一個(gè)錨點(diǎn)到場景中,當(dāng)然要想看到真實(shí)世界中的平地效果,需要我們自己使用SCNNode來渲染這個(gè)錨點(diǎn)
- 錨點(diǎn)只是一個(gè)位置
/**
平地類型,目前只有一個(gè),就是水平面
*/
@property (nonatomic, readonly) ARPlaneAnchorAlignment alignment;
/**
3軸矢量結(jié)構(gòu)體,表示平地的中心點(diǎn) x/y/z
*/
@property (nonatomic, readonly) vector_float3 center;
/**
3軸矢量結(jié)構(gòu)體,表示平地的大小(寬度和高度) x/y/z
*/
@property (nonatomic, readonly) vector_float3 extent;
@end
<h2 id="1.9">1.9-ARPointCloud</h2>
- ARPointCloud:點(diǎn)狀渲染云,主要用于渲染場景
@interface ARPointCloud : NSObject <NSCopying>
/**
點(diǎn)的數(shù)量
*/
@property (nonatomic, readonly) NSUInteger count;
/**
每一個(gè)點(diǎn)的位置的集合(結(jié)構(gòu)體帶*表示的是結(jié)構(gòu)體數(shù)組)
*/
@property (nonatomic, readonly) const vector_float3 *points;
@end
<h2 id="1.10">1.10-ARSCNView</h2>
-
AR視圖,在第一小節(jié)筆者介紹過,ARKit支持3D的AR場景和2D的AR場景,ARSCNView是3D的AR場景視圖
- 該類非常重要,且API較多,將在后續(xù)小節(jié)介紹
該類是整個(gè)ARKit框架中唯一兩個(gè)有代理的類其中之一
<h2 id="1.11">1.11-ARSession</h2>
-
AR會(huì)話,它的作用已經(jīng)在前面小節(jié)中介紹,這里不再累述
- 該類非常重要,且API較多,將在后續(xù)小節(jié)介紹
該類是整個(gè)ARKit框架中唯一兩個(gè)有代理的類其中之一
<h2 id="1.12">1.12-ARSessionConfiguration</h2>
-
ARSessionConfiguration會(huì)話追蹤配置,主要就是追蹤相機(jī)的配置
- 注意:該類還有一個(gè)子類:ARWorldTrackingSessionConfiguration,它們?cè)谕粋€(gè)API文件中
//會(huì)話追蹤配置類
@interface ARSessionConfiguration : NSObject <NSCopying>
/**
當(dāng)前設(shè)備是否支持,一般A9芯片以下設(shè)備不支持
*/
@property(class, nonatomic, readonly) BOOL isSupported;
/**
會(huì)話的對(duì)其方式,這里的對(duì)其指的是3D世界的坐標(biāo)。枚舉值見下方
*/
@property (nonatomic, readwrite) ARWorldAlignment worldAlignment;
/**
是否需要自適應(yīng)燈光效果,默認(rèn)是YES
*/
@property (nonatomic, readwrite, getter=isLightEstimationEnabled) BOOL lightEstimationEnabled;
@end
//世界會(huì)話追蹤配置,蘋果建議我們使用這個(gè)類,這個(gè)子類只有一個(gè)屬性,也就是可以幫助我們追蹤相機(jī)捕捉到的平地
@interface ARWorldTrackingSessionConfiguration : ARSessionConfiguration
/**
偵查類型。枚舉值見下方(默認(rèn)偵查平地)
*/
@property (nonatomic, readwrite) ARPlaneDetection planeDetection;
@end
//追蹤對(duì)其方式,這個(gè)決定了會(huì)話的參考坐標(biāo)系(參照物)
typedef NS_ENUM(NSInteger, ARWorldAlignment) {
/** 相機(jī)位置 vector (0, -1, 0) */
ARWorldAlignmentGravity,
/** 相機(jī)位置及方向. vector (0, -1, 0)
heading :(0, 0, -1) */
ARWorldAlignmentGravityAndHeading,
/** 相機(jī)方向. */
ARWorldAlignmentCamera
} NS_SWIFT_NAME(ARSessionConfiguration.WorldAlignment);
/**
偵查類型
/
API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos)
typedef NS_OPTIONS(NSUInteger, ARPlaneDetection) {
/* 不偵查. */
ARPlaneDetectionNone = 0,
/** 平地偵查 */
ARPlaneDetectionHorizontal = (1 << 0),
} NS_SWIFT_NAME(ARWorldTrackingSessionConfiguration.PlaneDetection);
<h2 id="1.13">1.13-ARSKView</h2>
- ARSKView也是AR視圖,只不過他是2D的,由于2D比3D簡單很多,并且ARSKView基本與ARSCNView類似,所以這里不做重復(fù)介紹。詳細(xì)內(nèi)容可參考ARSCNView