1

Using iOS14.2, XCode12.1, Swift5.3,

My first AR experience in my Xcode project works nicely.

In reality composer, I defined an anchor of type "image" and placed a 3D-object on its XY-center. In addition I added a small behaviour animation that makes the 3D-object flip when touched and send a touch-notification event right away. It all works very nicely in my App.

However, I would like to do some next steps and don't know how to achieve this.

In particular: step nr 3 in the below list I don't know how to do:

  1. tap on an AR-object and make it flip
  2. make the object disappear after the flip-animation
  3. make the object re-appear once the camera moves again over the anchored-image (or anchor-object)

The code for step 1 and 2 can be found below.

The problem I see is that the object never re-appears - even tough the camera points to the anchor-image.

Is there any way where you make a 3D object disappear (or hide) by touching it and make it re-appear once you move your camera towards the anchor-image (or anchor-object) again ??

import UIKit
import RealityKit

class MySceneViewController: UIViewController {
    
    @IBOutlet weak var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myScene1Anchor = try! MyScene1.loadScene()
        
        // arView.scene.addAnchor(myScene1Anchor)
        arView.scene.anchors.append(myScene1Anchor)
                
        // Register an onAction handler for the notification
        // action "tearTouched" you created in Reality Composer
        myScene1Anchor.actions.tearTouched.onAction = handleTapOnEntity(_:)
    }
    
    func handleTapOnEntity(_ entity: Entity?) {
        guard let entity = entity else { return }

        // this is the callback that kicks in once the touch and flip-animation has happened:
        // the object can be removed like this
        // !!!!!!!!!!!!! However, after removing, the object will never appear again.
        entity.removeFromParent()

        // ????????????? How can I make this disappearing better so that the object re-appears after the camera shows the anchor-image again ??????????
    }
}

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
iKK
  • 6,394
  • 10
  • 58
  • 131

1 Answers1

0

I found a workaround:

Make the entity-object hide once the flip-animation has finished (not remove like in the example above).

And this hiding is done by setting isEnabled = false for the entity.object.

Moreover, you need some sort of trigger to unhide the entity-object again. Here it is done with a simple target-action gesture-tap method. But you can choose a different mechanism, of course, to make the entity-object unhide.

As far as I read, there is no trigger possibility in realityKit AR to measure if a given anchor-type (such as image or object) re-enters the camera field.

Here the workaround code :

import UIKit
import RealityKit

class MySceneViewController: UIViewController {
    
    @IBOutlet weak var arView: ARView!
    private var myEntity: Entity?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let myScene1Anchor = try! MyScene1.loadScene()
        
        arView.scene.anchors.append(myScene1Anchor)
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
        arView.addGestureRecognizer(tap)

        // Register an onAction handler for the notification
        // action "tearTouched" you created in Reality Composer
        myScene1Anchor.actions.tearTouched.onAction = handleTapOnEntity(_:)
    }
    
    @objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
        self.myEntity?.isEnabled = true
    }
    
    func handleTapOnEntity(_ entity: Entity?) {
        
        guard let entity = entity else { return }
        
        self.myEntity = entity
        self.myEntity?.isEnabled = false
    }
}
iKK
  • 6,394
  • 10
  • 58
  • 131