Can I obtain the position of the main window in Vision OS?

Is there a way to retrieve the position of the main window in Vision OS? I'd like to implement a feature where users can drag the window and have a 3D model follow it

Answered by Vision Pro Engineer in 801995022

Hey @yangchao12,

You can do this only if you are in an immersive space. Use the onGeometryChange(for:of:action:) method to get the transform of the window and then pass that back to your immersive space and apply that on an individual entity or your entire view.

On your window add the following code to retrieve the location of the window in the immersive space:

.onGeometryChange(for: AffineTransform3D.self) {
    $0.transform(in: .immersiveSpace) ?? .identity
} action: { newValue in
    transform = newValue
}

Save this to a variable shared between your window and immersive space. For instance, you may choose to declare this as a @State variable in your App and then a @Binding in your window. Pass this variable to your immersive space and apply it to the entity or the RealityView. The following View modifiers applies the transform to the entire view:

.rotation3DEffect(transform.rotation ?? .identity)
.transform3DEffect(.init(translation: transform.translation))

Hope this helps,
Michael

Hey @yangchao12,

You can do this only if you are in an immersive space. Use the onGeometryChange(for:of:action:) method to get the transform of the window and then pass that back to your immersive space and apply that on an individual entity or your entire view.

On your window add the following code to retrieve the location of the window in the immersive space:

.onGeometryChange(for: AffineTransform3D.self) {
    $0.transform(in: .immersiveSpace) ?? .identity
} action: { newValue in
    transform = newValue
}

Save this to a variable shared between your window and immersive space. For instance, you may choose to declare this as a @State variable in your App and then a @Binding in your window. Pass this variable to your immersive space and apply it to the entity or the RealityView. The following View modifiers applies the transform to the entire view:

.rotation3DEffect(transform.rotation ?? .identity)
.transform3DEffect(.init(translation: transform.translation))

Hope this helps,
Michael

Can I obtain the position of the main window in Vision OS?
 
 
Q