This file contains 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
class GameViewController: UIViewController, SCNSceneRendererDelegate { |
This file contains 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
override func viewDidLoad() { | |
super.viewDidLoad() | |
let scene = SCNScene(named: "art.scnassets/ship.scn")! | |
scnView.delegate = self | |
scnView.scene = scene |
This file contains 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 var pipelineState: MTLRenderPipelineState! | |
private var vertexBuffer: MTLBuffer! |
This file contains 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
func setupMetalResources() { | |
guard let device = scnView.device else { | |
assertionFailure() | |
return | |
} | |
// We're drawing a simple triangle so we only need a position and a color | |
struct TriangleVertex { | |
var position: vector_float4 | |
var color: vector_float4 |
This file contains 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
override func viewDidAppear(_ animated: Bool) { | |
super.viewDidAppear(animated) | |
setupMetalResources() | |
} |
This file contains 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
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) { | |
// Used to encode additional rendering commands after SceneKit has drawn its content. | |
guard let encoder = scnView.currentRenderCommandEncoder else { return } | |
encoder.setRenderPipelineState(pipelineState) | |
encoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0) | |
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3) | |
} |
This file contains 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 UIKit | |
import ARKit | |
class ViewController: UIViewController, ARSCNViewDelegate { | |
@IBOutlet var sceneView: ARSCNView! | |
private var viewportSize: CGSize! | |
override var shouldAutorotate: Bool { return false } |
This file contains 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
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval) { | |
// Get the capture image (which is a cvPixelBuffer) from the current ARFrame | |
guard let capturedImage = sceneView.session.currentFrame?.capturedImage else { return } | |
let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: capturedImage, | |
orientation: .leftMirrored, | |
options: [:]) | |
do { | |
try imageRequestHandler.perform([objectDetectionRequest]) |
This file contains 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
lazy var objectDetectionRequest: VNCoreMLRequest = { | |
do { | |
let model = try VNCoreMLModel(for: YOLOv3TinyInt8LUT().model) | |
let request = VNCoreMLRequest(model: model) { [weak self] request, error in | |
self?.processDetections(for: request, error: error) | |
} | |
return request | |
} catch { | |
fatalError("Failed to load Vision ML model.") | |
} |
OlderNewer