Skip to content

Instantly share code, notes, and snippets.

@factoryhr
Created June 1, 2020 08:28
Show Gist options
  • Save factoryhr/dda8f277ba7b3fd9149006296e411a3e to your computer and use it in GitHub Desktop.
Save factoryhr/dda8f277ba7b3fd9149006296e411a3e to your computer and use it in GitHub Desktop.
import UIKit
import ARKit
import SceneKit
class ARViewController: UIViewController{
// First we create instances of ARSCNView, ARSession and ARWorldTracking configuration
let sceneView: ARSCNView = {
let scene = ARSCNView()
scene.translatesAutoresizingMaskIntoConstraints = false
return scene
}()
let session = ARSession()
let sessionConfiguration: ARWorldTrackingConfiguration = {
let config = ARWorldTrackingConfiguration()
config.planeDetection = .horizontal
return config
}()
//In viewDidLoad we setup our sceneView, there are plenty of options we can use to configure our scene
override func viewDidLoad(){
sceneView.delegate = self
sceneView.session = session
sceneView.automaticallyUpdatesLighting = true
sceneView.autoenablesDefaultLighting = true
sceneView.preferredFramesPerSecond = 60
}
//We use lifecycle methods viewWillAppear and viewWillDisappear to control when will our session start and pause
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
session.run(sessionConfiguration, options: [.removeExistingAnchors, .resetTracking])
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
session.pause()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment