How to use a pinch gesture in the 3d scene / Broken in ARKitExample

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.

Replies

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

Hi,


I was also trying to get it fixed, but I am still strugling with one of the most

disturbing and difficult programming languages; Swift (underscores, optionals,

lazy, etc.) I've ever seen. Even Objective C was much simpler. In the previous

version of ARKitExample, also objects were replaced when touching the screen,

need to change that too. It's a great demo by the way.


Best regards,


Marc

You cannot simply fix since the scale code is missing 😉

I will post those code when I am at home later

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.

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

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

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)

}

}

}