ARKit從入門到精通(1)-ARKit初體驗
轉載請標注出處:http://www.reibang.com/p/c97b230fa391
該系列文章共十篇酱床,筆者將由易到難循序漸進的介紹ARKit開發(fā)
-
廢話不多說果元,先看效果
- 桌子上的綠蘿太孤獨了扩借,給它來一個郁金香陪伴一下吧~
- 在椅子上擺瓶花吧~
- 飛機跟著攝像頭移動
- 臺燈圍繞著攝像機旋轉
1.1-AR技術簡介
增強現實技術(Augmented Reality,簡稱 AR)碌奉,是一種實時地計算攝影機影像的位置及角度并加上相應圖像、視頻寒砖、3D模型的技術赐劣,這種技術的目標是在屏幕上把虛擬世界套在現實世界并進行互動。
-
一個最簡單地AR場景實現所需要的技術以及步驟包含如下
- 1.多媒體捕捉現實圖像:如攝像頭
- 2.三維建模:3D立體模型
- 3.傳感器追蹤:主要追蹤現實世界動態(tài)物體的六軸變化哩都,這六軸分別是X魁兼、Y、Z軸位移及旋轉茅逮。其中位移三軸決定物體的方位和大小璃赡,旋轉三周決定物體顯示的區(qū)域。
- 4.坐標識別及轉換:3D模型顯示在現實圖像中不是單純的
frame
坐標點献雅,而是一個三維的矩陣坐標碉考。這基本上也是學習AR最難的部分,好在ARKit
幫助我們大大簡化了這一過程挺身。 - 4.除此之外侯谁,AR還可以與虛擬物體進行一些交互。
1.2-ARKit概述及特點介紹
1.ARKit是2017年6月6日章钾,蘋果發(fā)布iOS11系統(tǒng)所新增框架,它能夠幫助我們以最簡單快捷的方式實現AR技術功能墙贱。
-
2.ARKit框架提供了兩種AR技術,一種是基于3D場景(SceneKit)實現的增強現實贱傀,一種是基于2D場景(SpriktKit)實現的增強現實
- 一般主流都是基于3D實現AR技術惨撇,ARKit不僅支持3D游戲引擎
SceneKit
還支持2D游戲引擎SpriktKit
,這一點出乎筆者意料之外
- 一般主流都是基于3D實現AR技術惨撇,ARKit不僅支持3D游戲引擎
-
3.要想顯示AR效果府寒,必須要依賴于蘋果的游戲引擎框架(3D引擎SceneKit魁衙,2D引擎SpriktKit)报腔,主要原因是游戲引擎才可以加載物體模型。
- 雖然ARKit框架中視圖對象繼承于
UIView
剖淀,但是由于目前ARKit框架本身只包含相機追蹤纯蛾,不能直接加載物體模型,所以只能依賴于游戲引擎加載ARKit
- 雖然ARKit框架中視圖對象繼承于
4.誤區(qū)解讀:ARKit雖然是iOS11新出的框架纵隔,但并不是所有的iOS11系統(tǒng)都可以使用翻诉,而是必須要是處理器A9及以上才能夠使用,蘋果從iPhone6s開始使用A9處理器捌刮,也就是iPhone6及以前的機型無法使用ARKit
-
5.開發(fā)環(huán)境介紹
- 1.Xcode版本:Xcode9及以上
- 2.iOS系統(tǒng):iOS11及以上
- 3.iOS設備:處理器A9及以上(6S機型及以上)
- 4.MacOS系統(tǒng):10.12.4及以上(安裝Xcode9對Mac系統(tǒng)版本有要求)
- 目前只有Bete版本碰煌,鏈接地址:https://developer.apple.com/download/
1.3-ARKit初體驗之3D效果
- 1.打開Xcode9bete版本,新建一個工程绅作,選擇
Augmented Reality APP
(Xcode9新增),點擊next
- 2.在包含技術選項中選擇SceneKit
- 3.此時,Xcode會自動為我們生成一段極其簡潔的AR代碼
- 本小節(jié)主要體驗一下系統(tǒng)的AR效果拄查,代碼的具體含義以及ARKit框架的架構及具體使用將會在后期介紹
#import "ViewController.h"
@interface ViewController () <ARSCNViewDelegate>
//ARKit框架中用于3D顯示的預覽視圖
@property (nonatomic, strong) IBOutlet ARSCNView *sceneView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Set the view's delegate
//設置代理
self.sceneView.delegate = self;
// Show statistics such as fps and timing information
//ARKit統(tǒng)計信息
self.sceneView.showsStatistics = YES;
// Create a new scene
//使用模型創(chuàng)建節(jié)點(scn格式文件是一個基于3D建模的文件,使用3DMax軟件可以創(chuàng)建棚蓄,這里系統(tǒng)有一個默認的3D飛機)
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
// Set the scene to the view
//設置ARKit的場景為SceneKit的當前場景(SCNScene是Scenekit中的場景,類似于UIView)
self.sceneView.scene = scene;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Create a session configuration
//創(chuàng)建一個追蹤設備配置(ARWorldTrackingSessionConfiguration主要負責傳感器追蹤手機的移動和旋轉)
ARWorldTrackingSessionConfiguration *configuration = [ARWorldTrackingSessionConfiguration new];
// Run the view's session
// 開始啟動ARSession會話(啟動AR)
[self.sceneView.session runWithConfiguration:configuration];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Pause the view's session
// 暫停ARSession會話
[self.sceneView.session pause];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-
效果演示
- 3D效果AR特點:1.飛機能夠隨著攝像頭位置的變化而看到不同的部位(六軸) 2.飛機能夠隨著攝像頭的遠近進行縮放
1.2-ARKit初體驗之2D效果
示例效果是點擊屏幕碍脏,在中心點生成一個2D AR圖像
2D效果的AR與3D效果有一點的區(qū)別
1.使用步驟與3D基本類似梭依,在創(chuàng)建Xcode的時候選擇
SpriteKit
引擎
- 2.此時Xcode會為我們生成簡潔的2D地圖加載場景
- 完整代碼
#import "ViewController.h"
#import "Scene.h"
@interface ViewController () <ARSKViewDelegate>
//ARSKView是ARKit框架中負責展示2D AR的預覽視圖
@property (nonatomic, strong) IBOutlet ARSKView *sceneView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Set the view's delegate
//設置場景視圖代理
self.sceneView.delegate = self;
// Show statistics such as fps and node count
//顯示幀率
self.sceneView.showsFPS = YES;
//顯示界面節(jié)點(游戲開發(fā)中,一個角色對應一個節(jié)點)
self.sceneView.showsNodeCount = YES;
// Load the SKScene from 'Scene.sks'
//加載2D場景(2D是平面的)
Scene *scene = (Scene *)[SKScene nodeWithFileNamed:@"Scene"];
// Present the scene
//AR預覽視圖展現場景(這一點與3D視圖加載有區(qū)別)
[self.sceneView presentScene:scene];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Create a session configuration
//創(chuàng)建設備追蹤設置
ARWorldTrackingSessionConfiguration *configuration = [ARWorldTrackingSessionConfiguration new];
// Run the view's session
//開始啟動AR
[self.sceneView.session runWithConfiguration:configuration];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Pause the view's session
[self.sceneView.session pause];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - ARSKViewDelegate
//點擊界面會調用典尾,類似于touch begin方法 anchor是2D坐標的瞄點
- (SKNode *)view:(ARSKView *)view nodeForAnchor:(ARAnchor *)anchor {
// Create and configure a node for the anchor added to the view's session.
//創(chuàng)建節(jié)點(節(jié)點可以理解為AR將要展示的2D圖像)
SKLabelNode *labelNode = [SKLabelNode labelNodeWithText:@"???"];
//顯示在屏幕中心
labelNode.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
labelNode.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
return labelNode;
}
- (void)session:(ARSession *)session didFailWithError:(NSError *)error {
// Present an error message to the user
}
- (void)sessionWasInterrupted:(ARSession *)session {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
- (void)sessionInterruptionEnded:(ARSession *)session {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
@end
-
效果預覽
- 2D ARKit支持加載emoji表情符號圖片
- 2D 只有3軸(X/Y移動役拴,Z軸放大和縮小)钾埂,旋轉的情況下不會有效果(因為2D的圖像只是一個平面圖形)
- 下一小節(jié)將會ARKit架構及API介紹:[ARKit從入門到精通(2)-ARKit工作原理及流程介紹]http://www.reibang.com/p/0492c7122d2f