Created
March 1, 2022 17:32
-
-
Save timcowlishaw/24ba1ee5b08c16704eda3e98d562a1bb to your computer and use it in GitHub Desktop.
Cenatus PHASE blog post snippet 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreMotion | |
// [ Elided other imports ] | |
class ViewController: UIViewController, ARSession { // [ Elided other protocols ] | |
let hmm = CMHeadphoneMotionManager() | |
var deviceTransform: simd_float4x4 = matrix_identity_float4x4; | |
var headphoneTransform: simd_float4x4 = matrix_identity_float4x4; | |
override func viewDidLoad() { | |
hmm.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: {[weak self] motion, error in | |
guard let motion = motion, error == nil else { return } | |
self?.handleHeadMovement(motion) | |
}) | |
// [ Elided rest of method body] | |
} | |
func handleHeadMovement(_ motion: CMDeviceMotion) { | |
let m = motion.attitude.rotationMatrix | |
let x = SIMD4(Float(m.m11), Float(m.m21), Float(m.m31), 0) | |
let y = SIMD4(Float(m.m12), Float(m.m22), Float(m.m32), 0) | |
let z = SIMD4(Float(m.m13), Float(m.m23), Float(m.m33), 0) | |
let w = SIMD4(Float(0), Float(0), Float(0), Float(1)) | |
self.headphoneTransform = simd_float4x4(columns: (x, y, z, w)) | |
phaseListener.transform = deviceTransform * headphoneTransform | |
} | |
func session(_ session: ARSession, didUpdate frame: ARFrame) { | |
self.deviceTransform = frame.camera.transform | |
phaseListener.transform = deviceTransform * headphoneTransform | |
} | |
// [ Elided rest of class body] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment