ARKit初体验之2D效果

使用步骤与3D基本类似,在创建Xcode的时候选择SpriteKit引擎

import "ViewController.h"

import "Scene.h"

@interface ViewController ()

//ARSKView是ARKit框架中负责展示2D AR的预览视图 
@property (nonatomic, strong) IBOutlet ARSKView *sceneView;

@end

@implementation ViewController

(void)viewDidLoad { 
[super viewDidLoad];

// Set the view’s delegate 
//设置场景视图代理 
self.sceneView.delegate = self;

// Show statistics such as fps and node count

//显示帧率 
self.sceneView.showsFPS = YES; 
//显示界面节点(游戏开发中,一个角色对应一个节点) 
self.sceneView.showsNodeCount = YES;

// Load the SKScene from ‘Scene.sks’ 
//加载2D场景(2D是平面的) 
Scene scene = (Scene )[SKScene nodeWithFileNamed:@”Scene”];

// Present the scene 
//AR预览视图展现场景(这一点与3D视图加载有区别) 
[self.sceneView presentScene:scene]; 
}

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

// Create a session configuration 
//创建设备追踪设置 
ARWorldTrackingSessionConfiguration *configuration = [ARWorldTrackingSessionConfiguration new];

// Run the view’s session

//开始启动AR 
[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 - ARSKViewDelegate

//点击界面会调用,类似于touch begin方法 anchor是2D坐标的瞄点 
- (SKNode )view:(ARSKView )view nodeForAnchor:(ARAnchor *)anchor { 
// Create and configure a node for the anchor added to the view’s session.

//创建节点(节点可以理解为AR将要展示的2D图像)
SKLabelNode *labelNode = [SKLabelNode labelNodeWithText:@"..."]

results matching ""

    No results matching ""