Interface AssetManager
- All Known Implementing Classes:
DesktopAssetManager
AssetManager
provides an interface for managing the data assets
of a jME3 application.
The asset manager provides a means to register AssetLocator
s,
which are used to find asset data on disk, network, or other file system.
The asset locators are invoked in order of addition to find the asset data.
Use the registerLocator(java.lang.String, java.lang.Class)
method
to add new AssetLocator
s.
Some examples of locators:
FileLocator
- Used to find assets on the local file system.ClasspathLocator
- Used to find assets in the Java classpath
The asset data is represented by the AssetInfo
class, this
data is passed into the registered AssetLoader
s in order to
convert the data into a usable object. Use the
registerLoader(java.lang.Class, java.lang.String[])
method
to add loaders.
Some examples of loaders:
Once the asset has been loaded, it will be
post-processed
by the key's processor
.
If the key specifies a cache type
, the asset
will be cached in the specified cache. Next, the AssetProcessor
will be requested to AssetProcessor.createClone(java.lang.Object)
generate a clone for the asset. Some assets do not require cloning,
such as immutable or shared assets. Others, like models, must be cloned
so that modifications to one instance do not leak onto others.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addAssetEventListener
(AssetEventListener listener) Add anAssetEventListener
to receive events from thisAssetManager
.void
addClassLoader
(ClassLoader loader) Adds aClassLoader
that is used to loadclasses
that are needed for finding and loading Assets.<T> void
addToCache
(AssetKey<T> key, T asset) Inject an asset into the asset cache.void
Removes all asset event listeners.void
Clears the asset cache.<T> boolean
deleteFromCache
(AssetKey<T> key) Delete an asset from the asset cache.Retrieve the list of registered ClassLoaders that are used for loadingclasses
from asset files.<T> T
getFromCache
(AssetKey<T> key) Retrieve an asset from the asset cache.getShaderGenerator
(EnumSet<Caps> caps) Returns the shaderGenerator responsible for generating the shaders<T> T
Load an asset from a key, the asset will be located by one of theAssetLocator
implementations provided in theregisterLocator(java.lang.String, java.lang.Class)
call.Load an asset by name, calling this method is the same as callingloadAsset(new AssetKey(name))
.<T> T
loadAssetFromStream
(AssetKey<T> key, InputStream inputStream) Load an asset from anInputStream
.Load audio file, supported types are WAV or OGG.Load audio file, supported types are WAV or OGG.loadFilter
(FilterKey key) Loads a filter *.j3f file with a FilterKey.loadFilter
(String name) Loads a filter *.j3f file with a FilterKey.Load a font file.loadMaterial
(String name) Load a material instance (J3M) file.Loads a 3D model with a ModelKey.Loads a 3D model.loadTexture
(TextureKey key) Loads texture file, supported types are BMP, JPG, PNG, GIF, TGA, DDS, PFM, and HDR.loadTexture
(String name) Loads texture file, supported types are BMP, JPG, PNG, GIF, TGA, DDS, PFM, and HDR.locateAsset
(AssetKey<?> key) Manually locates an asset with the givenAssetKey
.void
registerLoader
(Class<? extends AssetLoader> loaderClass, String... extensions) Register anAssetLoader
by using a class object.void
registerLocator
(String rootPath, Class<? extends AssetLocator> locatorClass) Registers the given locator class for locating assets with thisAssetManager
.void
removeAssetEventListener
(AssetEventListener listener) Remove anAssetEventListener
from receiving events from thisAssetManager
void
removeClassLoader
(ClassLoader loader) Remove aClassLoader
from the list of registered ClassLoadersvoid
setShaderGenerator
(ShaderGenerator generator) Sets the shaderGenerator to generate shaders based on shaderNodes.void
unregisterLoader
(Class<? extends AssetLoader> loaderClass) Unregister aAssetLoader
from loading its assigned extensions.void
unregisterLocator
(String rootPath, Class<? extends AssetLocator> locatorClass) Unregisters the given locator class.
-
Method Details
-
addClassLoader
Adds aClassLoader
that is used to loadclasses
that are needed for finding and loading Assets. This does not allow loading assets from that classpath, use registerLocator for that.- Parameters:
loader
- A ClassLoader that Classes in asset files can be loaded from.
-
removeClassLoader
Remove aClassLoader
from the list of registered ClassLoaders- Parameters:
loader
- the ClassLoader to be removed
-
getClassLoaders
List<ClassLoader> getClassLoaders()Retrieve the list of registered ClassLoaders that are used for loadingclasses
from asset files.- Returns:
- an unmodifiable list
-
registerLoader
Register anAssetLoader
by using a class object.- Parameters:
loaderClass
- The loader class to register.extensions
- Which extensions this loader is responsible for loading, if there are already other loaders registered for that extension, they will be overridden - there should only be one loader for each extension.
-
unregisterLoader
Unregister aAssetLoader
from loading its assigned extensions. This undoes the effect of callingregisterLoader(java.lang.Class, java.lang.String[])
.- Parameters:
loaderClass
- The loader class to unregister.- See Also:
-
registerLocator
Registers the given locator class for locating assets with thisAssetManager
.AssetLocator
s are invoked in the order they were registered, to locate the asset by theAssetKey
. Once anAssetLocator
returns a non-null AssetInfo, it is sent to theAssetLoader
to load the asset. Once a locator is registered, it can be removed viaunregisterLocator(java.lang.String, java.lang.Class)
.- Parameters:
rootPath
- Specifies the root path from which to locate assets for the givenAssetLocator
. The purpose of this parameter depends on the type of theAssetLocator
.locatorClass
- The class type of theAssetLocator
to register.- See Also:
-
unregisterLocator
Unregisters the given locator class. This essentially undoes the operation done byregisterLocator(java.lang.String, java.lang.Class)
.- Parameters:
rootPath
- Should be the same as the root path specified inregisterLocator(java.lang.String, java.lang.Class)
.locatorClass
- The locator class to unregister- See Also:
-
addAssetEventListener
Add anAssetEventListener
to receive events from thisAssetManager
.- Parameters:
listener
- The asset event listener to add
-
removeAssetEventListener
Remove anAssetEventListener
from receiving events from thisAssetManager
- Parameters:
listener
- The asset event listener to remove
-
clearAssetEventListeners
void clearAssetEventListeners()Removes all asset event listeners. -
locateAsset
Manually locates an asset with the givenAssetKey
. This method should be used for debugging or internal uses.
The call will attempt to locate the asset by invoking theAssetLocator
that are registered with thisAssetManager
, in the same way that theloadAsset(com.jme3.asset.AssetKey)
method locates assets.- Parameters:
key
- TheAssetKey
to locate.- Returns:
- The
AssetInfo
object returned from theAssetLocator
that located the asset, or null if the asset cannot be located.
-
loadAssetFromStream
Load an asset from anInputStream
. In some cases it may be required to load an asset from memory or arbitrary streams so that registering a custom locator and key type is not necessary.- Type Parameters:
T
- The object type that will be loaded from the AssetKey instance.- Parameters:
key
- The AssetKey. Note that the asset will not be cached - following the same behavior as ifAssetKey.getCacheType()
returned null.inputStream
- The input stream from which the asset shall be loaded.- Returns:
- The loaded asset.
- Throws:
AssetLoadException
- If theAssetLoader
has failed to load the asset due to anIOException
or another error.
-
loadAsset
Load an asset from a key, the asset will be located by one of theAssetLocator
implementations provided in theregisterLocator(java.lang.String, java.lang.Class)
call. If located successfully, it will be loaded via the appropriateAssetLoader
implementation based on the file's extension, as specified in the callregisterLoader(java.lang.Class, java.lang.String[])
.- Type Parameters:
T
- The object type that will be loaded from the AssetKey instance.- Parameters:
key
- The AssetKey- Returns:
- The loaded asset.
- Throws:
AssetNotFoundException
- If all registered locators have failed to locate the asset.AssetLoadException
- If theAssetLoader
has failed to load the asset due to anIOException
or another error.
-
loadAsset
Load an asset by name, calling this method is the same as callingloadAsset(new AssetKey(name))
.- Parameters:
name
- The name of the asset to load.- Returns:
- The loaded asset, or null if failed to be loaded.
- See Also:
-
loadTexture
Loads texture file, supported types are BMP, JPG, PNG, GIF, TGA, DDS, PFM, and HDR.- Parameters:
key
- TheTextureKey
to use for loading.- Returns:
- The loaded texture, or null if failed to be loaded.
- See Also:
-
loadTexture
Loads texture file, supported types are BMP, JPG, PNG, GIF, TGA, DDS, PFM, and HDR. The texture will be loaded with mip-mapping enabled.- Parameters:
name
- The name of the texture to load.- Returns:
- The texture that was loaded
- See Also:
-
loadAudio
Load audio file, supported types are WAV or OGG.- Parameters:
key
- Asset key of the audio file to load- Returns:
- The audio data loaded
- See Also:
-
loadAudio
Load audio file, supported types are WAV or OGG. The file is loaded without stream-mode.- Parameters:
name
- Asset name of the audio file to load- Returns:
- The audio data loaded
- See Also:
-
loadModel
Loads a 3D model with a ModelKey. Models can be jME3 object files (J3O), OgreXML (mesh.xml), BLEND, FBX and OBJ files.- Parameters:
key
- Asset key of the model to load- Returns:
- The model that was loaded
- See Also:
-
loadModel
Loads a 3D model. Models can be jME3 object files (J3O), OgreXML (mesh.xml), BLEND, FBX and OBJ files.- Parameters:
name
- Asset name of the model to load- Returns:
- The model that was loaded
- See Also:
-
loadMaterial
Load a material instance (J3M) file.- Parameters:
name
- Asset name of the material to load- Returns:
- The material that was loaded
- See Also:
-
loadFont
Load a font file. Font files are in AngelCode text format, and are with the extension "fnt".- Parameters:
name
- Asset name of the font to load- Returns:
- The font loaded
- See Also:
-
loadFilter
Loads a filter *.j3f file with a FilterKey.- Parameters:
key
- Asset key of the filter file to load- Returns:
- The filter that was loaded
- See Also:
-
loadFilter
Loads a filter *.j3f file with a FilterKey.- Parameters:
name
- Asset name of the filter file to load- Returns:
- The filter that was loaded
- See Also:
-
setShaderGenerator
Sets the shaderGenerator to generate shaders based on shaderNodes.- Parameters:
generator
- the shaderGenerator
-
getShaderGenerator
Returns the shaderGenerator responsible for generating the shaders- Parameters:
caps
- a set of required capabilities- Returns:
- the shaderGenerator
-
getFromCache
Retrieve an asset from the asset cache. NOTE: Do not modify the returned asset! It is the same reference as what is stored in the cache, therefore any modifications to it will leak onto assets loaded from the same key in the future.- Type Parameters:
T
- The object type that will be retrieved from the AssetKey instance.- Parameters:
key
- The AssetKey to get from the cache.- Returns:
- The cached asset, if found. Otherwise,
null
. - Throws:
IllegalArgumentException
- Ifcaching
is disabled for the key.
-
addToCache
Inject an asset into the asset cache. NOTE: Do not modify the cached asset after storing! It is the same reference as what is stored in the cache, therefore any modifications to it will leak onto assets loaded from the same key in the future.- Type Parameters:
T
- The object type of the asset.- Parameters:
key
- The key where the asset shall be stored.asset
- The asset to inject into the cache.- Throws:
IllegalArgumentException
- Ifcaching
is disabled for the key.
-
deleteFromCache
Delete an asset from the asset cache.- Type Parameters:
T
- The object type of the AssetKey instance.- Parameters:
key
- The asset key to remove from the cache.- Returns:
- True if the asset key was found in the cache and was removed successfully. False if the asset key was not present in the cache.
- Throws:
IllegalArgumentException
- Ifcaching
is disabled for the key.
-
clearCache
void clearCache()Clears the asset cache.
-