AR的平面檢測和利用SceneKit構建幾何體

lijingpei2016發表於2018-05-17

ar的檢測方法是ARSCNView的代理的方法

主要程式碼如下:


 _jpARSCNView.delegate = self;

<ARSCNViewDelegate>

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

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

-(void)renderer:(id<SCNSceneRenderer>)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];

複製程式碼

程式碼

相關文章