【问题标题】:SCNCamera made programmatically not in the correct positionSCNCamera 以编程方式制作的位置不正确
【发布时间】:2019-05-03 12:53:39
【问题描述】:

我正在尝试以编程方式为我的MainScene.scn 文件创建一个摄像头。

我需要在代码中创建相机,因为我想做一个camera orbit 节点,这是我能想到的唯一方法。我也想继续使用我的场景文件。

这是我的视图控制器中的代码(简化):

import UIKit
import SceneKit

class GameViewController: UIViewController {

    // MARK: Scene objects
    private var gameView: SCNView!
    private var gameScene: SCNScene!
    private var gameCameraNode: SCNNode!

    // MARK: View controller overrides
    override func viewDidLoad() {
        super.viewDidLoad()

        // Setup game
        initView()
        initScene()
        initCamera()
    }
}

private extension GameViewController {

    // Initialise the view and scene
    private func initView() {
        self.view = SCNView(frame: view.bounds) // Create an SCNView to play the game within
        gameView = self.view as? SCNView // Assign the view
        gameView.showsStatistics = true // Show game statistics
        gameView.autoenablesDefaultLighting = true // Allow default lighting
        gameView.antialiasingMode = .multisampling2X // Use anti-aliasing for a smoother look
    }
    private func initScene() {
        gameScene = SCNScene(named: "art.scnassets/MainScene.scn")! // Assign the scene
        gameView.scene = gameScene // Set the game view's scene
        gameView.isPlaying = true // The scene is playing (not paused)
    }
    private func initCamera() {
        gameCameraNode = SCNNode()
        gameCameraNode.camera = SCNCamera()
        gameCameraNode.position = SCNVector3(0, 3.5, 27)
        gameCameraNode.eulerAngles = SCNVector3(-2, 0, 0)
        gameScene.rootNode.addChildNode(gameCameraNode)
        gameView.pointOfView = gameCameraNode
    }
}

可以很容易地粘贴此代码来替换默认的视图控制器代码。您需要做的就是添加MainScene.scn 并拖入类似框的东西。

如果您尝试代码,则相机位置错误。如果我在场景中为相机使用相同的属性,它可以工作,但这不是我想要的。

根据我的阅读,SceneKit 可能正在创建一个默认相机,如 herehere 所说。但是,正如他们在那些答案中所说的那样,我正在设置 pointOfView 属性,但它仍然不起作用。

如何以编程方式将我的相机放置在场景中的正确位置?

【问题讨论】:

    标签: ios swift scenekit programmatically-created scncamera


    【解决方案1】:

    一段时间后,我发现您实际上可以直接在 Scene Builder 中添加 空节点。我最初只想要一个程序化的答案,因为我想制作一个相机轨道节点,就像我链接到的问题一样。现在我可以添加一个空节点,我可以让轨道的子节点成为相机。

    这不需要任何代码,除非你想访问节点(例如改变位置或旋转):

    gameCameraNode = gameView.pointOfView // Use camera object from scene
    gameCameraOrbitNode = gameCameraNode.parent // Use camera orbit object from scene
    

    以下是创建轨道节点的步骤:

    1) 将其从Objects Library 拖入:

    2) 像这样设置你的Scene Graph

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2021-02-14
      • 1970-01-01
      • 2017-08-15
      • 2014-06-07
      • 2019-05-03
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      相关资源
      最近更新 更多