Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 虚拟现实中的SceneKit摄像机运动_Ios_Accelerometer_Scenekit_Core Motion_Virtual Reality - Fatal编程技术网

Ios 虚拟现实中的SceneKit摄像机运动

Ios 虚拟现实中的SceneKit摄像机运动,ios,accelerometer,scenekit,core-motion,virtual-reality,Ios,Accelerometer,Scenekit,Core Motion,Virtual Reality,在此帮助下,我为我的应用程序创建了VR版本 现在,当我开始移动我的手机时,我想移动我的相机节点。我试图使用核心运动的用户加速,但我不知道如何做到这一点。而且我对核心运动还是个新手 请帮我解决这个问题。此代码将适应您的设备方向。。。它基本上是一个圆球,中间有一个摄影阳极。核心运动控制摄影机视图 import UIKit import SceneKit import CoreMotion import SpriteKit import AVFoundation class Video360VC:

在此帮助下,我为我的应用程序创建了VR版本

现在,当我开始移动我的手机时,我想移动我的相机节点。我试图使用核心运动的用户加速,但我不知道如何做到这一点。而且我对核心运动还是个新手


请帮我解决这个问题。

此代码将适应您的设备方向。。。它基本上是一个圆球,中间有一个摄影阳极。核心运动控制摄影机视图

import UIKit
import SceneKit
import CoreMotion
import SpriteKit
import AVFoundation

class Video360VC: UIViewController {

    @IBOutlet weak var sceneView: SCNView!

    let motionManager = CMMotionManager()
    let cameraNode = SCNNode()
    var player: AVPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()
        startSelected()
    }

    func startNewPlayer(fileURL: URL, fileURLSub: URL){
        player = AVPlayer(url: fileURL)
        let videoNode = SKVideoNode(avPlayer: player!)
        let size = CGSize(width: 4096, height: 2048)

        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?.isMuted = true
        player?.play()
    }


    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

        //test mode help
        sceneView.showsStatistics = false
        sceneView.allowsCameraControl = false

        // 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 }

            self?.cameraNode.camera?.fieldOfView = 90
            self?.cameraNode.orientation = data.gaze(atOrientation: .landscapeRight)
        }
    }
}

extension CMDeviceMotion {

    func gaze(atOrientation orientation: UIInterfaceOrientation) -> SCNVector4 {

        let attitude = self.attitude.quaternion
        let aq = GLKQuaternionMake(Float(attitude.x), Float(attitude.y), Float(attitude.z), Float(attitude.w))

        let final: SCNVector4

        switch orientation {

        case .landscapeRight:

            let cq = GLKQuaternionMakeWithAngleAndAxis(Float(Float.pi/2), 0, 1, 0)
            let q = GLKQuaternionMultiply(cq, aq)

            final = SCNVector4(x: -q.y, y: q.x, z: q.z, w: q.w)

        case .landscapeLeft:

            let cq = GLKQuaternionMakeWithAngleAndAxis(Float(-Float.pi), 0, 1, 0)
            let q = GLKQuaternionMultiply(cq, aq)

            final = SCNVector4(x: q.y, y: -q.x, z: q.z, w: q.w)

        case .portraitUpsideDown:

            let cq = GLKQuaternionMakeWithAngleAndAxis(Float(Float.pi), 1, 0, 0)
            let q = GLKQuaternionMultiply(cq, aq)

            final = SCNVector4(x: -q.x, y: -q.y, z: q.z, w: q.w)

        case .unknown:

            fallthrough

        case .portrait:

            let cq = GLKQuaternionMakeWithAngleAndAxis(Float(-Float.pi), 1, 0, 0)
            let q = GLKQuaternionMultiply(cq, aq)

            final = SCNVector4(x: q.x, y: q.y, z: q.z, w: q.w)

        default:
            print("Default")
        }

        return final
    }
}