旋转 ARKIT 模型

Rotating an ARKIT model

我在使用平移手势识别器旋转我的模型时遇到了问题,有点不知所措。

每次我尝试添加手势时都无法找到节点并旋转它!

这是我下载叠加图像(漫反射内容)和 .SCN 文件的位置

 private func dowloadModel(){
        let configuration = URLSessionConfiguration.default
        let operationQueue = OperationQueue()
        let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)

        do {
            if restaurauntName == ""{
                throw MyError.FoundNil("Something hasnt loaded")
            }
            //overlay image
            let url1 = URL(string: "https://URL_Where_PNG_File_Is_Stored/Burger.png")
            let data1 = try? Data(contentsOf: url1!) //make sure your image in this url does exist
            self.imagesOne = UIImage(data: data1!)
        }

        catch {
            print("Unexpected error: \(error).")
            self.showAlert()
        }

        //.SCN file location
        let url = URL(string: "https://URL_Where_SCN_File_Is_Stored/Burger.scn")!

        let downloadTask = urlSession.downloadTask(with: url)
        downloadTask.resume()
    }

以下是我将模型添加到场景的方法:

 func addItem(hitTestResult : ARHitTestResult){


            let documentDirectories = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)

            if let documentDirectory = documentDirectories.first{
                let fileURL = documentDirectory.appendingPathComponent("Burger.scn")

                do{
                    let scene = try SCNScene(url: fileURL, options: nil)
                    let node = scene.rootNode.childNode(withName: "Burger", recursively: true)!

                    let material = SCNMaterial()
                    material.diffuse.contents = imagesOne
                    material.diffuse.wrapT = SCNWrapMode.repeat
                    material.diffuse.wrapS = SCNWrapMode.repeat
                    material.isDoubleSided = true
                    node.geometry?.materials = [material]
                    let transform = hitTestResult.worldTransform
                    let thirdColumn = transform.columns.3

                    node.scale = SCNVector3(x: 0.008, y: 0.008, z: 0.008)
                    node.pivot = SCNMatrix4MakeTranslation(0, -0.5, 0)
                    node.position = SCNVector3(thirdColumn.x, thirdColumn.y, thirdColumn.z)

                    self.sceneView.scene.rootNode.addChildNode(node)
                }
                catch{
                    print(error)
                }
            }
        }

这是成功将模型添加到场景中的添加模型手势识别器:

@objc func tapped(sender:UITapGestureRecognizer){

                let sceneView = sender.view as! ARSCNView
                let tapLocation = sender.location(in: sceneView)
                let hitTest = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)

                if !hitTest.isEmpty{
                    print("touched a horizontal surface")
                    self.addItem2(hitTestResult: hitTest.first!)
                }

                else{
                    print("no match")

                }
    }

总结一下我的问题,我可以将模型成功添加到场景中,但在使用平移手势识别器旋转模型时遇到问题。我该怎么做?

我希望能够像这样旋转它:https://www.youtube.com/watch?v=J1SA3AZumeU

请指教

我在这里找到了一个很好的答案:

 @objc func rotateNode(_ gesture: UIRotationGestureRecognizer){

        //Store The Rotation Of The CurrentNode
        var currentAngleY: Float = 0.0



        //1. Get The Current Rotation From The Gesture
        let rotation = Float(gesture.rotation)

        //2. If The Gesture State Has Changed Set The Nodes EulerAngles.y
        if gesture.state == .changed{
            isRotating = true
            node.eulerAngles.y = currentAngleY + rotation
        }

        //3. If The Gesture Has Ended Store The Last Angle Of The Cube
        if(gesture.state == .ended) {
            currentAngleY = node.eulerAngles.y
            isRotating = false
        }
    }