退出ARSCNView以使用allowsCameraControl旋转对象

时间:2017-08-18 16:51:45

标签: swift scenekit ios11 swift4 arkit

使用ARKit我在我的房间里创建了盒子,然后我希望能够退出AR场景以查看我的盒子并围绕它旋转。
我尝试使用allowsCameraControl = true,现在我可以缩放和拖动我的物体,但我希望相机转过来,而现在物体转过身来。我已经查看了604的WWDC视频,该视频解释了该选项self.sceneView.defaultCameraController.interactionMode = .orbitTurntable,但我无法使其正常工作......
基本上我所有的盒子都是'在水平面上我有一个topBox在我的天花板上。

我尝试过的事情:

func getCenterBox() -> SCNNode? {
    guard let _ = self.sceneView.scene.rootNode.childNode(withName: "box", recursively: true) else {
        return nil
    }

    let edges = self.sceneView.scene.rootNode.childNodes.filter { $0.name == "box" }
    let edgesByX = edges.sorted { $0.position.x < $1.position.x }
    let minX = edgesByX.first?.position.x
    let maxX = edgesByX.last?.position.x

    let edgesByZ = edges.sorted { $0.position.z < $1.position.z }
    let minZ = edgesByZ.first?.position.z
    let maxZ = edgesByZ.last?.position.z

    let centerBox = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.5)
    let centerNode = SCNNode(geometry: centerBox)
    let centerNodePosition = SCNVector3Make((maxX! - minX!)/2 + minX!, ((topBox?.position.y)! - (edges.first?.position.y)!)/2 + (edges.first?.position.y)!, (maxZ! - minZ!)/2 + minZ!)
    centerNode.position = centerNodePosition
    return SCNNode(geometry: SCNGeometry())
}

@IBAction func stopARTapped(_ sender: UIButton) {
    self.sceneView.allowsCameraControl = true
    self.sceneView.defaultCameraController.interactionMode = .orbitTurntable
    self.sceneView.defaultCameraController.inertiaEnabled = true
    self.sceneView.defaultCameraController.maximumHorizontalAngle = 0
    self.sceneView.defaultCameraController.maximumVerticalAngle = 0
    self.sceneView.defaultCameraController.minimumHorizontalAngle = 0
    self.sceneView.defaultCameraController.minimumVerticalAngle = 0

    guard let centerNode = getCenterBox() else {
        return
    }
    self.sceneView.scene.rootNode.addChildNode(centerNode)

    let lookAtConstraint = SCNLookAtConstraint(target: centerNode)

    if self.sceneView.pointOfView?.constraints == nil {
        self.sceneView.pointOfView?.constraints = [lookAtConstraint]
    } else {
        self.sceneView.pointOfView?.constraints?.append(lookAtConstraint)
    }
}

0 个答案:

没有答案