来源:https://www.jianshu.com/p/396a0d1c16f9
创建项目时,选择Augmented Reality App
屏幕快照 2018-05-15 下午3.03.48.pngContent Technology 选项选择 SceneKit
屏幕快照 2018-05-15 下午3.07.17.pngViewController里有一个自带的飞机模型,替换原有的代码:
#import "ViewController.h"
@interface ViewController () <ARSCNViewDelegate>
@property (nonatomic, strong) IBOutlet ARSCNView *sceneView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sceneView.delegate = self;
// Show statistics such as fps and timing information
self.sceneView.showsStatistics = YES;
//存放所有3D几何体的容器
SCNScene *scene = [[SCNScene alloc]init];
//想要绘制的3D立方体
SCNBox *boxGeometry = [SCNBox boxWithWidth:0.1 height:0.1 length:0.1 chamferRadius:0.0];
//将几何体包装为node以便添加到scene
SCNNode *boxNode = [SCNNode nodeWithGeometry:boxGeometry];
//把box放在摄像头正前方
boxNode.position = SCNVector3Make(0, 0, -0.5);
//rootNode是一个特殊的node,它是所有node的起点
[scene.rootNode addChildNode:boxNode];
//将scene赋给view
self.sceneView.scene = scene;
//默认光照
self.sceneView.autoenablesDefaultLighting = YES;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Create a session configuration
ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];
// Run the view's session
[self.sceneView.session runWithConfiguration:configuration];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Pause the view's session
[self.sceneView.session pause];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - ARSCNViewDelegate
/*
// Override to create and configure nodes for anchors added to the view's session.
- (SCNNode *)renderer:(id<SCNSceneRenderer>)renderer nodeForAnchor:(ARAnchor *)anchor {
SCNNode *node = [SCNNode new];
// Add geometry to the node...
return node;
}
*/
- (void)session:(ARSession *)session didFailWithError:(NSError *)error {
// Present an error message to the user
}
- (void)sessionWasInterrupted:(ARSession *)session {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
- (void)sessionInterruptionEnded:(ARSession *)session {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
@end
WechatIMG15.png
网友评论