I recently started learning AR in iOS. My first attempt was to import a model of glasses from a usdz
file into the reality composer and anchoring them to a face. That worked perfectly. I then tried to do it programatically, with the same file, but nothing happens.
Since it worked the first time, and when I load the file programatically I am able to print a description of the model (see below), I don't think the problem is in the file.
Here's the code
import UIKit
import ARKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
// this one does not show
var glassesEntity: Entity {
guard let fileUrl = Bundle.main.url(forResource: "glasses", withExtension: "usdz"),
let entity = try? Entity.loadModel(contentsOf: fileUrl) else {
fatalError("could not load entity")
}
entity.name = "glasses"
print(entity)
return entity
}
// this one works perfectly fine
var boxEntity: Entity {
let box = MeshResource.generateBox(size: 0.3)
return ModelEntity(mesh: box)
}
override func viewDidLoad() {
super.viewDidLoad()
let arConfiguration = ARFaceTrackingConfiguration()
arView.session.run(arConfiguration, options: [])
let faceAnchor = AnchorEntity(AnchoringComponent.Target.face)
arView.scene.addAnchor(faceAnchor)
faceAnchor.addChild(glassesEntity)
// If I uncomments this line, I see the box on my face
// faceAnchor.addChild(boxEntity)
}
}
Here's the terminal output. The first 3 lines appear anyway, even if I don't try to load the glasses model.
2021-04-05 10:52:39.589170+0300 FaceTracking[27470:2201299] Metal GPU Frame Capture Enabled
2021-04-05 10:52:39.589627+0300 FaceTracking[27470:2201299] Metal API Validation Enabled
Json Parse Error line 22: Json Deserialization; unknown member 'EnableGuidedFilterOcclusion' - skipping.
Warning: in AppendProperty at line 859 of sdf/path.cpp -- Can only append a property 'preliminary:anchoring:type' to a prim path (/)
Warning: in AppendProperty at line 859 of sdf/path.cpp -- Can only append a property 'triggers' to a prim path (/)
▿ 'glasses' : ModelEntity
⟐ ModelComponent
⟐ SynchronizationComponent
⟐ Transform
2021-04-05 10:52:40.549771+0300 FaceTracking[27470:2201340] [Graphics] Failed to find reflection for buffer clusterIndexTable