Set a default size of Volumetric window to fit the size of USDZ nodel

751 views Asked by At

I would like to download the USDZ 3D model from the web server within the visionOS app and display it with the Shared Space volumetric window which size set to fit the downloaded USDZ model.

Currently, after downloading USDZ and generating it as a Model Entity, Using openWindow, the ModelEntity is created as a volumetric WindowGroup in the RealityViewContent of the RealityView using openWindow. The Model Entity generated by downloading USDZ is added to the RealityViewContent of the RealityView in the View called by openWindow.

The USDZ downloaded by the above method appears in the volume on visionOS without any problems. However, the proportions of the USDZ tv model to be downloaded is not uniform, so it's not fit in the volume. The AR Quick Look in visionOS, however, scales the USDZ to its full size without missing any USDZ display when 100% is specified.

If there is any information on how to properly set the position and size of the USDZ files created by RealityKit for visionOS, I would appreciate it.

enter image description here

1

There are 1 answers

2
Andy Jazz On

Solution

The method defaultSize(..) is called on the WindowGroup object long before your model is downloaded from the website, and today, the size of a SwiftUI's volumetric window in visionOS is immutable after creation.

However, there is a very simple and effective solution that will help you to solve the problem. For that, use .resizable() modifier on ResolvedModel3D object that helps you to fill the container with a model. And use .aspectRatio(contentMode: .fit) group's modifier that helps to maintain proportions of the downloaded model.

import SwiftUI
import RealityKit

@main struct YourApp : App {
    var body: some Scene {
        WindowGroup {
            Group {
                ContentView()
            }
            .aspectRatio(contentMode: .fit)
        }
        .windowStyle(.volumetric)
        .defaultSize(width: 2, height: 2, depth: 2, in: .meters)
    }
}

struct ContentView : View {
    var url: URL
    let domain = "https://developer.apple.com/augmented-reality/quick-look/"
    
    init() {
        url = URL(string: domain + "models/drummertoy/toy_drummer_idle.usdz")!
    }
    var body: some View {
        Model3D(url: url) { model in
            model.resizable()
        } placeholder: {
            ProgressView()
        }
    }
}

enter image description here