Created
March 26, 2019 06:52
-
-
Save Bilguun132/2e47df259fac5724c7a5127b0f1fb92a to your computer and use it in GitHub Desktop.
ARKit-Video-Tutorial-2
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
// | |
// ViewController.swift | |
// Harry Pokker | |
// | |
// Created by Bilguun Batbold on 26/3/19. | |
// Copyright © 2019 Bilguun. All rights reserved. | |
// | |
import UIKit | |
import SceneKit | |
import ARKit | |
class ViewController: UIViewController, ARSCNViewDelegate { | |
@IBOutlet var sceneView: ARSCNView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Set the view's delegate | |
sceneView.delegate = self | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
// Create a session configuration | |
let configuration = ARImageTrackingConfiguration() | |
// first see if there is a folder called "ARImages" Resource Group in our Assets Folder | |
if let trackedImages = ARReferenceImage.referenceImages(inGroupNamed: "ARImages", bundle: Bundle.main) { | |
// if there is, set the images to track | |
configuration.trackingImages = trackedImages | |
// at any point in time, only 1 image will be tracked | |
configuration.maximumNumberOfTrackedImages = 1 | |
} | |
// Run the view's session | |
sceneView.session.run(configuration) | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
super.viewWillDisappear(animated) | |
// Pause the view's session | |
sceneView.session.pause() | |
} | |
// MARK: - ARSCNViewDelegate | |
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { | |
// if the anchor is not of type ARImageAnchor (which means image is not detected), just return | |
guard let imageAnchor = anchor as? ARImageAnchor, let fileUrlString = Bundle.main.path(forResource: "black", ofType: "mp4") else {return} | |
//find our video file | |
let videoItem = AVPlayerItem(url: URL(fileURLWithPath: fileUrlString)) | |
let player = AVPlayer(playerItem: videoItem) | |
//initialize video node with avplayer | |
let videoNode = SKVideoNode(avPlayer: player) | |
player.play() | |
// add observer when our player.currentItem finishes player, then start playing from the beginning | |
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil) { (notification) in | |
player.seek(to: CMTime.zero) | |
player.play() | |
print("Looping Video") | |
} | |
// set the size (just a rough one will do) | |
let videoScene = SKScene(size: CGSize(width: 480, height: 360)) | |
// center our video to the size of our video scene | |
videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2) | |
// invert our video so it does not look upside down | |
videoNode.yScale = -1.0 | |
// add the video to our scene | |
videoScene.addChild(videoNode) | |
// create a plan that has the same real world height and width as our detected image | |
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height) | |
// set the first materials content to be our video scene | |
plane.firstMaterial?.diffuse.contents = videoScene | |
// create a node out of the plane | |
let planeNode = SCNNode(geometry: plane) | |
// since the created node will be vertical, rotate it along the x axis to have it be horizontal or parallel to our detected image | |
planeNode.eulerAngles.x = -Float.pi / 2 | |
// finally add the plane node (which contains the video node) to the added node | |
node.addChildNode(planeNode) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment