Adding camera in SCNScene

时光毁灭记忆、已成空白 提交于 2019-12-11 09:06:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!