Skip to content

Instantly share code, notes, and snippets.

@RNHTTR
Last active November 14, 2018 02:40
Show Gist options
  • Save RNHTTR/c4f823499e9fb4337b796593d26135d9 to your computer and use it in GitHub Desktop.
Save RNHTTR/c4f823499e9fb4337b796593d26135d9 to your computer and use it in GitHub Desktop.
Create a basic AR object using ARKit and SCNKit
// Creating a simple Augmented Reality object with ARKit and SceneKit is surprisingly easy! But before you get started, you
// need to add a key-value pair to your application's Info.plist (Information Property List) file. Add the key "Privacy -
// Camera Usage Description" with the corresponding value "This application will use your camera for Augmented Reality", or
// some similarly descriptive note.
// Let's get started!
// You'll need to import SceneKit to create your object and ARKit to place it in your augmented reality space.
import UIKit
import SceneKit
import ARKit
// Make sure you conform to the ARSCNView Delegate.
class ARBasicObjectViewController: UIViewController, ARSCNViewDelegate {
// Declare an ARSCNView
var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Set the view frame to match the bounds of your view controller, and add the sceneView as a subview to your view
// controller's view.
self.sceneView = ARSCNView(frame: self.view.frame)
self.view.addSubview(self.sceneView)
// Assign the sceneView's delegate to itself, and optionally display statistics at the bottom of the view.
sceneView.delegate = self
sceneView.showsStatistics = true
// Create an SCNScene object to be used later and create an SCNBox. This box will be the 3D object that will be
// displayed. Note that the dimensions of the box are in meters, so choose appropriate values depending on your
// goals. Also, chamfer refers to an object's right-angled edge. The chamferRadius parameter is similar to editing
// the corner radius of two dimensional views in Swift; it determines how rounded (if at all) the edges of the box are.
let scene = SCNScene()
let box = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0.01)
// Create an SCNMaterial object, and use the diffuse.contents property to give the box some color. You can also
// assign static images to diffuse.contents. You can see that example when we explore an AR space with multiple
// objects.
let material = SCNMaterial()
material.diffuse.contents = UIColor.green
// Now create an SCNNode. Assign the box to the node's geometry property, and assign the materials to the
// geometry.materials property. Assign an SCNVector3 to the node's position property. Note that the parameters for the
// vector are in meters, so select appropriate values. Also note that a positive value for the z-coordinate will place
// the object behind you.
let node = SCNNode()
node.geometry = box
node.geometry?.materials = [material]
node.position = SCNVector3(x: 0, y: 0.1, z: -0.5)
// Add the node as a child node to the root node's childNodes array
scene.rootNode.addChildNode(node)
// Assign the scene to the scene property of the sceneView. Say that five times fast!
sceneView.scene = scene
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Allow your device to track the world around a device.. This class uses the rear camera and track's a device's
// position and orientation and detects flat surfaces in the real world.
// See https://developer.apple.com/documentation/arkit/arworldtrackingconfiguration for more details.
let configuration = ARWorldTrackingConfiguration()
// Run the sceneView's session with the configuration you just declared.
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the sceneView's session when the user is not making use of the AR view. This allows the device to avoid using
// computationally expensive AR rendering.
sceneView.session.pause()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment