【问题标题】:Adding camera in SCNScene在 SCNScene 中添加摄像头
【发布时间】:2020-03-12 22:27:02
【问题描述】:
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)
    }  
}

这就是我的代码的样子。我有以下问题。当我在场景中添加相机节点时,我的对象消失了。当我删除函数 createCameraNode 时,一切正常,我的飞船出现在屏幕上。我试图用 z 轴上的负值和正值更改相机位置,但仍然没有结果。谁能解释一下为什么?

【问题讨论】:

    标签: swift scenekit


    【解决方案1】:

    您可能希望将SCNViewpointOfView 属性设置为cameraNode。假设相机位置正确,我认为应该修复它。

    至于为什么当你删除cameraNode代码时它会起作用,这是因为如果场景中不存在默认摄像头(并且也设置了pointOfView),则会自动添加默认摄像头。如果场景中也没有灯光也是如此。

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      相关资源
      最近更新 更多