ARinChina-增强现实中国技术论坛

标题: ARKit从入门到精通(8)-ARKit捕捉平地 [打印本页]

作者: Scarlett_1990    时间: 2017-9-25 17:22
标题: ARKit从入门到精通(8)-ARKit捕捉平地
转载请注明出处:ARKit从入门到精通(8)-ARKit捕捉平地
1.1-ARKit捕捉平地实现流程介绍
1.2-完整代码
1.3-代码下载地址
在介绍完ARKit详细的工作原理以及所有的API之后,最令人期待的干货终于要来了!
废话不多说,先看效果

0901.gif


0902.gif

1.1-ARKit捕捉平地实现流程介绍#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)rendererid <SCNSceneRenderer>)renderer didAddNodeSCNNode *)node forAnchorARAnchor *)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)backUIButton *)btn{    [self dismissViewControllerAnimated:YES completion:nil];}- (void)viewDidAppearBOOL)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)rendererid <SCNSceneRenderer>)renderer didAddNodeSCNNode *)node forAnchorARAnchor *)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)rendererid <SCNSceneRenderer>)renderer willUpdateNodeSCNNode *)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.}*/@end1.3-代码下载地址
作者: 重庆生活网l    时间: 2017-10-26 21:39
支持一下吧,确实是不错的贴子。













第五代QQ机器人QQ群机器人论坛QQ机器人智能客服机器人QQplus机器人QQ群互联机器人..联系QQ4000017854

作者: wj7636    时间: 2019-3-17 11:59
此贴有意思~~~~~~~            伸缩门 http://www.ssmdd.com
作者: wj7636    时间: 2019-4-1 17:25
在遇到你之前,我对人世间是否有真正的圣人是怀疑的;而现在,我终于相信了!我曾经忘情于两汉的歌赋,我曾经惊讶于李杜的诗才,我曾经流连于宋元的词曲。但现在,我才知道我有多么浅薄!! 平移门 http://www.ssmdd.com
作者: wj7636    时间: 2019-4-7 14:48
如果你感觉我ID帅气,请粉我!! 悬浮门http://www.ssmdd.com
作者: wj7636    时间: 2019-4-8 16:39
【灌水也要讲技术保证句句是十五字】 伸缩门 http://www.ssmdd.com




欢迎光临 ARinChina-增强现实中国技术论坛 (http://dev.arinchina.com/) Powered by Discuz! X3.2