Class AssetLoader


  • public class AssetLoader
    extends java.lang.Object
    Consumes a blob of glTF 2.0 content (either JSON or GLB) and produces a FilamentAsset object, which is a bundle of Filament entities, material instances, textures, vertex buffers, and index buffers.

    AssetLoader does not fetch external buffer data or create textures on its own. Clients can use the provided ResourceLoader class for this, which obtains the URI list from the asset. This is demonstrated in the Kotlin snippet below.

    
     companion object {
         init {
            Gltfio.init() // or, use Utils.init() if depending on filament-utils
        }
     }
    
     override fun onCreate(savedInstanceState: Bundle?) {
    
         ...
    
         materialProvider = UbershaderLoader(engine)
         assetLoader = AssetLoader(engine, materialProvider, EntityManager.get())
    
         filamentAsset = assets.open("models/lucy.gltf").use { input ->
             val bytes = ByteArray(input.available())
             input.read(bytes)
             assetLoader.createAssetFromJson(ByteBuffer.wrap(bytes))!!
         }
    
         val resourceLoader = ResourceLoader(engine)
         for (uri in filamentAsset.resourceUris) {
             val buffer = loadResource(uri)
             resourceLoader.addResourceData(uri, buffer)
         }
         resourceLoader.loadResources(filamentAsset)
         resourceLoader.destroy()
         filamentAsset.releaseSourceData();
    
         scene.addEntities(filamentAsset.entities)
     }
    
     private fun loadResource(uri: String): Buffer {
         TODO("Load your asset here (e.g. using Android's AssetManager API)")
     }
     
    See Also:
    Animator, FilamentAsset, ResourceLoader
    • Constructor Detail

      • AssetLoader

        public AssetLoader​(@NonNull
                           com.google.android.filament.Engine engine,
                           @NonNull
                           MaterialProvider provider,
                           @NonNull
                           com.google.android.filament.EntityManager entities)
        Constructs an AssetLoader that can be used to create and destroy instances of FilamentAsset.
        Parameters:
        engine - the engine that the loader should pass to builder objects
        provider - an object that provides Filament materials corresponding to glTF materials
        entities - the EntityManager that should be used to create entities
    • Method Detail

      • destroy

        public void destroy()
        Frees all memory consumed by the native AssetLoader This does not not automatically free the cache of materials, nor does it free the entities for created assets (see destroyAsset).
      • createAssetFromBinary

        @Nullable
        public FilamentAsset createAssetFromBinary​(@NonNull
                                                   java.nio.Buffer buffer)
        Creates a FilamentAsset from the contents of a GLB file.
      • createAssetFromJson

        @Nullable
        public FilamentAsset createAssetFromJson​(@NonNull
                                                 java.nio.Buffer buffer)
        Creates a FilamentAsset from the contents of a GLTF file.
      • createInstancedAsset

        @Nullable
        public FilamentAsset createInstancedAsset​(@NonNull
                                                  java.nio.Buffer buffer,
                                                  @NonNull
                                                  FilamentInstance[] instances)
        Consumes the contents of a glTF 2.0 file and produces a primary asset with one or more instances. The given instance array must be sized to the desired number of instances. If successful, this method will populate the array with secondary instances whose resources are shared with the primary asset.
      • createInstance

        @Nullable
        public FilamentInstance createInstance​(@NonNull
                                               FilamentAsset asset)
        Adds a new instance to an instanced asset. Use this with caution. It is more efficient to pre-allocate a max number of instances, and gradually add them to the scene as needed. Instances can also be "recycled" by removing and re-adding them to the scene. NOTE: destroyInstance() does not exist because gltfio favors flat arrays for storage of entity lists and instance lists, which would be slow to shift. We also wish to discourage create/destroy churn, as noted above. This cannot be called after FilamentAsset#releaseSourceData(). This cannot be called on a non-instanced asset. Animation is not supported in new instances. See also AssetLoader#createInstancedAsset().
      • enableDiagnostics

        public void enableDiagnostics​(boolean enable)
        Allows clients to enable diagnostic shading on newly-loaded assets.
      • destroyAsset

        public void destroyAsset​(@NonNull
                                 FilamentAsset asset)
        Frees all memory associated with the given FilamentAsset.