2017-08-07 8 views
2

ARKitを学習し、検出された平面にオブジェクトを配置しようとしています。しかし、正しく動作せず、飛行機と3Dオブジェクトの間にスペースがあります。ARKIT:平面上のオブジェクトが正しく動作しない

は、ここに私の飛行機を検出するためのコードです:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { 
     position = SCNVector3Make(anchor.transform.columns.3.x, anchor.transform.columns.3.y, anchor.transform.columns.3.z) 

     guard let planeAnchor = anchor as? ARPlaneAnchor else { return } 

     let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z)) 

     planeNode = SCNNode(geometry: plane) 
     planeNode.position = position 
     planeNode.transform = SCNMatrix4MakeRotation(-Float.pi/2.0, 1.0, 0.0, 0.0) 
     node.addChildNode(planeNode) 
} 

そして3Dモデルは同じ位置になります:

object.position = position 

をしかし、私はアプリケーションを実行すると、オブジェクトとの間には大きなスペースがあります飛行機。私はなぜそれを理解していないのですか?

答えて

0

アンカーのために変換は世界座標に関連しています。 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)のノードは既にワールド座標に配置されています。だから、あなたが必要とするすべてが - ちょうどあなたのレンダリングノードの子ノードとして自ノードを追加します:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { 
     guard let planeAnchor = anchor as? ARPlaneAnchor else { return } 

     let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z)) 

     planeNode = SCNNode(geometry: plane) 
     planeNode.position = SCNVector3Zero // Position of `planeNode`, related to `node` 
     planeNode.transform = SCNMatrix4MakeRotation(-Float.pi/2.0, 1.0, 0.0, 0.0) 
     node.addChildNode(planeNode) 
    } 
+0

はまだ、残念ながら動作しません:(第一の材料のための10センチメートル半径と赤の色で – Sam

+0

使用SCNSphereジオメトリをテスト・ノードを配置しますそれを正しく配置するのが簡単になります。その後、デバッグした後、必要なオブジェクトを使用することができます。 –

関連する問題