[英]Move camera in Scenekit
I have a question about moving the camera up/down and left/right without rotating in SceneKit with a pan gesture.我有一个关于不使用平移手势在 SceneKit 中旋转的情况下向上/向下和向左/向右移动相机的问题。 I have found plenty of discussions about rotating the camera, which I am using for my rotation.
我发现了很多关于旋转相机的讨论,我用它来旋转。 But I can't figure out how to move the camera without rotating it.
但是我不知道如何在不旋转相机的情况下移动它。 I am trying to mimic, allowsCameraControl where the user can pan with two fingers to change the cameras x and y position.
我试图模仿,允许相机控制,用户可以用两个手指平移来改变相机的 x 和 y 位置。 Here is the code for my pan gesture recognizer, any help would be greatly appreciated, thanks!
这是我的平移手势识别器的代码,任何帮助将不胜感激,谢谢!
func handlePan(gestureRecognize: UIPanGestureRecognizer) {
let numberOfTouches = gestureRecognize.numberOfTouches
var translation = gestureRecognize.translation(in: gestureRecognize.view!)
var widthRatio = Float(translation.x) / Float(gestureRecognize.view!.frame.size.width) + lastWidthRatio
var heightRatio = Float(translation.y) / Float(gestureRecognize.view!.frame.size.height) + lastHeightRatio
if (numberOfTouches==fingersNeededToPan) {
self.cameraOrbit.eulerAngles.y = Float(-2 * M_PI) * widthRatio
self.cameraOrbit.eulerAngles.x = Float(-M_PI) * heightRatio
//for final check on fingers number
lastFingersNumber = fingersNeededToPan
}
if numberOfTouches == 2 {
self.cameraNode.position.x = -(Float(translation.x) / Float(gestureRecognize.view!.frame.size.width) + lastWidthRatio)
self.cameraNode.position.y = -(Float(translation.y) / Float(gestureRecognize.view!.frame.size.height) + lastHeightRatio)
}
lastFingersNumber = (numberOfTouches>0 ? numberOfTouches : lastFingersNumber)
if (gestureRecognize.state == .ended && lastFingersNumber==fingersNeededToPan) {
lastWidthRatio = widthRatio
lastHeightRatio = heightRatio
}
}
If you want to slide left/right, or up/down, then you don't need the cameraOrbit
node (that's what Scenekit camera orbit around object uses).如果您想向左/向右或向上/向下滑动,则不需要
cameraOrbit
节点(这是Scenekit 相机围绕对象轨道使用的节点)。
Instead, you just want to slide left/right (X axis) or up/down (Y axis) in response to touch.相反,您只想向左/向右(X 轴)或向上/向下(Y 轴)滑动以响应触摸。 It looks like you're doing that.
看起来你正在这样做。 But if
cameraNode
is a child of cameraOrbit
, you'll be moving in the coordinate system of cameraOrbit
, which is also being rotated by your gesture handler!但是,如果
cameraNode
是cameraNode
的子cameraOrbit
,则您将在cameraOrbit
的坐标系中移动,该坐标系也由您的手势处理程序旋转! Make the camera node a child of your scene's root node.使相机节点成为场景根节点的子节点。
Now the confounding thing happens when your camera isn't lined up with the root coordinate system.现在,当您的相机未与根坐标系对齐时,就会发生令人困惑的事情。 If the camera's X, Y, and Z axes are parallel to the scene's X/Y/Z axes, you can adjust the camera's X/Y positions to move.
如果相机的 X、Y 和 Z 轴与场景的 X/Y/Z 轴平行,则可以调整相机的 X/Y 位置以进行移动。 But if the camera's been rotated, or pointed off Z axis, you need to adjust the camera node's
transform
, to move left/right or up/down in the plane of the camera.但是如果相机被旋转了,或者指向了 Z 轴,你需要调整相机节点的
transform
,在相机的平面上向左/向右或向上/向下移动。 I'll try to expand this answer sometime soon to demonstrate that.我会尽快尝试扩展这个答案来证明这一点。
Now you can do it quite simple:现在你可以很简单地做到这一点:
scnView.allowsCameraControl = true
scnView.defaultCameraController.interactionMode = .orbitTurntable
scnView.defaultCameraController.inertiaEnabled = true
scnView.defaultCameraController.maximumVerticalAngle = 89
scnView.defaultCameraController.minimumVerticalAngle = -89
to move camera with saved orientation use使用保存的方向移动相机
func stepLeft() {
print("stepLeft")
if let pov = view.pointOfView {
pov.localTranslate(by: SCNVector3(-0.5, 0, 0))
}
}
func stepRight() {
print("stepLeft")
if let pov = view.pointOfView {
pov.localTranslate(by: SCNVector3(0.5, 0, 0))
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.