ARKit最简单的一个Demo

AR最简单的一个Demo

参考了苹果官网关于搭建一个最基本的AR效果,然后这里把demo讲一下,由于有很多概念没有理解完全,所以有些地方有纰漏的话,可以拍砖一起讨论。


这里一定要注意,开发环境为Xcode 9, 运行环境是iphone 6s及以上设备,系统是 iOS 11

因为苹果规定(点这里查询)ARKit是运行在 A9 和 A10处理器上,所以 iphone 或者 ipad 自行对照自己的CPU。

如果运行Demo后出现这个提示,都表示当前运行的设备不支持ARKit:

2017-06-07 11:41:35.317768+0800 ARDemo[2970:1228240] [Session] Unable to run the session, configuration is not supported on this device: ARWorldTrackingSessionConfiguration: 0x60800009f310 planeDetection=Horizontal worldAlignment=Gravity lightEstimation=Enabled>


使用ARKit需要理解有2个东西:

1、ARSCNView 一种显示AR体验的视图,它通过3D SceneKit内容增强了相机视图。
2、ARSKView 一种显示AR体验的视图,增加了2D SpriteKit内容的相机视图。

这里使用了第一个,增加一个3D效果的视图。

在使用前,首先得需要再 viewWillAppear 方法里面实例化一个会话配置类,在配置类对象里设置会话如何将真实的设备运动映射到3D场景的坐标系统里,这里默认是使用重力,应该是使用陀螺仪;还需要制定其他几个值。最后再把配置类对象设置到视图的会话中。如下:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    // Create a session configuration
    ARWorldTrackingSessionConfiguration *configuration = [ARWorldTrackingSessionConfiguration new];
    configuration.worldAlignment = ARWorldAlignmentGravity;
//    configuration.lightEstimationEnabled = YES;
    configuration.planeDetection = ARPlaneDetectionHorizontal;
    // Run the view's session
    [self.sceneView.session runWithConfiguration:configuration];
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

接下来在 viewDidLoad 方法里面设置了 ARSCNView 的代理,然后在下述这个方法里添加在视图里要显示的内容:

- (void)renderer:(id<SCNSceneRenderer>)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor
{
    ARPlaneAnchor *planeAnchor = anchor;

    SCNPlane *plane = [SCNPlane planeWithWidth:planeAnchor.extent.x height:planeAnchor.extent.z];
    SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
    planeNode.position = SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z);

    planeNode.transform = SCNMatrix4MakeRotation(- M_PI/2, 1, 0, 0);

    [node addChildNode:planeNode];
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这里添加一个系统的视图 ARPlaneAnchor ,其实系统还内置了很多视图,可以用作基本使用。在这个代理方法设置好 视图的各种属性后,就添加到 ARSCNView 对象中。

运行效果如下:

图示

最后把Demo献上,点击这里下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值