This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ARKit | |
import SceneKit | |
public class PlaneNode: SCNNode { | |
// MARK: - Public functions | |
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(), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - ARSCNViewDelegate | |
func renderer(_: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? { | |
guard let _ = anchor as? ARPlaneAnchor else { return nil } | |
// We return a special type of SCNNode for ARPlaneAnchors | |
return PlaneNode() | |
} | |
func renderer(_: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SceneKit | |
public class SpotlightNode: SCNNode { | |
// MARK: - Lifecycle | |
public override init() { | |
super.init() | |
commonInit() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SceneKit | |
public class TouchNode: SCNNode { | |
// MARK: - Lifecycle | |
public override init() { | |
super.init() | |
commonInit() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |