天天看點

ARKit從入門到精通(8)-ARKit捕捉平地

  • 轉載請注明出處:ARKit從入門到精通(8)-ARKit捕捉平地
  • 1.1-ARKit捕捉平地實作流程介紹
  • 1.2-完整代碼
  • 1.3-代碼下載下傳位址
  • 在介紹完

    ARKit

    詳細的工作原理以及所有的API之後,最令人期待的幹貨終于要來了!
  • 廢話不多說,先看效果
    • 桌子上的綠蘿太孤獨了,給它來一個郁金香陪伴一下吧~
ARKit從入門到精通(8)-ARKit捕捉平地

0901.gif

  • 在椅子上擺瓶花吧~
ARKit從入門到精通(8)-ARKit捕捉平地

0902.gif

1.1-ARKit捕捉平地實作流程介紹

  • 平地捕捉需要一點時間,ARKit内部會進行比較複雜的算法,是以有時候可能沒有那麼快,需要耐心等待。
  • 1.搭建自定義ARKit工作環境,詳情請見筆者

    ARKit從入門到精通(3)-ARKit自定義實作

    這篇文章
  • 2.配置

    ARSessionConfiguration

    捕捉平地事件,實作

    ARSCNViewDelegate

    監聽捕捉平地回調
  • 3.通過

    ARSCNView

    的代理擷取平地錨點

    ARPlaneAnchor

    的位置,添加一個用于展示渲染平地的3D模型(上圖中一個紅色的平地)
    • 在前面小節筆者已經強調過,ARKit架構隻負責捕捉真實世界的圖像,虛拟世界的場景由SceneKit架構來加載。是以ARKit捕捉到的是一個平地的空間,而這個空間本身是沒有東西的(一片空白,隻是空氣而已),要想讓别人能夠更加真實的看到這一個平地的空間,需要我們使用一個3D虛拟物體來放入這個空間
  • 4.開啟延遲線程,在平地的位置添加一個花瓶節點
    • 此處一定要注意:花瓶節點是添加到代理捕捉到的節點中,而不是AR試圖的根節點。因為捕捉到的平地錨點是一個本地坐标系,而不是世界坐标系
  • 核心代碼介紹
#pragma mark -搭建ARKit環境


//懶加載會話追蹤配置
- (ARSessionConfiguration *)arSessionConfiguration
{
    if (_arSessionConfiguration != nil) {
        return _arSessionConfiguration;
    }

    //1.建立世界追蹤會話配置(使用ARWorldTrackingSessionConfiguration效果更加好),需要A9晶片支援
    ARWorldTrackingSessionConfiguration *configuration = [[ARWorldTrackingSessionConfiguration alloc] init];
    //2.設定追蹤方向(追蹤平面,後面會用到)
    configuration.planeDetection = ARPlaneDetectionHorizontal;
    _arSessionConfiguration = configuration;
    //3.自适應燈光(相機從暗到強光快速過渡效果會平緩一些)
    _arSessionConfiguration.lightEstimationEnabled = YES;

    return _arSessionConfiguration;

}

#pragma mark -- ARSCNViewDelegate



