zoukankan      html  css  js  c++  java
  • ARKit从入门到精通(8)-ARKit捕捉平地

    • 在椅子上摆瓶花吧~

    1.1-ARKit捕捉平地实现流程介绍

    • 平地捕捉需要一点时间,ARKit内部会进行比较复杂的算法,所以有时候可能没有那么快,需要耐心等待。

    • 1.搭建自定义ARKit工作环境,详情请见笔者ARKit从入门到精通(3)-ARKit自定义实现这篇文章

    • 2.配置ARConfiguration捕捉平地事件,实现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
  • 相关阅读:
    VituralBox 虚拟机网路设置 主机无线
    布局
    Git 安装
    剑指offer——33分行从上到下打印二叉树
    剑指offer——32从上到下打印二叉树
    剑指offer——31序列化二叉树
    剑指offer——30栈的压入、弹出序列
    剑指offer——30包含min函数的栈
    剑指offer——29顺时针打印矩阵
    剑指offer——28对称的二叉树
  • 原文地址:https://www.cnblogs.com/weicyNo-1/p/8065409.html
Copyright © 2011-2022 走看看