美文网首页
ARKit和SceneKit的一些代码片段(备忘)

ARKit和SceneKit的一些代码片段(备忘)

作者: GanGai | 来源:发表于2023-10-17 15:53 被阅读0次

工具类头部声明,其他一些启动方法等忽略

@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
  1. 在检测到的水平面上面新增一个水平面
- (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];
        
        }
    }
}
  1. 新增一个垂直平面
- (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]]];
}
  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];
    }
}
  1. 新增一个自定义贝塞尔曲线绘制的立体形状
- (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;
}
  1. 通过遍历寻找场景的平面新增水平面
- (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];
    }
}
  1. 在相机前方位置新增几何体
- (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]]];
}

相关文章

  • ARKit & OpenGL ES - ARKit原理篇

    iOS11推出了新框架ARKit,通过ARKit和SceneKit可以很方便的制作AR App。苹果也提供了AR基...

  • ARKit(二)

    ARSCNView 可以添加 3D 对象进去,它无缝的提供了 ARKit 和 SceneKit 的整合,如果需要用...

  • 函数式Swift5 - 案例研究 QuickCheck

    本文是一个系列,是函数式Swift的读书笔记(其实是为了备忘) 测试通常由一些代码片段和预期结果组成。执行代码之后...

  • ARKit和CoreLocation:第三部分

    演示代码ARKit和CoreLocation:第一部分ARKit和CoreLocation:第二部分ARKit和C...

  • ARKit和CoreLocation:第一部分

    演示代码ARKit和CoreLocation:第一部分ARKit和CoreLocation:第二部分ARKit和C...

  • ARKit和CoreLocation:第二部分

    演示代码ARKit和CoreLocation:第一部分ARKit和CoreLocation:第二部分ARKit和C...

  • 10-Materials材质

    文章选自掘金苹果API搬运工的文章[SceneKit专题]10-Materials材质主要记录自己在学习ARKit...

  • 12-Shadows阴影

    文章选自掘金苹果API搬运工的文章[SceneKit专题]12-Shadows阴影主要记录自己在学习ARKit的过...

  • 2-physics物理效果

    文章选自掘金苹果API搬运工的文章[SceneKit专题]2-physics物理效果主要记录自己在学习ARKit的...

  • 通过Metal展示AR体验

    在相机信息流上控制你的app虚拟内容渲染。 一、概述 ARKit包含视图类,可轻松显示SceneKit或Sprit...

网友评论

      本文标题:ARKit和SceneKit的一些代码片段(备忘)

      本文链接:https://www.haomeiwen.com/subject/arqqidtx.html