swift - SCNCamera 限制弧球旋转

标签 swift scenekit

我有一个场景设置,其中包含围绕对象旋转的 SCNCamera。

限制相机可以围绕物体实现的旋转范围的最佳方法是什么?

示例:我不能围绕整个球体旋转,而是如何将旋转限制在单个半球内?

我的第一个尝试是查看是否有任何 .allowsCameraControl 的限制。找不到任何东西。

然后我尝试调整 c# Unity:mouse orbit script , 运气不好。

一些关于如何处理或解决这个问题的建议会很好。

样板 Arcball 感谢 this answer.

var lastWidthRatio: Float = 0
var lastHeightRatio: Float = 0

let camera = SCNCamera()
let cameraNode = SCNNode()
let cameraOrbit = SCNNode()

override func viewDidLoad() {
    super.viewDidLoad()

    // create a new scene
    let scene = SCNScene(named: "art.scnassets/ship.scn")!

    // create and add a camera to the scene

    camera.usesOrthographicProjection = true
    camera.orthographicScale = 9
    camera.zNear = 0
    camera.zFar = 100

    cameraNode.position = SCNVector3(x: 0, y: 0, z: 50)
    cameraNode.camera = camera

    cameraOrbit.addChildNode(cameraNode)
    scene.rootNode.addChildNode(cameraOrbit)

    // retrieve the ship node
    let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!

    // retrieve the SCNView
    let scnView = self.view as! SCNView

    // set the scene to the view
    scnView.scene = scene

    // add a tap gesture recognizer
    let gesture = UIPanGestureRecognizer(target: self, action: "panDetected:");
    scnView.addGestureRecognizer(gesture);
}


func panDetected(sender: UIPanGestureRecognizer) {
    let translation = sender.translationInView(sender.view!)
    let widthRatio = Float(translation.x) / Float(sender.view!.frame.size.width) + lastWidthRatio
    let heightRatio = Float(translation.y) / Float(sender.view!.frame.size.height) + lastHeightRatio
    self.cameraOrbit.eulerAngles.y = Float(-2 * M_PI) * widthRatio
    self.cameraOrbit.eulerAngles.x = Float(-M_PI) * heightRatio

    print(Float(-2 * M_PI) * widthRatio)
    if (sender.state == .Ended) {
        lastWidthRatio = widthRatio % 1
        lastHeightRatio = heightRatio % 1
    }
}

最佳答案

