Show dynamically changed text from RealtyKit entity in a QLPreviewController

146 views Asked by At

There is a good question and answer here about dynamically changing text of an entity in a Reality Composer scene. Is there a way to do the same but show it in a QLPreviewController rather than an ARView?

Here is code for loading a previewItem (from a URL) into the QLPreviewController. So I would like to dynamically change the text in the file before returning the preview item to display. Assume I am using a .rcproject that has a bubble text, for example.

class ARQLViewController: UIViewController, 
                          QLPreviewControllerDataSource, 
                          QLPreviewControllerDelegate {

    var allowsContentScaling = true
    var webViewModel: WebViewModel

    init(webViewModelReceived: WebViewModel) {
        webViewModel = webViewModelReceived
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidAppear(_ animated: Bool) {
        setupOnAppear()
    }

    func setupOnAppear() {
        let previewController = QLPreviewController()
        previewController.dataSource = self
        previewController.delegate = self
        present(previewController, animated: true, completion: nil)
    }

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }

    func previewController(_ controller: QLPreviewController, 
                    previewItemAt index: Int) -> QLPreviewItem {
        let url = webViewModel.afUrlForAR  
        let previewItem = ARQuickLookPreviewItem(fileAt: url!)
        previewItem.allowsContentScaling = allowsContentScaling
        return previewItem
    }
}
1

There are 1 answers

0
Andy Jazz On

About QLPreviewController

AR QuickLook is based on the RealityKit engine and it serves to preview AR QuickLook items in .usdz or .reality format with lighting (including soft contact shadows and PBR materials), light estimation, animation, gestures, anchoring, sound and file sharing. It was conceived as a zero-config view controller for AR scenes.

Since the model is QLPreviewItem or ARQuickLookPreviewItem, you will not be able to change model's parameters or its hierarchy. So the answer is: you can preview .usdz and .reality models with animation and physics in QLPreviewController but you cannot change their parameters, the way you used to do in RealityKit.

The code for creating an ARQuickLook app is short and concise:

import QuickLook

class ViewController: UIViewController, QLPreviewControllerDataSource {

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int { 1 }

    override func viewDidAppear(_ animated: Bool) {
        let arController = QLPreviewController()
        arController.dataSource = self
        present(arController, animated: true)
    }

    func previewController(_ controller: QLPreviewController,
                    previewItemAt index: Int) -> QLPreviewItem {
        
        let path = Bundle.main.path(forResource: "ar3", ofType: "usdz")!
        return URL(fileURLWithPath: path) as QLPreviewItem
    }
}