2015-11-05 46 views
2

這幾天我在學習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

創建全零的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) 
} 
+0

問題就迎刃而解了。非常感謝!! –

+0

請將答案標記爲已接受,而不是留下「謝謝」意見。 –

相關問題