ARView -> iOS 18.0 (22A5338b)

Hello,

Im not able to get any 3d object visible in ARView.

struct ARViewContainer: UIViewRepresentable {

var trackingState: ARCamera.TrackingState? = nil

func makeUIView(context: Context) -> ARView {
    // Create the view.
    let view = ARView(frame: .zero)
    
    // Set the coordinator as the session delegate.
    view.session.delegate = context.coordinator
    
    let anchor = AnchorEntity(plane: .horizontal)
    
    let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.3), materials: [SimpleMaterial(color: .red, isMetallic: true)])
    box.generateCollisionShapes(recursive: true)
    
    anchor.addChild(box)
    
    view.scene.addAnchor(anchor)
    
    // Return the view.
    return view
}

final class Coordinator: NSObject, ARSessionDelegate {
    
    var parent: ARViewContainer
    
    init(_ parent: ARViewContainer) {
        self.parent = parent
    }
    
    func session(_ session: ARSession, cameraDidChangeTrackingState camera: ARCamera) {
        print("Camera tracking state: \(camera.trackingState)")
        parent.trackingState = camera.trackingState
    }
}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

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

}

View is loaded correctly but anything cant appear. I also tried to create 3D object in

func updateUIView(_ uiView: ARView, context: Context) {
    
        let anchor = AnchorEntity(plane: .horizontal)
        
        let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.3), materials: [SimpleMaterial(color: .red, isMetallic: true)])
        box.generateCollisionShapes(recursive: true)
        
        anchor.addChild(box)
        
        uiView.scene.addAnchor(anchor)

        print("Added into the view")
}

Print statement is printed but there is still no object in the ARView. Is it bug or what am I missing?

Answered by DTS Engineer in 801182022

Hello @Patresko,

Your code worked fine for me on iOS 18.1 beta (22B5007p). Are you sure that a horizontal plane was detected? If not, your anchor will never be placed in the scene which could explain not seeing the cube.

Best regards,

Greg

Hello @Patresko,

Your code worked fine for me on iOS 18.1 beta (22B5007p). Are you sure that a horizontal plane was detected? If not, your anchor will never be placed in the scene which could explain not seeing the cube.

Best regards,

Greg

ARView -> iOS 18.0 (22A5338b)
 
 
Q