首页
学习
活动
专区
圈层
工具
发布

全景iOS
EN

Stack Overflow用户
提问于 2017-05-15 21:09:32
回答 1查看 1.1K关注 0票数 0

因此,我创造了一个很好的全景观众,但相机是指向向下。意思是说,如果我把手机平放在桌子上,它就会旋转,并能很好地环视球体。我希望能够保持正常(向上)并环顾四周。这是我的密码。

我在这里使用了一个扩展名:

代码语言:javascript
复制
import UIKit
import SceneKit
import CoreMotion

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 UIApplication.shared.statusBarOrientation {

    case .landscapeRight:

        let cq = GLKQuaternionMakeWithAngleAndAxis(Float(Double.pi), 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(-Double.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(Double.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(-Double.pi), 1, 0, 0)
        let q = GLKQuaternionMultiply(cq, aq)

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

    return final
}
}

然后,视图控制器如下所示:

代码语言:javascript
复制
import UIKit
import SceneKit
import CoreMotion

class ViewController: UIViewController {

let motionManager = CMMotionManager()
let cameraNode = SCNNode()


@IBOutlet weak var sceneView: SCNView!

override func viewDidLoad() {
    super.viewDidLoad()

    guard let imagePath = Bundle.main.path(forResource: "Hellbrunn25", ofType: "jpg") else {
        fatalError("Failed to find path for panaromic file.")
    }
    guard let image = UIImage(contentsOfFile:imagePath) else {
        fatalError("Failed to load panoramic image")
    }

    let scene = SCNScene()
    sceneView.scene = scene
    sceneView.allowsCameraControl = true

    //Create node, containing a sphere, using the panoramic image as a texture
    let sphere = SCNSphere(radius: 20.0)
    sphere.firstMaterial!.isDoubleSided = true
    sphere.firstMaterial!.diffuse.contents = image
    let sphereNode = SCNNode(geometry: sphere)
    sphereNode.position = SCNVector3Make(0,0,0)
    scene.rootNode.addChildNode(sphereNode)

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

    guard motionManager.isDeviceMotionAvailable else {
        fatalError("Device motion is not available")
    }

    // Action
    motionManager.deviceMotionUpdateInterval = 1.0 / 60.0
    motionManager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: {
       (deviceDidMove, Error)-> Void in

        let attitude: CMAttitude = deviceDidMove!.attitude

        self.cameraNode.orientation = SCNVector4Make(Float(attitude.quaternion.x), Float(attitude.quaternion.y), Float(attitude.quaternion.z), Float(attitude.quaternion.w))


    })

}
   override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func deviceDidMove(motion: CMDeviceMotion?, error: NSError?) {

    if let motion = motion {
        let orientation = motion.gaze(atOrientation: UIApplication.shared.statusBarOrientation)
        cameraNode.orientation = orientation
    }
}


}

我怎样才能让摄像机在图像上朝前看。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-16 17:21:11

我想好了,如果有人在找答案的话。我将肖像模式的情况更改为:

代码语言:javascript
复制
let cq = GLKQuaternionMakeWithAngleAndAxis(Float(-Double.pi*0.5), 1, 0, 0)
let q = GLKQuaternionMultiply(cq, aq)

final = SCNVector4(x: q.x, y: q.y, z: q.z, w: q.w)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43988856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档