'Android: How to load a GLTF or GLB file from remote URL in filament

I'm trying to load a remote asset (.glb or .gltf) into the ModelViewer in filament. The Google Filament sample-gltf-viewer shows a RemoteServer object but I'm not seeing how I can load a remote asset from a URL (e.g. https://github.com/kelvinwatson/glb-files/raw/main/DamagedHelmet.glb)

I've tried the below but I get "Unable to parse glb file".

The code for RemoteServer doesn't indicate where we can pass a URL.

val glbUrl = "<URL TO YOUR GLB ASSET>"

private fun loadGlbRemote() {
    lifecycleScope.launch {
        withContext(Dispatchers.IO) {
        val url = URL(glbUrl)
        val connection = url.openConnection()
        connection.connect()
        val inputStream: InputStream = BufferedInputStream(url.openStream())
        val len = connection.contentLength
        val byteArray = ByteArray(len)
        inputStream.read(byteArray, 0, byteArray.size)
        val byteBuffer = ByteBuffer.wrap(byteArray)
        modelViewer.loadModelGlb(byteBuffer)
        inputStream.close()
    }
implementation 'com.google.android.filament:filament-android:1.7.0'
implementation 'com.google.android.filament:filament-utils-android:1.7.0'
implementation 'com.google.android.filament:gltfio-android:1.7.0'`

Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source