Search code examples
iosswiftaugmented-realityscenekitarkit

How can I rotate and scale an ARKit model using gestures?


I am trying to rotate and scale an ARKit model like in this video:

Here is my code:

Add Gestures:

let tapGesure = UITapGestureRecognizer(target: self, action: #selector(handleTap))
sceneView.addGestureRecognizer(tapGesure)

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(moveNode(_:)))
self.view.addGestureRecognizer(panGesture)

let rotateGesture = UIRotationGestureRecognizer(target: self, action: #selector(rotateNode(_:)))
self.view.addGestureRecognizer(rotateGesture)

Here are the corresponding methods:

Tap Gesture:

// MARK: - Gesture Recognizers
@objc func handleTap(gesture: UITapGestureRecognizer) {

    let location = gesture.location(in: sceneView)
    guard let hitTestResult = sceneView.hitTest(location, types: .existingPlane).first else { return }
    let position = SCNVector3Make(hitTestResult.worldTransform.columns.3.x,
                                  hitTestResult.worldTransform.columns.3.y,
                                  hitTestResult.worldTransform.columns.3.z)
    addFoodModelTo(position: position)
}

Pan Gesture with UIPanGestureRecognizer:

@objc func moveNode(_ gesture: UIPanGestureRecognizer) {

    if !isRotating {

        //1. Get The Current Touch Point
        let currentTouchPoint = gesture.location(in: self.sceneView)

        //2. Get The Next Feature Point Etc
        guard let hitTest = self.sceneView.hitTest(currentTouchPoint, types: .existingPlane).first else { return }

        //3. Convert To World Coordinates
        let worldTransform = hitTest.worldTransform

        //4. Set The New Position
        let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)

        //5. Apply To The Node
        baseNode.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)  
    }
}

Rotate Gesture with UIRotationGestureRecognizer:

@objc func rotateNode(_ gesture: UIRotationGestureRecognizer){

    //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
            baseNode.eulerAngles.y = currentAngleY + rotation
        }

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

But it doesn't work. What am I doing wrong?


Solution

  • For rotation gesture implement the following methodology:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let rotationGesture = UIRotationGestureRecognizer(target: self, 
                                                          action: #selector(rotation))
    
        self.sceneView.addGestureRecognizer(rotationGesture)
    }
    

    then:

    let myScene = SCNScene(named: "scene.scn")!
    let modelNode: SCNNode = myScene.rootNode
    var originalRotation: SCNVector3? = nil
    

    also you can check any important condition:

    private func nodeMethod(at position: CGPoint) -> SCNNode? {
    
        let n = self.sceneView.hitTest(position, options: nil).first(where: { 
            $0.node !== modelNode 
        })?.node
    
        return n
    }
    

    and then:

    @objc func rotation(_ gesture: UIRotationGestureRecognizer) {
    
        let location = gesture.location(in: self.sceneView)
        
        guard let node = nodeMethod(at: location) else { return }
        
        switch gesture.state {
            case .began:
                originalRotation = node.eulerAngles
            case .changed:
                guard var originalRotation = originalRotation else { return }
                originalRotation.y -= Float(gesture.rotation)
                node.eulerAngles = originalRotation
            default:
                originalRotation = nil
        }
    }
    

    P.S.

    Check where is a pivot point of your model located. If it's outside model's container, your model will be rotated and scaled unpredictably. Control pivot point's position using the following code:

    modelNode.simdPivot.columns.3.z = 0.25
    

    Also read about Quaternion vs Euler Rotation here and here.

    And in case you need to lock any axis for rotation, read this post.