Search code examples
androidopengl-es3darcoresceneform

Download a 3D model in real time using Sceneform


How to download 3D model and show it in my Arcore app in realtime using Sceneform?


Solution

  • Is it possible to download a 3D model (.obj format) and load it in my Arcore app in realtime?

    Yes, it is!

    1. Install the Google Sceneform Tools (Beta) plugin

    In Android Studio open the Plugins settings:

    • Windows: File > Settings > Plugins > Browse Repositories
    • macOS: Android Studio > Preferences > Plugins

    Then click Browse repositories and install the Google Sceneform Tools (Beta).

    2. Import a 3D asset

    Sceneform supports 3D assets in OBJ, FBX, and glTF formats. Follow these steps to import an asset:

    • Verify that your project's app folder contains a sampledata folder. If the folder does not exist, create it by right-clicking the app folder in the Project window and then selecting New > Sample Data Directory. The sampledata folder is part of your Android Studio project, but its contents are not included in your APK.

    • Copy the 3D asset into the sampledata folder.

    • Do not copy the asset into the assets or res folders as this will cause it to be included in your APK unnecessarily.

    Right click the 3D asset and select Import Sceneform Asset to begin the import process.

    When you click Finish, the wizard does the following:

    • Adds the Sceneform gradle plugin to your project's build.gradle if it doesn't already exist.

      dependencies { classpath 'com.google.ar.sceneform:plugin:1.0.0' }

    • Updates your app's build.gradle file to include an apply plugin line, and a sceneform.asset() entry for each imported asset:

      apply plugin: 'com.google.ar.sceneform.plugin'

      sceneform.asset('sampledata/models/andy.obj', 'default', 'sampledata/models/andy.sfa', 'src/main/res/raw/andy')

    These entries create two gradle tasks:

    • createAsset-<asset-name>: creates an SFA (Sceneform Asset Definition) file if it does not yet exist. The file contains a complete, human-readable description of the asset. It points to the models and textures in your source asset, and also defines materials by providing material parameters for Sceneform's physically based materials. This task will not overwrite an existing SFA file, which means any modifications you make to the SFA file after import won't be overwritten.

      • compileAsset-<asset-name>: compiles the SFA file into an SFB (the Sceneform Binary asset) file. This file gets built into your app's APK and is loaded at runtime to create the renderable.

    For more information, see the Sceneform Gradle Plugin reference.

    • Opens the SFA and SFB in the Viewer window, allowing you to iterate on the asset by modifying the SFA, compiling the asset, and previewing the SFB. See the SFA file format reference for a list of parameters you can tweak.

    3. Create the Renderable

    Once you have the asset in the SFB format, you can build a ModelRenderable and attach it to a node in the scene as follows:

    ModelRenderable.builder()
        .setSource(this, R.raw.andy)
        .build()
        .thenAccept(renderable -> andyRenderable = renderable)
        .exceptionally(
            throwable -> {
            Log.e(TAG, "Unable to load Renderable.", throwable);
            return null;
        });
    

    Source: Import and Preview 3D assets (June 18, 2018)