ios – Methods to change RealityKit modelEntity texture passing information mannequin updates from one other SwiftUI View?

Spread the love


So you possibly can see the 2 photographs under, the ‘earlier than’ and after.

The display screen has two views: an ARView [bottom] and a standard SwiftUI View [top].

The SwiftUI view shows the identical picture because the ARView does. The picture comes from the identical information mannequin which is shared amongst them. ( It is the identical picture, totally different zoom/crop).

enter image description here

On the second picture, I’ve modified the SwiftUI View picture utilizing a picture picker. Nevertheless, this does not replace the picture displayed on the ARView.

I can not inform if it has one thing to do with @ObservableObject or it if it is the ‘obtain as an URL’ difficulty with RealityKit textures what’s holding me again…

enter image description here

import SwiftUI
import RealityKit

struct ContentView : View {
    @State var shouldShowImagePicker = false
    @State var picture: UIImage?
    @State var information: WeatherData
    
    var physique: some View {
        NavigationView {
            Listing(DataModel.information, id: .id) { object in
                HStack {
                    Textual content("(object.myListTitle)")
                        .font(.title)
                    Spacer()
                    Button("Edit"){
                        
                    }
                }
                VStack{
                    Spacer()
                    Textual content("(object.myListBodyText)")
                    Spacer()
                    Divider()

                }
                VStack {
                    if let picture = self.picture {
                        Picture(uiImage: picture)
                            .resizable()
                            .scaledToFill()
                            .body(width: 143, peak: 300)                           // .cornerRadius(80)
                            .onTapGesture {
                                shouldShowImagePicker = true
                            }
                           
                    } else {
                        Picture(object.picImage)
                            .body(width: 143, peak: 300)
                            .onTapGesture {
                                shouldShowImagePicker = true
                            }
                           
                    }
                }
               
            }
            .navigationViewStyle(StackNavigationViewStyle())
            .fullScreenCover(isPresented: $shouldShowImagePicker, onDismiss: nil) {
                ImagePicker(picture: $picture)
                    .ignoresSafeArea()
            }//END present imagePicker Sheet
            
            
        }
        ARViewContainer(information: DataModel.information[0], picture: $picture).edgesIgnoringSafeArea(.all)

    }
    
}

import SwiftUI
import RealityKit

struct ARViewContainer: UIViewRepresentable {
    @State var information: WeatherData
    @Binding var picture: UIImage?

    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(body: .zero)

        // Create a dice mannequin
        
        
        
        let picInBox = ModelEntity(mesh: .generateBox(measurement: simd_make_float3(0.6, 0.5, 0.075), cornerRadius: 0.01))
        picInBox.place = simd_make_float3(0, 0, -0.9)
        
        if let texture = strive? TextureResource.load(named: information.picImage) {
            var imageMaterial = UnlitMaterial()
            imageMaterial.baseColor = MaterialColorParameter.texture(texture)
            picInBox.mannequin?.supplies = [imageMaterial]
        }

         
        
        let anchor = AnchorEntity()
        anchor.youngsters.append(picInBox)

        // Add the horizontal aircraft anchor to the scene
        arView.scene.anchors.append(anchor)

        return arView
        
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {}
    
}

#Preview {
    ContentView(information: DataModel.information[0])
}


import UIKit
import SwiftUI

struct WeatherData: Hashable {
    var id: Int
    var picImage: String
    var myListTitle: String
    var myListBodyText: String
    //var picture: UIImage? = UIImage(named: "cat")
    
}

class DataModel: NSObject {
    static let information: [WeatherData] = [
        WeatherData(id: 1, picImage: "cat", myListTitle: "Stuff to-do", myListBodyText: "These are the things I actually have to do: blah blah"),
   
    ]
    //var picture: UIImage? = UIImage(named: "cat")
    var picture: UIImage? = UIImage(named: "cat")
    var imageURL: URL? = Bundle.fundamental.url(forResource: "cat", withExtension: ".png")

}

Leave a Reply

Your email address will not be published. Required fields are marked *