将相机移到轻按的SCNNode

时间:2019-06-24 12:16:28

标签: ios swift scenekit scnnode

我正在使用SceneKitSwift尝试移动相机,使其“聚焦”在所选节点上。我了解我已启用defaultCameraController,但是我试图通过dollyrotatetranslateInCameraSpaceBy来调整摄像机的位置,但是没有动画过渡-它只是跳到了新位置。

照相机是否可以像Google Maps滑动/缩放到搜索到的位置一样滑动到某个位置?

任何帮助将不胜感激:)

这是我的代码:

import UIKit
import SceneKit

class ViewController: UIViewController {

var gameView: SCNView!
var scene: SCNScene!
var cameraNode: SCNNode!

override func viewDidLoad() {
    super.viewDidLoad()

    // Scene
    scene = SCNScene()

    // Camera
    cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3(0, 0, 10)
    scene.rootNode.addChildNode(cameraNode)

    // Light


    /*
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light?.type = .omni
    lightNode.position = SCNVector3(0, 10, 2)
    scene.rootNode.addChildNode(lightNode)
    */
    // Stars
    //let stars = SCNParticleSystem(named: "starsParticles.scnp", inDirectory: nil)!
    //scene.rootNode.addParticleSystem(stars)

    // Earth
    let earthNode = itemPlate()
    earthNode.position = SCNVector3(0, 0, 0)
    scene.rootNode.addChildNode(earthNode)

    // Create orbiting moonOne
    let moonNodeOne = itemPlate()
    moonNodeOne.position = SCNVector3(3, 0, 0)
    earthNode.addChildNode(moonNodeOne)

    // Create orbiting moonOne
    let moonNodeTwo = itemPlate()
    moonNodeTwo.position = SCNVector3(5, 3, 2)
    earthNode.addChildNode(moonNodeTwo)

    // Create orbiting moonOne
    let moonNodeThree = itemPlate()
    moonNodeThree.position = SCNVector3(-4, -3, 5)
    earthNode.addChildNode(moonNodeThree)

    // Scene formation
    gameView = self.view as! SCNView
    gameView.scene = scene
    gameView.showsStatistics = true
    gameView.allowsCameraControl = true
    gameView.autoenablesDefaultLighting = true
    gameView.defaultCameraController.interactionMode = .fly
    gameView.defaultCameraController.inertiaEnabled = true
    gameView.defaultCameraController.maximumVerticalAngle = 89
    gameView.defaultCameraController.minimumVerticalAngle = -89
    scene.background.contents = UIImage(named: "orangeBg.jpg")

}

override var prefersStatusBarHidden: Bool {
    return true
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: gameView)
    let hitList = gameView.hitTest(location, options: nil)

    if let hitObject = hitList.first {
        let node = hitObject.node

        // Update camera position
        //gameView.defaultCameraController.translateInCameraSpaceBy(x: node.position.x, y: node.position.y, z: node.position.z + 5)
        let onScreenPoint:CGPoint = CGPoint(x: 1.0, y: 1.0)
        let viewport:CGSize = CGSize(width: 50, height: 50)

        gameView.defaultCameraController.dolly(by: 1.0, onScreenPoint: onScreenPoint, viewport: viewport)


        //let newCameraPosition = SCNVector3Make(node.position.x, node.position.y, node.position.z + 10)
        print("NODE_HIT_OBJECT_COORDS: \(node.position.x), \(node.position.y) \(node.position.y)")

        //let moveToAction = SCNAction.move(by: newCameraPosition, duration: 1.0)


    }
}

}

1 个答案:

答案 0 :(得分:0)

您可以在代码中实现这样的方法(对不起,我使用macOS项目而不是iOS,但是几乎一样):

func handleClick(_ gestureRecognizer: NSGestureRecognizer) {

    let scnView = self.view as! SCNView       
    let p = gestureRecognizer.location(in: scnView)
    let hitResults = scnView.hitTest(p, options: [:])

    if hitResults.count > 0 {

        let result = hitResults[0]
        let nodePosition = result.node.position.z
        var matrix = matrix_identity_float4x4

        SCNTransaction.begin()
        SCNTransaction.animationDuration = 1.5       // duration in seconds
        matrix.columns.3.z = Float(nodePosition + 5.0)
        scnView.pointOfView?.position.z = CGFloat(matrix.columns.3.z)
        SCNTransaction.commit()
    }
} 

或者,作为第二种逻辑选择,您可以使用SceneKit的constraints

func handleClick(_ gestureRecognizer: NSGestureRecognizer) {

    let scnView = self.view as! SCNView
    let p = gestureRecognizer.location(in: scnView)
    let hitResults = scnView.hitTest(p, options: [:])

    if hitResults.count > 0 {

        let result = hitResults[0]
        let nodePosition = result.node
        let constraint1 = SCNLookAtConstraint(target: nodePosition)
        let constraint2 = SCNDistanceConstraint(target: nodePosition)
        constraint2.minimumDistance = 5
        constraint2.maximumDistance = 9

        SCNTransaction.begin()
        SCNTransaction.animationDuration = 1.5
        scnView.pointOfView?.constraints = [constraint2, constraint1]
        SCNTransaction.commit()
    }
}

P.S。这两种方法不是开箱即用的解决方案,而是提示如何实现您想要的。