7 Replies
      Latest reply: Oct 1, 2017 11:33 AM by Dikkesnoek RSS
      bnjmm Level 1 Level 1 (10 points)

        In a previous version of ARKitExample it was possible to make a pinch gesture to scale a model - in the curent version there is still a toggle switch for 'Pinch to resize objects' but it doesn't seem to allow pinching. Is this is a bug?

         

        It would be really helpful to have a working example of how to use a pinch gesture to resize a model Maybe there are other examples.

        • Re: How to use a pinch gesture in the 3d scene / Broken in ARKitExample
          modeminho Level 1 Level 1 (0 points)

          ive noticed this as well. did you manage to solve this problem ?

          • Re: How to use a pinch gesture in the 3d scene / Broken in ARKitExample
            TonyPham Level 2 Level 2 (80 points)

            The problem is the example is missing the code to scale models even involving varians are still there. You may fix as following:

             

            Open file TwoFingerGesture.swift, add below function into the end:

             

               func updateScaling(of virtualObject: VirtualObject, span: CGPoint) {
                    let distanceBetweenFingers = span.length()
                    if !scaleThresholdPassed {
                        let fingerSpread = abs(distanceBetweenFingers - initialDistanceBetweenFingers)
                        var threshold = scaleThreshold
                        if translationThresholdPassed || rotationThresholdPassed {
                            threshold = scaleThresholdHarder
                        }
                        if fingerSpread > threshold {
                            scaleThresholdPassed = true
                            baseDistanceBetweenFingers = distanceBetweenFingers
                        }
                    }
                    if scaleThresholdPassed {
                        if baseDistanceBetweenFingers != 0 {
                            let relativeScale = distanceBetweenFingers / baseDistanceBetweenFingers
                            let newScale = objectBaseScale * Float(relativeScale)
                            // Uncomment the block below to "snap" the 3D model to 100%.
                            /
                             if newScale >= 0.96 && newScale <= 1.04 {
                             newScale = 1.0 // Snap scale to 100% when getting close.
                             }*/
                            virtualObject.scale = SCNVector3Make(newScale, newScale, newScale)
                            if let nodeWhichReactsToScale = virtualObject.reactsToScale() {
                                nodeWhichReactsToScale.reactToScale()
                            }
                        }
                    }
                }
            

              

             

            Find the function func updateGesture() and add to the end of the function:

             

                if UserDefaults.standard.bool(for: .scaleWithPinchGesture) {
                        updateScaling(of: virtualObject, span: spanBetweenTouches)
                }
            

                

             

             

            Now, you may compile and run the app, pinch to scale with fingers put anywhere.

              • Re: How to use a pinch gesture in the 3d scene / Broken in ARKitExample
                Dikkesnoek Level 1 Level 1 (0 points)

                Hi Tony,

                 

                Thanks a lot. I've to study for a while before I can write missing code like this

                I realy don't know why they changed the code so much. The first demo was

                great with a lot of options and easy to modify to learn more about ARKit.

                 

                Regards,

                 

                Marc

                  • Re: How to use a pinch gesture in the 3d scene / Broken in ARKitExample
                    Dikkesnoek Level 1 Level 1 (0 points)

                    Hello Tony,

                     

                    Works great, thanks a lot!

                     

                    Maybe you can give me a hint for the following: when I select a new object (for example a vase) I want to

                    remove the previous object (say the candle) from the view. Normally this is not difficult to find, .addView,

                    etc. but there are to many classes in this project.

                     

                    Thanks in advance,

                     

                    Marc

                      • Re: How to use a pinch gesture in the 3d scene / Broken in ARKitExample
                        Dikkesnoek Level 1 Level 1 (0 points)

                        Think I found it (ViewController+ObjectSelection.swift):

                         

                            func virtualObjectSelectionViewController(_: VirtualObjectSelectionViewController, didSelectObjectAt index: Int)

                            {

                                guard let cameraTransform = session.currentFrame?.camera.transform

                                else

                                {

                                    return

                                }

                           

                                let definition = VirtualObjectManager.availableObjects[index]

                                let object = VirtualObject(definition: definition)

                                let position = focusSquare?.lastPosition ?? float3(0)

                                virtualObjectManager.removeAllVirtualObjects()        // THIS IS WHAT I'VE ADDED

                                virtualObjectManager.loadVirtualObject(object, to: position, cameraTransform: cameraTransform)

                                if object.parent == nil

                                {

                                    serialQueue.async

                                    {

                                        self.sceneView.scene.rootNode.addChildNode(object)

                                    }

                                }

                            }