简体   繁体   English

获取 SCNSphere 上的点击位置 - Swift (SceneKit) / iOS

[英]Getting location of tap on SCNSphere - Swift (SceneKit) / iOS

I want to create a sample application that allows the user to get information about continents on a globe when they tap on them.我想创建一个示例应用程序,允许用户在点击地球上的大陆时获取有关它们的信息。 In order to do this, I need to figure out the location where a user taps on an SCNSphere object in a scene (SceneKit).为了做到这一点,我需要找出用户在场景 (SceneKit) 中点击 SCNSphere 对象的位置。 I attempted to do it like this:我试图这样做:

import UIKit
import SceneKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = SCNScene()

        /* Lighting and camera added (hidden)*/

        let earthNode = SCNSphere(radius: 1)
        /* Added styling to the Earth (hidden)*/
        earthNode.name = "Earth"
        scene.rootNode.addChildNode(earthNode)

        let sceneView = self.view as! SCNView
        sceneView.scene = scene

        sceneView.allowsCameraControl = true

        // add a tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
        sceneView.addGestureRecognizer(tapGesture)

    }


    @objc func handleTap(_ gestureRecognize: UIGestureRecognizer) {
        // retrieve the SCNView
        let sceneView = self.view as! SCNView

        // check what nodes are tapped
        let p = gestureRecognize.location(in: scnView)
        let hitResults = sceneView.hitTest(p, options: [:])
        // check that we clicked on at least one object
        if hitResults.count > 0 {
            // retrieved the first clicked object
            let result: SCNHitTestResult = hitResults[0]

            print(result.node.name!)
            print("x: \(p.x) y: \(p.y)") // <--- THIS IS WHERE I PRINT THE COORDINATES


        }
    }

}

When I actually run this code however and click on an area on my sphere, it prints out the coordinates of the tap on the screen instead of where I tapped on the sphere.但是,当我实际运行此代码并单击球体上的某个区域时,它会打印出屏幕上点击的坐标,而不是我点击球体的位置。 For example, the coordinates are the same when I tap on the center of the sphere, and when I tap it in the center again after rotating the sphere.例如,当我点击球体的中心时,坐标是相同的,当我旋转球体后再次点击它的中心时,坐标是相同的。

I want to know where on the actual sphere I pressed, not just where I click on the screen.我想知道我按下了实际球体的哪个位置,而不仅仅是我在屏幕上单击的位置。 What is the best way that I should go about this problem?我应该解决这个问题的最佳方法是什么?

In the hitResult, you can get result.textureCoordinates which tells you the point in your map textures.在 hitResult 中,您可以获得 result.textureCoordinates,它告诉您地图纹理中的点。 From this point, you are supposed to know the location of your map as the map should have coordinations which was mapped to textures.从这一点来看,您应该知道地图的位置,因为地图应该具有映射到纹理的坐标。

 @objc func handleTap(_ gestureRecognize: UIGestureRecognizer) {
    // retrieve the SCNView
    let sceneView = self.view as! SCNView

    // check what nodes are tapped
    let p = gestureRecognize.location(in: scnView)
    let hitResults = sceneView.hitTest(p, options: [:])
    // check that we clicked on at least one object
    if hitResults.count > 0 {
        // retrieved the first clicked object
        let result: SCNHitTestResult = hitResults[0]

        print(result.node.name!)
        print(result.textureCoordinates(withMappingChannel 0)) // This line is added here. 
        print("x: \(p.x) y: \(p.y)") // <--- THIS IS WHERE I PRINT THE COORDINATES


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM