I want to convert a world coordinate to the local coordinate of a RealityKit entity.
According to the docs, this should be possible using convert(position:from:)
, called on the entity.
Since it did not work as expected, I wrote a little test project. I used Apple’s template for a visionOS immersive app, and modified it slightly. This is my immersive view:
struct ImmersiveView: View {
var body: some View {
RealityView { content in
var defaultMaterial = UnlitMaterial()
defaultMaterial.color.tint = .brown
let mesh = MeshResource.generateBox(size: 1)
let boxEntity = ModelEntity(mesh: mesh, materials: [defaultMaterial])
let anchorEntity = AnchorEntity(world: SIMD3<Float>(0, 0, -4))
anchorEntity.addChild(boxEntity)
content.add(anchorEntity)
let coordinate = SIMD3<Float>(0.1, 0.2, 0.3)
let coordinateFrom = boxEntity.convert(position: coordinate, from: nil)
let coordinateTo = boxEntity.convert(position: coordinate, to: nil)
print("coordinateFrom: (coordinateFrom)")
print("coordinateTo: (coordinateTo)")
}
}
}
It contains a box visible in front of the main window. The box is not at the origin (z = -4).
Unexpectedly, coordinateFrom
as well as coordinateTo
are equal to coordinate
. I expected that coordinateFrom
is different, because the box is not at the origin.
What am I missing?