【问题标题】:RealityKit – Displaying .reality file with SwiftUIRealityKit - 使用 SwiftUI 显示 .reality 文件
【发布时间】:2023-03-26 02:20:01
【问题描述】:

我正在尝试使用 ARView 显示现实文件,但是当模型打开时它非常小并且手势不起作用。有人可以指出我做错了什么吗? 不使用手机摄像头即可显示实景文件。

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))
            })
    }
}

【问题讨论】:

    标签: swiftui arkit realitykit


    【解决方案1】:

    使用以下代码为您的模型设置适当的比例:

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

    如果您想查看这段代码的外观以及它在loadModelAsync() 方法的receiveValue 块中的位置,请查看this post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多