Skip to content

Instantly share code, notes, and snippets.

@piemonte
Last active May 2, 2019 18:17
Show Gist options
  • Save piemonte/4fd443818430952c187eeca544e700d5 to your computer and use it in GitHub Desktop.
Save piemonte/4fd443818430952c187eeca544e700d5 to your computer and use it in GitHub Desktop.
NextLevel + ARKit Recording Example
//
// NextLevelARKitExample.swift
// NextLevel + ARKit Example
//
// The MIT License (MIT)
//
// Copyright (c) 2016-present patrick piemonte (http://patrickpiemonte.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
import NextLevel
public class MixedRealityViewController: UIViewController {
// MARK: - ivars
internal var _bufferRenderer: NextLevelBufferRenderer?
// MARK: - object lifecycle
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
public required init?(coder aDecoder: NSCoder) {
fatalError("\(#function) has not been implemented")
}
deinit {
}
// MARK: - view lifecycle
public override func viewDidLoad() {
super.viewDidLoad()
// ... setup views
// setup NextLevel
let nextLevel = NextLevel.shared
nextLevel.delegate = self
nextLevel.deviceDelegate = self
nextLevel.videoDelegate = self
nextLevel.captureMode = .arKit
nextLevel.isVideoCustomContextRenderingEnabled = true
nextLevel.videoStabilizationMode = .off
nextLevel.frameRate = 60
// setup video out rendering
self._bufferRenderer = NextLevelBufferRenderer(view: self._arView)
}
}
// MARK: - ARSCNViewDelegate
extension MixedRealityViewController: ARSCNViewDelegate {
public func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
guard let _ = self._arView.pointOfView else {
return
}
if NextLevel.shared.isRecording {
if let _ = self._arView.session.currentFrame?.capturedImage {
self._bufferRenderer?.renderer(renderer, didRenderScene: scene, atTime: time)
}
}
}
}
// MARK: - NextLevelVideoDelegate
extension MixedRealityViewController: NextLevelVideoDelegate {
// enabled by isCustomContextVideoRenderingEnabled
public func nextLevel(_ nextLevel: NextLevel, renderToCustomContextWithImageBuffer imageBuffer: CVPixelBuffer, onQueue queue: DispatchQueue) {
if let frame = self._bufferRenderer?.videoBufferOutput {
nextLevel.videoCustomContextImageBuffer = frame
}
}
}
@piemonte
Copy link
Author

piemonte commented May 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment