-
public class AssetLoaderConsumes a blob of glTF 2.0 content (either JSON or GLB) and produces a FilamentAsset object, 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 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 = 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)") }
-
-
Constructor Summary
Constructors Constructor Description AssetLoader(Engine engine, MaterialProvider provider, EntityManager entities)Constructs an AssetLoaderthat can be used to create and destroy instances of FilamentAsset.
-
Method Summary
Modifier and Type Method Description voiddestroy()Frees all memory consumed by the native AssetLoaderThis does not not automatically free the cache of materials, nordoes it free the entities for created assets (see destroyAsset).FilamentAssetcreateAsset(@NonNull() Buffer buffer)Creates a FilamentAsset from the contents of a GLB or GLTF file. FilamentAssetcreateInstancedAsset(@NonNull() Buffer buffer, @NonNull() Array<FilamentInstance> instances)Consumes the contents of a glTF 2.0 file and produces a primary asset with one or moreinstances.The given instance array must be sized to the desired number of instances. FilamentInstancecreateInstance(@NonNull() FilamentAsset asset)Adds a new instance to the asset.Use this with caution. voidenableDiagnostics(boolean enable)Allows clients to enable diagnostic shading on newly-loaded assets. voiddestroyAsset(@NonNull() FilamentAsset asset)Frees all memory associated with the given FilamentAsset. -
-
Constructor Detail
-
AssetLoader
AssetLoader(Engine engine, MaterialProvider provider, EntityManager entities)
Constructs anAssetLoaderthat can be used to create and destroy instances of FilamentAsset.- 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
void destroy()
Frees all memory consumed by the native
AssetLoaderThis does not not automatically free the cache of materials, nordoes it free the entities for created assets (see destroyAsset).
-
createAsset
@Nullable() FilamentAsset createAsset(@NonNull() Buffer buffer)
Creates a FilamentAsset from the contents of a GLB or GLTF file.
-
createInstancedAsset
@Nullable() FilamentAsset createInstancedAsset(@NonNull() Buffer buffer, @NonNull() Array<FilamentInstance> instances)
Consumes the contents of a glTF 2.0 file and produces a primary asset with one or moreinstances.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 withthe primary asset.
-
createInstance
@Nullable() 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, andgradually add them to the scene as needed. Instances can also be "recycled" by removing andre-adding them to the scene.NOTE: destroyInstance() does not exist because gltfio favors flat arrays for storage ofentity lists and instance lists, which would be slow to shift. We also wish to discouragecreate/destroy churn, as noted above.This cannot be called after FilamentAsset#releaseSourceData().See also AssetLoader#createInstancedAsset().
-
enableDiagnostics
void enableDiagnostics(boolean enable)
Allows clients to enable diagnostic shading on newly-loaded assets.
-
destroyAsset
void destroyAsset(@NonNull() FilamentAsset asset)
Frees all memory associated with the given FilamentAsset.
-
-
-
-