我需要一个 SCNNode
来看起来像一个真实世界的对象:即当用户在打开相机的情况下旋转他的设备时位于相同的位置。我应该移动相机节点,还是调整场景根节点的位置?
最佳答案
我决定移动相机节点。
我设计的节点层次结构如下:
root node
-- light and content code
-- content node (root node of a SCNScene created upon a .dae file)
-- light node
-- camera node
因此,我设法通过以下代码移动相机节点
func sceneSetup() {
if motionManager == nil {
motionManager = CMMotionManager()
}
if motionManager?.deviceMotionAvailable != nil {
motionManager?.deviceMotionUpdateInterval = 1.0 / 60.0
motionManager?.startDeviceMotionUpdatesToQueue(NSOperationQueue(), withHandler: {
[weak self] (data: CMDeviceMotion?, error: NSError?) in
if self!.initialAttitude == nil {
// capture the initial position
self!.initialAttitude = data!.attitude
return
}
// make the new position value to be comparative to initial one
data!.attitude.multiplyByInverseOfAttitude(self!.initialAttitude!)
let xRotationDelta = data!.attitude.pitch as! Float
let yRotationDelta = data!.attitude.roll as! Float
let zRotationDelta = data!.attitude.yaw as! Float
NSOperationQueue.mainQueue().addOperationWithBlock {
self?.rotateCamera(-yRotationDelta, y: xRotationDelta, z: zRotationDelta)
}
})
}
}
这是 rotateCamera
实现
func rotateCamera(x: Float, y: Float, z: Float) {
cameraNode?.eulerAngles.x = x
cameraNode?.eulerAngles.y = y
cameraNode?.eulerAngles.z = z
}
值得一提的是,zPosition增大,分子视差效果会更加明显
关于ios - 如何使用设备运动更新将 SCNNode 固定在特定位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997105/