RealityKit Basics: Coordinate Space Conversion

RealityViewContent provides several ways to values convert between coordinate spaces.

In the article covering RealityViewContent we saw that one thing this provides is access to coordinate conversion helpers.

These generally follow the same pattern.

  • The units/type we need to convert
  • The origin coordinate space
  • The result coordinate space

One of the most common uses is to scale RealityKit Entities when a volume is resized. We can do this by converting the frame of a GeometryReader3D (a bounding box) to a Vector we can use to scale an entity.

struct Example108: View {
    var body: some View {
        GeometryReader3D { proxy in
            RealityView { content in

                guard let scene = try? await Entity(named: "ToyRocket", in: realityKitContentBundle) else { return }
                content.add(scene)

            } update: { content in
                if let rocket = content.entities.first {
                    let newFrame = content.convert(proxy.frame(in: .global), from: .global, to: .scene)
                    rocket.scale = [newFrame.extents.x, newFrame.extents.y, newFrame.extents.z]
                }

            }
            .debugBorder3D(.white)
        }
    }
}

See also

Download the Xcode project with this and many more examples from Step Into Vision.

Some examples are provided as standalone Xcode projects. You can find those here.

Questions or feedback?