Skip to content

Instantly share code, notes, and snippets.

@jungchris
Created June 19, 2020 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jungchris/d4277d3c23acf22145d89d8a27327425 to your computer and use it in GitHub Desktop.
Save jungchris/d4277d3c23acf22145d89d8a27327425 to your computer and use it in GitHub Desktop.
ARWorkshop 3 Grafitti in 3D
//
// ViewController.swift
// helloARAppWorkshop2
//
// Created by Chris Jungmann on 6/17/20.
// Copyright © 2020 Chris Jungmann. All rights reserved.
//
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
// Create a new scene
// let scene = SCNScene(named: "art.scnassets/ship.scn")!
// Set the scene to the view
// sceneView.scene = scene
sceneView.autoenablesDefaultLighting = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
@IBAction func addSomething(_ sender: Any) {
// self.addSimpleShapes()
}
@IBOutlet weak var touchButton: UIButton!
// MARK: - Workshop III
// MARK: - Utilities
func randomNumber(minValue: CGFloat, maxValue: CGFloat) -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(minValue - maxValue) + min(minValue, maxValue)
}
// MARK: - ARSCNViewDelegate
/*
// Override to create and configure nodes for anchors added to the view's session.
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
return node
}
*/
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) {
print("Will render")
guard let pointOfView = sceneView.pointOfView else {return}
let transform = pointOfView.transform
let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33)
let location = SCNVector3(transform.m41, transform.m42, transform.m43)
let cameraOrientationAndLocation = orientation + location
print(cameraOrientationAndLocation)
DispatchQueue.main.async {
if self.touchButton.isHighlighted {
let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.01))
sphereNode.position = cameraOrientationAndLocation
sphereNode.geometry?.firstMaterial?.diffuse.contents = UIColor.black
sphereNode.geometry?.firstMaterial?.diffuse.intensity = 0.75
sphereNode.geometry?.firstMaterial?.transparency = 0.45
self.sceneView.scene.rootNode.addChildNode(sphereNode)
}
}
}
func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
}
func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
}
postfix operator +
func +(left: SCNVector3, right: SCNVector3) -> SCNVector3 {
return SCNVector3Make(left.x + right.x, left.y + right.y, left.z + right.z)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment