ios – Reality kit auto generated collision box appearing below the model


I’m trying to load a rigged 3D model in Reality Kit. I was able to load it but the auto generated collision box is placed under the model. And I’m trying to move the model when a button is pressed. How can I do it in Reality Kit. Is it possible to move a 3D model on the detected plane in reality kit

Here is what I'm getting

Here is my code

import SwiftUI
import RealityKit
import Combine

struct ContentView : View {
    @State private var yOffset: Float = 0.0
    
   
    @State var btnPressed = false
    var body: some View {
        VStack{
            ARViewContainer(yOffset: $yOffset)
            Button {
                btnPressed.toggle()
                yOffset += 0.1
            //    arView.arView.debugOptions = btnPressed ? [] : [.showPhysics,.showFeaturePoints,.showAnchorOrigins]
            } label: {
                Text("Show Debug")
            }

          
        }
        
       
    }
}

struct ARViewContainer: UIViewRepresentable {
    @Binding var yOffset: Float
    
    let arView = ARView(frame: .zero)
    func makeUIView(context: Context) -> ARView {
        

        
        arView.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action:#selector(context.coordinator.handleTap)))
        context.coordinator.arview = arView
        
        
//        let boxMesh = MeshResource.generateBox(size: 0.05)
//        let boxMaterial = SimpleMaterial(color: UIColor.red, isMetallic: true)
//        let boxEntity = ModelEntity(mesh: boxMesh, materials: [boxMaterial])
//        boxEntity.generateCollisionShapes(recursive: true)
//        let planeAnchor = AnchorEntity(plane: .horizontal)
//        planeAnchor.addChild(boxEntity)
//        arView.scene.addAnchor(planeAnchor)
        return arView
        
    }
    
    
    
    func updateUIView(_ uiView: ARView, context: Context) {
        if let modelEntity = uiView.scene.anchors.compactMap({ $0 as? ModelEntity }).first {
                   modelEntity.position.y += yOffset
               }
    }
    
    
    func makeCoordinator() -> ARCoordinator {
        return ARCoordinator()
    }
    
}

#Preview {
    ContentView()
}







class ARCoordinator: NSObject{
    
    var arview: ARView?
    var cancellable: AnyCancellable?
    
    @objc func handleTap(_ recognizer: UITapGestureRecognizer){
        if let arview{
            let tapLocation = recognizer.location(in: arview)
            arview.debugOptions = [.showPhysics]
//            if let tappedEntity = arview.entity(at: tapLocation) as? ModelEntity{
//                tappedEntity.model?.materials = [SimpleMaterial(color: UIColor.purple, isMetallic: true)]
//            }
           let results = arview.raycast(from: tapLocation, allowing: .estimatedPlane, alignment: .horizontal)
            if let hitResult = results.first{
                //                let anchorEntity = AnchorEntity(raycastResult: hitResult)
                //                let boxMesh = MeshResource.generateBox(size: 0.2)
                //                let boxMaterial = SimpleMaterial(color: UIColor.red, isMetallic: true)
                //                let boxEntity = ModelEntity(mesh: boxMesh, materials: [boxMaterial])
                //                anchorEntity.addChild(boxEntity)
                //                boxEntity.generateCollisionShapes(recursive: true)
                //                arview.installGestures(.all, for: boxEntity)
                
                let anchorEntity = AnchorEntity(raycastResult: hitResult)
                cancellable =  ModelEntity.loadModelAsync(named: "Evangelion_-_Eva_01_Rigged")
                    .sink { [self] result  in
                        switch result{
                        case .finished:
                            print("Finished")
                            cancellable?.cancel()
                            break
                        case .failure(let error):
                            print(error)
                            break
                            
                            
                        }
                    } receiveValue: { modelEnity in
                        modelEnity.generateCollisionShapes(recursive: true)
                
                        anchorEntity.addChild(modelEnity)
                        arview.installGestures(.all, for: modelEnity)
                        arview.scene.addAnchor(anchorEntity)
                    }
                
            }
        }
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img