iOS---ARKit

ARKit 将 iOS 设备的摄像头和设备动作检测 (Motion) 功能,集成到您的应用或者游戏当中,从而为用户提供增强现实体验。
重点:ARKit虽然是iOS11新出的框架,但并不是所有的iOS11系统都可以使用,而是必须要是处理器A9及以上才能够使用,苹果从iPhone6s开始使用A9处理器,也就是iPhone6及以前的机型无法使用ARKit
实现代码如下:
#import "ARSCNViewController.h"
#import //3D游戏框架
#import //ARKit框架
#import
@interface ARSCNViewController ()
@property (nonatomic,strong) ARSCNView *arSCNView; //创建AR视图:展示3D界面
@property (nonatomic,strong) ARSession *arSession; //AR会话,负责管理相机追踪配置及3D相机坐标
@property (nonatomic,strong) ARConfiguration *arConfiguration; //会话追踪配置:负责追踪相机的运动
@property (nonatomic,strong) SCNNode *scnNode; //模型
@end
@implementationARSCNViewController
/*
重点:1.默认情况下,节点SCNNode的x/y/z位置是(0,0,0),也就是摄像头所在的位置,每一个ARSession在启动时,摄像头的位置就是3D世界的原点,而且这个原点不再随着摄像头的移动而改变,是第一次就永久固定的 */
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewDidAppear:(BOOL)animated{
[superviewDidAppear:animated];


//重点:将开启ARSession的代码放入viewDidAppear而不是viewDidLoad中,这样可以避免线程延迟的问题。开启ARSession的代码可不可以放入viewDidLoad中呢?答案是可以的,但是不建议那么做
[self.view addSubview:self.arSCNView]; //将AR视图添加到当前视图
[self.arSession runWithConfiguration:self.arConfiguration]; //开启AR会话,相机开始工作
}
#pragma mark----会话追踪配置
- (ARConfiguration*)arConfiguration{


if (!_arConfiguration) {
//1.创建世界追踪会话配置(需要A9芯片支持)
ARWorldTrackingConfiguration *configuration = [[ARWorldTrackingConfiguration alloc]init];
//2.设置追踪方向
configuration.planeDetection = ARPlaneDetectionHorizontal;
_arConfiguration= configuration;
//3.自适应灯光
[_arConfiguration setLightEstimationEnabled:YES];


}
return _arConfiguration;
}
#pragma mark-----拍摄会话
- (ARSession*)arSession{


if (!_arSession) {


_arSession= [[ARSessionalloc]init];
}
return _arSession;
}
#pragma mark----创建AR视图
- (ARSCNView*)arSCNView{


if (!_arSCNView) {
//1.创建AR视图
_arSCNView= [[ARSCNViewalloc]initWithFrame:self.view.bounds];
//2.设置视图会话
_arSCNView.session = self.arSession;
//自动刷新灯光
_arSCNView.automaticallyUpdatesLighting = YES;


}
return _arSCNView;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
//1.使用场景加载scn文件(scn格式文件是一个基于3D建模的文件,使用3DMax软件可以创建)
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"]; //art.scnassets/ship.scn---如何创建后缀为.scnassets的文件夹,就是选择一个为-Asset Catalog类型的,然后将其后缀改为.scnassets,然后拖入后缀为.scn和png的文件。具体的实现在下方。
//2.获取节点---所有的场景有且只有一个根节点,其他所有节点都是根节点的子节点
SCNNode *node = scene.rootNode.childNodes[0];


//将节点(物体)添加到当前屏幕中
[self.arSCNView.scene.rootNodeaddChildNode:node];


}
- (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
解析:
ARSession类
这是一个单例, 是 ARKit 的核心类, 用于控制设备摄像头,处理传感器数据,对捕获的图像进行分析等等。
ARConfiguration类
跟踪设备方向的一个基本配置, 在运行时,需要指定AR运行的配置
ARWorldTrackingConfiguration类 配置跟踪设备的方向和位置,以及检测设备摄像头所看到的现实世界的表面
ARSCNView类
来增强相机通过 3D SceneKit 所捕捉到的内容并展示 AR 效果的一个 view
ARSKView类
来增强相机通过 2D SpriteKit 所捕捉到的内容并展示 AR 效果的 个 view
ARAnchor类
真实世界的位置和方向,于在一个AR场景中放置 个物体
ARPlaneAnchor类
在 个AR Session 会话中检测一个真实世界中平面的位置和方向的相关信息
ARHitTestResult类
在一个AR Session会话中通过检测相机视图中的一个点来获取真实世界中表面的相关信息
ARFrame类
捕获一个视频图像和位置追踪信息作为一个AR 会话的 部分。
ARCamera类 在一个AR会话中摄像机的位置和成像特征信息为捕获视频帧
ARLightEstimate类 在 个AR会话中估计场景照明信息关联到 个捕获的视频帧
Understanding Augmented Reality


-如何创建后缀为.scnassets的文件夹

iOS---ARKit
文章图片
【iOS---ARKit】

iOS---ARKit
文章图片

    推荐阅读