Skip to content

Instantly share code, notes, and snippets.

@RNHTTR
Last active September 22, 2017 13:37
Show Gist options
  • Save RNHTTR/2061df49d3a7f856e5c047cd096c468d to your computer and use it in GitHub Desktop.
Save RNHTTR/2061df49d3a7f856e5c047cd096c468d to your computer and use it in GitHub Desktop.
Create a simple 3D text object in augmented reality
// Creating a 3D text object with ARKit isn't overwhelming. It might not be immediately obvious, but these objects could
// be quite valuable moving forward. Imagine dropping a 3D advertisement in someone's augmented reality universe...
// We'll get started in a sec, but first 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 ARTextViewController: 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
// Declare an SCNScene object for use later.
let scene = SCNScene()
// Create a geometry using SCNText. The string that you pass will be the 3D text that's displayed in your AR world.
// The extrusion depth is how far along the z-axis the text will extend, that is how deep the text extends away from you.
// Use the firstMaterial.diffuse.contents property to change the color of your text.
let textGeometry = SCNText(string: "Hello World!", extrusionDepth: 1.0)
textGeometry.firstMaterial?.diffuse.contents = UIColor.blue
// Create an SCNNode and pass the textGeometry as a parameter.
// Determine your node's position using an SCNVector3. To place it at eye-level, directly in front of you, you will
// need to pass negative values. The text will start from left to right, with the first letter beginning at the
// x-coordinate. A positive value for the z-coordinate will place your text behind you, so give it a negative value
// to place the text in front of you. Finally, scale down the text significantly for all coordinates, or else it will
// be huge. The x-coordinate assigned to the scaling vector expands or compresses the node horizontally, the y-coordinate
// expands or compresses the node vertically, and the z-coordinate expands or compresses the depth of the node. Values
// greater than one will increase the size of the object, while values less than one will make the object smaller.
let textNode = SCNNode(geometry: textGeometry)
textNode.position = SCNVector3(x: -1, y: -0.2, z: -1)
textNode.scale = SCNVector3(x: 0.02, y: 0.02, z: 0.02)
// Add the node as a child node to the root node's childNodes array
scene.rootNode.addChildNode(textNode)
// 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