【问题标题】:SceneKit camera rotation around an objectSceneKit 相机围绕对象旋转
【发布时间】:2016-11-23 12:09:12
【问题描述】:

我正在尝试制作一个围绕物体旋转的相机。因此,相机应该连接到一个球体上。为此,我找到了这个人解释如何进行设置的堆栈:Rotate SCNCamera node looking at an object around an imaginary sphere

我的问题是相机的 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
    }
}
}

如果有人有想法,将非常欢迎 ;-)

【问题讨论】:

    标签: swift trigonometry scenekit


    【解决方案1】:

    您将需要一个额外的手势识别器(可能是 UIRotationGestureRecognizer)来在其自己的 Z 轴上旋转相机(有效地围绕其中心旋转视图)。

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 2019-01-26
      • 2018-02-06
      • 2018-01-23
      • 2013-08-30
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多