前提・実現したいこと
地球の3Dオブジェクトを使用してタップした位置の天気を表示するアプリを作ろうと思っています。
質問
タップした3Dオブジェクトの位置の座標を取得する方法は有るでしょうか?
該当のソースコード
Swift
1import UIKit 2import SceneKit 3 4class ObjectTest_1: UIViewController { 5 6 var mapName: String = "EarthMap" 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 11 EarthDisplay() 12 13 // Do any additional setup after loading the view. 14 } 15 16 override func didReceiveMemoryWarning() { 17 super.didReceiveMemoryWarning() 18 } 19 20 @IBAction func changeTec(_ sender: Any) { 21 if mapName == "EarthMap" { 22 mapName = "EarthMap2" 23 }else if mapName == "EarthMap2" { 24 mapName = "EarthMap" 25 } 26 27 EarthDisplay() 28 } 29 30 func EarthDisplay() { 31 //SCNSceneを初期化 32 let scene = SCNScene() 33 34 //ジオメトリを設定する SCNGeometry に球を描画する SCNSphere から半径2の球を設定 35 let sphere: SCNGeometry = SCNSphere(radius: 3) 36 //チルドノードにさっきのジオメトリを設定 37 let geometryNode = SCNNode(geometry: sphere) 38 //2秒で1回転するアニメーション 39 //geometryNode.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 1, y: 2, z: 2, duration: 2))) 40 //シーンのルートノードにさっきのチルドノードを設定 41 scene.rootNode.addChildNode(geometryNode) 42 43 44 //球に対して地球の地図テクスチャを貼る 45 sphere.firstMaterial?.diffuse.contents = UIImage(named: mapName) 46 47 //チルドのーどを設定 48 let cameraNode = SCNNode() 49 //チルドのーどのカメラプロパティにSCNCameraを設定 50 cameraNode.camera = SCNCamera() 51 //z軸を10にして手前に移動する(zは大きくなるほどオブジェクトが遠くなる) 52 cameraNode.position = SCNVector3(x: -2, y: 3, z: 6) 53 //シーンのルートノードにチルドノードを設定 54 scene.rootNode.addChildNode(cameraNode) 55 56 //シーンを配置 57 let scnView = self.view as! SCNView 58 scnView.allowsCameraControl = true 59 60 scnView.backgroundColor = UIColor.orange 61 scnView.scene = scene 62 scnView.showsStatistics = true 63 64 let tapAction = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:))) 65 scnView.addGestureRecognizer(tapAction) 66 67 68 } 69 70 71 //タップ時の挙動 72 @objc func handleTap(_ gestureRecognize: UIGestureRecognizer) { 73 print("タップされました") 74 let scnView = self.view as! SCNView 75 // let earthObject = scnView.scene?.rootNode.childNode(withName: "sphere", recursively: true)! 76 77 let p: CGPoint = gestureRecognize.location(in: scnView) 78 print(p) 79 80 } 81 82 83### 試したこと 84 85ここに問題に対して試したことを記載してください。 86 87### 補足情報(FW/ツールのバージョンなど) 88 89ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/28 18:20