ARCamera 是捕捉现实世界图像的相机,同时也是3D虚拟世界的相机。
这个view主要是输出ARCamera的五个基础属性:
transform: matrix_float4x4 相机的位置姿态
eulerangles: vector_float3 相机方向的矢量欧拉角
imageResolution: CGSize 相机获取到图像的大小(像素)
intrinsics: matrix_float3x3 相机内参矩阵
projectionMatrix: matrix_float4x4 投影矩阵
这次我们换一种加载模型的方式
//从文件中获取飞船节点
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
shipNode = scene.rootNode.childNodes[0];
//对飞船进行缩放,设定飞船的位置,飞船的所有子节点也需要缩放
shipNode.scale = SCNVector3Make(0.2, 0.2, 0.2);
shipNode.position = SCNVector3Make(0, -1,-1.8);
for (SCNNode *node in shipNode.childNodes) {
node.scale = SCNVector3Make(0.2, 0.2, 0.2);
}
// 把飞船加入场景的根结点
[sceneView.scene.rootNode addChildNode:shipNode];
我们使用ARSessionDelegate来捕获的视频帧图像和跟踪状态。
-(void)session:(ARSession *)session didUpdateFrame:(nonnull ARFrame *)frame{
... ...
}
完整代码:
@interface ARCameraViewController ()<ARSCNViewDelegate,ARSessionDelegate>{
IBOutlet ARSCNView *sceneView;
IBOutlet UILabel *label;
SCNNode *shipNode;
}
@end
@implementation ARCameraViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Set the session's delegate
sceneView.session.delegate=self;
SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
shipNode = scene.rootNode.childNodes[0];
shipNode.scale = SCNVector3Make(0.2, 0.2, 0.2);
shipNode.position = SCNVector3Make(0, -1,-1.8);
for (SCNNode *node in shipNode.childNodes) {
node.scale = SCNVector3Make(0.2, 0.2, 0.2);
node.position = SCNVector3Make(0, -1,-1.8);
}
[sceneView.scene.rootNode addChildNode:shipNode];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];
[sceneView.session runWithConfiguration:configuration];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[sceneView.session pause];
}
-(void)session:(ARSession *)session didUpdateFrame:(nonnull ARFrame *)frame{
matrix_float4x4 matrix=frame.camera.transform;
NSMutableString *matrixStr=[[NSMutableString alloc]init];
[matrixStr appendString:@"transform:\n"];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][0],matrix.columns[1][0],matrix.columns[2][0],matrix.columns[3][0]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][1],matrix.columns[1][1],matrix.columns[2][1],matrix.columns[3][1]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][2],matrix.columns[1][2],matrix.columns[2][2],matrix.columns[3][2]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][3],matrix.columns[1][3],matrix.columns[2][3],matrix.columns[3][3]]];
vector_float3 eulerangles=frame.camera.eulerAngles;
[matrixStr appendString:@"eulerangles:\n"];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f\n",eulerangles.x, eulerangles.y,eulerangles.z]];
CGSize size=frame.camera.imageResolution;
[matrixStr appendString:@"imageResolution:\n"];
[matrixStr appendString:[NSString stringWithFormat:@"%.0f %.0f\n",size.width, size.height]];
matrix_float3x3 intrinsics=frame.camera.intrinsics;
[matrixStr appendString:@"intrinsics:\n"];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f\n",intrinsics.columns[0][0],intrinsics.columns[1][0],intrinsics.columns[2][0]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f\n",intrinsics.columns[0][1],intrinsics.columns[1][1],intrinsics.columns[2][1]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f\n",intrinsics.columns[0][2],intrinsics.columns[1][2],intrinsics.columns[2][2]]];
matrix=frame.camera.projectionMatrix;
[matrixStr appendString:@"projectionMatrix:\n"];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][0],matrix.columns[1][0],matrix.columns[2][0],matrix.columns[3][0]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][1],matrix.columns[1][1],matrix.columns[2][1],matrix.columns[3][1]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][2],matrix.columns[1][2],matrix.columns[2][2],matrix.columns[3][2]]];
[matrixStr appendString:[NSString stringWithFormat:@"%.2f %.2f %.2f %.2f\n",matrix.columns[0][3],matrix.columns[1][3],matrix.columns[2][3],matrix.columns[3][3]]];
label.backgroundColor=[UIColor lightGrayColor];
label.text=matrixStr;
//飞船y轴转角跟随相机变化
shipNode.eulerAngles=SCNVector3Make(0, eulerangles.y, 0);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
网友评论