join us on Discord for support, updates, and all things Snap AR! See you there!
Quick Tip: Geolocated Lens, Compass Heading, Magnetic North
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
-
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") } }
3 -
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?
0 -
__
0 -
Thank you for sharing such information.
0