< Back to Blog

Fetching Models at Runtime with Sceneform and ARCore

2018-09-30

Remotely fetching a 3D model and displaying it in AR

Happy dog with a stick

This article was first posted on Medium.com. Click here to check out the original!

The latest release of Sceneform (v1.5) introduces the ability to load models at runtime, this solves the problem of shipping large APKs with the models bundled at compile time.

In this quick post Iā€™ll demonstrate how to fetch a gLTF (GL Transmission Format) model straight from Google Poly. Poly is a great website which allows you to download free to use 3D models, just be sure to credit the author. For the purposes of this demo Iā€™ll use this Beagle model created by Google.

Such a good puppy

The problem here is that I do not have a direct URI in which I can fetch the gLTF model from. After a bit of digging through the source I was able to find the link that I will use in our code.

Update: Thanks to Stanlo Slasinski for pointing out that Poly has a REST API we can use.

Ctrl + f ā€œgltfā€ seemed to do the trick!

Now I have the remote download link for our gLTF model I can load it on app launch. Iā€™ve branched off of my AR Demo Repo I used when I wrote ā€œBuild your first Android AR app with ARCore and Sceneform in 5 minutesā€.

Build your first Android AR app with ARCore and Sceneform in 5 minutes*

*your mileage may vary

heartbeat.fritz.ai

cgathergood/Your-First-AR-App-with-Sceneform

Contribute to cgathergood/Your-First-AR-App-with-Sceneform development by creating an account on GitHub.

github.com

Lets grab the latest version of Sceneform in our appā€™s build.gradle file and include the new assets library.

implementation ā€œcom.google.ar.sceneform.ux:sceneform-ux:1.5.0ā€
implementation ā€˜com.google.ar.sceneform:assets:1.5.0ā€™

And donā€™t for get about updating our top level gradle file.

classpath ā€˜com.google.ar.sceneform:plugin:1.5.0ā€™

Now swapping into the MainActivity.kt, we can replace our asset name with the new url we retrieved earlier.

addObject(Uri.parse("https://poly.googleusercontent.com/downloads/0BnDT3T1wTE/85QOHCZOvov/Mesh_Beagle.gltf"))

Then we need to modify the placeObject() method to handle our remote resource.

/**

* @param fragment our fragment

* @param anchor ARCore anchor from the hit test

* @param model our 3D model of choice (in this case from our remote url)

*

* Uses the ARCore anchor from the hitTest result and builds the Sceneform nodes.

* It starts the asynchronous loading of the 3D model using the ModelRenderable builder.

*/

private fun placeObject(fragment: ArFragment, anchor: Anchor, model: Uri) {

ModelRenderable.builder()

.setSource(fragment.context, RenderableSource.builder().setSource(

fragment.context,

model,

RenderableSource.SourceType.GLTF2).build())

.setRegistryId(model)

.build()

.thenAccept {

addNodeToScene(fragment, anchor, it)

}

.exceptionally {

Toast.makeText(this@MainActivity, "Could not fetch model from $model", Toast.LENGTH_SHORT).show()

return@exceptionally null

}

}

And itā€™s as easy as that! After a short delay the model is successfully retrieved and added to the scene just as if it was bundled locally with our app.

Downloadable Doggie

The only downside of this approach is that so far I canā€™t see anyway to control the size of the model that has been downloaded. As you can see the model above takes up a significant amount of space, a solution to this would either be hosting smaller models in general or giving the user more control over scaling the model.

Update: Thanks to Tim Psiaki for pointing out the setScale() method in RenderableSource to solve the above problem.

I think this new addition to Sceneform opens up a lot of doors for developers, now that we can host our 3D models we wonā€™t need to ship bloated APKs and be able to serve these models only if and when our users need them.

If you want to check out this code thereā€™s a feature/fetch-model-remotely branch on this repo you can play around with.

cgathergood/Your-First-AR-App-with-Sceneform

Contribute to cgathergood/Your-First-AR-App-with-Sceneform development by creating an account on GitHub.

github.com

Also if you have any questions or comments on this post or anything at all AR related feel free to reach out to me on Twitter, cheers!

Calum Gathergood (@cgathergood) | Twitter

The latest Tweets from Calum Gathergood (@cgathergood). Scottish Software Engineer in Sydney currently working withā€¦

twitter.com