0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【ARKit】平面を検出し、大きさを更新する

Posted at

##1.ARKitを使うための設定をする
####変数の設定

let configuration = ARWorldTrackingConfiguration()//コンフィギュレーションの作成
@IBOutlet weak var sceneView: ARSCNView! //ARSCNViewの紐付け

####ViewDidLoad()


 override func viewDidLoad() {
    super.viewDidLoad()
    self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints,ARSCNDebugOptions.showWorldOrigin]//座標と特徴点の表示
    self.configuration.planeDetection = .horizontal//水平面の検出
    self.sceneView.session.run(configuration)//セッション開始
    self.sceneView.delegate = self
        // Do any additional setup after loading the view.
    }

##2.SCNNodeを返り値に持つ関数の作成

func createNode(planeAnchor: ARPlaneAnchor) -> SCNNode{
        let plameNode = SCNNode(geometry: SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z)))
        planeNode.geometry?.firstMaterial?.diffuse.contents = UIColor.white
        planeNode.geometry?.firstMaterial?.isDoubleSided = true
        planeNode.position = SCNVector3(planeAnchor.center.x,planeAnchor.center.y,planeAnchor.center.z)
        planeNode.eulerAngles = SCNVector3(90.degreesToRadians,0,0)
        return lavaNode
    }

##3.renderer()に平面をが追加、更新された時の挙動を記述する。

####平面が追加された時
引数にdidAddがあるrendererを指定します。

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
        let planeNode = createNode(planeAnchor: planeAnchor)
        self.sceneView.scene.rootNode.addChildNode(planeNode)
    }

####平面が更新された時
引数にdidUpdateがあるものを指定します。

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
        node.enumerateChildNodes{ (childNode, _) in
            childNode.removeFromParentNode()
        }
        let planeNode = createNode(planeAnchor: planeAnchor)
        node.addChildNode(planeNode)
    }
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?