RealityKit saving changed scale/rotation gesture values after installGestures

Hi, does anyone know how to save a ModelEntity's gesture values after it has been transformed? As in, if I have a ball and I enlarge it, is there a way to grab the value that it was scaled by so that it can be used again later. I want to be able to load the AR app again and see the AR Object at a specific scale/rotation without using ARWorldMap if possible.


I'm currently using installGestures to add gestures to my entity but I don't see an easy way to grab scaled values.


Also, in addition, is there a way to have some sort of observer for event gesture changes on an AR Entity? I know that there exists an EntityScaleGestureRecognizer but I'm not sure how to use it.


Thanks in advance.

Replies

The method installGestures returns a list of EntityGestureRecognizer objects, take these, find which one is also an EntityScaleGestureRecognizer if multiple are returned, and then add a target to that one as with any other UIGestureRecogniser.

"I'm currently using installGestures to add gestures to my entity but I don't see an easy way to grab scaled values."


The installGestures modify the transform of the entity that they are operating on. You can always inspect these properties, for example, if you need to get the scale of an entity, just query its scale property.

Hi fullstackdev,

To expand on the above answers, the EntityGestureRecognizer can be utilized and handled like any other that conforms to UIGestureRecognizer. When you add gestures to your ARView for your entity, you can add an objc function as its target, and access the gesture's states. Please consider the following code:

arView.installGestures(.all, for: clonedEntity).forEach { entityGesture in
    entityGesture.addTarget(self, action: #selector(handleEntityGesture(_:)))
}

Then our target function would look something like this:

@objc func handleEntityGesture(_ sender: UIGestureRecognizer) {
    // Scale
    if let scaleGesture = sender as? EntityScaleGestureRecognizer {
        switch scaleGesture.state {
        case .began:
            // Handle things that happen when gesture begins
        case .changed:
            // Handle things during the gesture
        case .ended:
            // Handle things when the gesture has ended
        default:
            return
        }
    }
}

In the .ended case, you can grab the entity's scale like so, and do whatever you want with it. If you need to store it somewhere, consider passing it into some other type of function:

case .ended:
    let scale = scaleGesture.entity?.transform.scale
    // Do what you want with the scale...

You can also access the other gesture recognizers by casting the gesture as either EntityTranslationGestureRecognizer, or EntityRotationGestureRecognizer and access their states similarly like the above.

Hope this helps.