【问题标题】:RealityKit – How to access the property in a Scene programmatically?RealityKit – 如何以编程方式访问场景中的属性?
【发布时间】:2022-01-16 03:51:54
【问题描述】:

我一直在通过从库下载使用 Reality composer 和 Frame Entity。

我想访问对象的属性以编程方式提供要在框架中显示的图像。

在这里你可以看到有一个配置,我可以从我的画廊导入照片。但我想以编程方式进行。 那就是我想访问框架对象的该属性并以编程方式提供图像。

但我做不到。

do {
    let boxAnchor = try ImageFrame.loadScene()
        
    guard let imageFrame = boxAnchor.findEntity(named: "imageFrame")
    else { return }

    //how to access the property of imageFrame???
    arView.scene.anchors.append(boxAnchor)
} catch {
    print("error: \(error.localizedDescription)")
}

如何以编程方式提供图像?

【问题讨论】:

    标签: swift augmented-reality realitykit reality-composer


    【解决方案1】:

    当然需要在模型的hierarchy的深处寻找需要的ModelEntity。

    使用这个 SwiftUI 解决方案:

    struct ARViewContainer: UIViewRepresentable {
        
        func makeUIView(context: Context) -> ARView {
            
            let arView = ARView(frame: .zero)
            let pictureScene = try! Experience.loadPicture()
            pictureScene.children[0].scale *= 4
            
            print(pictureScene)
            
            let edgingModel = pictureScene.masterpiece?.children[0] as! ModelEntity
    
            edgingModel.model?.materials = [SimpleMaterial(color: .brown,
                                                      isMetallic: true)]
            
            var mat = SimpleMaterial()
    
            // Here's a great old approach for assigning a texture in iOS 14.5
            mat.baseColor = try! .texture(.load(named: "MonaLisa", in: nil))
            
            let imageModel = pictureScene.masterpiece?.children[0]
                                                      .children[0] as! ModelEntity
            imageModel.model?.materials = [mat]
            
            arView.scene.anchors.append(pictureScene)
            return arView
        }
    
        func updateUIView(_ uiView: ARView, context: Context) { }
    }
    

    【讨论】:

    • @Andy_Jazz,我无言以对,“谢谢你”对你来说将是非常少的感谢。仍然“非常感谢”。我希望我能成为你的实习生。 :'(
    猜你喜欢
    • 2019-07-19
    • 2019-12-04
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2011-02-26
    相关资源
    最近更新 更多