ARKit API

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;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市冤馏,隨后出現(xiàn)的幾起案子日麸,更是在濱河造成了極大的恐慌,老刑警劉巖逮光,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件代箭,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡涕刚,警方通過(guò)查閱死者的電腦和手機(jī)嗡综,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)杜漠,“玉大人极景,你說(shuō)我怎么就攤上這事察净。” “怎么了盼樟?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵氢卡,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我晨缴,道長(zhǎng)译秦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任喜庞,我火速辦了婚禮诀浪,結(jié)果婚禮上棋返,老公的妹妹穿的比我還像新娘延都。我一直安慰自己,他們只是感情好睛竣,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布晰房。 她就那樣靜靜地躺著,像睡著了一般射沟。 火紅的嫁衣襯著肌膚如雪殊者。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天验夯,我揣著相機(jī)與錄音猖吴,去河邊找鬼。 笑死挥转,一個(gè)胖子當(dāng)著我的面吹牛海蔽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绑谣,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼党窜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了借宵?” 一聲冷哼從身側(cè)響起幌衣,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎壤玫,沒想到半個(gè)月后豁护,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡欲间,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年楚里,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片括改。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡腻豌,死狀恐怖家坎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吝梅,我是刑警寧澤虱疏,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布炸茧,位于F島的核電站肪虎,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏芯勘。R本人自食惡果不足惜右冻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一装蓬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧纱扭,春花似錦牍帚、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至肃叶,卻和暖如春蹂随,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背因惭。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工岳锁, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蹦魔。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓激率,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親版姑。 傳聞我的和親對(duì)象是個(gè)殘疾皇子柱搜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容