Sign up for our live Camera Kit office hour on July 17 @ 9:30am (PT) / 4:30pm (GMT)

Quick Tip: Geolocated Lens, Compass Heading, Magnetic North

aidan
aidan Posts: 32 🔥
edited October 2023 in General #1

Hi all, I learned if you wait for the ARSession to be ready you can update its configuration information! This means you can set worldAlignment to .gravityAndHeading so your Lens lines up with true North instead of the arbitrary orientation of your phone which opens up the possibility to do map-based AR applications. I've added the code to CustomizedCameraController.swift

@State var arSessionInput = ARSessionInput();
self.cameraKit.start(
     input: avSessionInput,
     arInput: arSessionInput,
     cameraPosition: .back,
     videoOrientation: orientation,
     dataProvider: self.configureDataProvider(),
     hintDelegate: self,
     textInputContextProvider: textInputContextProvider,
     agreementsPresentationContextProvider: agreementsPresentationContextProvider
)

waitForSession()
func waitForSession() {
     if arSessionInput.session.currentFrame != nil {
          print("session ready!");
          let newConfiguration = ARWorldTrackingConfiguration()
          newConfiguration.worldAlignment = .gravityAndHeading
          arSessionInput.session.run(newConfiguration) 
     } else {
          DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
               self.waitForSession()
          }
     }
}

It's a rough workaround but it gets the result! FYI you may have to setup location services and other permissions and settings beforehand for this to work.

Comments

  • aidan
    aidan Posts: 32 🔥

    Bonus: if you'd like to get your Lens camera's x,y,z coordinates you can do so with the following. I'm using this to update the user's geolocation with finer precision

    func getARPosition () {
            if let currentFrame = self.arSessionInput.session.currentFrame {
                let cameraTransform = currentFrame.camera.transform
                let cameraPosition = cameraTransform.columns.3
                let x = cameraPosition.x
                let y = cameraPosition.y
                let z = cameraPosition.z
                print("Camera position: (\(x), \(y), \(z))")
            } else {
                print("Unable to access current frame")
            }
        }
    
  • arashp
    arashp Posts: 52 🔥🔥

    Thank you for your awesome post @aidan. I am guessing it would be useful to get a callback when ARSession is ready so that the app doesn't have to wait and poll?

  • aidan
    aidan Posts: 32 🔥

    @arashp possibly! I think what would be more useful in this specific instance is the ability to input custom settings into ARSessionInput(session:ARSession) before initializing CameraKit. Maybe it's possible but I find it overrides the values I set

  • arashp
    arashp Posts: 52 🔥🔥

    Got it, thanks again @aidan

  • Hey. Just seeing this post and I think it could really help with a problem I'm trying to solve. Any chance you could walk through how you did this?

  • look forward meeting you

  • Thank you for sharing such information.