gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:08:19 28 4
gpt4 key购买 nike

我正在玩一个使用 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 .多谢 !

最佳答案

从您的解释中我可以了解到,您对所谓的 Field of View 有疑问。它可能看起来像相机是 zoomed 但实际上它不是。在您的相机中寻找 FOV 设置,尝试 90 度等设置。

关于ios - 错误的相机位置播放 360 度视频,使用 SceneKit 和 CoreMotion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534643/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com