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
func loadEntitySync() { | |
// Create an world anchor at the origin and add it to the scene | |
let anchor = AnchorEntity(world: [0,0,0]) | |
arView.scene.addAnchor(anchor) | |
// Handle errors in case the file doesn't exist | |
do { | |
let usdzPath = "path/to/usdz/asset" | |
let modelEntity = try ModelEntity.loadModel(named: usdzPath) | |
anchor.addChild(modelEntity) |
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 Combine | |
func loadEntityAsync() { | |
// Create an world anchor at the origin and add it to the scene | |
let anchor = AnchorEntity(world: [0,0,0]) | |
arView.scene.addAnchor(anchor) | |
let usdzPath = "path/to/usdz/asset" | |
// Load the asset asynchronously |
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 RealityKit | |
import UIKit | |
class ViewController: UIViewController { | |
let arView: ARView | |
func addGestures() { | |
// Use any entity. Here we're using a cube with size 0.5m | |
let entity = ModelEntity(mesh: MeshResource.generateBox(size: 0.5)) | |
entity.generateCollisionShapes(recursive: true) |
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
guard let query = arView.makeRaycastQuery(from: arView.center, allowing: .existingPlaneInfinite, alignment: .horizontal) else { return } | |
guard let raycastResult = arView.session.raycast(query).first else { return } | |
// set a transform to an existing entity | |
let transform = Transform(matrix: raycastResult.worldTransform) | |
entity.transform = transform | |
// or anchor an entity to an ARRaycastResult | |
let anchor = AnchorEntity(raycastResult: raycastResult) | |
anchor.addChild(entity) |
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
let _ = arView.trackedRaycast(from: arView.center, allowing: .existingPlaneInfinite, alignment: .horizontal, updateHandler: { results in | |
guard let result = results.first else { return } | |
let anchor = AnchorEntity(raycastResult: result) | |
anchor.addChild(entity) | |
self.arView.scene.addAnchor(anchor) | |
}) |
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
let audioFilePath = "path/to/audio/file" | |
let entity = Entity() | |
do { | |
let resource = try AudioFileResource.load(named: audioFilePath, in: nil, inputMode: .spatial, loadingStrategy: .preload, shouldLoop: true) | |
let audioController = entity.prepareAudio(resource) | |
audioController.play() | |
// If you want to start playing right away, you can replace lines 7-8 with line 11 below |
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 RealityKit | |
import Combine | |
class ViewController: UIViewController { | |
@IBOutlet var arView: ARView! | |
override func viewDidLoad() { | |
super.viewDidLoad() |
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
class ViewController: UIViewController { | |
@IBOutlet var arView: ARView! | |
var videoPlayer: AVPlayer! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
guard let path = Bundle.main.path(forResource: "myFileName", ofType: "mp4") else { return } | |
let videoURL = URL(fileURLWithPath: path) |
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! | |
var anchorCount = 0 | |
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 MLCompute | |
struct Model { | |
let batchSize: Int | |
let imageSize: Int | |
let outSize: Int | |
let numberOfClasses: Int | |
let imagesTensor: MLCTensor | |
let labelsTensor: MLCTensor |