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

查看:338
本文介绍了将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.

推荐答案

(快速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)
}

如果您想在某个"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)

获取摄像头节点

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

To get the camera node, it depends if you're using SCNKit, 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天全站免登陆