3

我曾经将xFovyFovSCNCamera值设置为从相机field of view和我的视图计算的值frame size。这在我的AR引擎实现中运行良好(使用SceneKit, not ARKit)。在新的 Xcode 中,我看到警告,xFov并且yFov已被弃用,我应该使用fieldOfViewandfocalLength代替。我想更新我的代码,但我不知道如何将这些变量设置为与我以前拥有的完全对应。最接近的是设置fieldOfView为我之前为 xFov * 2 使用的值 - 但它并不完全相同,对象实际上看起来更小一些。是否有某种方法可以计算这些新值,就像我设置xFov and yFov给定值一样?我拥有的是我以前拥有的这些价值观xFov and yFov所以我想计算fieldOfViewfocalLength

这就是我得到xFovyFov重视的方式:

private func updateSceneInformation() {
    guard let captureDevice = captureDevice, captureDevice.hasMediaType(AVMediaType.video) else { return }

    let xFov: Double, yFov: Double
    let cameraFieldOfView = Double(captureDevice.activeFormat.videoFieldOfView)
    let videoDimentions = CMVideoFormatDescriptionGetDimensions(captureDevice.activeFormat.formatDescription)
    let aspect = Double(videoDimentions.width) / Double(videoDimentions.height)
    let videoSize: CGSize
    if UIApplication.shared.statusBarOrientation.isPortrait {
        yFov      = cameraFieldOfView
        xFov      = yFov / aspect
        videoSize = CGSize(videoDimensions: videoDimentions).rotated90Degrees
    }
    else {
        xFov      = cameraFieldOfView
        yFov      = xFov / aspect
        videoSize = CGSize(videoDimensions: videoDimentions)
    }
    let aspectSize  = videoSize.aspectFill(fitToSize: bounds.size)
    let originPoint = CGPoint(x: (bounds.width - aspectSize.width) / 2, y: (bounds.height - aspectSize.height) / 2)
    let sceneFrame  = CGRect(origin: originPoint, size: aspectSize)
    let fieldOfView = FOV(horizontal: xFov, vertical: yFov)

    let newSceneInformation = SceneInformation(fieldOfView: fieldOfView, frame: sceneFrame, interfaceOrientation: UIApplication.shared.statusBarOrientation)
    if sceneInformation != newSceneInformation {
        sceneInformation = newSceneInformation
    }
}

在 iPhone 6s 水平上,这给了我

SceneInformation(fieldOfView: (58.63214874267578, 32.98058366775513), frame: (0.0, -0.09375, 667.0, 375.1875), interfaceOrientation: .landscapeRight)

Frame是 的框架SCNView

这就是我之前应用这些值的方式(按预期工作)

override var sceneInformation: SceneInformation {
    didSet {
        cameraNode.camera?.xFov = Double(sceneInformation.fieldOfView.horizontal)
        cameraNode.camera?.yFov = Double(sceneInformation.fieldOfView.vertical)
        sceneView.frame = sceneInformation.frame
    }
}
4

0 回答 0