也许这对读者有用。

    class GameViewController: UIViewController {

    var cameraOrbit = SCNNode()
    let cameraNode = SCNNode()
    let camera = SCNCamera()


    //HANDLE PAN CAMERA
    var lastWidthRatio: Float = 0
    var lastHeightRatio: Float = 0.2
    var WidthRatio: Float = 0
    var HeightRatio: Float = 0.2
    var fingersNeededToPan = 1
    var maxWidthRatioRight: Float = 0.2
    var maxWidthRatioLeft: Float = -0.2
    var maxHeightRatioXDown: Float = 0.02
    var maxHeightRatioXUp: Float = 0.4

    //HANDLE PINCH CAMERA
    var pinchAttenuation = 20.0  //1.0: very fast ---- 100.0 very slow
    var lastFingersNumber = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // create a new scene
        let scene = SCNScene(named: "art.scnassets/ship.scn")!

        // create and add a light to the scene
        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light!.type = SCNLightTypeOmni
        lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
        scene.rootNode.addChildNode(lightNode)

        // create and add an ambient light to the scene
        let ambientLightNode = SCNNode()
        ambientLightNode.light = SCNLight()
        ambientLightNode.light!.type = SCNLightTypeAmbient
        ambientLightNode.light!.color = UIColor.darkGrayColor()
        scene.rootNode.addChildNode(ambientLightNode)

    //Create a camera like Rickster said
        camera.usesOrthographicProjection = true
        camera.orthographicScale = 9
        camera.zNear = 1
        camera.zFar = 100

        cameraNode.position = SCNVector3(x: 0, y: 0, z: 50)
        cameraNode.camera = camera
        cameraOrbit = SCNNode()
        cameraOrbit.addChildNode(cameraNode)
        scene.rootNode.addChildNode(cameraOrbit)

        //initial camera setup
        self.cameraOrbit.eulerAngles.y = Float(-2 * M_PI) * lastWidthRatio
        self.cameraOrbit.eulerAngles.x = Float(-M_PI) * lastHeightRatio

        // retrieve the SCNView
        let scnView = self.view as! SCNView

        // set the scene to the view
        scnView.scene = scene

        //allows the user to manipulate the camera
        scnView.allowsCameraControl = false  //not needed

        // add a tap gesture recognizer
        let panGesture = UIPanGestureRecognizer(target: self, action: "handlePan:")
        scnView.addGestureRecognizer(panGesture)

        // add a pinch gesture recognizer
        let pinchGesture = UIPinchGestureRecognizer(target: self, action: "handlePinch:")
        scnView.addGestureRecognizer(pinchGesture)
    }

    func handlePan(gestureRecognize: UIPanGestureRecognizer) {

        let numberOfTouches = gestureRecognize.numberOfTouches()

        let translation = gestureRecognize.translationInView(gestureRecognize.view!)

        if (numberOfTouches==fingersNeededToPan) {

           widthRatio = Float(translation.x) / Float(gestureRecognize.view!.frame.size.width) + lastWidthRatio
           heightRatio = Float(translation.y) / Float(gestureRecognize.view!.frame.size.height) + lastHeightRatio

            //  HEIGHT constraints
            if (heightRatio >= maxHeightRatioXUp ) {
                heightRatio = maxHeightRatioXUp
            }
            if (heightRatio <= maxHeightRatioXDown ) {
                heightRatio = maxHeightRatioXDown
            }


            //  WIDTH constraints
            if(widthRatio >= maxWidthRatioRight) {
                widthRatio = maxWidthRatioRight
            }
            if(widthRatio <= maxWidthRatioLeft) {
                widthRatio = maxWidthRatioLeft
            }

            self.cameraOrbit.eulerAngles.y = Float(-2 * M_PI) * widthRatio
            self.cameraOrbit.eulerAngles.x = Float(-M_PI) * heightRatio

            print("Height: \(round(heightRatio*100))")
            print("Width: \(round(widthRatio*100))")


            //for final check on fingers number
            lastFingersNumber = fingersNeededToPan
        }

        lastFingersNumber = (numberOfTouches>0 ? numberOfTouches : lastFingersNumber)

        if (gestureRecognize.state == .Ended && lastFingersNumber==fingersNeededToPan) {
            lastWidthRatio = widthRatio
            lastHeightRatio = heightRatio
            print("Pan with \(lastFingersNumber) finger\(lastFingersNumber>1 ? "s" : "")")
        }
    }

    func handlePinch(gestureRecognize: UIPinchGestureRecognizer) {
        let pinchVelocity = Double.init(gestureRecognize.velocity)
        //print("PinchVelocity \(pinchVelocity)")

        camera.orthographicScale -= (pinchVelocity/pinchAttenuation)

        if camera.orthographicScale <= 0.5 {
            camera.orthographicScale = 0.5
        }

        if camera.orthographicScale >= 10.0 {
            camera.orthographicScale = 10.0
        }

    }

关于swift - SCNCamera 限制弧球旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967838/

相关文章:

swift 4 + Alamofire : parse array of custom struct

swift - xCode、Swift : working with vs. 无需 Storyboard即可工作

ios - ARKit和Vision –如何在找到的QR码上放置SCNPlane?

ios - 自动签名无法解决 "Project"目标权利的问题

ios - Xcode 8.1 选择仅显示 iPhone 7 和 7s 的初始设备 View

ios - 从 xc10 > xc11 迁移 SceneKit 应用程序,我遇到 "xcrun: error: unable to find utility "scntool",不是开发人员工具或在 PATH 中"

ios - 用于建筑信息建模的 Sprite Kit 与 Scene Kit

swift - 我可以复制一个SCNNode并保持规模吗

swift - 通过 Swift 设置 ARKit 方向

swift - Xcode10.1 RestKit错误_$s7RestKit11DynamicKeysVN