//添加節點時候調用(當開啟平地捕捉模式之後,如果捕捉到平地,ARKit會自動添加一個平地節點)
- (void)renderer:(id <SCNSceneRenderer>)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor
{

    if(self.arType != ARTypePlane)
    {
        return;
    }

    if ([anchor isMemberOfClass:[ARPlaneAnchor class]]) {
        NSLog(@"捕捉到平地");

        //添加一個3D平面模型,ARKit隻有捕捉能力,錨點隻是一個空間位置,要想更加清楚看到這個空間,我們需要給空間添加一個平地的3D模型來渲染他

        //1.擷取捕捉到的平地錨點
        ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
        //2.建立一個3D物體模型    (系統捕捉到的平地是一個不規則大小的長方形,這裡筆者将其變成一個長方形,并且是否對平地做了一個縮放效果)
        //參數分别是長寬高和圓角
        SCNBox *plane = [SCNBox boxWithWidth:planeAnchor.extent.x* height: length:planeAnchor.extent.x* chamferRadius:];
        //3.使用Material渲染3D模型(預設模型是白色的,這裡筆者改成紅色)
        plane.firstMaterial.diffuse.contents = [UIColor redColor];

        //4.建立一個基于3D物體模型的節點
        SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
        //5.設定節點的位置為捕捉到的平地的錨點的中心位置  SceneKit架構中節點的位置position是一個基于3D坐标系的矢量坐标SCNVector3Make
        planeNode.position =SCNVector3Make(planeAnchor.center.x, , planeAnchor.center.z);

        //self.planeNode = planeNode;
        [node addChildNode:planeNode];


        //2.當捕捉到平地時,2s之後開始在平地上添加一個3D模型

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //1.建立一個花瓶場景
            SCNScene *scene = [SCNScene sceneNamed:@"Models.scnassets/vase/vase.scn"];
            //2.擷取花瓶節點(一個場景會有多個節點,此處我們隻寫,花瓶節點則預設是場景子節點的第一個)
            //所有的場景有且隻有一個根節點,其他所有節點都是根節點的子節點
            SCNNode *vaseNode = scene.rootNode.childNodes[];

            //4.設定花瓶節點的位置為捕捉到的平地的位置,如果不設定,則預設為原點位置,也就是相機位置
            vaseNode.position = SCNVector3Make(planeAnchor.center.x, , planeAnchor.center.z);

            //5.将花瓶節點添加到目前螢幕中
            //!!!此處一定要注意:花瓶節點是添加到代理捕捉到的節點中,而不是AR試圖的根節點。因為捕捉到的平地錨點是一個本地坐标系,而不是世界坐标系
            [node addChildNode:vaseNode];
        });
    }
}
           

1.2-完整代碼

#import "ARSCNViewViewController.h"

//3D遊戲架構
#import <SceneKit/SceneKit.h>
//ARKit架構
#import <ARKit/ARKit.h>

@interface ARSCNViewViewController ()<ARSCNViewDelegate,ARSessionDelegate>

//AR視圖:展示3D界面
@property(nonatomic,strong)ARSCNView *arSCNView;

//AR會話,負責管理相機追蹤配置及3D相機坐标
@property(nonatomic,strong)ARSession *arSession;

//會話追蹤配置:負責追蹤相機的運動
@property(nonatomic,strong)ARSessionConfiguration *arSessionConfiguration;

//飛機3D模型(本小節加載多個模型)
@property(nonatomic,strong)SCNNode *planeNode;

@end

@implementation ARSCNViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];



    // Do any additional setup after loading the view.
}

- (void)back:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    //1.将AR視圖添加到目前視圖
    [self.view addSubview:self.arSCNView];
    //2.開啟AR會話(此時相機開始工作)
    [self.arSession runWithConfiguration:self.arSessionConfiguration];


    //添加傳回按鈕
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"傳回" forState:UIControlStateNormal];
    btn.frame = CGRectMake(self.view.bounds.size.width/, self.view.bounds.size.height, , );
    btn.backgroundColor = [UIColor greenColor];
    [btn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}


#pragma mark -搭建ARKit環境


//懶加載會話追蹤配置
- (ARSessionConfiguration *)arSessionConfiguration
{
    if (_arSessionConfiguration != nil) {
        return _arSessionConfiguration;
    }

    //1.建立世界追蹤會話配置(使用ARWorldTrackingSessionConfiguration效果更加好),需要A9晶片支援
    ARWorldTrackingSessionConfiguration *configuration = [[ARWorldTrackingSessionConfiguration alloc] init];
    //2.設定追蹤方向(追蹤平面,後面會用到)
    configuration.planeDetection = ARPlaneDetectionHorizontal;
    _arSessionConfiguration = configuration;
    //3.自适應燈光(相機從暗到強光快速過渡效果會平緩一些)
    _arSessionConfiguration.lightEstimationEnabled = YES;

    return _arSessionConfiguration;

}

//懶加載拍攝會話
- (ARSession *)arSession
{
    if(_arSession != nil)
    {
        return _arSession;
    }
    //1.建立會話
    _arSession = [[ARSession alloc] init];
    _arSession.delegate = self;
    //2傳回會話
    return _arSession;
}

//建立AR視圖
- (ARSCNView *)arSCNView
{
    if (_arSCNView != nil) {
        return _arSCNView;
    }
    //1.建立AR視圖
    _arSCNView = [[ARSCNView alloc] initWithFrame:self.view.bounds];

    //2.設定代理  捕捉到平地會在代理回調中傳回
    _arSCNView.delegate = self;

    //2.設定視圖會話
    _arSCNView.session = self.arSession;
    //3.自動重新整理燈光(3D遊戲用到,此處可忽略)
    _arSCNView.automaticallyUpdatesLighting = YES;

    return _arSCNView;
}

