Class AssetLoader
- java.lang.Object
-
- com.google.android.filament.gltfio.AssetLoader
-
public class AssetLoader extends java.lang.ObjectConsumes a blob of glTF 2.0 content (either JSON or GLB) and produces aFilamentAssetobject, which is a bundle of Filament textures, vertex buffers, index buffers, etc. An asset is composed of 1 or more FilamentInstance objects which contain entities and components.AssetLoader does not fetch external buffer data or create textures on its own. Clients can use the provided
ResourceLoaderclass 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 = UbershaderProvider(engine) assetLoader = AssetLoader(engine, materialProvider, EntityManager.get()) filamentAsset = assets.open("models/lucy.gltf").use { input -> val bytes = ByteArray(input.available()) input.read(bytes) assetLoader.createAsset(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 Summary
Constructors Constructor Description AssetLoader(com.google.android.filament.Engine engine, MaterialProvider provider, com.google.android.filament.EntityManager entities)Constructs anAssetLoaderthat can be used to create and destroy instances ofFilamentAsset.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FilamentAssetcreateAsset(java.nio.Buffer buffer)Creates aFilamentAssetfrom the contents of a GLB or GLTF file.FilamentInstancecreateInstance(FilamentAsset asset)Adds a new instance to the asset.FilamentAssetcreateInstancedAsset(java.nio.Buffer buffer, FilamentInstance[] instances)Consumes the contents of a glTF 2.0 file and produces a primary asset with one or more instances.voiddestroy()Frees all memory consumed by the nativeAssetLoaderThis does not not automatically free the cache of materials, nor does it free the entities for created assets (see destroyAsset).voiddestroyAsset(FilamentAsset asset)Frees all memory associated with the givenFilamentAsset.voidenableDiagnostics(boolean enable)Allows clients to enable diagnostic shading on newly-loaded assets.
-
-
-
Constructor Detail
-
AssetLoader
public AssetLoader(@NonNull com.google.android.filament.Engine engine, @NonNull MaterialProvider provider, @NonNull com.google.android.filament.EntityManager entities)Constructs anAssetLoaderthat can be used to create and destroy instances ofFilamentAsset.- Parameters:
engine- the engine that the loader should pass to builder objectsprovider- an object that provides Filament materials corresponding to glTF materialsentities- the EntityManager that should be used to create entities
-
-
Method Detail
-
destroy
public void destroy()
Frees all memory consumed by the nativeAssetLoaderThis does not not automatically free the cache of materials, nor does it free the entities for created assets (see destroyAsset).
-
createAsset
@Nullable public FilamentAsset createAsset(@NonNull java.nio.Buffer buffer)
Creates aFilamentAssetfrom the contents of a GLB or 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 the 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(). 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 givenFilamentAsset.
-
-