工具类头部声明,其他一些启动方法等忽略
@interface XWAIARCamera ()<ARSCNViewDelegate>
/// AR场景配置
@property (nonatomic, strong) ARWorldTrackingConfiguration *configuration;
/// AR场景会话
@property (nonatomic, strong) ARSession *session;
/// AR场景view
@property (nonatomic, readwrite, strong) ARSCNView *sceneView;
/// 中心点图标:
@property (nonatomic, strong) UIImageView *targetImageView;
/// 父亲视图
@property (nonatomic, weak) UIView *sceneViewSuperView;
/// 距离测量所有线条节点元素
@property (nonatomic, strong) NSMutableArray<ARMeasureElement *> *elementList;
/// 当前震动的节点
@property (nonatomic, strong) SCNNode *shakeNode;
@end
API_AVAILABLE(ios(13.0))
@interface XWAIARCamera ()<ARCoachingOverlayViewDelegate>
/// AR指示器
@property (nonatomic, strong) ARCoachingOverlayView *coachingOverlay;
@end
- 在检测到的水平面上面新增一个水平面
- (void)goodAddBoxPlane {
NSArray<ARHitTestResult *> *results = [self.sceneView hitTest:CGPointMake(CGRectGetWidth(self.sceneView.frame)/2.0, CGRectGetHeight(self.sceneView.frame)/2.0) types:ARHitTestResultTypeExistingPlane];
// NSLog(@"results: %@",results);
if(results && results.count > 0) {
ARHitTestResult *result = results.firstObject;
NSLog(@"找到锚点: %@",result.anchor);
simd_float4x4 transform = result.worldTransform;
SCNVector3 worldPosition = SCNVector3Make(transform.columns[3].x, transform.columns[3].y, transform.columns[3].z);
NSLog(@"\nx:%f \n y:%f \n z:%f\n",worldPosition.x,worldPosition.y,worldPosition.z);
if ([result.anchor isKindOfClass:[ARPlaneAnchor class]]) {
ARPlaneAnchor *findPlaneAnchor = (ARPlaneAnchor *)result.anchor;
NSLog(@"extentX:%f extentY:%f extentZ:%f",findPlaneAnchor.extent.x,findPlaneAnchor.extent.y,findPlaneAnchor.extent.z);
NSLog(@"centerX:%f centerY:%f centerZ:%f",findPlaneAnchor.center.x,findPlaneAnchor.center.y,findPlaneAnchor.center.z);
SCNNode *node = [self.sceneView nodeForAnchor:findPlaneAnchor];
SCNBox *plane = [SCNBox boxWithWidth:findPlaneAnchor.extent.x height:0.0 length:findPlaneAnchor.extent.z chamferRadius:0];
plane.firstMaterial.writesToDepthBuffer = NO;
plane.firstMaterial.diffuse.contents = [UIColor xwai_randomColor];
SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
planeNode.position = SCNVector3Make(findPlaneAnchor.center.x, 0, findPlaneAnchor.center.z);
[node addChildNode:planeNode];
}
}
}
- 新增一个垂直平面
- (void)addSCNPlane {
matrix_float4x4 translation = matrix_identity_float4x4;
translation.columns[3].z = -0.2;
// translation.columns[3].y = -0.1;
matrix_float4x4 transform = matrix_multiply(self.session.currentFrame.camera.transform, translation);
SCNVector3 position = SCNVector3Make(transform.columns[3].x, transform.columns[3].y, transform.columns[3].z);
SCNPlane *plane = [SCNPlane planeWithWidth:0.05 height:0.05];
plane.firstMaterial.diffuse.contents = [UIColor greenColor];
plane.firstMaterial.lightingModelName = SCNLightingModelConstant;
plane.firstMaterial.doubleSided = YES;
SCNNode *geoNode = [SCNNode nodeWithGeometry:plane];
geoNode.position = position; // SCNVector3Make(0, 0, -0.5);
[self.sceneView.scene.rootNode addChildNode:geoNode];
[geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:0 z:2 duration:1]]];
}
- 新增一个球体
- (void)addBall {
NSArray<ARHitTestResult *> *results = [self.sceneView hitTest:CGPointMake(CGRectGetWidth(self.sceneView.frame)/2.0, CGRectGetHeight(self.sceneView.frame)/2.0) types:ARHitTestResultTypeExistingPlane];
if(results && results.count > 0) {
ARHitTestResult *result = results.firstObject;
// NSLog(@"平面距离:%f",result.distance);
simd_float4x4 transform = result.worldTransform;
SCNVector3 worldPosition = SCNVector3Make(transform.columns[3].x, transform.columns[3].y, transform.columns[3].z);
NSLog(@"x:%f \n y:%f \n z:%f\n",worldPosition.x,worldPosition.y,worldPosition.z);
SCNSphere *dot = [SCNSphere sphereWithRadius:0.75]; // 0.5
dot.firstMaterial.diffuse.contents = [UIColor redColor];
dot.firstMaterial.lightingModelName = SCNLightingModelConstant;
dot.firstMaterial.doubleSided = YES;
SCNNode *startNode = [SCNNode nodeWithGeometry:dot];
startNode.scale = SCNVector3Make(1/300.0, 1/300.0, 1/300.0);
startNode.position = worldPosition;
[self.sceneView.scene.rootNode addChildNode:startNode];
}
}
- 新增一个自定义贝塞尔曲线绘制的立体形状
- (void)addCustomShapeByCurrentFrame {
matrix_float4x4 translation = matrix_identity_float4x4;
translation.columns[3].z = -0.3;
matrix_float4x4 transform = matrix_multiply(self.session.currentFrame.camera.transform, translation);
SCNVector3 position = SCNVector3Make(transform.columns[3].x, transform.columns[3].y, transform.columns[3].z);
SCNPlane *plane = [SCNPlane planeWithWidth:0.05 height:0.05];
plane.firstMaterial.diffuse.contents = [UIColor greenColor];
plane.firstMaterial.lightingModelName = SCNLightingModelConstant;
plane.firstMaterial.doubleSided = YES;
SCNSphere *dot = [SCNSphere sphereWithRadius:0.05]; // 0.5
dot.firstMaterial.diffuse.contents = [UIColor redColor];
dot.firstMaterial.lightingModelName = SCNLightingModelConstant;
dot.firstMaterial.doubleSided = YES;
UIBezierPath *bezierPath = [self getBezierPath];
// UIBezierPath *piePiece = [UIBezierPath bezierPath];
// [piePiece addArcWithCenter:CGPointZero radius:0.0750 startAngle:0.0 endAngle:M_PI/6 clockwise:YES];
// [piePiece closePath];
// bezierPath = piePiece;
SCNShape *customShape = [SCNShape shapeWithPath:bezierPath extrusionDepth:0.01];
// customShape.chamferRadius = 0.2;
// customShape.chamferMode = SCNChamferModeBoth;
customShape.firstMaterial.diffuse.contents = [UIColor xwai_randomColorWithoutAlpha];
customShape.firstMaterial.lightingModelName = SCNLightingModelConstant;
customShape.firstMaterial.doubleSided = YES;
// shapeNode.eulerAngles = SCNVector3Make(0,-M_PI_2/6, 0);
SCNNode *geoNode = [SCNNode nodeWithGeometry:customShape];
// geoNode.eulerAngles = SCNVector3Make(0, 0, M_PI);
geoNode.position = position;
[self.sceneView.scene.rootNode addChildNode:geoNode];
// [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:0 z:2 duration:1]]];
}
- (UIBezierPath *)getBezierPathFrom3DPointList:(NSArray<NSValue *> *)nodeArray {
UIBezierPath *path = [UIBezierPath new];
for (int i = 0; i < nodeArray.count; i++) {
SCNVector3 node = nodeArray[i].SCNVector3Value;
CGPoint tempPoint = CGPointMake(node.x, node.z);
if(i == 0) {
[path moveToPoint:tempPoint];
} else {
[path addLineToPoint:tempPoint];
}
}
[path closePath];
return path;
}
- (UIBezierPath *)getBezierPath {
/*
CGPoint firstPoint = CGPointMake(0.10, 0.10);
CGPoint secondPoint = CGPointMake(0.20, 0.10);
CGPoint thridPoint = CGPointMake(0.20, 0.15);
CGPoint fourthPoint = CGPointMake(0.15, 0.15);
CGPoint fifthPoint = CGPointMake(0.15, 0.20);
CGPoint sixthPoint = CGPointMake(0.10, 0.20);
NSArray *pointArray = @[
NSStringFromCGPoint(firstPoint),
NSStringFromCGPoint(secondPoint),
NSStringFromCGPoint(thridPoint),
NSStringFromCGPoint(fourthPoint),
NSStringFromCGPoint(fifthPoint),
NSStringFromCGPoint(sixthPoint)
];
//*/
//*/
CGPoint firstPoint = CGPointMake(0.00, 0.00);
CGPoint secondPoint = CGPointMake(0.10, 0.05);
CGPoint thridPoint = CGPointMake(0.05, 0.10);
CGPoint fourthPoint = CGPointMake(0.00, 0.00);
NSArray *pointArray = @[
NSStringFromCGPoint(firstPoint),
NSStringFromCGPoint(secondPoint),
NSStringFromCGPoint(thridPoint),
NSStringFromCGPoint(fourthPoint)
];
//*/
UIBezierPath *path = [UIBezierPath new];
for (int i = 0; i < pointArray.count; i++) {
CGPoint tempPoint = CGPointFromString(pointArray[i]);
if(i == 0) {
[path moveToPoint:tempPoint];
} else {
[path addLineToPoint:tempPoint];
}
}
[path closePath];
return path;
}
- 通过遍历寻找场景的平面新增水平面
- (void)addPlaneByForinSceneView {
NSLog(@"%@",self.sceneView.scene.rootNode.childNodes);
ARPlaneAnchor *findPlaneAnchor = nil;
for (SCNNode *node in self.sceneView.scene.rootNode.childNodes) {
NSLog(@"%@",[self.sceneView anchorForNode:node]);
ARAnchor *anchor = [self.sceneView anchorForNode:node];
if([anchor isKindOfClass:[ARPlaneAnchor class]]) {
NSLog(@"平面锚点: %@",anchor);
findPlaneAnchor = (ARPlaneAnchor *)anchor;
break;
}
}
if(findPlaneAnchor) {
SCNNode *node = [self.sceneView nodeForAnchor:findPlaneAnchor];
// NSLog(@"%f %f %f",planeAnchor.extent.x,planeAnchor.extent.y,planeAnchor.extent.z);
SCNBox *plane = [SCNBox boxWithWidth:findPlaneAnchor.extent.x height:0 length:findPlaneAnchor.extent.z chamferRadius:0];
plane.firstMaterial.diffuse.contents = [UIColor xwai_randomColor];
SCNNode *planeNode = [SCNNode nodeWithGeometry:plane];
planeNode.position = SCNVector3Make(findPlaneAnchor.center.x, 0, findPlaneAnchor.center.z);
[node addChildNode:planeNode];
}
}
- 在相机前方位置新增几何体
- (void)addBoxDemo {
CGFloat s = 0.1; // 10厘米
SCNBox *planeBox = [SCNBox boxWithWidth:s height:s length:0.025 chamferRadius:0]; // 以米为单位 0.0025
// 3. 使用Material渲染3D模型(默认模型是白色的)
planeBox.firstMaterial.diffuse.contents = [UIColor whiteColor];
// 4. 创建一个基于3D物体模型的节点
SCNNode *planeNode = [SCNNode nodeWithGeometry:planeBox];
// 5. 设置节点的位置为捕捉到的平地的锚点的中心位置
// SceneKit中节点的位置position是一个基于3D坐标系的矢量坐标SCNVector3Make
planeNode.position = SCNVector3Make(0.0, 0.0, -0.5); // 以米为单位
[self.sceneView.scene.rootNode addChildNode:planeNode];
[planeNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:0 z:2 duration:1]]];
}
网友评论