3

I am trying to scale a 3D model of a chair in ARKit using SceneKit. Here is my code for the pinch gesture:

 @objc func pinched(recognizer :UIPinchGestureRecognizer) {

        var deltaScale :CGFloat = 0.0

        deltaScale = 1 - self.lastScale - recognizer.scale

        print(recognizer.scale)

        let sceneView = recognizer.view as! ARSCNView
        let touchPoint = recognizer.location(in: sceneView)

        let scnHitTestResults = self.sceneView.hitTest(touchPoint, options: nil)

        if let hitTestResult = scnHitTestResults.first {

            let chairNode = hitTestResult.node

            chairNode.scale = SCNVector3(deltaScale,deltaScale,deltaScale)
            self.lastScale = recognizer.scale

        }

    }

It does scale but for some weird reason it inverts the 3D model upside down. Any ideas why? Also although the scaling works but it is not as smooth and kinda jumps from different scale factors when used in multiple progressions using pinch to zoom.

john doe
  • 9,220
  • 23
  • 91
  • 167

1 Answers1

13

Here is how I scale my nodes:

/// Scales An SCNNode
///
/// - Parameter gesture: UIPinchGestureRecognizer
@objc func scaleObject(gesture: UIPinchGestureRecognizer) {

    let location = gesture.location(in: sceneView)
    let hitTestResults = sceneView.hitTest(location)
    guard let nodeToScale = hitTestResults.first?.node else {
        return
    }

    if gesture.state == .changed {

        let pinchScaleX: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.x))
        let pinchScaleY: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.y))
        let pinchScaleZ: CGFloat = gesture.scale * CGFloat((nodeToScale.scale.z))
        nodeToScale.scale = SCNVector3Make(Float(pinchScaleX), Float(pinchScaleY), Float(pinchScaleZ))
        gesture.scale = 1

    }
    if gesture.state == .ended { }

}

In my example current node refers to an SCNNode, although you can set this however you like.

atulkhatri
  • 10,896
  • 3
  • 53
  • 89
BlackMirrorz
  • 7,217
  • 2
  • 20
  • 31
  • Thanks! What is the purpose of gesture.scale = 1 line. – john doe Feb 25 '18 at 03:06
  • what about dragging the node ? for example I need to touch the node and move it around the scene – iOS.Lover Jun 04 '18 at 15:49
  • Hey @BlackMirrorz, thank you so much for your detailed answers! I and my team stuck on an issue for days ah :/ I posted an interesting question: https://stackoverflow.com/questions/63662318/arkit-apply-filter-cifilter-to-a-specific-part-vertex-of-an-arfaceanchor Would love your suggestions! – Roi Mulia Aug 31 '20 at 07:29
  • God bless you, brother! This worked prefectly! – Binaya Thapa Magar Nov 14 '21 at 16:13
  • This scales the node relative to the node's center, can you please hint how could we scale the node according to the center of the pinch gesture? – Petru Lutenco Nov 10 '22 at 10:30