Post

Replies

Boosts

Views

Activity

Plane detection does not work in simulators
I have configured ARKit and PlaneDetectionProvider, but after running the code in the simulator, PlaneEntity is not displayed correctly import Foundation import ARKit import RealityKit class PlaneViewModel: ObservableObject{ var session = ARKitSession() let planeData = PlaneDetectionProvider(alignments: [.horizontal]) var entityMap: [UUID: Entity] = [:] var rootEntity = Entity() func start() async { do { if PlaneDetectionProvider.isSupported { try await session.run([planeData]) for await update in planeData.anchorUpdates { if update.anchor.classification == .window { continue } switch update.event { case .added, .updated: updatePlane(update.anchor) case .removed: removePlane(update.anchor) } } } } catch { print("ARKit session error \(error)") } } func updatePlane(_ anchor: PlaneAnchor) { if entityMap[anchor.id] == nil { // Add a new entity to represent this plane. let entity = ModelEntity( mesh: .generateText(anchor.classification.description) ) entityMap[anchor.id] = entity rootEntity.addChild(entity) } entityMap[anchor.id]?.transform = Transform(matrix: anchor.originFromAnchorTransform) } func removePlane(_ anchor: PlaneAnchor) { entityMap[anchor.id]?.removeFromParent() entityMap.removeValue(forKey: anchor.id) } } var body: some View { @stateObject var planeViewModel = PlaneViewModel() RealityView { content in content.add(planeViewModel.rootEntity) } .task { await planeViewModel.start() } }
1
0
610
Dec ’23