I am trying to do a hit test of sorts between a person in my ARFrame and a RealityKit Entity. So far I have been able to use the position value of my entity and project it to a CGPoint which I can match up with the ARFrame's segmentationBuffer to determine whether a person intersects with that entity. Now I want to find out if that person is at the same depth as that entity. How do I relate the SIMD3 position value for the entity, which is in meters I think, to the estimatedDepthData value?
It's difficult to say where you've gone wrong, the following method will extract the value at the provided image coordinate from the depth texture:
I recommend that you start here, make sure you can get valid values, and then work forward from there to see where your issue is. It is likely an error in converting between coordinate spaces somewhere.
Code Block extension CVPixelBuffer { func value(column: Int, row: Int) -> Float? { guard CVPixelBufferGetPixelFormatType(self) == kCVPixelFormatType_DepthFloat32 else { return nil } CVPixelBufferLockBaseAddress(self, .readOnly) if let baseAddress = CVPixelBufferGetBaseAddress(self) { let width = CVPixelBufferGetWidth(self) let index = column + row*width let offset = index * MemoryLayout<Float>.stride let value = baseAddress.load(fromByteOffset: offset, as: Float.self) CVPixelBufferUnlockBaseAddress(self, .readOnly) return value } CVPixelBufferUnlockBaseAddress(self, .readOnly) return nil } }
I recommend that you start here, make sure you can get valid values, and then work forward from there to see where your issue is. It is likely an error in converting between coordinate spaces somewhere.