问题
import UIKit
import SceneKit
class Scene: SCNScene {
var cameraPosition = SCNVector3Make(0, 0, 10)
var lightPosition = SCNVector3Make(0, 0, 0)
var ship = SCNNode()
func setup() {
createCameraNode()
createShipNode()
createAmbientLight()
createLight()
}
func createCameraNode () {
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = cameraPosition
self.rootNode.addChildNode(cameraNode)
}
func createShipNode() {
ship = self.rootNode.childNodeWithName("ship", recursively: true)!
}
func createAmbientLight() {
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = SCNLightTypeAmbient
ambientLightNode.light!.color = UIColor.darkGrayColor()
self.rootNode.addChildNode(ambientLightNode)
}
func createLight() {
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = SCNLightTypeOmni
lightNode.position = lightPosition
self.rootNode.addChildNode(lightNode)
}
}
This is how my code looks like. I have the following problem. When I add camera node in Scene, my object disappear. When I remove function createCameraNode, everything is ok, my spaceship appears on screen. I have tried to change camera position with negative and positive values on z axis, but still no result. Can someone explain me why?
回答1:
You may want to set the SCNView's pointOfView property to the cameraNode. That I think should fix it, assuming the camera is positioned correctly.
As to why it works when you remove the cameraNode code, it is because a default camera is added automatically if none exists in the scene (and the pointOfView is set too). It is the same if there are no lights in the scene too.
来源:https://stackoverflow.com/questions/28480706/adding-camera-in-scnscene