ARKit从入门到精通(3)-ARKit自定义实现
1.1-创建一个简单的工程
1.上一小节中介绍过,
ARSCNView
是UIView
的子类的子类,所以从理论上来说,我们应用框架UIKit
是可以加载AR场景的
0401.png
2.给界面添加一个按钮开启AR之旅,创建一个
ARSCNViewController:继承于UIViewController
,点击按钮跳转到自定义ARSCNViewController
0402.png
1.2-搭建ARKit工作环境
一个完整的ARKit工作环境必须要搭建三个对象:
ARSCNView
(一旦创建,系统会帮我们创建一个场景Scene和相机),ARSession
(开启AR和关闭AR都是靠它),ARSessionConfiguration
(少了会话追踪配置,AR会话是无法独立工作的)定义全局属性
#import "ARSCNViewViewController.h"//3D游戏框架#import <SceneKit/SceneKit.h>//ARKit框架#import <ARKit/ARKit.h>@interface ARSCNViewViewController ()//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
懒加载(笔者个人习惯)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;}//懒加载拍摄会话- (ARSession *)arSession{ if(_arSession != nil) { return _arSession; } //1.创建会话 _arSession = [[ARSession alloc] init]; //2返回会话 return _arSession;}//创建AR视图- (ARSCNView *)arSCNView{ if (_arSCNView != nil) { return _arSCNView; } //1.创建AR视图 _arSCNView = [[ARSCNView alloc] initWithFrame:self.view.bounds]; //2.设置视图会话 _arSCNView.session = self.arSession; //3.自动刷新灯光(3D游戏用到,此处可忽略) _arSCNView.automaticallyUpdatesLighting = YES; return _arSCNView;}
1.3-开启AR扫描
我们只需要先将AR视图添加到当前UIView中,然后开启AR会话即可开始我们的AR之旅
***这里需要特别注意的是,最好将开启
ARSession的代码放入viewDidAppear而不是
viewDidLoad中,这样可以避免线程延迟的问题。开启
ARSession的代码可不可以放入
viewDidLoad中呢?答案是可以的,但是笔者不建议大家那么做***
@implementation ARSCNViewViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; //1.将AR视图添加到当前视图 [self.view addSubview:self.arSCNView]; //2.开启AR会话(此时相机开始工作) [self.arSession runWithConfiguration:self.arSessionConfiguration];}
1.4-点击屏幕添加一个3D虚拟物体
默认情况下,节点
SCNNode
的x/y/z位置是(0,0,0),也就是摄像头所在的位置,每一个ARSession在启动时,摄像头的位置就是3D世界的原点,而且这个原点不再随着摄像头的移动而改变,是第一次就永久固定的想要让飞机显示在你想要的位置,就需要更加深入的研究ARKit框架,需要了解ARKit的坐标系及API,笔者将会在下一小节慢慢介绍
pragma mark- 点击屏幕添加飞机
请点击此处输入图片描述
-
(void)touchesBegan:(NSSet<UITouch *> )touches withEvent:(UIEvent )event
{
//1.使用场景加载scn文件(scn格式文件是一个基于3D建模的文件,使用3DMax软件可以创建,这里系统有一个默认的3D飞机)——–在右侧我添加了许多3D模型,只需要替换文件名即可
SCNScene scene = [SCNScene sceneNamed:@”Models.scnassets/ship.scn”];
//2.获取飞机节点(一个场景会有多个节点,此处我们只写,飞机节点则默认是场景子节点的第一个)
//所有的场景有且只有一个根节点,其他所有节点都是根节点的子节点
SCNNode shipNode = scene.rootNode.childNodes[0];
//3.将飞机节点添加到当前屏幕中
[self.arSCNView.scene.rootNode addChildNode:shipNode];
}
1.5-效果展示
在笔者Xcode左侧已经导入了好几个3D模型,只需要修改文件名既可以加载不同的3D模型,注意路径区别
0403.png
飞机
0404.gif
来张椅子坐一下吧
椅子比较大,我们需要适当调整一下位置
0405.png
0405.gi
1.6-完整代码及代码下载地址
完整代码
#import "ARSCNViewViewController.h"//3D游戏框架#import <SceneKit/SceneKit.h>//ARKit框架#import <ARKit/ARKit.h>@interface ARSCNViewViewController ()//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;@[email protected] ARSCNViewViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; //1.将AR视图添加到当前视图 [self.view addSubview:self.arSCNView]; //2.开启AR会话(此时相机开始工作) [self.arSession runWithConfiguration:self.arSessionConfiguration];}#pragma mark- 点击屏幕添加飞机- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //1.使用场景加载scn文件(scn格式文件是一个基于3D建模的文件,使用3DMax软件可以创建,这里系统有一个默认的3D飞机)--------在右侧我添加了许多3D模型,只需要替换文件名即可 SCNScene *scene = [SCNScene sceneNamed:@"Models.scnassets/chair/chair.scn"]; //2.获取飞机节点(一个场景会有多个节点,此处我们只写,飞机节点则默认是场景子节点的第一个) //所有的场景有且只有一个根节点,其他所有节点都是根节点的子节点 SCNNode *shipNode = scene.rootNode.childNodes[0]; //椅子比较大,可以可以调整Z轴的位置让它离摄像头远一点,,然后再往下一点(椅子太高我们坐不上去)就可以看得全局一点 shipNode.position = SCNVector3Make(0, -1, -1);//x/y/z/坐标相对于世界原点,也就是相机位置 //3.将飞机节点添加到当前屏幕中 [self.arSCNView.scene.rootNode addChildNode:shipNode];}#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]; //2返回会话 return _arSession;}//创建AR视图- (ARSCNView *)arSCNView{ if (_arSCNView != nil) { return _arSCNView; } //1.创建AR视图 _arSCNView = [[ARSCNView alloc] initWithFrame:self.view.bounds]; //2.设置视图会话 _arSCNView.session = self.arSession; //3.自动刷新灯光(3D游戏用到,此处可忽略) _arSCNView.automaticallyUpdatesLighting = YES; return _arSCNView;}- (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
代码下载地址
http://download.****.net/detail/u013263917/9867258