RealityKit – How to access the property in a Scene programmatically?
Asked Answered
I

1

1

I have been using Reality composer and a Frame Entity by downloading from library.

enter image description here

I want to access the properties of the object to programmatically give an image to show in the frame.

enter image description here

Here you can see there is a configure and i can import photo from my gallery. but i want to do it programmatically. that is i want to access that property of the frame object and provide image programmatically.

But I can't do it.

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

How to provide image programmatically?

Incalescent answered 12/12, 2021 at 7:22 Comment(0)
C
2

Of course, you need to look for the required ModelEntity in the depths of the model's hierarchy.

Use this SwiftUI solution:

enter image description here

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

enter image description here

Chapbook answered 12/12, 2021 at 13:26 Comment(1)
@Andy_Jazz, I am speechless, "Thank you" will be very little gratitute from me to you. Still "Thanks a ton". I wish i could be an intern of you. :'(Incalescent

© 2022 - 2024 — McMap. All rights reserved.