轉載請注明出處:ARKit從入門到精通(3)-ARKit自定義實現
1.1-創建一個簡單的工程 1.上一小節中介紹過,ARSCNView是UIView的子類的子類,所以從理論上來說,我們應用框架UIKit是可以加載AR場景的 0401.png
2.給界面添加一個按鈕開啟AR之旅,創建一個ARSCNViewController:繼承于UIViewController,點擊按鈕跳轉到自定義ARSCNViewController 0402.png
1.2-搭建ARKit工作環境 #import "ARSCNViewViewController.h"
#import <SceneKit/SceneKit.h>
#import <ARKit/ARKit.h> @interface ARSCNViewViewController ()
@property (
nonatomic ,
strong )ARSCNView *arSCNView;
@property (
nonatomic ,
strong )ARSession *arSession;
@property (
nonatomic ,
strong )ARSessionConfiguration *arSessionConfiguration;
@property (
nonatomic ,
strong )
SCNNode *planeNode;
@end #pragma mark -搭建ARKit環境
- (ARSessionConfiguration *)arSessionConfiguration
{
if (
_arSessionConfiguration !=
nil ) {return
_arSessionConfiguration ;}ARWorldTrackingSessionConfiguration *configuration = [[ARWorldTrackingSessionConfiguration alloc] init];configuration.planeDetection = ARPlaneDetectionHorizontal;
_arSessionConfiguration = configuration;
_arSessionConfiguration .lightEstimationEnabled = YES;return
_arSessionConfiguration ;}
- (ARSession *)arSession
{
if (
_arSession !=
nil ){return
_arSession ;}
_arSession = [[ARSession alloc] init];return
_arSession ;
}
- (ARSCNView *)arSCNView
{
if (
_arSCNView !=
nil ) {return
_arSCNView ;}
_arSCNView = [[ARSCNView alloc] initWithFrame:self.view.bounds];
_arSCNView .session = self.arSession;
_arSCNView .automaticallyUpdatesLighting = YES;return
_arSCNView ;
}
1.3-開啟AR掃描 我們只需要先將AR視圖添加到當前UIView中,然后開啟AR會話即可開始我們的AR之旅 ***這里需要特別注意的是,最好將開啟ARSession的代碼放入viewDidAppear而不是viewDidLoad中,這樣可以避免線程延遲的問題。開啟ARSession的代碼可不可以放入viewDidLoad中呢?答案是可以的,但是筆者不建議大家那么做*** @implementation ARSCNViewViewController - (
void )viewDidLoad {[
super viewDidLoad];
}- (
void )viewDidAppear:(
BOOL )animated
{[
super viewDidAppear:animated];[
self .view addSubview:
self .arSCNView];[
self .arSession runWithConfiguration:
self .arSessionConfiguration];}
1.4-點擊屏幕添加一個3D虛擬物體 默認情況下,節點SCNNode的x/y/z位置是(0,0,0),也就是攝像頭所在的位置,每一個ARSession在啟動時,攝像頭的位置就是3D世界的原點,而且這個原點不再隨著攝像頭的移動而改變,是第一次就永久固定的 想要讓飛機顯示在你想要的位置,就需要更加深入的研究ARKit框架,需要了解ARKit的坐標系及API,筆者將會在下一小節慢慢介紹 pragma mark- 點擊屏幕添加飛機 (void)touchesBegan:(NSSet<UITouch *>?)touches withEvent:(UIEvent? )event { //1.使用場景加載scn文件(scn格式文件是一個基于3D建模的文件,使用3DMax軟件可以創建,這里系統有一個默認的3D飛機)--------在右側我添加了許多3D模型,只需要替換文件名即可 SCNScene?scene = [SCNScene sceneNamed:@"Models.scnassets/ship.scn"]; //2.獲取飛機節點(一個場景會有多個節點,此處我們只寫,飛機節點則默認是場景子節點的第一個) //所有的場景有且只有一個根節點,其他所有節點都是根節點的子節點 SCNNode? shipNode = scene.rootNode.childNodes[0];
//3.將飛機節點添加到當前屏幕中 [self.arSCNView.scene.rootNode addChildNode:shipNode]; }
1.5-效果展示 在筆者Xcode左側已經導入了好幾個3D模型,只需要修改文件名既可以加載不同的3D模型,注意路徑區別 0403.png
0404.gif
0405.png
0405.gif
1.6-完整代碼及代碼下載地址 #import "ARSCNViewViewController.h"
#import <SceneKit/SceneKit.h>
#import <ARKit/ARKit.h> @interface ARSCNViewViewController ()
@property (
nonatomic ,
strong )ARSCNView *arSCNView;
@property (
nonatomic ,
strong )ARSession *arSession;
@property (
nonatomic ,
strong )ARSessionConfiguration *arSessionConfiguration;
@property (
nonatomic ,
strong )
SCNNode *planeNode;
@end @implementation ARSCNViewViewController - (
void )viewDidLoad {[
super viewDidLoad];
}- (
void )viewDidAppear:(
BOOL )animated
{[
super viewDidAppear:animated];[
self .view addSubview:
self .arSCNView];[
self .arSession runWithConfiguration:
self .arSessionConfiguration];}
#pragma mark- 點擊屏幕添加飛機
- (
void )touchesBegan:(
NSSet <
UITouch *> *)touches withEvent:(
UIEvent *)event
{
SCNScene *scene = [
SCNScene sceneNamed:
@"Models.scnassets/chair/chair.scn" ];
SCNNode *shipNode = scene.rootNode.childNodes[
0 ];shipNode.position =
SCNVector3Make (
0 ,
-1 ,
-1 );[
self .arSCNView.scene.rootNode addChildNode:shipNode];
}
#pragma mark -搭建ARKit環境
- (ARSessionConfiguration *)arSessionConfiguration
{
if (_arSessionConfiguration !=
nil ) {
return _arSessionConfiguration;}ARWorldTrackingSessionConfiguration *configuration = [[ARWorldTrackingSessionConfiguration alloc] init];configuration.planeDetection = ARPlaneDetectionHorizontal;_arSessionConfiguration = configuration;_arSessionConfiguration.lightEstimationEnabled =
YES ;
return _arSessionConfiguration;}
- (ARSession *)arSession
{
if (_arSession !=
nil ){
return _arSession;}_arSession = [[ARSession alloc] init];
return _arSession;
}
- (ARSCNView *)arSCNView
{
if (_arSCNView !=
nil ) {
return _arSCNView;}_arSCNView = [[ARSCNView alloc] initWithFrame:
self .view.bounds];_arSCNView.session =
self .arSession;_arSCNView.automaticallyUpdatesLighting =
YES ;
return _arSCNView;
}- (
void )didReceiveMemoryWarning {[
super didReceiveMemoryWarning];
}
@end
總結
以上是生活随笔 為你收集整理的ARKit从入门到精通(3)-ARKit自定义实现 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。