LoginSignup
6
7

More than 5 years have passed since last update.

黄色い点以外も出せる!?ARPointCloudをカスタマイズして色や形を変えてみよう

Posted at
sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]

を書けば、黄色い花粉のようなfeaturePointsは簡単に表示することができます。

今記事では、この点群の見た目をカスタムしてみましょう。

カスタム完成イメージ

赤い点 赤いコーン

赤い点のARPointCloud作り方

Nodeを作成

赤い点なら

let node = SCNNode()
let geometry = SCNSphere(radius: 0.001)
geometry.firstMaterial?.diffuse.contents = UIColor.red
node.geometry = geometry
return node

赤いコーンなら

let node = SCNNode()
let geometry = SCNCone(topRadius: 0.001, bottomRadius: 0.01, height: 0.03)
geometry.firstMaterial?.diffuse.contents = UIColor.red
node.geometry = geometry
return node

点群を表示する

まずはsceneView.sesionをARSessionDelegateに準拠させる。

featurePointsを保存するために変数作っときます。

private var featurePoints: [SCNNode] = []

override func viewDidLoad() {
    super.viewDidLoad()

    sceneView.session.delegate = self
}

ARSessionDelegateのsession(didUpdate frameメソッドで、ARFrameが毎回取得できるので、その中からrawFeaturePoints(ARPointCloud)を取得し、そのpointsでそれぞれの点のxyz座標を取れます。

extension ARPointCloudViewController: ARSessionDelegate {
    func session(_ session: ARSession, didUpdate frame: ARFrame) {
        / ARFrameのrawFeaturePointsのpointsで座標を取得
        guard let pointPositions = frame.rawFeaturePoints?.points else {
            return
        }
        // 前の点群Nodesを取り除く
        self.featurePoints.forEach { $0.removeFromParentNode() }

        // 新しい点群Nodes作成
        let featurePointNodes: [SCNNode] = pointPositions.map { position in
            let node = sphereNode.clone()
            node.position = SCNVector3(position.x, position.y, position.z)
            return node
        }

        // 新しい点群Nodesを貼り付け
        featurePointNodes.forEach { sceneView.scene.rootNode.addChildNode($0) }

        // 一旦保存
        self.featurePoints = featurePointNodes
    }
}

完成

Nodeをカスタマイズして、色々な点を出してみましょう。

赤い点 赤いコーン

サンプルコード

https://github.com/kboy-silvergym/ARKit-Emperor のARPointCloudにあります!

参考文献

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7