1

Im trying to display reality file using ARView but when the model is opened it is super small and the gestures are not working. Can someone please point me out what I am doing wrong? The reality file should be displayed without using the phone camera.

struct AugmentedRealityView: UIViewRepresentable {

var name: String
var url: URL

func makeUIView(context: Context) -> ARView {
    let arView = ARView(frame: .zero)
    arView.cameraMode = .nonAR
    
    load(url: url, name: name) {
        result in
        
        switch result {
        case .success(let model):
            let anchor = AnchorEntity()
            arView.scene.addAnchor(anchor)
            
            let parentEntity = ModelEntity()
            parentEntity.addChild(model)

            // Handle the gestures for rotation and scale
            let entityBounds = model.visualBounds(relativeTo: parentEntity)
            parentEntity.collision = CollisionComponent(shapes: [ShapeResource.generateBox(size: entityBounds.extents).offsetBy(translation: entityBounds.center)])
            arView.installGestures(.all, for: parentEntity)
            anchor.addChild(parentEntity)
            
        case .failure(let error):
            print("\nError: \(error)")
        }
    }

    return arView
}

func updateUIView(_ uiView: ARView, context: Context) { }

private func load(url: URL, name: String, completionHandler: @escaping (Result<Entity, Error>) -> ()) {
    var cancellable: AnyCancellable? = nil
    
    cancellable = ModelEntity.loadAsync(contentsOf: url, withName: name)
        .sink(
            receiveCompletion: { loadCompletion in
                
                if case let .failure(error) = loadCompletion {
                    completionHandler(.failure(error))
                }
                cancellable?.cancel()
            },
            receiveValue: { model in
                
                completionHandler(.success(model))
            })
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Johanna
  • 21
  • 3

1 Answers1

0

Use the following code to set an appropriate scale for your model:

let anchor = AnchorEntity()

anchor.addChild(model)
anchor.scale = [50, 50, 50]   
arView.scene.anchors.append(anchor)

If you want to see how this code may look like and where is its place in receiveValue block of loadModelAsync() method, look at this post.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220