ARAnchor
@interface ARAnchor : NSObject
/**
?標(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)造方法,一般我們無(wú)需構(gòu)造粱快。因?yàn)樘砑右粋€(gè)3D物體時(shí)ARKit會(huì)有代理告知我們物體的錨點(diǎn)
?*/
- (instancetype)initWithTransform:(matrix_float4x4)transform;
@end
ARError
/作用域恬砂,一般會(huì)表示是哪一個(gè)類出現(xiàn)問(wè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,
};
ARFrame
@interface ARFrame : NSObject
/**
時(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 *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è)是長(zhǎng)方形藐吮,這是一個(gè)二維空間溺拱。而相機(jī)捕捉到的是一個(gè)由這個(gè)二維空間射出去的長(zhǎng)方體,我們點(diǎn)擊屏幕一個(gè)點(diǎn)可以理解為在這個(gè)長(zhǎng)方體的邊緣射出一條線谣辞,這一條線上可能會(huì)有多個(gè)3D物體模型
point:2D坐標(biāo)點(diǎn)(手機(jī)屏幕某一點(diǎn))
ARHitTestResultType:捕捉類型? 點(diǎn)還是面
(NSArray *):追蹤結(jié)果數(shù)組? 詳情見本章節(jié)ARHitTestResult類介紹
?*/
- (NSArray *)hitTest:(CGPoint)point types:(ARHitTestResultType)types;
/**
相機(jī)窗口的的坐標(biāo)變換(可用于相機(jī)橫豎屏的旋轉(zhuǎn)適配)
?*/
- (CGAffineTransform)displayTransformWithViewportSize:(CGSize)viewportSize orientation:(UIInterfaceOrientation)orientation;
@end
ARHitTestResult
//捕捉類型枚舉
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ī)為場(chǎng)景原點(diǎn)的坐標(biāo)迫摔,而每一個(gè)3D物體自身有一個(gè)場(chǎng)景,本地坐標(biāo)就是相對(duì)于這個(gè)場(chǎng)景的坐標(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í)場(chǎng)景中的位置歉闰,是一個(gè)4x4的矩陣
?*/
@property (nonatomic, strong, nullable, readonly) ARAnchor *anchor;
@end
@interface ARLightEstimate : NSObject
/**
燈光強(qiáng)度? 范圍0-2000 默認(rèn)1000
?*/
@property (nonatomic, readonly) CGFloat ambientIntensity;
@end
/**
平地類型辖众,目前只有一個(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
@interface ARPointCloud : NSObject
/**
?點(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
//會(huì)話追蹤配置類
@interface ARSessionConfiguration : NSObject
/**
當(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
ARSCNView
@interface ARSCNView : SCNView
/**
代理
?*/
@property (nonatomic, weak, nullable) id delegate;
/**
AR會(huì)話
?*/
@property (nonatomic, strong) ARSession *session;
/**
場(chǎng)景
?*/
@property(nonatomic, strong) SCNScene *scene;
/**
是否自動(dòng)適應(yīng)燈光
?*/
@property(nonatomic) BOOL automaticallyUpdatesLighting;
/**
返回對(duì)應(yīng)節(jié)點(diǎn)的錨點(diǎn)渊胸,節(jié)點(diǎn)是一個(gè)3D虛擬物體,它的坐標(biāo)是虛擬場(chǎng)景中的坐標(biāo)台妆,而錨點(diǎn)ARAnchor是ARKit中現(xiàn)實(shí)世界的坐標(biāo)翎猛。
?*/
- (nullable ARAnchor *)anchorForNode:(SCNNode *)node;
/**
返回對(duì)應(yīng)錨點(diǎn)的物體
?*/
- (nullable SCNNode *)nodeForAnchor:(ARAnchor *)anchor;
/**
根據(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è)是長(zhǎng)方形疫稿,這是一個(gè)二維空間。而相機(jī)捕捉到的是一個(gè)由這個(gè)二維空間射出去的長(zhǎng)方體鹃两,我們點(diǎn)擊屏幕一個(gè)點(diǎn)可以理解為在這個(gè)長(zhǎng)方體的邊緣射出一條線遗座,這一條線上可能會(huì)有多個(gè)3D物體模型
point:2D坐標(biāo)點(diǎn)(手機(jī)屏幕某一點(diǎn))
ARHitTestResultType:捕捉類型? 點(diǎn)還是面
(NSArray *):追蹤結(jié)果數(shù)組? 詳情見本章節(jié)ARHitTestResult類介紹
數(shù)組的結(jié)果排序是由近到遠(yuǎn)
?*/
- (NSArray *)hitTest:(CGPoint)point types:(ARHitTestResultType)types;
@end
ARSCNViewDelegate
//代理
#pragma mark - ARSCNViewDelegate
//代理的內(nèi)部實(shí)現(xiàn)了SCNSceneRendererDelegate:scenekit代理 和ARSessionObserver:ARSession監(jiān)聽(KVO機(jī)制)
@protocol ARSCNViewDelegate
@optional
/**
自定義節(jié)點(diǎn)的錨點(diǎn)
?*/
- (nullable SCNNode *)renderer:(id )renderer nodeForAnchor:(ARAnchor *)anchor;
/**
當(dāng)添加節(jié)點(diǎn)是會(huì)調(diào)用,我們可以通過(guò)這個(gè)代理方法得知我們添加一個(gè)虛擬物體到AR場(chǎng)景下的錨點(diǎn)(AR現(xiàn)實(shí)世界中的坐標(biāo))
?*/
- (void)renderer:(id )renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor;
/**
將要刷新節(jié)點(diǎn)
?*/
- (void)renderer:(id )renderer willUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor;
/**
?已經(jīng)刷新節(jié)點(diǎn)
?*/
- (void)renderer:(id )renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor;
/**
?移除節(jié)點(diǎn)
?*/
- (void)renderer:(id )renderer didRemoveNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor;
@end
ARSession
@interface ARSession : NSObject
/**
?代理
?*/
@property (nonatomic, weak) id delegate;
/**
指定代理執(zhí)行的線程(主線程不會(huì)有延遲怔毛,子線程會(huì)有延遲)员萍,不指定的話默認(rèn)主線程
?*/
@property (nonatomic, strong, nullable) dispatch_queue_t delegateQueue;
/**
相機(jī)當(dāng)前的位置(是由會(huì)話追蹤配置計(jì)算出來(lái)的)
?*/
@property (nonatomic, copy, nullable, readonly) ARFrame *currentFrame;
/**
?會(huì)話追蹤配置
?*/
@property (nonatomic, copy, nullable, readonly) ARSessionConfiguration *configuration;
/**
運(yùn)行會(huì)話(這行代碼就是開啟AR的關(guān)鍵所在)
?*/
- (void)runWithConfiguration:(ARSessionConfiguration *)configuration NS_SWIFT_UNAVAILABLE("Use run(_:options:)");
/**
運(yùn)行會(huì)話,只是多了一個(gè)參數(shù)ARSessionRunOptions:作用就是會(huì)話斷開重連時(shí)的行為拣度。ARSessionRunOptionResetTracking:表示充值追蹤? ARSessionRunOptionRemoveExistingAnchors:移除現(xiàn)有錨點(diǎn)
?*/
- (void)runWithConfiguration:(ARSessionConfiguration *)configuration options:(ARSessionRunOptions)options NS_SWIFT_NAME(run(_:options:));
/**
暫停會(huì)話
?*/
- (void)pause;
/**
添加錨點(diǎn)
?*/
- (void)addAnchor:(ARAnchor *)anchor NS_SWIFT_NAME(add(anchor:));
/**
移除錨點(diǎn)
?*/
- (void)removeAnchor:(ARAnchor *)anchor NS_SWIFT_NAME(remove(anchor:));
@end
//session代理分類兩部分碎绎,一個(gè)是觀察者(KVO) 一個(gè)是委托者(代理)
#pragma mark - ARSessionObserver
//session KVO觀察者
@protocol ARSessionObserver
@optional
/**
?session失敗
?*/
- (void)session:(ARSession *)session didFailWithError:(NSError *)error;
/**
相機(jī)改變追蹤狀態(tài)
?*/
- (void)session:(ARSession *)session cameraDidChangeTrackingState:(ARCamera *)camera;
/**
?session意外斷開(如果開啟ARSession之后,APP退到后臺(tái)就有可能導(dǎo)致會(huì)話斷開)
?*/
- (void)sessionWasInterrupted:(ARSession *)session;
/**
session會(huì)話斷開恢復(fù)(短時(shí)間退到后臺(tái)再進(jìn)入APP會(huì)自動(dòng)恢復(fù))
?*/
- (void)sessionInterruptionEnded:(ARSession *)session;
@end
#pragma mark - ARSessionDelegate
@protocol ARSessionDelegate
@optional
/**
?更新相機(jī)位置
?*/
- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame;
/**
添加錨點(diǎn)
?*/
- (void)session:(ARSession *)session didAddAnchors:(NSArray*)anchors;
/**
刷新錨點(diǎn)
?*/
- (void)session:(ARSession *)session didUpdateAnchors:(NSArray*)anchors;
/**
移除錨點(diǎn)
?*/
- (void)session:(ARSession *)session didRemoveAnchors:(NSArray*)anchors;
@end
ARCamera
@interface ARCamera : NSObject
/**
?4x4矩陣表示相機(jī)位置抗果,同ARAnchor
?*/
@property (nonatomic, readonly) matrix_float4x4 transform;
/**
相機(jī)方向(旋轉(zhuǎn))的矢量歐拉角
分別是x/y/z
?*/
@property (nonatomic, readonly) vector_float3 eulerAngles;
/**
?相機(jī)追蹤狀態(tài)(在下方會(huì)有枚舉值介紹)
?*/
@property (nonatomic, readonly) ARTrackingState trackingState NS_REFINED_FOR_SWIFT;
/**
追蹤運(yùn)動(dòng)類型
?*/
@property (nonatomic, readonly) ARTrackingStateReason trackingStateReason NS_REFINED_FOR_SWIFT;
/**
相機(jī)曲率(筆者有點(diǎn)費(fèi)解筋帖,反復(fù)揣摩應(yīng)該是與焦距相關(guān)參數(shù))
3x3矩陣
?fx 0? px
?0? fy? py
?0? 0? 1
?*/
@property (nonatomic, readonly) matrix_float3x3 intrinsics;
/**
攝像頭分辨率
?*/
@property (nonatomic, readonly) CGSize imageResolution;
/**
投影矩陣
*/
@property (nonatomic, readonly) matrix_float4x4 projectionMatrix;
/**
創(chuàng)建相機(jī)投影矩陣
?*/
- (matrix_float4x4)projectionMatrixWithViewportSize:(CGSize)viewportSize orientation:(UIInterfaceOrientation)orientation zNear:(CGFloat)zNear zFar:(CGFloat)zFar;
@end
//相機(jī)追蹤狀態(tài)枚舉
typedef NS_ENUM(NSInteger, ARTrackingState) {
? ? /**不被允許 */
? ? ARTrackingStateNotAvailable,
? ? /**最小 */
? ? ARTrackingStateLimited,
? ? /**正常. */
? ? ARTrackingStateNormal,
} NS_REFINED_FOR_SWIFT;
/**
?追蹤運(yùn)動(dòng)類型
?*/
API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(macos, watchos, tvos)
typedef NS_ENUM(NSInteger, ARTrackingStateReason) {
? ? /**無(wú). */
? ? ARTrackingStateReasonNone,
? ? /**運(yùn)動(dòng). */
? ? ARTrackingStateReasonExcessiveMotion,
? ? /**臉部捕捉. */
? ? ARTrackingStateReasonInsufficientFeatures,
} NS_REFINED_FOR_SWIFT;