AR的平面检测和利用SceneKit构建几何体

ar的检测方法是ARSCNView的代理的方法

主要代码如下:


 _jpARSCNView.delegate = self;



-(void)renderer:(id)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor ;

-(void)renderer:(id)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor ;

-(void)renderer:(id)renderer didRemoveNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor ;

构建几何体的主要代码如下:

利用SCNBox来创建

    SCNScene * jpScene = [[SCNScene alloc]init];

    SCNBox * jpBox = [SCNBox boxWithWidth:0.1 height:0.1 length:0.1 chamferRadius:0];

    SCNNode * jpNode = [SCNNode nodeWithGeometry:jpBox];

    jpNode.position = SCNVector3Make(0, 0, -0.5);

    [jpScene.rootNode addChildNode:jpNode];
    
    self.jpARSCNView.scene = jpScene;

利用UIImage 和 SCNMaterial 来创建

        self.anchor = anchor;
        
        //extent 范围
        CGFloat w = anchor.extent.x;
        
        CGFloat h = anchor.extent.z;
        
        _planeGeometry = [SCNPlane planeWithWidth:w height:h];
        
        SCNMaterial * material = [[SCNMaterial alloc]init];
        
        UIImage * image = [UIImage imageNamed:@"fabric"];
        
        //扩散 - diffuse
        material.diffuse.contents = image;
        
        material.lightingModelName = SCNLightingModelPhysicallyBased;
        
        _planeGeometry.materials = @[material];
        
        SCNNode * planeNode = [SCNNode nodeWithGeometry:_planeGeometry];
        
        planeNode.position = SCNVector3Make(anchor.center.x, 0, anchor.center.z);
        
        // SceneKit 里的平面默认是垂直的,所以需要旋转90度来匹配 ARKit 中的平面
        CGFloat x = -M_PI/2.0;
        planeNode.transform = SCNMatrix4MakeRotation(x, 1.0, 0.0, 0.0);
        
        [self setTextureScale];
        
        [self addChildNode:planeNode];

代码

你可能感兴趣的:(AR的平面检测和利用SceneKit构建几何体)