Skip to content

Instantly share code, notes, and snippets.

@RNHTTR
Last active September 22, 2017 13:40
Show Gist options
  • Save RNHTTR/08d6128541e75bc620a9a1f8d26bd205 to your computer and use it in GitHub Desktop.
Save RNHTTR/08d6128541e75bc620a9a1f8d26bd205 to your computer and use it in GitHub Desktop.
Add a second object (a sphere) to your Augmented Reality space
// Adding a second object to your Augmented Reality space is not much different from adding the first, so we will change
// things up here a bit. Here we will create a sphere in addition to a box and we will give our objects some style.
// If you haven't already, 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 and edit your objects 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 ARMultipleObjectsViewController: 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 for your new box. You will notice that we are assigning a UIImage named "marioSide"
// to the material's diffuse.contents property. Download a square image from the web, and store the image in your
// Assets.xcassets folder. Give it the same name that you use here in line 44, e.g. "marioSide".
let boxMaterial = SCNMaterial()
boxMaterial.diffuse.contents = UIImage(named: "marioSide")
// 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 boxNode = SCNNode()
boxNode.geometry = box
boxNode.geometry?.materials = [boxMaterial]
boxNode.position = SCNVector3(x: 0.2, y: 0.1, z: -0.5)
// Similar to the box we just created, we will create an SCNSphere and follow esssentially the same pattern. Create
// the objecet, assign it a material, create a node, and assign it to a position in AR space.
let sphere = SCNSphere(radius: 0.2)
let sphereMaterial = SCNMaterial()
sphereMaterial.diffuse.contents = UIImage(named: "bobOmb")
let sphereNode = SCNNode()
sphereNode.geometry = sphere
sphereNode.geometry?.materials = [sphereMaterial]
sphereNode.position = SCNVector3(x: -0.2, y: -0.2, z: -1.0)
// Add the nodes as child nodes to the root node's childNodes array
scene.rootNode.addChildNode(boxNode)
scene.rootNode.addChildNode(sphereNode)
// 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