Reality Kit, Get distance between floor and ceiling

Hi, does anyone know if it is an easy way to determine the distance between floor and ceiling in vision Pro?

Answered by Vision Pro Engineer in 811710022

I haven't tested this, but I think something like this should work:

struct MyView: View {
    
    @State private var floor: AnchorEntity?
    @State private var ceiling: AnchorEntity?

    var body: some View {
        RealityView { content, attachments in
            
            // Run a spatial tracking session to get end user approval for world sensing.
            let session = SpatialTrackingSession()
            let config = SpatialTrackingSession.Configuration(tracking: [.plane])
            await session.run(config)
            
            // Add two anchor entities for floor and ceiling to the RealityView.
            let floor = AnchorEntity(.plane(.horizontal, classification: .floor, minimumBounds: [0.1, 0.1]))
            let ceiling = AnchorEntity(.plane(.horizontal, classification: .ceiling, minimumBounds: [0.1, 0.1]))
            content.add(floor)
            content.add(ceiling)
            self.floor = floor
            self.ceiling = ceiling
        }
        .task {
            while true {
                try? await Task.sleep(nanoseconds: 1_000_000_000)
                
                guard let floor, let ceiling else { continue }

                // Compute room height if floor and ceiling have been detected.
                if floor.isAnchored && ceiling.isAnchored {
                    // Y axis is gravity aligned, so absolute distance between the anchors on the Y axis is the room height.
                    let roomHeight = let roomHeight = abs(floor.position.y - ceiling.position.y)
                    print("roomHeight: \(roomHeight)")
                    break
                } else {
                   maybeShowSomeUIToUserAskingThemToLookAtTheFloorOrCeiling()
                }
            }
        }
    }
}
Accepted Answer

I haven't tested this, but I think something like this should work:

struct MyView: View {
    
    @State private var floor: AnchorEntity?
    @State private var ceiling: AnchorEntity?

    var body: some View {
        RealityView { content, attachments in
            
            // Run a spatial tracking session to get end user approval for world sensing.
            let session = SpatialTrackingSession()
            let config = SpatialTrackingSession.Configuration(tracking: [.plane])
            await session.run(config)
            
            // Add two anchor entities for floor and ceiling to the RealityView.
            let floor = AnchorEntity(.plane(.horizontal, classification: .floor, minimumBounds: [0.1, 0.1]))
            let ceiling = AnchorEntity(.plane(.horizontal, classification: .ceiling, minimumBounds: [0.1, 0.1]))
            content.add(floor)
            content.add(ceiling)
            self.floor = floor
            self.ceiling = ceiling
        }
        .task {
            while true {
                try? await Task.sleep(nanoseconds: 1_000_000_000)
                
                guard let floor, let ceiling else { continue }

                // Compute room height if floor and ceiling have been detected.
                if floor.isAnchored && ceiling.isAnchored {
                    // Y axis is gravity aligned, so absolute distance between the anchors on the Y axis is the room height.
                    let roomHeight = let roomHeight = abs(floor.position.y - ceiling.position.y)
                    print("roomHeight: \(roomHeight)")
                    break
                } else {
                   maybeShowSomeUIToUserAskingThemToLookAtTheFloorOrCeiling()
                }
            }
        }
    }
}
Reality Kit, Get distance between floor and ceiling
 
 
Q