2016-07-20 11 views
2

私はオブジェクトの周りを回転するカメラを作ろうとしています。したがって、カメラは球に取り付けなければなりません。そのために、私はこの男がセットアップを行う方法を説明するこのスタックを見つけました:Rotate SCNCamera node looking at an object around an imaginary sphereSceneKitオブジェクトの周りのカメラの回転

私の問題はカメラのZ軸です。私はここで

XとYの周りではなく、Z.

の周りを回転することができます私のコードです:

class SceneManager 
{ 
private let scene: SCNScene 
private let view: SCNView 
private let cameraOrbit: SCNNode 
private var cameraOrbitLastRatioWidth: Float = 0 
private var cameraOrbitLastRatioHeight: Float = 0.2 
private var maxWidthRatioRight: Float = 0.2 
private var maxWidthRatioLeft: Float = -0.2 
private var maxHeightRatioXDown: Float = 0.02 
private var maxHeightRatioXUp: Float = 0.4 

init(view: SCNView, assetFolder: String, sceneName: String, cameraName: String, backgroundColor: UIColor) { 
    self.view = view 
    self.scene = SCNScene(named: (assetFolder + "/" + sceneName))! 
    self.view.pointOfView = self.scene.rootNode.childNodeWithName(cameraName, recursively: true) 
    if self.view.pointOfView == nil { 
     print("Error: Inexistent camera specified in init SceneManager") 
     exit(1) 
    } 
    self.cameraOrbit = SCNNode() 
    self.cameraOrbit.addChildNode(self.view.pointOfView!) 
    self.scene.rootNode.addChildNode(self.cameraOrbit) 
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panHandler(_:))) 
    panGesture.maximumNumberOfTouches = 1 
    self.view.allowsCameraControl = false 
    self.view.addGestureRecognizer(panGesture) 
    self.view.backgroundColor = backgroundColor 
    self.view.scene = self.scene 
} 

@objc private func panHandler(sender: UIPanGestureRecognizer) { 
    let translation = sender.translationInView(sender.view!) 
    let ratioWidth = Float(translation.x)/Float(sender.view!.frame.size.width) + self.cameraOrbitLastRatioWidth 
    let ratioHeight = Float(translation.y)/Float(sender.view!.frame.size.height) + self.cameraOrbitLastRatioHeight 
    if (sender.state == UIGestureRecognizerState.Changed) { 
     if self.cameraOrbitLastRatioWidth >= maxHeightRatioXUp { 
      self.cameraOrbitLastRatioWidth = maxHeightRatioXUp 
     } 
     if self.cameraOrbitLastRatioWidth <= maxHeightRatioXDown { 
      self.cameraOrbitLastRatioWidth = maxHeightRatioXDown 
     } 
     if self.cameraOrbitLastRatioHeight >= maxWidthRatioRight { 
      self.cameraOrbitLastRatioHeight = maxWidthRatioRight 
     } 
     if self.cameraOrbitLastRatioHeight <= maxWidthRatioLeft { 
      self.cameraOrbitLastRatioHeight = maxWidthRatioLeft 
     } 
     self.cameraOrbit.eulerAngles.y -= Float(M_PI_4 * 3) * ratioWidth 
     self.cameraOrbit.eulerAngles.x -= Float(M_PI_4) * ratioHeight 
    } 
    if (sender.state == UIGestureRecognizerState.Ended) { 
     self.cameraOrbitLastRatioWidth = ratioWidth 
     self.cameraOrbitLastRatioHeight = ratioHeight 
    } 
} 
} 

誰もが、それは非常に歓迎;-)

答えて

0

だろうという考えを持っている場合あなたが必要となります追加のジェスチャ認識機能(おそらくUIRotationGestureRecognizer)を使用してカメラを独自のZ軸(効果的に中央を中心に回転させる)で回転させます。

関連する問題