美文网首页
ARKit基础(三)——ARKit平面识别

ARKit基础(三)——ARKit平面识别

作者: 梁间 | 来源:发表于2018-08-16 19:50 被阅读0次

这个view主要展示ARKit如何检测平面。

首先指定ARWorldTrackingConfiguration的planeDetection属性。

ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];
configuration.planeDetection = ARPlaneDetectionHorizontal;
[sceneView.session runWithConfiguration:configuration];

ARPlaneDetectionHorizontal 检测水平面
ARPlaneDetectionVertical 检测垂直面

我们使用ARSCNViewDelegate来捕获的视频帧图像和跟踪状态。
当ARkit发现一个新的平面会调用这个方法:

- (void)renderer:(id <SCNSceneRenderer>)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor
{
}

我们需要判断这个事件是否是发现新平面

[anchor isMemberOfClass:[ARPlaneAnchor class]]

如果发现了新的平面,我们添加一个节点来渲染这个平面

ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
SCNBox *plane = [SCNBox boxWithWidth:planeAnchor.extent.x height:0 length:planeAnchor.extent.z chamferRadius:0];
plane.firstMaterial.diffuse.contents = [UIColor colorWithWhite:0.4 alpha:0.6];
        
SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
planeNode.position =SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z);
        
[node addChildNode:planeNode];

ARKit会在运行中不断修正已经发现的平面,当ARkit修正平面时会调用这个方法。

- (void)renderer:(id<SCNSceneRenderer>)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor{
}

我们需要在这个方法中同步修正我们用来渲染的节点.

ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
SCNNode *planeNode=node.childNodes[0];
SCNBox *plane = (SCNBox *)planeNode.geometry;
        
plane.width=planeAnchor.extent.x;
plane.length=planeAnchor.extent.z;
        
planeNode.position=SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z);

完整代码:

@interface ARPlaneAnchorViewController ()<ARSCNViewDelegate>{
    IBOutlet ARSCNView *sceneView;
}

@end

@implementation ARPlaneAnchorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    sceneView.delegate = self;
}

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

    ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];
    configuration.planeDetection = ARPlaneDetectionHorizontal;

    [sceneView.session runWithConfiguration:configuration];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [sceneView.session pause];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - ARSCNViewDelegate
- (void)renderer:(id <SCNSceneRenderer>)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor{
    if ([anchor isMemberOfClass:[ARPlaneAnchor class]]) {
        ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
        SCNBox *plane = [SCNBox boxWithWidth:planeAnchor.extent.x height:0 length:planeAnchor.extent.z chamferRadius:0];
        plane.firstMaterial.diffuse.contents = [UIColor colorWithWhite:0.4 alpha:0.6];
        
        SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
        planeNode.position =SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z);
        
        [node addChildNode:planeNode];
    }
}

- (void)renderer:(id<SCNSceneRenderer>)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor{
    if ([anchor isMemberOfClass:[ARPlaneAnchor class]]) {
        ARPlaneAnchor *planeAnchor = (ARPlaneAnchor *)anchor;
        SCNNode *planeNode=node.childNodes[0];
        SCNBox *plane = (SCNBox *)planeNode.geometry;
        
        plane.width=planeAnchor.extent.x;
        plane.length=planeAnchor.extent.z;
        
        planeNode.position=SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z);
    }
}

@end

相关文章

  • ARKit基础(三)——ARKit平面识别

    这个view主要展示ARKit如何检测平面。 首先指定ARWorldTrackingConfiguration的p...

  • ARkit初体验

    一.AR&ARkit ARkit。三部分,相机姿态估计, 环境感知(平面估计)及光源感知。 ARkit和AR和巨大...

  • ARKit功能demo

    ARKit点击屏幕增加文字 ARKit点击屏幕增加3D模型 ARKit检测到平面自动增加3D模型 QuickLoo...

  • 详解iOS之ARkit为何碾压对手(一)

    在这篇文章中,我们能学习到 1,ARKit简介与基本了解 2,ARKit的基本技术原理,平面检测 3,ARKit的...

  • ARKit构建AR世界

    本编主要讲使用ARKit进行构建AR世界并实现图片识别、平面捕捉、人脸识别功能并在真实世界中创建虚拟场景,从而达到...

  • 理解苹果ARKit 1和2

    DEMO包含功能平面检测、环境纹理、物体操作(添加、选中、拖动、缩放、旋转)、图像识别、环境音效 什么是ARKit...

  • ios中使用ARKit识别平面图片或真实世界对象

    ARKit1.5新增功能 iOS 11.3 上的视频分辨率提升至 1080p,其余不变; 新增竖直平面识别ARWo...

  • ARKit识别平面-Objective-C

    简单的ARKit的Demo,网上很多,包括Xcode自建的AR项目也可以直接实现一个飞机效果的Demo,不在赘述基...

  • 8月iOS Library Top5 ---2017.08

    1.ARKit-CoreLacation ARKit-CoreLacation将ARKit和CoreLocatio...

  • ARKit Work Shop Demo

    ARKit文章: 到底有多强?苹果的增强现实框架:ARKit ARKit进阶:物理世界 ARKit进阶:材质 De...

网友评论

      本文标题:ARKit基础(三)——ARKit平面识别

      本文链接:https://www.haomeiwen.com/subject/wmurvftx.html