使用ARKit,通过摄像机捕捉到真实世界并在其中加入计算机程序创造的虚拟物品,检测平面,并在平面上覆盖上一张网图,最终效果图如下

  • 创建ARKit项目,Xcode会自动在Main.storyboard中为项目创建一个ARSCNView

注意,这个地方选择“SceneKit” 此时运行项目,可以看到一个3D的飞机模型,删除多余的代码,然后进行项目的简单设置

  • 项目简单设置
- (void)viewDidLoad {[super viewDidLoad];[self setupScene];
}- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];[self setupSession];
}- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];// 暂停 session[self.sceneView.session pause];
}- (void)setupScene{//设置ARSCNViewDelegate代理self.sceneView.delegate = self;// 显示 fps 等信息self.sceneView.showsStatistics = YES;SCNScene *scene = [SCNScene new];self.sceneView.scene = scene;//添加光线self.sceneView.autoenablesDefaultLighting = YES;//显示 debugging information 3D坐标系和探测点self.sceneView.debugOptions = ARSCNDebugOptionShowWorldOrigin|ARSCNDebugOptionShowFeaturePoints;
}- (void)setupSession {// 配置 session configurationARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];//检测水平方向configuration.planeDetection = ARPlaneDetectionHorizontal;[self.sceneView.session runWithConfiguration:configuration];
}
复制代码
  • ARSCNViewDelegate协议方法 项目中使用到的几个协议方法的介绍
