我正在与Swift和SceneKit合作开发一款iOS应用程序.当用户在屏幕上点击时,我想在SceneKit中用户刚刚点击的位置渲染一个点.我怎么能做到这一点?
我正在使用unprojectPoint()方法,但我的对象未呈现在预期位置.
我有一个场景视图,相机是这样的:
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
然后我在点击位置渲染这个点,如下所示:
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
// retrieve the SCNView
let scnView = self.view as! SCNView
// create dot
let geo = SCNSphere(radius: CGFloat(0.1))
geo.firstMaterial?.diffuse.contents = UIColor.red
let dotNode = SCNNode(geometry: geo)
let p = gestureRecognize.location(in: scnView)
let uP = scnView.unprojectPoint(SCNVector3(p.x, p.y, 5))
// HERE - how to set position for the dot ?
dotNode.position = SCNVector3(uP.x, uP.y, 5)
scnView.scene?.rootNode.addChildNode(dotNode)
}
这是我的Github回购:https://github.com/rudyhuynh/SceneKitExample