使用touch在SceneKit中移动特定节点 [英] Move a specific node in SceneKit using touch

查看:103
本文介绍了使用touch在SceneKit中移动特定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个节点的场景.我想通过点击它来选择一个节点(如果我什么也没点击,我什么也不想发生),并使他仅在XY轴(我知道Z轴的位置)上跟随我的手指.是否有任何方法可以将视图中的位置转换为SceneKit坐标?

I have a scene with multiple nodes. I want to select a node by tapping it (if I tap nothing I want nothing to happen) and make him follow my finger only on XY axis (I know position on Z axis). Is there any method that converts location in view to SceneKit coords?

经过几番研究,我发现了这一点,而这正是我想要的,但是我没有得到代码.有人可以向我解释或帮助我确定如何解决我的问题吗? https://www.youtube.com/watch?v=xn9Bt2PFp0g

After few researches I found this and it's exactly what I want, but I don't get the code. Can somebody explain me or help me figure how can I solve my problem? https://www.youtube.com/watch?v=xn9Bt2PFp0g

推荐答案

func CGPointToSCNVector3(view: SCNView, depth: Float, point: CGPoint) -> SCNVector3 {
    let projectedOrigin = view.projectPoint(SCNVector3Make(0, 0, depth))
    let locationWithz   = SCNVector3Make(Float(point.x), Float(point.y), projectedOrigin.z)
    return view.unprojectPoint(locationWithz)
}

看起来很简单,我做了一个获取3个参数的函数. View是附加场景的SCNView,depth是节点的z值,point是CGPoint,表示3D场景在屏幕上的投影.

Looks like was pretty simple, I've made a function that gets 3 parameters. View is the SCNView where scene is attached to, depth is the z value of node, and point is a CGPoint that represents projection of 3D scene on screen.

这篇关于使用touch在SceneKit中移动特定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