Prerequisites: you should be familiar with Swift and XCode.
- Select Create New XCode project
- Select the Augmented Reality App option
- Name your Project
- Add a video file to your project to be shown instead of the real card live photo
- Import SceneKit Catalog, then navigate to art.scnassets and create a new file .scn
- Add the plane object for your video and 3D text
Import UIKit SceneKit ARKit
You need to add ARSCNViewDelegate protocol
Add an image file, name it, put fit measurements.
Then add this simple code
*
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
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
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
}
*
Finally. Unfortunately, I couldn't run the project because my ios 12.4.8 . this version is lower than the fit one, thank you for following these steps.