1

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
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Yotam
  • 9,789
  • 13
  • 47
  • 68

1 Answers1

1

There's a couple of things you could try...

First, load the model like this:

guard let entity = try? Entity.load(
  named: "glasses"
) else { fatalError("") }

Then, after it's anchored, change the rest to this:

val glEnt = self.glassesEntity
faceAnchor.addChild(glEnt)
let relNil = glEnt.visualBounds(relativeTo: nil)
let relAnch = glEnt.visualBounds(relativeTo: faceAnchor)

print("centre: \(relNil.center)\n radius:\(relNil.boundingRadius)")
print("centre: \(relAnch.center)\n radius:\(relAnch.boundingRadius)")

See if those centre and radius values are roughly what you'd expect. The first one will be in world space the second is relative to the face anchor.

maxxfrazer
  • 1,173
  • 6
  • 15