Skip to content

Instantly share code, notes, and snippets.

import PlaygroundSupport
import UIKit
@testable import MVCMovieInfoFramework
NSSetUncaughtExceptionHandler { exception in
print("💥 Exception thrown: \(exception)")
}
PlaygroundPage.current.needsIndefiniteExecution = true
import PlaygroundSupport
import UIKit
@testable import MVCMovieInfoFramework
NSSetUncaughtExceptionHandler { exception in
print("💥 Exception thrown: \(exception)")
}
PlaygroundPage.current.needsIndefiniteExecution = true
@giln
giln / StartDetection.swift
Created March 4, 2019 14:50
StartDetection
private func startDetection() {
// To avoid force unwrap in VNImageRequestHandler
guard let buffer = currentBuffer else { return }
handDetector.performDetection(inputBuffer: buffer) { outputBuffer, _ in
// Here we are on a background thread
var previewImage: UIImage?
var normalizedFingerTip: CGPoint?
defer {
@giln
giln / TouchNode.swift
Created March 4, 2019 14:49
TouchNode
import SceneKit
public class TouchNode: SCNNode {
// MARK: - Lifecycle
public override init() {
super.init()
commonInit()
}
@giln
giln / CVPixelBuffer.swift
Created March 4, 2019 14:49
CVPixelBuffer
import CoreVideo
extension CVPixelBuffer {
func searchTopPoint() -> CGPoint? {
// Get width and height of buffer
let width = CVPixelBufferGetWidth(self)
let height = CVPixelBufferGetHeight(self)
let bytesPerRow = CVPixelBufferGetBytesPerRow(self)
@giln
giln / SpotlightNode.swift
Created March 4, 2019 14:48
SpotlightNode
import SceneKit
public class SpotlightNode: SCNNode {
// MARK: - Lifecycle
public override init() {
super.init()
commonInit()
}
// This allows the material to be invisible but still receive shadows and perform occlusion (hide objects behind them).
let material = SCNMaterial()
material.lightingModel = .constant
material.writesToDepthBuffer = true
material.colorBufferWriteMask = []
geom.firstMaterial = material
geom.update(from: planeAnchor.geometry)
@giln
giln / UpdatePlane.swift
Created March 4, 2019 14:47
UpdatePlane
public func update(from planeAnchor: ARPlaneAnchor) {
// We need to create a new geometry each time because it does not seem to update correctly for physics
guard let device = MTLCreateSystemDefaultDevice(),
let geom = ARSCNPlaneGeometry(device: device) else {
fatalError()
}
geom.firstMaterial?.diffuse.contents = UIColor.blue.withAlphaComponent(0.3)
geom.update(from: planeAnchor.geometry)
@giln
giln / Selector.swift
Created March 4, 2019 14:46
Selector
// MARK: - Actions
@objc func viewDidTap(recognizer: UITapGestureRecognizer) {
// We get the tap location as a 2D Screen coordinate
let tapLocation = recognizer.location(in: sceneView)
// To transform our 2D Screen coordinates to 3D screen coordinates we use hitTest function
let hitTestResults = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
// We cast a ray from the point tapped on screen, and we return any intersection with existing planes
@giln
giln / BallNode.swift
Created March 4, 2019 14:45
BallNode
import SceneKit
public class BallNode: SCNNode {
// MARK: - Lifecycle
public convenience init(radius: CGFloat) {
self.init()
let sphere = SCNSphere(radius: radius)
// We create a Physically Based Rendering material