#pragma mark -- ARSCNViewDelegate



//添加節點時候調用(當開啟平地捕捉模式之後,如果捕捉到平地,ARKit會自動添加一個平地節點)
- (void)renderer:(id <SCNSceneRenderer>)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor
{

    if(self.arType != ARTypePlane)
    {
        return;
    }

    if ([anchor isMemberOfClass:[ARPlaneAnchor class]]) {
        NSLog(@"捕捉到平地");

        //添加一個3D平面模型,ARKit隻有捕捉能力,錨點隻是一個空間位置,要想更加清楚看到這個空間,我們需要給空間添加一個平地的3D模型來渲染他

        //1.擷取捕捉到的平地錨點
        ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
        //2.建立一個3D物體模型    (系統捕捉到的平地是一個不規則大小的長方形,這裡筆者将其變成一個長方形,并且是否對平地做了一個縮放效果)
        //參數分别是長寬高和圓角
        SCNBox *plane = [SCNBox boxWithWidth:planeAnchor.extent.x* height: length:planeAnchor.extent.x* chamferRadius:];
        //3.使用Material渲染3D模型(預設模型是白色的,這裡筆者改成紅色)
        plane.firstMaterial.diffuse.contents = [UIColor redColor];

        //4.建立一個基于3D物體模型的節點
        SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
        //5.設定節點的位置為捕捉到的平地的錨點的中心位置  SceneKit架構中節點的位置position是一個基于3D坐标系的矢量坐标SCNVector3Make
        planeNode.position =SCNVector3Make(planeAnchor.center.x, , planeAnchor.center.z);

        //self.planeNode = planeNode;
        [node addChildNode:planeNode];


        //2.當捕捉到平地時,2s之後開始在平地上添加一個3D模型

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //1.建立一個花瓶場景
            SCNScene *scene = [SCNScene sceneNamed:@"Models.scnassets/vase/vase.scn"];
            //2.擷取花瓶節點(一個場景會有多個節點,此處我們隻寫,花瓶節點則預設是場景子節點的第一個)
            //所有的場景有且隻有一個根節點,其他所有節點都是根節點的子節點
            SCNNode *vaseNode = scene.rootNode.childNodes[];

            //4.設定花瓶節點的位置為捕捉到的平地的位置,如果不設定,則預設為原點位置,也就是相機位置
            vaseNode.position = SCNVector3Make(planeAnchor.center.x, , planeAnchor.center.z);

            //5.将花瓶節點添加到目前螢幕中
            //!!!此處一定要注意:花瓶節點是添加到代理捕捉到的節點中,而不是AR試圖的根節點。因為捕捉到的平地錨點是一個本地坐标系,而不是世界坐标系
            [node addChildNode:vaseNode];
        });
    }
}

//重新整理時調用
- (void)renderer:(id <SCNSceneRenderer>)renderer willUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor
{
    NSLog(@"重新整理中");
}

//更新節點時調用
- (void)renderer:(id <SCNSceneRenderer>)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor
{
    NSLog(@"節點更新");

}

//移除節點時調用
- (void)renderer:(id <SCNSceneRenderer>)renderer didRemoveNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor
{
    NSLog(@"節點移除");
}

#pragma mark -ARSessionDelegate

//會話位置更新(監聽相機的移動),此代理方法會調用非常頻繁,隻要相機移動就會調用,如果相機移動過快,會有一定的誤差,具體的需要強大的算法去優化,筆者這裡就不深入了
- (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame
{
    NSLog(@"相機移動");

}
- (void)session:(ARSession *)session didAddAnchors:(NSArray<ARAnchor*>*)anchors
{
    NSLog(@"添加錨點");

}


- (void)session:(ARSession *)session didUpdateAnchors:(NSArray<ARAnchor*>*)anchors
{
    NSLog(@"重新整理錨點");

}


- (void)session:(ARSession *)session didRemoveAnchors:(NSArray<ARAnchor*>*)anchors
{
    NSLog(@"移除錨點");

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
           

1.3-代碼下載下傳位址

  • *ARKit從入門到精通Demo:http://download.csdn.net/detail/u013263917/9868679
  • 筆者已經将8、9、10三小節的代碼合并成一個完整的小demo,供讀者交流學習