相机没有使用SceneKit

时间:2015-11-05 12:50:40

标签: ios swift scenekit

这些天我正在学习scenekit。但是有一些问题。

我创建一个.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我设置了。

你能告诉我我的代码出了什么问题吗?非常感谢

1 个答案:

答案 0 :(得分:1)

创建所有零的SCNQuaternion并不意味着什么。您已经沿着由全零"单位"指定的轴指定了0的旋转。向量。如果您尝试修改此代码的版本,则在尝试更改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)
}