Adding 3D models to the project

3D models, also called renderables, can be added in different ways:

  • Created from basic shapes (sphere, cube, and so on) and materials and combined programmatically to generate more complex objects.
  • Created from standard Android widgets via ViewRenderable. They are rendered as interactive flat cards in the scene.
  • Imported from other programs as 3D assets. The supported formats include OBJ, FBX, and glTF (and glb). Animations are only supported in FBX.

We are going to use the first option and import a .obj file using Sceneform.

To import a model into our project, we have to create an external folder to contain the model (this folder won't be compiled into the final app) and then import the model into the project using Sceneform. Follow these steps to do so:

  1. Create a sampledata folder in your project by right-clicking on the app folder of your project and selecting New|Sample Data Directory.

Adding a Sample Data Directory to the project
If the sampledata folder doesn't appear automatically on your project, you can also create it manually from the file explorer, inside the app folder of your project. Call it sampledata and it will appear in Android Studio.
  1. From the resources pack provided for this project, copy the engine folder into the sampledata folder:

The newly added engine folder containing the model and its resources

Now, we will import the .obj file through Sceneform to include the 3D model as an asset.

  1. Right-click the engine.obj file and select Import Sceneform Asset:

Selecting Import Sceneform Asset for our .obj file
  1. Leave the default values as they are. This will create a Sceneform asset (.sfa) file inside sampledata and a Sceneform binary asset (.sfb) inside the assets folder:

Default import values for the model

During the importing process, the following lines will automatically be added to the end of your app's build.gradle by Sceneform:

sceneform.asset('sampledata/engine/engine.obj',
'default',
'sampledata/engine/engine.sfa',
'src/main/assets/engine')
  1. Leave Gradle syncing. When it finishes, two windows will open: a text file with the model's description and its 3D preview:

Our newly imported model's text view and preview

The final step will be to merge all the elements into the AR scene.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset