Android:如何在 filament 中从远程 URL 加载 GLTF 或 GLB 文件
Android: How to load a GLTF or GLB file from remote URL in filament
我正在尝试将远程资产(.glb 或 .gltf)加载到 filament 中的 ModelViewer
。 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)
我已尝试以下操作,但出现“无法解析 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'`
如有任何帮助,我们将不胜感激。
问题已解决。问题是我没有完全下载文件,需要在主线程上调用 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
我正在尝试将远程资产(.glb 或 .gltf)加载到 filament 中的 ModelViewer
。 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)
我已尝试以下操作,但出现“无法解析 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'`
如有任何帮助,我们将不胜感激。
问题已解决。问题是我没有完全下载文件,需要在主线程上调用 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