使用SceneKit和CoreMotion播放360度视频的相机位置错误

时间:2017-11-28 15:00:47

标签: ios swift sprite-kit scenekit 360-virtual-reality

我正在玩一个使用SpriteKit的360度视频播放器,SceneKit and CoreMotion.播放器到目前为止工作,但视频总是放大一点。看起来相机位置不在(0,0,0),但在Z轴上有点不对。不幸的是,我还没有找到一种方法来调整它。要重现此行为,只需在视频播放和捏缩放时点按屏幕即可。这样可以通过手势控制相机,双击可以通过设备返回相机控制。

import UIKit
import SceneKit
import CoreMotion
import SpriteKit
import AVFoundation

class Video360VC: UIViewController {
let motionManager = CMMotionManager()
let cameraNode = SCNNode()
@IBOutlet weak var sceneView: SCNView!

@IBAction func exitBtnClicked(_ sender: Any) {
performSegueToReturnBack()
}

func createSphereNode(material: AnyObject?) -> SCNNode {
    let sphere = SCNSphere(radius: 20.0)
    sphere.firstMaterial!.isDoubleSided = true
    sphere.firstMaterial!.diffuse.contents = material
    let sphereNode = SCNNode(geometry: sphere)
    sphereNode.position = SCNVector3Make(0,0,0)
    sphereNode.rotation = SCNVector4Make(1, 0, 0, Float.pi)
    return sphereNode
}

func configureScene(node sphereNode: SCNNode) {
    // Set the scene
    let scene = SCNScene()
    sceneView.scene = scene
    sceneView.showsStatistics = true
    sceneView.allowsCameraControl = true

    // Camera, ...
    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3Make(0, 0, 0)
    scene.rootNode.addChildNode(sphereNode)
    scene.rootNode.addChildNode(cameraNode)
}

func startCameraTracking() {
    motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
    motionManager.startDeviceMotionUpdates(to: OperationQueue.main) {
        [weak self](data: CMDeviceMotion?, error: Error?) in
        guard let data = data else { return }
        let attitude: CMAttitude = data.attitude

        self?.cameraNode.eulerAngles = SCNVector3Make(-   Float(attitude.roll + Double.pi/2), Float(attitude.yaw), -Float(attitude.pitch))
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    guard let fileURL = Bundle.main.url(forResource: "360Test2", withExtension: "mp4") else {
        print("Video File not found")
        return
    }

    let player = AVPlayer(url: fileURL)
    let videoNode = SKVideoNode(avPlayer: player)
    let size = CGSize(width: 1280, height: 720)
    videoNode.size = size
    videoNode.position = CGPoint(x: size.width/2, y: size.height/2)

    let spriteScene = SKScene(size: size)
    spriteScene.scaleMode = .resizeFill
    spriteScene.addChild(videoNode)

    let sphereNode = createSphereNode(material:spriteScene)
    configureScene(node: sphereNode)
    guard motionManager.isDeviceMotionAvailable else {
        fatalError("Device motion is not available")
    }
    startCameraTracking()
    player.play()
}

override func viewWillAppear(_ animated: Bool) {
    self.sceneView.play(self)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

我使用此video进行测试,也可以下载here。 非常感谢 !

1 个答案:

答案 0 :(得分:1)

从你的解释中我可以收集的是,你所谓的Field of View有问题。它可能看起来像是zoomed,但实际上并非如此。在相机中查找FOV设置尝试设置为90度。