从触摸位置 swift 获取 SCNNode 环境中的矢量 [英] Get vector in SCNNode environment from touch location swift

查看:45
本文介绍了从触摸位置 swift 获取 SCNNode 环境中的矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的相机的位置和方向,屏幕上的 CGPoint 触摸位置,我需要在我的 3d SCNNode 环境中我在屏幕上触摸的方向上的线(最好是矢量),我怎样才能得到这个?
代码片段会很有帮助.

I have the position and orientation of my camera, the CGPoint touch location on the screen, I need the line (preferably vector) in the direction that I touched on the screen in my 3d SCNNode environment, how can I get this?
A code snippet would be very helpful.

推荐答案

您可以使用 SCNSceneRenderer.unprojectPoint(_:) 方法.

You can use the SCNSceneRenderer.unprojectPoint(_:) method for this.

此方法由SCNView 实现,将您点的坐标作为SCNVector3.在视图的坐标空间中设置前两个元素.Apple 描述了第三个元素的使用:

This method, which is implemented by SCNView, takes the coordinates of your point as a SCNVector3. Set the first two elements in the coordinate space of your view. Apple describes the use of the third element:

点参数的 z 坐标描述了相对于渲染器视锥体(由其定义)的近和远剪裁平面取消投影点的深度观点看法节点).取消投影 z 坐标为 0.0 的点将返回近剪裁平面上的一个点;取消投影 z 坐标为 1.0 的点将返回远剪裁平面上的一个点.

The z-coordinate of the point parameter describes the depth at which to unproject the point relative to the near and far clipping planes of the renderer’s viewing frustum (defined by its pointOfView node). Unprojecting a point whose z-coordinate is 0.0 returns a point on the near clipping plane; unprojecting a point whose z-coordinate is 1.0 returns a point on the far clipping plane.

您不是在寻找这些点的位置,而是在寻找连接它们的线.只需减去两者即可得到这条线.

You are not looking for the location of these points, but for the line that connects them. Just subtract both to get the line.

func getDirection(for point: CGPoint, in view: SCNView) -> SCNVector3 {
    let farPoint  = view.unprojectPoint(SCNVector3Make(point.x, point.y, 1))
    let nearPoint = view.unprojectPoint(SCNVector3Make(point.x, point.y, 0))

    return SCNVector3Make(farPoint.x - nearPoint.x, farPoint.y - nearPoint.y, farPoint.z - nearPoint.z) 
}

这篇关于从触摸位置 swift 获取 SCNNode 环境中的矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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