Home > Blockchain >  How do I stop ARView session on present modally segue using storyboard?
How do I stop ARView session on present modally segue using storyboard?

Time:03-15

I've got two ViewControllers containing an ARView. Code is as follows:

import UIKit
import RealityKit
import ARKit

class fvBoat: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
        let fvBoatAnchor = try! Vard.loadFvBoatScene()
            var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
    
           
        override func viewDidLoad() {
                super.viewDidLoad()
            fvBoatAnchor.generateCollisionShapes(recursive: true)
            let fvBoat = fvBoatAnchor.fvBoatObject as? Entity & HasCollision
                arView.installGestures(for: fvBoat!)
                arView.scene.addAnchor(fvBoatAnchor)
                arView.session.delegate = self
        }
           
            func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = AnchorEntity()
                    let modelEntity = fvBoatAnchor.fvBoatObject!
                    anchorEntity.addChild(modelEntity)
                    arView.scene.addAnchor(anchorEntity)
                    anchorEntity.transform.matrix = $0.transform
                    imageAnchorToEntity[$0] = anchorEntity
                }
            }

            func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = imageAnchorToEntity[$0]
                    anchorEntity?.transform.matrix = $0.transform
                }
            }
            func installGestures(on object:ModelEntity){
                
                object.generateCollisionShapes(recursive: true)
                arView.installGestures([.rotation,.scale], for: object)
            }
    }

Both view controller has the same code as above.

Whenever I navigate to the next ARview using a Present Modally segue, my framerate drops significantly. How do I make sure that ARview session is closed down when I segue to the next ARview?

Storyboard view of the ViewController

Also tried adding this function, but not sure why it doesn't work...

func leaveScene() {

        arView?.session.pause()
        arView?.removeFromSuperview()
        arView = nil

    }

CodePudding user response:

You turned off not everything that needed to be turned off.

func leaveScene() {

    arView?.session.pause()
    arView?.session.delegate = nil
    arView?.scene.anchors.removeAll()
    arView?.removeFromSuperview()
    arView?.window?.resignKey()
    arView = nil
}

P.S. But arView will not be deallocated from memory.

CodePudding user response:

This solved it for me. Using the code from @Andy I added the leaveScene function and called it as using send event from the UIbutton:

@IBAction func leaveScene(_ sender: Any) {
        leaveScene()
    }

Which make my code look like this.


import UIKit
import RealityKit
import ARKit

class fvBridge: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
        let fvBridgeAnchor = try! Experience.loadFvBridgeScene()
            var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
    
           
        override func viewDidLoad() {
                super.viewDidLoad()
            fvBridgeAnchor.generateCollisionShapes(recursive: true)
            let fvBridge = fvBridgeAnchor.fvBridgeObject as? Entity & HasCollision
                arView.installGestures(for: fvBridge!)
                arView.scene.addAnchor(fvBridgeAnchor)
                arView.session.delegate = self
        }
           
            func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = AnchorEntity()
                    let modelEntity = fvBridgeAnchor.fvBridgeObject!
                    anchorEntity.addChild(modelEntity)
                    arView.scene.addAnchor(anchorEntity)
                    anchorEntity.transform.matrix = $0.transform
                    imageAnchorToEntity[$0] = anchorEntity
                }
            }

            func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = imageAnchorToEntity[$0]
                    anchorEntity?.transform.matrix = $0.transform
                }
            }
            func installGestures(on object:ModelEntity){
                
                object.generateCollisionShapes(recursive: true)
                arView.installGestures([.rotation,.scale], for: object)
            }
    func leaveScene() {

        arView?.session.pause()
        arView?.session.delegate = nil
        arView?.scene.anchors.removeAll()
        arView?.removeFromSuperview()
        arView?.window?.resignKey()
        arView = nil
    }
    
    @IBAction func leaveScene(_ sender: Any) {
        leaveScene()
    }
}
  • Related