我试图将远程资产(.glb或.gltf)以长丝方式加载到ModelViewer中。谷歌丝样例-gltf-查看器显示了一个RemoteServer对象,但我不知道如何从RemoteServer加载远程资产(例如https://github.com/kelvinwatson/glb-files/raw/main/DamagedHelmet.glb)。
我尝试了下面的方法,但是我“无法解析glb文件”。
RemoteServer的代码并不表示我们可以在哪里传递一个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'`任何帮助都将不胜感激。
发布于 2022-02-24 19:58:53
问题已经解决了。问题是我没有完全下载文件,需要在主线程上调用modelViewer.loadModelGlb。
以下是工作代码:
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()解决方案已包括在已提交的问题中:https://github.com/google/filament/issues/5255
https://stackoverflow.com/questions/71245871
复制相似问题