Created
February 26, 2021 15:41
-
-
Save an000029/63e4c9504ed2b4aa7ea266aecc364c8a to your computer and use it in GitHub Desktop.
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 SceneKit | |
import ARKit | |
class ViewController: UIViewController, ARSCNViewDelegate { | |
@IBOutlet var sceneView: ARSCNView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Set the view's delegate | |
sceneView.delegate = self | |
// Show statistics such as fps and timing information | |
sceneView.showsStatistics = true | |
// Create a new scene in order to instantiate card.scn | |
let scene = SCNScene(named: "art.scnassets/card.scn")! | |
// Set the scene to the view | |
sceneView.scene = scene | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
// Create a session configuration in order to recognize the card | |
let configuration = ARImageTrackingConfiguration() | |
guard let arReferenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources", bundle: nil) else { return } | |
configuration.trackingImages = arReferenceImages | |
sceneView.session.run(configuration) | |
// 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) { | |
guard anchor is ARImageAnchor else { return } | |
guard let card = sceneView.scene.rootNode.childNode(withName: "card", recursively: false) else { return } | |
card.removeFromParentNode() | |
node.addChildNode(card) | |
card.isHidden = false | |
let videoURL = Bundle.main.url(forResource: "pirate", withExtension: "mp4")! | |
let videoPlayer = AVPlayer(url: videoURL) | |
let videoScene = SKScene(size: CGSize(width: 720.0, height: 1280.0)) | |
let videoNode = SKVideoNode(avPlayer: videoPlayer) | |
videoNode.position = CGPoint(x: videoScene.size.width / 2, y: videoScene.size.height / 2) | |
videoNode.size = videoScene.size | |
videoNode.yScale = -1 | |
videoNode.play() | |
videoScene.addChild(videoNode) | |
guard let video = card.childNode(withName: "video", recursively: true) else { return } | |
video.geometry?.firstMaterial?.diffuse.contents = videoScene | |
} | |
func session(_ session: ARSession, didFailWithError error: Error) { | |
// Present an error message to the user | |
} | |
func sessionWasInterrupted(_ session: ARSession) { | |
// Inform the user that the session has been interrupted, for example, by presenting an overlay | |
} | |
func sessionInterruptionEnded(_ session: ARSession) { | |
// Reset tracking and/or remove existing anchors if consistent tracking is required | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment