首页 > 解决方案 > 如何防止 SceneKit 在设备旋转时更改相机 FOV / 位置?

问题描述

由于没有处理旋转或改变任何东西的代码,当我的设备从纵向旋转到横向时,加载并居中在 SceneKit 视图中的模型会更改透视、缩放和拉伸。

ViewController 有一个 SceneKit 视图,它固定在父视图的顶部、底部和侧面。这被证实即使在旋转时也能粘附。

所需设备为 iPhone SE 至 12.9 iPad Pro。

在 iPad 上,这种视觉问题大大减少,但在 iPhone 上,失真、缩放和透视变化非常明显。

我不知道如何嵌入照片,否则我会向您展示,但如果模型在纵向 iPhone 上是正面和中心且 1 英寸 x 1 英寸正方形,当您旋转到横向时,它会变成 0.25 英寸垂直和 0.5 英寸水平。

这是设置场景的代码。

        let scene = SCNScene()
        let shipScene = SCNScene(named: "merchant.scn")

        guard let shipNode = shipScene?.rootNode.childNode(withName: "merchant", recursively: true)
        else
        {
            fatalError("ShipModel is not found!")
        }

        self.shipNode = shipNode
        self.shipNode?.position = SCNVector3(x:0, y:0, z:0)
        scene.rootNode.addChildNode(self.shipNode!)

        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .omni
        lightNode.position = SCNVector3(x:0, y:2, z:10)

        let cameraNode = SCNNode()
        self.cameraNode = cameraNode
        self.cameraNode?.camera = SCNCamera()
        self.cameraNode?.position = SCNVector3(x:0, y:0, z:8)
        self.cameraNode?.addChildNode(lightNode)

        scene.rootNode.addChildNode(self.cameraNode!)
        self.sceneKitView?.scene = scene
        self.sceneKitView?.showsStatistics = false
        self.sceneKitView?.backgroundColor = #colorLiteral(red: 0.08822073042, green: 0.08822073042, blue: 0.08822073042, alpha: 1)
        self.sceneKitView?.allowsCameraControl = false

课堂上什么都没有发生,但拉伸、缩小和透视变化发生在设备旋转上。

它们在 iPhone 上更糟,但在 iPad 上大大减少。

标签: camerarotationscenekitfieldofview

解决方案



推荐阅读