从CGPoint获取SCNVector3 [英] Get SCNVector3 from CGPoint

查看:134
本文介绍了从CGPoint获取SCNVector3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从CGPoint获取SCNVector3.我正在使用手势识别器来获取触摸的位置(作为CGPoint).

I am trying to get a SCNVector3 from a CGPoint. I am using a gesture recognizer to get the location of a touch (as a CGPoint).

问题在于,当我进行测试时,触摸并不总是会碰到东西,因为并不总是会碰到任何物体. (触摸空白处,将船移至空白处.)

The problem is that the touch doesn't always hit something when I hit test because there isn't always an object being touched. (Touch an empty space to move your ship to that empty spot).

我发现的其他堆栈溢出问题使用SCNHitTestResult获取worldCoordinates,但这对我不起作用.

Other Stack Overflow question that I have found uses the SCNHitTestResult to get the worldCoordinates but that doesn't work for me.

有人知道如何找到这个吗?鉴于我当然知道z坐标.移动的飞船始终以z位置1移动.

Does anyone know how to find this? Given that I know the z coordinate of course. Ships that move always move with a z position of 1.

我需要worldCoordinates能够使用将SCNNode移动到具有CGPoint的接触点的动作.谢谢!

I Need worldCoordinates to be able to use actions that move a SCNNode to a touch point which has a CGPoint. Thanks!

推荐答案

那么,您要将视图空间中的点转换为场景空间中的点吗?当然,要抓住的是场景空间具有三维空间,而视图空间没有三维空间.您使用SCNView(或其他渲染器)方法

So, you want to turn a point in view space into a point in scene space? The catch to that, of course, is that scene space has a third dimension and view space doesn't. You use the SCNView (or other renderer) methods projectPoint and unprojectPoint to convert between scene space, which is 3D, and view space, which is... also 3D? Yep — two dimensions of screen pixelspoints, and one of normalized depth: the z-coordinate is 0 for points on the near clipping plane and 1 for points on the far clipping plane.

无论如何,您有一个有用的约束,因为您希望将视图空间点映射到场景空间中的特定平面(z=1)上.如果场景空间的方向使得该平面与视图方向正交,即相机直接指向+ z或-z方向,则约束将更为有用.

Anyhow, you have a useful constraint in that you're looking to map view-space points onto a specific plane (z=1) in scene space. You have an even more useful constraint if your scene space is oriented so that said plane is orthogonal to the view direction — i.e. the camera is pointing directly in the +z or -z direction.

如果要将视图空间点映射到特定的场景空间深度,则需要知道该平面的视图空间深度是多少.为此使用projectPoint

If you want to map a view-space point to a particular scene-space depth, you need to know what the view-space depth for that plane is. Use projectPoint for that:

SCNVector3 projectedPlaneCenter = [view projectPoint:planeNode.position];
float projectedDepth = projectedPlaneCenter.z;

现在,抓住它,只要需要将触摸位置映射到该平面上,就可以使用它:

Now, hold onto that and you can make use of it whenever you need to map a touch location onto that plane:

CGPoint vp = [recognizer locationInView:view];
SCNVector3 vpWithDepth = SCNVector3Make(vp.x, vp.y, projectedDepth);
SCNVector3 scenePoint = [view unprojectPoint:vpWithDepth];


如果场景的z轴未与相机平行,则难度会增加一些-您必须确定z=1平面在哪里与所处理的任何视点无关.在这种情况下,您可能会发现向场景中添加不可见的SCNPlane并使用hitTest/worldCoordinates方法来定位该平面上的点会更容易.


If your scene isn't oriented with the z-axis parallel to the camera, it's a bit harder — you have to work out where your z=1 plane is independently for any view-space point you process. In that case, you might find it easier to add an invisible SCNPlane to your scene and use the hitTest/worldCoordinates method to locate points on that plane.

这篇关于从CGPoint获取SCNVector3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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