ゴール
部屋の間取り図を作成するアプリを作りたいのですが、実装方法が分からないため、質問させて下さい。
①ARKitで垂直面(部屋の壁)を検出
②画面タップで球体(sphere)を表示(x,y,z) = (0,0,0)
③球体を長押しで消える
④部屋の壁(4面)に沿うように球体を設置
⑤最初に設置した球体と、最後に設置した球体が接触したら完了
現状
①〜④までは実装できていると思います。ですが、⑤の機能を実装するために必要な「各オブジェクトの設置された順番を管理する機能」がどうすれば実装できるか分かりません先輩方のお力をお借りしたいです。よろしくお願い致します。
swift
1import UIKit 2import SceneKit 3import ARKit 4 5class ViewController: UIViewController, ARSCNViewDelegate { 6 7 @IBOutlet var sceneView: ARSCNView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 // Set the view's delegate 13 sceneView.delegate = self 14 sceneView.scene = SCNScene() 15 sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints] 16 sceneView.autoenablesDefaultLighting = true 17 18 let configuration = ARWorldTrackingConfiguration() 19 configuration.planeDetection = .vertical 20 sceneView.session.run(configuration) 21 22 // タップイベントハンドラの登録 23 let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapView)) 24 sceneView.addGestureRecognizer(tapGesture) 25 26 // ロングプレスイベントハンドラの登録 27 let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressView)) 28 sceneView.addGestureRecognizer(longPressGesture) 29 } 30 31 @objc func longPressView(sender: UILongPressGestureRecognizer) { 32 if sender.state == .began { 33 let location = sender.location(in: sceneView) 34 let hitTest = sceneView.hitTest(location) 35 if let result = hitTest.first { 36 if result.node.name == "sphere" 37 { 38 result.node.removeFromParentNode(); 39 } 40 } 41 } 42 } 43 44 @objc func tapView(sender: UITapGestureRecognizer) { 45 // タップした座標にヒットする平面を取得する 46 let location = sender.location(in: sceneView) 47 let hitTestResult = sceneView.hitTest(location, types: .existingPlane) 48 if let result = hitTestResult.first { 49 // 球体を作成 50 let sphereNode = SCNNode() 51 sphereNode.name = "sphere" 52 sphereNode.geometry = SCNSphere(radius: 0.05) 53 54 // Sphereのマテリアルを設定 55 let material = SCNMaterial() 56 material.diffuse.contents = UIColor.red 57 material.diffuse.intensity = 0.8; 58 sphereNode.geometry?.materials = [material] 59 60 // Sphereの座標を設定 61 sphereNode.position = SCNVector3( 62 result.worldTransform.columns.3.x, 63 result.worldTransform.columns.3.y, 64 result.worldTransform.columns.3.z 65 ) 66 67 sceneView.scene.rootNode.addChildNode(sphereNode) 68 } 69 } 70 71 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { 72 }
あなたの回答
tips
プレビュー