1. 程式人生 > >ARKit從入門到精通(8)-ARKit捕捉平地

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

別人 button 節點 length width 平面 viewview ane truct

  • 轉載:http://blog.csdn.net/hdfqq188816190/article/details/73360287

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

  • 1.2-完整代碼
  • 1.3-代碼下載地址

  • 在介紹完ARKit詳細的工作原理以及所有的API之後,最令人期待的幹貨終於要來了!

  • 廢話不多說,先看效果

    • 桌子上的綠蘿太孤獨了,給它來一個郁金香陪伴一下吧~
技術分享
0901.gif
  • 在椅子上擺瓶花吧~
技術分享
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*0.3 height:0 length:planeAnchor.extent.x*0.3 chamferRadius:0];
        //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, 0, planeAnchor.center.z);

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


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

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

            //4.設置花瓶節點的位置為捕捉到的平地的位置,如果不設置,則默認為原點位置,也就是相機位置
            vaseNode.position = SCNVector3Make(planeAnchor.center.x, 0, 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/2-50, self.view.bounds.size.height-100, 100, 50);
    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*0.3 height:0 length:planeAnchor.extent.x*0.3 chamferRadius:0];
        //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, 0, planeAnchor.center.z);

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


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

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

            //4.設置花瓶節點的位置為捕捉到的平地的位置,如果不設置,則默認為原點位置,也就是相機位置
            vaseNode.position = SCNVector3Make(planeAnchor.center.x, 0, 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

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