【问题标题】:Camera doesn't point to the place i want using SceneKit相机没有指向我想要使用 SceneKit 的地方
【发布时间】:2016-02-06 07:33:50
【问题描述】:

这些天我在学习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 我设置了。

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

【问题讨论】:

    标签: ios swift scenekit


    【解决方案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)
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2018-08-01
      • 2018-01-08
      • 2016-02-09
      • 2013-12-08
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多