Skip to content

Instantly share code, notes, and snippets.

@delasign
Created August 13, 2018 19:37
Show Gist options
  • Save delasign/7097226b7b9b84c38e2e7825d48596c1 to your computer and use it in GitHub Desktop.
Save delasign/7097226b7b9b84c38e2e7825d48596c1 to your computer and use it in GitHub Desktop.
An ARKit Placement Square
//
// ViewController.swift
//
// Created by Oscar De la Hera Gomez on 8/13/18.
// Copyright © 2018 Delasign. All rights reserved.
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
var positioningPlaneNode:SCNNode = SCNNode();
override func viewDidLoad() {
super.viewDidLoad()
screenWidth = self.view.frame.width;
screenHeight = self.view.frame.height;
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints] //, ARSCNDebugOptions.showWorldOrigin]
// CREATE OUT PLANE FOR THE MARKER
// 1 CREATE A PLANE
let plane = SCNPlane(width: 0.1, height: 0.1);
// 2 SET IT TO BE TRANSPARENT - WE WILL CHANGE THIS WHEN ITS RESET
plane.materials.first?.diffuse.contents = UIColor.white.withAlphaComponent(0);
// 3 ADD THE PLANE TO THE NODE
self.positioningPlaneNode = SCNNode(geometry: plane);
// 4 SET AN INITIAL POSITION, AND A ROTIATION TO REFERENCE A HORIZONTAL PLANE
self.positioningPlaneNode.position = SCNVector3(0,0,0);
self.positioningPlaneNode.eulerAngles.x = -.pi / 2
// 5 ADD THE NODE TO THE SCENE
self.sceneView.scene.rootNode.addChildNode(self.positioningPlaneNode);
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = .horizontal;
// 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()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
// 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 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
}
// MARK : ARKIT RENDERER
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
}
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
}
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
// PERFORM A HIT TEST
// let centerOfScreen:CGPoint = CGPoint(x: screenWidth/2, y: screenHeight/2);
let centerOfScreen:CGPoint = CGPoint(x: 0.5, y: 0.5);
let currentFrame:ARFrame? = self.sceneView.session.currentFrame;
guard currentFrame != nil else {
return
}
let results:[ARHitTestResult] = currentFrame!.hitTest(centerOfScreen, types: [.estimatedHorizontalPlane, .featurePoint]);
if results.count < 1 {
return;
}
else {
// USE THE HITTEST TO CHANGE THE POSITION OF THE PLANE.
let firstResult:ARHitTestResult = results.first!;
print(firstResult);
self.positioningPlaneNode.position = SCNVector3Make(
firstResult.worldTransform.columns.3.x,
firstResult.worldTransform.columns.3.y,
firstResult.worldTransform.columns.3.z
);
// self.positioningPlaneNode.rotation = firstResult.worldTransform
self.positioningPlaneNode.geometry!.materials.first?.diffuse.contents = UIColor.white.withAlphaComponent(0.5);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment