Skip to content

Commit

Permalink
added SceneKit rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
AFathi committed Apr 21, 2018
1 parent 7fb760d commit 054d23c
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 3 deletions.
14 changes: 14 additions & 0 deletions ARVideoKit/Rendering/RenderAR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal struct RenderAR {
}else if let view = view as? ARSKView {
guard let rawBuffer = view.session.currentFrame?.capturedImage else{return nil}
return rawBuffer
}else if let _ = view as? SCNView {
return buffer
}
return nil
}
Expand Down Expand Up @@ -99,6 +101,18 @@ internal struct RenderAR {
}
guard let buffer = renderedFrame!.buffer else{return nil};
return buffer;
}else if let _ = view as? SCNView {
let size = UIScreen.main.bounds.size
var renderedFrame:UIImage?
pixelsQueue.sync {
renderedFrame = renderEngine.snapshot(atTime: self.time, with: size, antialiasingMode: .none);
}
if let _ = renderedFrame {
}else{
renderedFrame = renderEngine.snapshot(atTime: time, with: size, antialiasingMode: .none);
}
guard let buffer = renderedFrame!.buffer else{return nil};
return buffer;
}
return nil;
}
Expand Down
16 changes: 16 additions & 0 deletions ARVideoKit/Sources/ARView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ fileprivate var recentAngle = 0
parentVC = vc
}

@objc init?(SceneKit:SCNView) {
super.init()
NotificationCenter.default.addObserver(self, selector: #selector(deviceDidRotate), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

ViewAR.orientation = .portrait

guard let vc = SceneKit.parent else {
return
}

parentVC = vc
}

@objc fileprivate func deviceDidRotate() {
guard var views = parentVC?.view.subviews else {
return
Expand Down
30 changes: 28 additions & 2 deletions ARVideoKit/Sources/RecordAR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ fileprivate var renderer:RenderAR!
logAR.message("Error occurred while loading SK Video Assets : \(error). Please download \"video.scnassets\" from\nwww.ahmedbekhit.com/ARVideoKitAssets")
}
}

/**
Initialize 🌞🍳 `RecordAR` with an `SCNView` 🚀.
*/
@objc override public init?(SceneKit: SCNView) {
super.init(SceneKit: SceneKit)
view = SceneKit
setup()
}

//MARK: - Internal threads
internal let writerQueue = DispatchQueue(label:"com.ahmedbekhit.WriterQueue")
internal let gifWriterQueue = DispatchQueue(label: "com.ahmedbekhit.GIFWriterQueue", attributes: .concurrent)
Expand All @@ -142,6 +152,8 @@ fileprivate var renderer:RenderAR!
return view.parent!
}else if let view = view as? ARSKView {
return view.parent!
}else if let view = view as? SCNView {
return view.parent!
}
return nil
}
Expand Down Expand Up @@ -180,6 +192,7 @@ fileprivate var renderer:RenderAR!
let vidPath = "\(documentsDirectory)/\(formatter.string(from: date))ARVideo.mp4"
return URL(fileURLWithPath: vidPath, isDirectory: false)
}

//MARK: - Internal Video Setup
internal func setup() {
if let view = view as? ARSCNView {
Expand Down Expand Up @@ -211,6 +224,16 @@ fileprivate var renderer:RenderAR!
gpuLoop.preferredFramesPerSecond = fps.rawValue
gpuLoop.add(to: .main, forMode: .commonModes)

status = .readyToRecord
}else if let view = view as? SCNView {
guard let mtlDevice = MTLCreateSystemDefaultDevice() else {logAR.message("ERROR:- This device does not support Metal");return}
renderEngine = SCNRenderer(device: mtlDevice, options: nil)
renderEngine.scene = view.scene

gpuLoop = CADisplayLink(target: self, selector: #selector(renderFrame))
gpuLoop.preferredFramesPerSecond = fps.rawValue
gpuLoop.add(to: .main, forMode: .commonModes)

status = .readyToRecord
}

Expand Down Expand Up @@ -669,6 +692,10 @@ fileprivate var renderer:RenderAR!
ViewAR.orientation = .portrait

view.session.run(configuration)
}else if let _ = view as? SCNView {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
ViewAR.orientation = .portrait

}

onlyRenderWhileRec = onlyRenderWhileRecording
Expand All @@ -689,12 +716,11 @@ internal extension RecordAR {
@objc internal func renderFrame() {
//frame rendering
if self.onlyRenderWhileRec && !isRecording && !isRecordingGIF {return}

guard let buffer = renderer.buffer else{return}
guard let rawBuffer = renderer.rawBuffer else{logAR.message("ERROR:- An error occurred while rendering the camera's main buffers.");return}
guard let size = renderer.bufferSize else{logAR.message("ERROR:- An error occurred while rendering the camera buffer.");return}
renderer.ARcontentMode = contentMode

self.writerQueue.sync {
var time:CMTime {return CMTimeMakeWithSeconds(renderer.time, 1000000);}

Expand Down
Binary file modified Framework Build/ARVideoKit.framework/ARVideoKit
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ enum RecordARMicrophoneStatus : NSInteger;
enum RecordARMicrophonePermission : NSInteger;
@class ARSCNView;
@class ARSKView;
@class SCNView;
@class UIImage;

/// This class renders the <code>ARSCNView</code> or <code>ARSKView</code> content with the device’s camera stream to generate a video 📹, photo 🌄, live photo 🎇 or GIF 🎆.
Expand Down Expand Up @@ -334,10 +335,14 @@ SWIFT_CLASS("_TtC10ARVideoKit8RecordAR") SWIFT_AVAILABILITY(ios,introduced=11.0)
@property (nonatomic) BOOL adjustGIFForSharing;
/// A boolean that enables or disables clearing cached media after exporting to Camera Roll. Default is <code>true</code>.
@property (nonatomic) BOOL deleteCacheWhenExported;
/// A boolean that enables or disables using envronment light rendering. Default is <code>false</code>.
@property (nonatomic) BOOL enableAdjsutEnvironmentLighting;
/// Initialize 🌞🍳 <code>RecordAR</code> with an <code>ARSCNView</code> 🚀.
- (nullable instancetype)initWithARSceneKit:(ARSCNView * _Nonnull)ARSceneKit OBJC_DESIGNATED_INITIALIZER;
/// Initialize 🌞🍳 <code>RecordAR</code> with an <code>ARSKView</code> 👾.
- (nullable instancetype)initWithARSpriteKit:(ARSKView * _Nonnull)ARSpriteKit OBJC_DESIGNATED_INITIALIZER;
/// Initialize 🌞🍳 <code>RecordAR</code> with an <code>SCNView</code> 🚀.
- (nullable instancetype)initWithSceneKit:(SCNView * _Nonnull)SceneKit OBJC_DESIGNATED_INITIALIZER;
/// A method that renders a photo 🌄 and returns it as <code>UIImage</code>.
- (UIImage * _Nonnull)photo SWIFT_WARN_UNUSED_RESULT;
/// A method that renders a <code>PHLivePhoto</code> 🎇 and returns <code>PHLivePhotoPlus</code> in the completion handler.
Expand Down
2 changes: 1 addition & 1 deletion Framework Build/ARVideoKit.framework/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2017 Ahmed Fathi Bekhit - [email protected]
Copyright 2017 Ahmed Fathi Bekhit, www.ahmedbekhit.com, [email protected]

ARVideoKit is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Framework Build/ARVideoKit.framework/video.scnassets/vid.scn
Binary file not shown.

0 comments on commit 054d23c

Please sign in to comment.