如何在SwiftUI中使用RealityKit的图像AnchorEntity?

How to use RealityKit's image AnchorEntity in SwiftUI?

我想使用 RealityKit 的 AnchorEntity,该 AnchorEntity 是用图像类型的锚定组件目标初始化的。这是我正在尝试做的事情:

let anchorEntity = AnchorEntity(.image(group: "AR Resources", 
                                        name: "imgAnchor"))

我找到了零个关于如何使用它的文档。

有谁知道正确的做法吗?

您可以在 SwiftUI 中使用 AnchorEntity(.image(...)) ,几乎与在 UIKit 中使用它的方式相同。首先在 Project Navigator 窗格中单击 Assets.xcassets 并创建 AR Resources 文件夹。将您的图像拖到那里。在 Inspector 中设置它的物理尺寸。然后copy/paste代码:

import SwiftUI
import RealityKit

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        let entity = ModelEntity(mesh: .generateBox(size: 0.1))
        let anchor = AnchorEntity(.image(group: "AR Resources", 
                                          name: "image.png"))
        entity.setParent(anchor)
        arView.scene.anchors.append(anchor)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
}

struct ContentView: View {
    var body: some View {
        ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}