简体   繁体   English

SceneKit 相机导致对象不显示

[英]SceneKit camera causes objects to Not Show Up

I want to be able to create a custom camera node in SceneKit and view my scene from it (instead of the default camera).我希望能够在 SceneKit 中创建自定义相机节点并从中查看我的场景(而不是默认相机)。

However, I've been encountering a very strange issue with SceneKit:但是,我在使用 SceneKit 时遇到了一个非常奇怪的问题:

  • If I use SCNCamera , nothing shows up in my scene.如果我使用SCNCamera ,我的场景中不会显示任何内容。
  • If I don't use SCNCamera , the objects in my scene render correctly.如果我不使用SCNCamera ,我场景中的对象会正确渲染。

This is the code I am using (very simple code; from a tutorial):这是我正在使用的代码(非常简单的代码;来自教程):

import UIKit
import SceneKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let sceneView = SCNView()
        sceneView.frame = self.view.frame
        self.view.addSubview(sceneView)

        let scene = SCNScene()

        sceneView.autoenablesDefaultLighting = true
        sceneView.allowsCameraControl = true

        let cameraNode = SCNNode()
        // If the below line of code is commented out (so no SCNCamera is added), everything shows up
        cameraNode.camera = SCNCamera()
        scene.rootNode.addChildNode(cameraNode)

        let sphere = SCNSphere(radius: 5)
        sphere.firstMaterial?.diffuse.contents = UIColor.red

        let sphereNode = SCNNode(geometry: sphere)
        cameraNode.addChildNode(sphereNode)

        sceneView.backgroundColor = UIColor.green
        sceneView.scene = scene

    }
}

This seems pretty straightforward, yet I can't find any reason why this is happening on SO, etc.这看起来很简单,但我找不到任何原因为什么会在 SO 上发生这种情况,等等。

Strangely, I also observe that if I try to access the camera node via sceneView.pointOfView , I get nil , even though sceneView.allowsCameraControl is set to true奇怪的是,我还观察到如果我尝试通过sceneView.pointOfView访问相机节点,我得到nil ,即使sceneView.allowsCameraControl设置为true

Any help is appreciated!任何帮助表示赞赏!

The sphere is a child node of the camera, without any offset (its position is (0, 0, 0) ) and so the camera is inside the sphere.球体是相机的子节点,没有任何偏移(其position(0, 0, 0) ),因此相机位于球体内部 And if the sphere's material isn't doubleSided then you won't see anything.如果球体的材料不是doubleSided那么你将看不到任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM