Skip to content

Instantly share code, notes, and snippets.

@jungchris
Last active June 17, 2020 20:42
Show Gist options
  • Save jungchris/9ea283bcc81658b9959e3b53c2585b78 to your computer and use it in GitHub Desktop.
Save jungchris/9ea283bcc81658b9959e3b53c2585b78 to your computer and use it in GitHub Desktop.
Functions to Add Simple Shapes in ARKit for iOS Workshop
class ViewController: UIViewController {
@IBOutlet weak var sceneView: ARSCNView!
// declare world tracker
let configuration = ARWorldTrackingConfiguration()
override func viewDidLoad() {
super.viewDidLoad()
// the debug options are defined as an array. In this case we want feature points and the x,y,z, origin displayed
self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
// once the view has loaded, we start the session using the "configuration" we declared earlier
self.sceneView.session.run(configuration)
}
/* SNIP */
// we will call the following functions from a button added to the bottom on the view
func addSimpleShapes() {
let node = SCNNode()
// pre-defined shapes & size
node.geometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0.003)
// node.geometry = SCNCapsule(capRadius: 0.05, height: 0.5)
// node.geometry = SCNCone(topRadius: 0.05, bottomRadius: 0.1, height: 0.1)
// node.geometry = SCNCylinder(radius: 0.05, height: 0.1)
// node.geometry = SCNSphere(radius: 0.1)
// node.geometry = SCNTorus(ringRadius: 0.2, pipeRadius: 0.05)
// node.geometry = SCNPlane(width: 0.1, height: 0.1)
// node.geometry = SCNPyramid(width: 0.1, height: 0.1, length: 0.1)
// appearance
node.geometry?.firstMaterial?.diffuse.contents = UIColor.green
// define light source color
node.geometry?.firstMaterial?.specular.contents = UIColor.white
// position
let x = randomNumber(minValue: -0.2, maxValue: 0.2)
let y = randomNumber(minValue: -0.2, maxValue: 0.2)
let z = randomNumber(minValue: -0.2, maxValue: 0.2)
node.position = SCNVector3(x, y, z)
// add to scene
self.sceneView.scene.rootNode.addChildNode(node)
}
func addShapesWithRelativePositioning() {
let pyramidNode = SCNNode()
// relative positioning
pyramidNode.geometry = SCNPyramid(width: 0.1, height: 0.1, length: 0.1)
// appearance
pyramidNode.geometry?.firstMaterial?.diffuse.contents = UIColor.yellow
// define light source color
pyramidNode.geometry?.firstMaterial?.specular.contents = UIColor.white
// position
let x = 0.0
let y = 0.0
let z = -0.5
pyramidNode.position = SCNVector3(x, y, z)
// add to scene
self.sceneView.scene.rootNode.addChildNode(pyramidNode)
// define a cylinder node
let cylinderNode = SCNNode(geometry: SCNCylinder(radius: 0.05, height: 0.07))
cylinderNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
cylinderNode.position = SCNVector3(0.1, 0.1, 0.1)
// define a box node
let boxNode = SCNNode(geometry: SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0.003))
boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
boxNode.position = SCNVector3(0.1, 0.1, 0.1)
// add relative to pyramid node position
pyramidNode.addChildNode(cylinderNode)
cylinderNode.addChildNode(boxNode)
}
func addBezierPaths() {
let node = SCNNode()
// bezier paths
let path = UIBezierPath()
path.move(to: CGPoint(x:0, y:0))
// house
path.addLine(to: CGPoint(x:0, y:0.2))
path.addLine(to: CGPoint(x: 0.2, y: 0.3))
path.addLine(to: CGPoint(x: 0.4, y: 0.2))
path.addLine(to: CGPoint(x: 0.4, y: 0.0))
// path.addCurve(to: CGPoint(x:0.1, y:0.3), controlPoint1: CGPoint(x:0.05, y:0.02), controlPoint2: CGPoint(x:0.05, y:0.04))
let shape = SCNShape(path: path, extrusionDepth: 0.2)
node.geometry = shape
// appearance
node.geometry?.firstMaterial?.diffuse.contents = UIColor.orange
// position
let x = 0.0
let y = 0.0
let z = -0.5
node.position = SCNVector3(x, y, z)
// define light source color
node.geometry?.firstMaterial?.specular.contents = UIColor.white
// add to scene
self.sceneView.scene.rootNode.addChildNode(node)
}
func randomNumber(minValue: CGFloat, maxValue: CGFloat) -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(minValue - maxValue) + min(minValue, maxValue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment