RealityKit–;用SwiftUI显示.reality文件

RealityKit–;用SwiftUI显示.reality文件,swiftui,arkit,realitykit,Swiftui,Arkit,Realitykit,我试图用ARView显示现实文件,但当模型打开时,它是超小的,而且手势不起作用。有人能指出我做错了什么吗? 真实文件应在不使用手机摄像头的情况下显示 struct AugmentedRealityView: UIViewRepresentable { var name: String var url: URL func makeUIView(context: Context) -> ARView { let arView = ARView(frame: .zero) a

我试图用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))
            })
    }
}
struct-AugmentedRealityView:UIViewRepresentable{
变量名称:String
var-url:url
func makeUIView(上下文:context)->ARView{
让arView=arView(帧:.0)
arView.cameraMode=.nonAR
加载(url:url,名称:name){
导致
切换结果{
成功案例(let模型):
让锚定=锚定实体()
arView.scene.addAnchor(锚定)
让parentEntity=ModelEntity()
parentEntity.addChild(模型)
//处理旋转和缩放的手势
让entityBounds=model.visualBounds(relativeTo:parentEntity)
parentEntity.collision=CollisionComponent(形状:[ShapeResource.generateBox(大小:entityBounds.extents).offsetBy(转换:entityBounds.center)])
arView.installPictures(.all,for:parentEntity)
anchor.addChild(父实体)
案例。失败(let错误):
打印(“\n错误:\(错误)”)
}
}
返回arView
}
func updateUIView(uiView:ARView,context:context){}
私有函数加载(url:url,名称:字符串,completionHandler:@escaping(Result)->()){
var可取消:anyCancelable?=nil
Cancelable=ModelEntity.loadAsync(contentsOf:url,withName:name)
.水槽(
receiveCompletion:{loadCompletion in
如果案例失败(错误)=加载完成{
completionHandler(.failure(error))
}
可取消?.cancel()
},
receiveValue:{model in
completionHandler(.success(model))
})
}
}

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

let anchor = AnchorEntity()

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

如果您想查看此代码的外观以及它在
loadmodelsync()
方法的
receiveValue
块中的位置,请查看。

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

let anchor = AnchorEntity()

anchor.addChild(model)
anchor.scale = [50, 50, 50]   
arView.scene.anchors.append(anchor)
如果您想查看此代码的外观以及它在
loadmodelsync()
方法的
receiveValue
块中的位置,请查看