gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:46:42 26 4
gpt4 key购买 nike

我想创建一个示例应用程序,允许用户在点击地球上的大陆时获取有关大陆的信息。为此,我需要找出用户在场景 (SceneKit) 中点击 SCNSphere 对象的位置。我试图这样做:

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


}
}

}

但是,当我实际运行此代码并单击球体上的某个区域时,它会在屏幕上打印出点击的坐标,而不是我在球体上点击的位置。例如,当我点击球体的中心时,坐标是相同的,当我旋转球体后再次在中心点击它时,坐标是相同的。

我想知道我在实际球体上按下的位置,而不仅仅是我在屏幕上单击的位置。解决这个问题的最佳方法是什么?

最佳答案

在 hitResult 中,您可以获得 result.textureCoordinates,它告诉您 map 纹理中的点。从这一点开始,您应该知道 map 的位置,因为 map 应该具有映射到纹理的坐标。

 @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


}
}

关于ios - 获取 SCNSphere 上的点击位置 - Swift (SceneKit)/iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54409801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com