我正在学习增强现实并使用 Google 的 AR-Core 开发 AR 应用程序。它适用于单个模型,但我在想是否可以在数据库中上传不同的 3D 模型,应用程序将从数据库中获取模型并显示在屏幕上。
编辑:我想从数据库加载模型运行时。除了 GLTF2 之外,还有其他方法可以在运行时加载,例如 OBJ 或 FBX。
您在应用程序中使用多个模型的方式的两个示例,我认为这就是您的目标,是:
第一个例子如下所示。这将在应用程序的“raw”文件夹中加载并构建所有可渲染对象,在 Main ACtivity 的 onCreate 函数中(此示例是下面的 Kotlin)。这个例子使用了一个“MetaRenderable”类,它只是一个带有属性的类,用于存储可渲染名称和可渲染本身。它通过在原始文件夹中的名称中查找已知文本字符串来搜索可渲染文件,但您可以简单地为所有“.sfb”文件查找每个文件:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Check that AR is supported on this device
if (!checkIsSupportedDeviceOrFinish(this)) {
return
}
//Create the set of renderables
val rawFields: Array<Field> = R.raw::class.java.fields
for (i in 0 .. rawFields.count() - 1) {
val resID = rawFields[i].getInt(rawFields[i])
val value = TypedValue()
resources.getValue(resID, value, true)
val rawFileName = value.string.toString()
if (rawFileName.contains("com_yourpackagename_")) {
//This is a renderable so build it and add it to the list
Log.d(TAG,"rawFileName: " + rawFileName)
ModelRenderable.builder()
.setSource(this, resID)
.build()
.thenAccept{
val newMetaRenderable:MetaRenderable = MetaRenderable()
newMetaRenderable.name = rawFields[i].name.substringAfter("com_yourpackagename_renderable_")
newMetaRenderable.renderable = it
listOfMetaRenderables.add(newMetaRenderable)
//Set the default renderable
if(rawFileName.contains("your_defaul_trenderable_name")) {
selectedRenderable = it
}
}
.exceptionally {
Log.d(TAG, "Could not build Renderable id: " + resID)
Toast.makeText(this@MainActivity, "Could not build one of the renderables", Toast.LENGTH_LONG).show()
return@exceptionally null
}
}
}
对于第二个示例方法,在运行时加载,您可以使用 RenderableSource.Builder 对象。这将允许您在运行时为给定的 URL 构建可渲染文件,尽管目前仅限于“GLTF2”文件,AFAIK。
RenderableSource.Builder
setSource(Context context, Uri modelUri, RenderableSource.SourceType sourceType) 设置要导入的资产的 Uri。
文档在这里:
这里有一个示例项目,可以从 Google 的 Poly 网站浏览、下载和构建模型:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句