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.
Issue has been resolved. The issue was that I wasn't fully downloading the file, and that the modelViewer.loadModelGlb
needed to be called on the main thread.
Here is the working code:
val glbUrl = "<URL TO YOUR GLB ASSET>"
URL(glbUrl).openStream().use { inputStream: InputStream ->
val inputStream = BufferedInputStream(inputStream)
ByteArrayOutputStream().use { output->
inputStream.copyTo(output)
val byteArr = output.toByteArray()
val byteBuffer = ByteBuffer.wrap(byteArr)
val rewound = byteBuffer.rewind()
withContext(Dispatchers.Main) {
modelViewer.destroyModel()
modelViewer.loadModelGlb(rewound)
modelViewer.transformToUnitCube()
Solution has been included in the filed issue: https://github.com/google/filament/issues/5255