Hi, I'm new to Xcode and I have a little problem I can't solve. I used the code that I attached. If I load the SpiralTest.usdz file everything works fine. If I load the SpiralTest.rcproject file it fails. But I have to use the rcproject-file. How do I have to customize the code. I tried to send the files with this message, but i can't choose .usdz or zip. But you can test ist with any other usdz and rcproject file. Thanks for your help. Thomas
import UIKit
import RealityKit
import ARKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewWillAppear(_ animated: Bool){
super.viewWillAppear(true)
arView.session.delegate = self
setupCustomObjectView()
let tabGesture = UITapGestureRecognizer(target: self, action: #selector(onTap))
arView.addGestureRecognizer(tabGesture)
}
func setupCustomObjectView () {
arView.automaticallyConfigureSession = false
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]
configuration.environmentTexturing = .automatic
arView.session.run(configuration)
}
@IBAction func onTap(recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: arView)
let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .horizontal)
if let firstResult = results.first {
let anchor = ARAnchor(name: "SpiralTest.rcproject“, transform: firstResult.worldTransform)
arView.session.add(anchor: anchor)
} else {
print("Object placement failed")
}
}
func placeObject(name entityName: String, for anchor:ARAnchor){
let entity = try! ModelEntity.loadModel(named: entityName)
entity.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation, .scale], for: entity)
let anchorEntity = AnchorEntity(anchor: anchor)
anchorEntity.addChild(entity)
arView.scene.addAnchor(anchorEntity)
}
}
extension ViewController: ARSessionDelegate {
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
for anchor in anchors {
if let anchorObject = anchor.name, anchorObject == "SpiralTest.rcproject“ {
placeObject(name: anchorObject, for: anchor)
}
}
}
}