/**
当一个新的node映射上去时调用
*/
- (void)renderer:(id <SCNSceneRenderer>)rendererdidAddNode:(SCNNode *)nodeforAnchor:(ARAnchor *)anchor {
}/**
当一个node属性发生变化时调用
*/
- (void)renderer:(id<SCNSceneRenderer>)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor/**
当一个node移除时调用
*/
- (void)renderer:(id<SCNSceneRenderer>)renderer didRemoveNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor{
复制代码
  • 渲染一个平面 先创建一个Plane类,继承至SCNNode,需要两个方法,一个初始化方法和一个更新方法

在初始化方法中传入位置信息ARPlaneAnchor,平面的宽高坐标都从ARPlaneAnchor获取,平面发生变化时,需要更新位置属性

- (instancetype)initWithAnchor:(ARPlaneAnchor *)anchor;- (void)update:(ARPlaneAnchor *)anchor;
复制代码

声明一个平面属性

@property (nonatomic, strong) SCNPlane *planeGeometry;
复制代码

初始化方法中创建平面信息

- (instancetype)initWithAnchor:(ARPlaneAnchor *)anchor{self = [super init];self.planeGeometry = [SCNPlane planeWithWidth:anchor.extent.x height:anchor.extent.z];//平面展示网格SCNMaterial *material = [SCNMaterial new];UIImage *img = [UIImage imageNamed:@"tron_grid"];material.diffuse.contents = img;self.planeGeometry.materials = @[material];SCNNode *planeNode = [SCNNode nodeWithGeometry:self.planeGeometry];//将平面移到传入的位置planeNode.position = SCNVector3Make(anchor.extent.x, 0, anchor.extent.z);//平面在SceneKit中默认是vertical的,所以要旋转90度planeNode.transform = SCNMatrix4MakeRotation(-M_PI/2.0, 1.0, 0.0, 0.0);//设置平面的相关属性[self setTextureScale];[self addChildNode:planeNode];return self;
}//更新平面属性
- (void)update:(ARPlaneAnchor *)anchor{self.planeGeometry.width = anchor.extent.x;self.planeGeometry.height = anchor.extent.z;self.position = SCNVector3Make(anchor.center.x, 0, anchor.center.z);[self setTextureScale];
}
复制代码
  • 使用平面Plane类

声明一个字典保存平面

@property (nonatomic, strong) NSMutableDictionary *planes;
复制代码

在ARSCNViewDelegate协议的回调方法中,通过Anchor创建平面

- (void)renderer:(id<SCNSceneRenderer>)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor{if (![anchor isKindOfClass:[ARPlaneAnchor class]]) {return;}Plane *plane = [[Plane alloc] initWithAnchor:(ARPlaneAnchor *)anchor];[self.planes setObject:plane forKey:anchor.identifier];[node addChildNode:plane];
}
复制代码

当移动摄像头时,需要重新渲染Plane,所以需要在ARSCNViewDelegate的另一个回调方法中更新状态

- (void)renderer:(id<SCNSceneRenderer>)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor{Plane *plane = [self.planes objectForKey:anchor.identifier];if (plane == nil) {return;}[plane update:(ARPlaneAnchor *)anchor];
}
复制代码

我还在学习摸索ARKit中,这只是ARKit的一点基本使用,要想做出酷炫的AR应用,这点知识是远远不够的。项目github地址

转载于:https://juejin.im/post/5aa0fbe56fb9a028b54776b8

ARKit Plane Detection (平面检测)相关推荐

  1. ARKit之路-平面检测

    版权声明:Davidwang原创文章,严禁用于任何商业途径,授权后方可转载.   平面检测是很多AR应用的基础,无论是ARKit.ARCore还是Huawei AREngine.SenseAR等SDK ...

  2. ARKit-带你走进全新的世界(四:平面检测)

    简介: 上篇回顾: 上篇文章中我们简单写了一个AR尺子测量距离的demo,计算距离的公式--> A(x1,y1,z1),B(x2,y2,z2),则A,B之间的距离为d=√[(x1-x2)^2+( ...

  3. Fast and Accurate Ground Plane Detection for the Visually Impaired from 3D Organized Point Clouds

    Fast and Accurate Ground Plane Detection for the Visually Impaired from 3D Organized Point Clouds 摘要 ...

  4. AR的平面检测和利用SceneKit构建几何体

    ar的检测方法是ARSCNView的代理的方法 主要代码如下: _jpARSCNView.delegate = self;<ARSCNViewDelegate>-(void)rendere ...

  5. [深度学习]Object detection物体检测之概述

    一.Object detection物体检测与其他计算机视觉问题的区别与联系 在这里.有必要解释一下几大计算机视觉问题的区别与联系.说起物体检测是,那是计算机视觉之中一个比较热门的问题. 而它与图像识 ...

  6. CV之Face Detection:Face Detection人脸检测原理及其常见分类技术

    CV之Face Detection:Face Detection人脸检测原理及其常见分类技术 目录 人脸探测的原理 线性分类器 影像金字塔 滑动窗格 人脸探测的原理 将图片转为HOG图片以后,结合其他 ...

  7. TFLite Objec Detection IOS 检测核心代码说明

    文章目录 TFLite Objec Detection IOS 检测核心代码说明 简要说明 Object Detection 执行 object detection 所使用的模型 代码分析 Model ...

  8. 翻译:PlaneRCNN: 3D Plane Detection and Reconstruction from a Single Image

    图1.本文提出了一种深度神经网络结构PlaneRCNN,该结构检测平面区域,并从单个RGB图像重建分段平面深度图.从左到右,输入图像.分割的平面区域.估计的深度图和重建的平面. 摘要 本文提出了一种深 ...

  9. CV之FR/FD:人脸识别之Face Detection人脸检测原理及其常见分类技术

    CV之FR/FD:人脸识别之Face Detection人脸检测原理及其常见分类技术 目录 人脸识别之Face Detection人脸检测原理及其常见分类技术 人脸探测的原理 线性分类器 影像金字塔 ...

最新文章

  1. [C] [最短路] 只有5行的算法:Floyd-Warshall
  2. 每日起床前做这五个动作,可保障你终身不感冒——奥运福娃设计师韩美林老师亲测可用
  3. DIV+CSS网页布局常用的一些基础知识
  4. PyTorch入门v2.pptx
  5. Tomcat+Nginx+Memcached集群部署
  6. css框架之960 Grid System 基本原理及使用方法
  7. python安装库失败cannot determine archive_pip 无法安装 pip
  8. mysql-sql命令
  9. 自反而缩,虽千万人,吾往矣。
  10. dubbo和zookeper使用_Dubbox与Zookeeper简介及入门小案例
  11. php 图片轮换 代码,jQuery实现图片轮播特效代码分享
  12. 系统主数据管理之供应商(Supplier)一 供应商的分类概述
  13. Win10+外接显示器 “未检测到其他显示器”
  14. 资产负债及银行资产负债业务
  15. zlib压缩解压缩文件
  16. CMD命令窗口光标消失解决方案
  17. 《软件体系结构》知识点整理
  18. OpenSSL生成CA自签名根证书和颁发证书和证书提取
  19. word中自动生成的目录中,编号和文本间距过大
  20. woo语言实现 m3u8流媒体视频文件 下载并播放

热门文章

  1. 使用OpenCV进行简单的图像分割
  2. linux 模拟打电话,Android 调用打电话和发短信功能
  3. php语句创建数据表,用mysql语句创建数据表详细教程
  4. 为什么属龙的有二婚命_为什么属龙的有二婚命 一生婚姻运势详解
  5. 2021安徽高考成绩及录取结婚查询,2020安徽高考录取结果查询时间及通知书发放时间...
  6. jQuery Validate focusCleanup: true
  7. mysql optimize 用法_mysql中OPTIMIZE TABLE的作用
  8. mschart走势图 vc_[VC] 解决MSChart闪烁的问题
  9. c语言 链表_小陈的C语言笔记---链表(详细讲解基本操作和概念)
  10. 调查问卷java源码_2020年Java技术趋势