将 SceneKit 对象放置在 SCNCamera 当前方向的前面 [英] Position a SceneKit object in front of SCNCamera's current orientation

查看:17
本文介绍了将 SceneKit 对象放置在 SCNCamera 当前方向的前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击屏幕时创建一个新的 SceneKit 节点,并让它以设定的距离直接出现在相机前面.为了测试,这将是一个 SCNText 读取你点击这里".它也应该与视线成直角 - 即面对"相机.

I would like to create a new SceneKit node when the user taps the screen, and have it appear directly in front of the camera at a set distance. For testing, this will be a SCNText reads reads "you tapped here". It should also be at right angles to the line of sight - that is, "face on" to the camera.

那么,鉴于 self.camera.orientation SCNVector4(或类似的),我该怎么做:

So, given the self.camera.orientation SCNVector4 (or similar), how can I:

  1. 生成 SCNVector3,将节点置于摄像机前面"给定距离处?
  2. 生成定位节点的 SCNVector4(或 SCNQuaternion),使其面向"相机?

我怀疑 (2) 的答案是它与 `self.camera.orientation?但是(1)让我有点迷茫.

I suspect that the answer to (2) is that it's the same as the `self.camera.orientation? But (1) has me a little lost.

我看到许多类似的问题,例如 这个,但没有答案.

I see a number of similar questions, like this one, but no answers.

推荐答案

(Swift 4)

嘿,如果您想将对象放置在相对于另一个节点(例如相机节点)的某个位置,并且与参考节点的方向相同,则可以使用这个更简单的函数:

Hey, you can use this simpler function if you want to put the object a certain position relative to another node (e.g. the camera node) and also in the same orientation as the reference node:

func updatePositionAndOrientationOf(_ node: SCNNode, withPosition position: SCNVector3, relativeTo referenceNode: SCNNode) {
    let referenceNodeTransform = matrix_float4x4(referenceNode.transform)

    // Setup a translation matrix with the desired position
    var translationMatrix = matrix_identity_float4x4
    translationMatrix.columns.3.x = position.x
    translationMatrix.columns.3.y = position.y
    translationMatrix.columns.3.z = position.z

    // Combine the configured translation matrix with the referenceNode's transform to get the desired position AND orientation
    let updatedTransform = matrix_multiply(referenceNodeTransform, translationMatrix)
    node.transform = SCNMatrix4(updatedTransform)
}

如果您想将 'node' 放在某个 'cameraNode' 前面 2m 处,您可以这样称呼它:

If you'd like to put 'node' 2m right in front of a certain 'cameraNode', you'd call it like:

let position = SCNVector3(x: 0, y: 0, z: -2)
updatePositionAndOrientationOf(node, withPosition: position, relativeTo: cameraNode)

获取相机节点

要获取相机节点,这取决于您使用的是 SceneKit、ARKit 还是其他框架.下面是 ARKit 和 SceneKit 的示例.

To get the camera node, it depends if you're using SceneKit, ARKit, or other framework. Below are examples for ARKit and SceneKit.

使用 ARKit,您可以使用 ARSCNView 来渲染与相机内容重叠的 SCNScene 的 3D 对象.您可以从 ARSCNView 的 pointOfView 属性中获取相机节点:

With ARKit, you have ARSCNView to render the 3D objects of an SCNScene overlapping the camera content. You can get the camera node from ARSCNView's pointOfView property:

let cameraNode = sceneView.pointOfView

对于 SceneKit,您有一个 SCNView 来渲染 SCNScene 的 3D 对象.您可以创建相机节点并将它们放置在您想要的任何位置,因此您可以执行以下操作:

For SceneKit, you have an SCNView that renders the 3D objects of an SCNScene. You can create camera nodes and position them wherever you want, so you'd do something like:

let scnScene = SCNScene()
// (Configure scnScene here if necessary)
scnView.scene = scnScene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(0, 5, 10)  // For example
scnScene.rootNode.addChildNode(cameraNode)

一旦设置了相机节点,您就可以使用与 ARKit 相同的方式访问当前相机:

Once a camera node has been setup, you can access the current camera in the same way as ARKit:

let cameraNode = scnView.pointOfView

这篇关于将 SceneKit 对象放置在 SCNCamera 当前方向的前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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