今日やったこと
- ARアプリの続き
- 平面を探す
- 平面にグリッドを表示させる
DitectPlaneAR
平面を探し、見つけたらテキストを表示するアプリ
let configuration = ARWorldTrackingConfiguration()
が平面を見つけてくれるそう。
以下での言及されている。
https://qiita.com/chino_tweet/items/2591648bea43dfccd898
ViewController.swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Run the view's session
sceneView.session.run(configuration)
}
GridOverlay
上記Appの派生。
平面を見つけたら、そこにグリッド(十字線)を表示させる
GridPlane.swift
import Foundation
import SceneKit
import ARKit
class GridPlane: SCNNode{
var anchor : ARPlaneAnchor
var planeGeometry : SCNPlane!
init(anchor: ARPlaneAnchor) {
self.anchor = anchor
super.init()
setup()
}
func update(anchor: ARPlaneAnchor){
self.planeGeometry.width = CGFloat(anchor.extent.x)
self.planeGeometry.height = CGFloat(anchor.extent.z)
self.position = SCNVector3Make(anchor.center.x, 0, anchor.center.z)
}
private func setup(){
self.planeGeometry = SCNPlane(width: CGFloat(self.anchor.extent.x), height: CGFloat(self.anchor.extent.z))
let material = SCNMaterial()
material.diffuse.contents = UIImage(named: "overlay_grid.png")
self.planeGeometry.materials = [material]
let planenode = SCNNode(geometry: self.planeGeometry)
planenode.position = SCNVector3Make(anchor.center.x,0,anchor.center.z)
planenode.transform = SCNMatrix4MakeRotation(Float(-Double.pi / 2.0), 1.0, 0.0 , 0.0)
self.addChildNode(planenode)
}
required init?(coder aDecoder: NSCoder){
fatalError("Fatal Error!")
}
}