Answers for "arkit remove modelentity"

0

arkit remove modelentity

import SwiftUI
import RealityKit

struct ARViewContainer: UIViewRepresentable {
    
    @Binding var showed: Bool
    let anchor = AnchorEntity(world: [0, 0,-1])
    
    func makeUIView(context: Context) -> ARView {      
        let arView = ARView(frame: .zero)
        let cube = MeshResource.generateBox(size: 0.8, cornerRadius: 0.02)
        let material = SimpleMaterial(color: .red, isMetallic: true)
        let cubeEntity = ModelEntity(mesh: cube, materials: [material])
        anchor.addChild(cubeEntity)
        arView.scene.addAnchor(anchor)
        return arView
    }        
    func updateUIView(_ uiView: ARView, context: Context) {
        if showed == true {
            uiView.scene.removeAnchor(anchor)
        }
    }
}

struct ContentView : View {
    
    @State private var show = false

    var body: some View {
        VStack {
            ARViewContainer(showed: $show)
            VStack {
                Button(action: { self.show.toggle() }) {
                    Text("Remove Model")
                }
            }
        }
    }
}
Posted by: Guest on July-19-2021

Browse Popular Code Answers by Language