从触摸位置swift获取SCNNode环境中的向量

时间:2022-11-15 21:44:27

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.

我有我的相机的位置和方向,屏幕上的CGPoint触摸位置,我需要在我的3D SCNNode环境中触摸屏幕的方向上的线(最好是矢量),我该怎么做到这一点?代码段非常有用。

1 个解决方案

#1


1  

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

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

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:

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

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.

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

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) 
}

#1


1  

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

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

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:

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

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.

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

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) 
}