相机没有指向我想要使用 SceneKit 的地方

Camera doesn't point to the place i want using SceneKit

我正在学习scenekit这些days.but有一些问题。

我创建一个 .scn 文件并在 (0,0,0) 处放置一个球体 我使用这些代码将相机放在节点上

let frontCamera = SCNCamera()
frontCamera.yFov = 45
frontCamera.xFov = 45
let topNode = SCNNode()
topNode.camera = frontCamera
topNode.position = SCNVector3(4, 0, 0)
topNode.orientation = SCNQuaternion(0, 0, 0, 0)
scene!.rootNode.addChildNode(topNode)
topView.pointOfView = topNode
topView.allowsCameraControl = true

当我 运行 我无法看到任何东西,直到我点击我的模拟器并使用这个 属性 ,allowsCameraControl 我设置。

你能告诉我我的代码哪里错了吗?非常感谢

创建一个全为零的 SCNQuaternion 并没有任何意义。您已指定旋转 0,沿由全零 "unit" 向量指定的轴。如果您尝试使用此修改后的代码版本,您会发现在尝试更改 topNode 的方向后并没有真正发生任何变化。您仍在围绕所有 3 个分量都为零的轴旋转:

let topNode = SCNNode()
topNode.camera = frontCamera
topNode.position = SCNVector3(4, 0, 0)
print(topNode.orientation, topNode.rotation)
-> SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 1.0) SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 0.0)

topNode.orientation = SCNQuaternion(0, 0, 0, 0)
print(topNode.orientation, topNode.rotation)
->SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 1.0) SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 3.14159)

您已将 X 轴移出 4 个单位来放置相机 (topNode.position)。在通常的方向上,这意味着向右 4 个单位,正 Y 运行 从屏幕底部到顶部,正 Z 运行 从屏幕到你的眼睛。你想绕 Y 轴旋转。相机的方向在其父节点的负 Z 轴下方。所以让我们顺时针旋转 1/4,然后尝试设置 rotation(比四元数更容易思考):

topNode.rotation = SCNVector4Make(0, 1, 0, Float(M_PI_2))
print(topNode.orientation, topNode.rotation)
-> SCNVector4(x: 0.0, y: 1.0, z: 0.0, w: -4.37114e-08) SCNVector4(x: 0.0, y: 1.0, z: 0.0, w: 3.14159)

您可能会发现注销甚至显示相机节点的 rotationorientationeulerAngles(它们都表达了相同的概念,只是使用不同的轴),因为你手动操作相机。

为了完整起见,这里是整个 viewDidLoad:

@IBOutlet weak var sceneView: SCNView!

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.scene = SCNScene()

    let sphere = SCNSphere(radius: 1.0)
    let sphereNode = SCNNode(geometry: sphere)
    sceneView.scene?.rootNode.addChildNode(sphereNode)

    let frontCamera = SCNCamera()
    frontCamera.yFov = 45
    frontCamera.xFov = 45
    let topNode = SCNNode()
    topNode.camera = frontCamera
    topNode.position = SCNVector3(4, 0, 0)
    sceneView.scene?.rootNode.addChildNode(topNode)
    print(topNode.orientation, topNode.rotation)
    topNode.orientation = SCNQuaternion(0, 0, 0, 0)
    print(topNode.orientation, topNode.rotation)

    topNode.rotation = SCNVector4Make(0, 1, 0, Float(M_PI_2))
    print(topNode.orientation, topNode.rotation)
}