如何找到在使用SceneKit场景我的鼠标点? [英] How do I find my mouse point in a scene using SceneKit?

查看:1022
本文介绍了如何找到在使用SceneKit场景我的鼠标点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成立了SceneKit场景,相继出台了命中测试选择一个项目。不过,我希望能够移动该项目沿平面在我的场景。我不断收到鼠标拖动事件,但不知道如何变换场景的二维坐标到三维坐标。

I have set up a scene in SceneKit and have issued a hit-test to select an item. However, I want to be able to move that item along a plane in my scene. I continue to receive mouse drag events, but don't know how to transform those 2D coordinates into 3D coordinate in the scene.

我的情况是很简单的。摄像头位于0,0,50,指着0,0,0。我只是想为0的z值拖我的对象沿Z平面。

My case is very simple. The camera is located at 0, 0, 50 and pointed at 0, 0, 0. I just want to drag my object along the z-plane with a z-value of 0.

点击测试用得好好的,但我怎么翻译从拖动事件鼠标点到场景的3D对象,我拖着一个新的位置?

The hit-test works like a charm, but how do I translate the mouse point from a drag event into a new position in the scene for the 3D object I am dragging?

推荐答案

您不需要使用看不见的几何图形 - 场景套件可以做你需要的所有坐标转换,而无需打测试不可见的对象。基本上你需要做同样的事情,你会在2D绘图应用程序移动对象:找到了 MOUSEDOWN之间的偏差:位置和目标位置,然后为每个的mouseMoved:,添加偏移到新的鼠标位置设置对象的新位置。

You don't need to use invisible geometry — Scene Kit can do all the coordinate conversions you need without having to hit test invisible objects. Basically you need to do the same thing you would in a 2D drawing app for moving an object: find the offset between the mouseDown: location and the object position, then for each mouseMoved:, add that offset to the new mouse location to set the object's new position.

下面是你可以使用的方法...

Here's an approach you could use...

  1. 点击测试最初的点击位置,你已经在做的事情。这可以让你的 SCNHitTestResult 对象确定要移动的节点,对吧?

  1. Hit-test the initial click location as you're already doing. This gets you an SCNHitTestResult object identifying the node you want to move, right?

检查<一href="https://developer.apple.com/library/mac/documentation/SceneKit/Reference/SCNHitTestResult_Class/Reference/SCNHitTestResult.html#//apple_ref/occ/instp/SCNHitTestResult/worldCoordinates"><$c$c>worldCoordinates那命中测试结果性能。如果要移动的节点的子场景的<一个href="https://developer.apple.com/library/mac/documentation/SceneKit/Reference/SCNScene_Class/Reference/SCNScene.html#//apple_ref/occ/instp/SCNScene/rootNode"><$c$c>rootNode,这是你想要的载体查找偏移。 (否则,你就需要将其转换为要移动的坐标节点的父系统 - 看<一href="https://developer.apple.com/library/Mac/documentation/SceneKit/Reference/SCNNode_Class/Reference/SCNNode.html#//apple_ref/occ/instm/SCNNode/convertPosition%3atoNode%3a"><$c$c>convertPosition:toNode: convertPosition:FROMNODE:

您会需要一个参考深度这一点,所以你可以比较的mouseMoved:的位置吧。使用<一个href="https://developer.apple.com/library/Mac/documentation/SceneKit/Reference/SCNSceneRenderer_Protocol/Reference/SCNSceneRenderer.html#//apple_ref/occ/intfm/SCNSceneRenderer/projectPoint%3a"><$c$c>projectPoint:转换在步骤2中(在3D场景中的一个点)返回到屏幕空间得到了矢量 - 这让你一个三维向量,其x和y坐标是一个屏幕空间中的点,其z坐标告诉你的深度相对于裁剪平面的点( 0.0 是近平面, 1.0 是远平面)。稳守此z坐标在使用的mouseMoved:

You're going to need a reference depth for this point so you can compare mouseMoved: locations to it. Use projectPoint: to convert the vector you got in step 2 (a point in the 3D scene) back to screen space — this gets you a 3D vector whose x- and y-coordinates are a screen-space point and whose z-coordinate tells you the depth of that point relative to the clipping planes (0.0 is on the near plane, 1.0 is on the far plane). Hold onto this z-coordinate for use during mouseMoved:.

减去<一href="https://developer.apple.com/library/mac/documentation/SceneKit/Reference/SCNNode_Class/Reference/SCNNode.html#//apple_ref/occ/instp/SCNNode/position"><$c$c>position节点,你想从你在步骤2中这可以让你得到的鼠标点击的对象的位置偏移鼠标位置向量移动。抓住这个载体 - 你需要它,直到拖拽结束

Subtract the position of the node you want to move from the mouse location vector you got in step 2. This gets you the offset of the mouse click from the object's position. Hold onto this vector — you'll need it until dragging ends.

的mouseMoved:,从建设新的鼠标位置,并在第3步得到那么深度值的屏幕坐标一个新的三维矢量,转换这个载体向现场坐标使用<一个href="https://developer.apple.com/library/Mac/documentation/SceneKit/Reference/SCNSceneRenderer_Protocol/Reference/SCNSceneRenderer.html#//apple_ref/occ/intfm/SCNSceneRenderer/unprojectPoint%3a"><$c$c>unprojectPoint: - 这是在场景中的3D空间中的鼠标位置(相当于你的命中测试得到了一个,但不需要打场景几何体)

On mouseMoved:, construct a new 3D vector from the screen coordinates of the new mouse location and the depth value you got in step 3. Then, convert this vector into scene coordinates using unprojectPoint: — this is the mouse location in your scene's 3D space (equivalent to the one you got from the hit test, but without needing to "hit" scene geometry).

添加您在步骤3到第5步中得到了新的位置偏移了 - 这是新的位置将节点移动到。 (注:实时拖动一下吧,你应该确保这个位置上的变化不是动画默认情况下,当前的持续时间<一个。 href="https://developer.apple.com/library/mac/documentation/SceneKit/Reference/SCNTransaction_Class/Introduction/SCNTransaction.html"><$c$c>SCNTransaction为零,所以你不必担心这个,除非你已经改变了它。)

Add the offset you got in step 3 to the new location you got in step 5 - this is the new position to move the node to. (Note: for live dragging to look right, you should make sure this position change isn't animated. By default the duration of the current SCNTransaction is zero, so you don't need to worry about this unless you've changed it already.)

(这有点把我的头顶部,因此你应该仔细检查相关的文档和头文件。你也许可以简化这个有点用一些数学。)

(This is sort of off the top of my head, so you should probably double-check the relevant docs and headers. And you might be able to simplify this a bit with some math.)

这篇关于如何找到在使用SceneKit场景我的鼠标点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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