sceneView.pointOfView绕根节点的轴旋转

问题描述 投票:0回答:1

如何保证sceneView.pointOfView的旋转是围绕根节点的轴发生的?

代码:

import UIKit
import SceneKit

class RootVC: UIViewController, UIGestureRecognizerDelegate {
    
    lazy var sceneView: SCNView = {
        let view = SCNView()
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(sceneView)
        sceneView.frame = view.frame
        
        setupScene()
        setupGesture()
    }
    
    func setupScene() {
        guard let scene = SCNScene(named: "Apple_iPad_Pro.usdz") else  {
            return
        }
        scene.rootNode.position = SCNVector3(x: 0, y: 282.523/2, z: 0)
        scene.rootNode.setPivotCenter()
        sceneView.scene = scene
        
        sceneView.allowsCameraControl = true
        sceneView.autoenablesDefaultLighting = true
        sceneView.pointOfView?.scale = .init(x: 1, y: 1, z: 0.3)
    }

    func setupGesture() {
        let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
        doubleTap.numberOfTapsRequired = 2
        doubleTap.delegate = self
        sceneView.addGestureRecognizer(doubleTap)
    }
    
    @objc func doubleTapped(sender: UITapGestureRecognizer) {
        let rotationAction = SCNAction.rotate(by: .pi * 2, around: SCNVector3(x: 0, y: 1, z: 0), duration: 1)
        sceneView.pointOfView?.runAction(rotationAction)
        //sceneView.scene?.rootNode.runAction(rotationAction) (Note: I don't want to apply rotation directly to the root or any other nodes.)
    }

}

extension SCNNode {
    
    func setPivotCenter() {
        let (min, max) = boundingBox
        let x = min.x + 0.5 * (max.x - min.x)
        let y = min.y + 0.5 * (max.y - min.y)
        let z = min.z + 0.5 * (max.z - min.z)
        self.pivot = SCNMatrix4MakeTranslation(x, y, z)
    }
    
}

注意:我不想直接将旋转应用于根或任何其他节点。

结果不正确:

Result

我需要什么:

enter image description here

swift scenekit arkit
1个回答
0
投票

选项#1(更简单): 您无需旋转 PointOfView 相机,只需在其自己的轴上旋转节点即可获取节点参考 (

-childNodeWithName:recursively:
)。

选项#2: 使用相机上的

SCNLookAtConstraint
,只需移动相机位置即可。

© www.soinside.com 2019 - 2024. All rights reserved.