Package org.ngengine
Class AsyncAssetManager
java.lang.Object
org.ngengine.AsyncAssetManager
- All Implemented Interfaces:
AssetManager
,Closeable
,AutoCloseable
An asynchronous wrapper for the JME AssetManager that allows loading assets in a separate thread and
provides callbacks for when the asset is loaded. This is useful to avoid blocking the main thread during
asset loading.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AsyncAssetManager
(AssetManager assetManager) protected
AsyncAssetManager
(AssetManager assetManager, Runner callbackRunner) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAssetEventListener
(AssetEventListener listener) Add anAssetEventListener
to receive events from thisAssetManager
.<T> void
addToCache
(AssetKey<T> key, T asset) Inject an asset into the asset cache.protected <T,
E> void async
(E key, Function<E, T> function, BiConsumer<T, Throwable> callback) void
Removes all asset event listeners.void
Clears the asset cache.void
close()
<T> boolean
deleteFromCache
(AssetKey<T> key) Delete an asset from the asset cache.<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 theAssetManager.registerLocator(java.lang.String, java.lang.Class)
call.Load an asset by name, calling this method is the same as callingloadAsset(new AssetKey(name))
.<T> void
loadAssetAsync
(AssetKey<T> key, BiConsumer<T, Throwable> callback) void
loadAssetAsync
(String name, BiConsumer<Object, Throwable> callback) <T> T
loadAssetFromStream
(AssetKey<T> key, InputStream inputStream) Load an asset from anInputStream
.<T> void
loadAssetFromStreamAsync
(AssetKey<T> key, InputStream inputStream, BiConsumer<T, Throwable> callback) Load audio file, supported types are WAV or OGG.Load audio file, supported types are WAV or OGG.void
loadAudioAsync
(AudioKey key, BiConsumer<AudioData, Throwable> callback) void
loadAudioAsync
(String name, BiConsumer<AudioData, Throwable> callback) loadFilter
(FilterKey key) Loads a filter *.j3f file with a FilterKey.loadFilter
(String name) Loads a filter *.j3f file with a FilterKey.void
loadFilterAsync
(FilterKey key, BiConsumer<FilterPostProcessor, Throwable> callback) void
loadFilterAsync
(String name, BiConsumer<FilterPostProcessor, Throwable> callback) Load a font file.void
loadFontAsync
(String name, BiConsumer<BitmapFont, Throwable> callback) loadMaterial
(String name) Load a material instance (J3M) file.void
loadMaterialAsync
(String name, BiConsumer<Material, Throwable> callback) Loads a 3D model with a ModelKey.Loads a 3D model.void
loadModelAsync
(ModelKey key, BiConsumer<Spatial, Throwable> callback) void
loadModelAsync
(String name, BiConsumer<Spatial, Throwable> callback) 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.void
loadTextureAsync
(TextureKey key, BiConsumer<Texture, Throwable> callback) void
loadTextureAsync
(String name, BiConsumer<Texture, Throwable> callback) locateAsset
(AssetKey<?> key) Manually locates an asset with the givenAssetKey
.void
locateAssetAsync
(AssetKey<?> key, BiConsumer<AssetInfo, Throwable> callback) static AsyncAssetManager
of
(AssetManager assetManager, Application app) 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
<T> void
runInLoaderThread
(Function<AsyncAssetManager, T> function, BiConsumer<T, Throwable> callback) void
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.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.jme3.asset.AssetManager
addClassLoader, getClassLoaders, removeClassLoader
-
Field Details
-
assetManager
-
-
Constructor Details
-
AsyncAssetManager
-
AsyncAssetManager
-
-
Method Details
-
of
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
runInLoaderThread
public <T> void runInLoaderThread(Function<AsyncAssetManager, T> function, BiConsumer<T, Throwable> callback) -
async
-
loadAssetAsync
-
loadAsset
Description copied from interface:AssetManager
Load an asset from a key, the asset will be located by one of theAssetLocator
implementations provided in theAssetManager.registerLocator(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 callAssetManager.registerLoader(java.lang.Class, java.lang.String[])
.- Specified by:
loadAsset
in interfaceAssetManager
- Type Parameters:
T
- The object type that will be loaded from the AssetKey instance.- Parameters:
key
- The AssetKey- Returns:
- The loaded asset.
-
loadAssetAsync
-
loadAsset
Description copied from interface:AssetManager
Load an asset by name, calling this method is the same as callingloadAsset(new AssetKey(name))
.- Specified by:
loadAsset
in interfaceAssetManager
- Parameters:
name
- The name of the asset to load.- Returns:
- The loaded asset, or null if failed to be loaded.
- See Also:
-
loadTextureAsync
-
loadTexture
Description copied from interface:AssetManager
Loads texture file, supported types are BMP, JPG, PNG, GIF, TGA, DDS, PFM, and HDR.- Specified by:
loadTexture
in interfaceAssetManager
- Parameters:
key
- TheTextureKey
to use for loading.- Returns:
- The loaded texture, or null if failed to be loaded.
- See Also:
-
loadTextureAsync
-
loadTexture
Description copied from interface:AssetManager
Loads texture file, supported types are BMP, JPG, PNG, GIF, TGA, DDS, PFM, and HDR. The texture will be loaded with mip-mapping enabled.- Specified by:
loadTexture
in interfaceAssetManager
- Parameters:
name
- The name of the texture to load.- Returns:
- The texture that was loaded
- See Also:
-
loadAudioAsync
-
loadAudio
Description copied from interface:AssetManager
Load audio file, supported types are WAV or OGG.- Specified by:
loadAudio
in interfaceAssetManager
- Parameters:
key
- Asset key of the audio file to load- Returns:
- The audio data loaded
- See Also:
-
loadAudioAsync
-
loadAudio
Description copied from interface:AssetManager
Load audio file, supported types are WAV or OGG. The file is loaded without stream-mode.- Specified by:
loadAudio
in interfaceAssetManager
- Parameters:
name
- Asset name of the audio file to load- Returns:
- The audio data loaded
- See Also:
-
loadModelAsync
-
loadModel
Description copied from interface:AssetManager
Loads a 3D model with a ModelKey. Models can be jME3 object files (J3O), OgreXML (mesh.xml), BLEND, FBX and OBJ files.- Specified by:
loadModel
in interfaceAssetManager
- Parameters:
key
- Asset key of the model to load- Returns:
- The model that was loaded
- See Also:
-
loadModelAsync
-
loadModel
Description copied from interface:AssetManager
Loads a 3D model. Models can be jME3 object files (J3O), OgreXML (mesh.xml), BLEND, FBX and OBJ files.- Specified by:
loadModel
in interfaceAssetManager
- Parameters:
name
- Asset name of the model to load- Returns:
- The model that was loaded
- See Also:
-
loadMaterialAsync
-
loadMaterial
Description copied from interface:AssetManager
Load a material instance (J3M) file.- Specified by:
loadMaterial
in interfaceAssetManager
- Parameters:
name
- Asset name of the material to load- Returns:
- The material that was loaded
- See Also:
-
loadFontAsync
-
loadFont
Description copied from interface:AssetManager
Load a font file. Font files are in AngelCode text format, and are with the extension "fnt".- Specified by:
loadFont
in interfaceAssetManager
- Parameters:
name
- Asset name of the font to load- Returns:
- The font loaded
- See Also:
-
loadFilterAsync
-
loadFilter
Description copied from interface:AssetManager
Loads a filter *.j3f file with a FilterKey.- Specified by:
loadFilter
in interfaceAssetManager
- Parameters:
key
- Asset key of the filter file to load- Returns:
- The filter that was loaded
- See Also:
-
loadFilterAsync
-
loadFilter
Description copied from interface:AssetManager
Loads a filter *.j3f file with a FilterKey.- Specified by:
loadFilter
in interfaceAssetManager
- Parameters:
name
- Asset name of the filter file to load- Returns:
- The filter that was loaded
- See Also:
-
locateAssetAsync
-
locateAsset
Description copied from interface:AssetManager
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 theAssetManager.loadAsset(com.jme3.asset.AssetKey)
method locates assets.- Specified by:
locateAsset
in interfaceAssetManager
- Parameters:
key
- TheAssetKey
to locate.- Returns:
- The
AssetInfo
object returned from theAssetLocator
that located the asset, or null if the asset cannot be located.
-
loadAssetFromStreamAsync
public <T> void loadAssetFromStreamAsync(AssetKey<T> key, InputStream inputStream, BiConsumer<T, Throwable> callback) -
loadAssetFromStream
Description copied from interface:AssetManager
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.- Specified by:
loadAssetFromStream
in interfaceAssetManager
- 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.
-
registerLoader
Description copied from interface:AssetManager
Register anAssetLoader
by using a class object.- Specified by:
registerLoader
in interfaceAssetManager
- 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
Description copied from interface:AssetManager
Unregister aAssetLoader
from loading its assigned extensions. This undoes the effect of callingAssetManager.registerLoader(java.lang.Class, java.lang.String[])
.- Specified by:
unregisterLoader
in interfaceAssetManager
- Parameters:
loaderClass
- The loader class to unregister.- See Also:
-
registerLocator
Description copied from interface:AssetManager
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 viaAssetManager.unregisterLocator(java.lang.String, java.lang.Class)
.- Specified by:
registerLocator
in interfaceAssetManager
- 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
Description copied from interface:AssetManager
Unregisters the given locator class. This essentially undoes the operation done byAssetManager.registerLocator(java.lang.String, java.lang.Class)
.- Specified by:
unregisterLocator
in interfaceAssetManager
- Parameters:
rootPath
- Should be the same as the root path specified inAssetManager.registerLocator(java.lang.String, java.lang.Class)
.locatorClass
- The locator class to unregister- See Also:
-
addAssetEventListener
Description copied from interface:AssetManager
Add anAssetEventListener
to receive events from thisAssetManager
.- Specified by:
addAssetEventListener
in interfaceAssetManager
- Parameters:
listener
- The asset event listener to add
-
removeAssetEventListener
Description copied from interface:AssetManager
Remove anAssetEventListener
from receiving events from thisAssetManager
- Specified by:
removeAssetEventListener
in interfaceAssetManager
- Parameters:
listener
- The asset event listener to remove
-
clearAssetEventListeners
public void clearAssetEventListeners()Description copied from interface:AssetManager
Removes all asset event listeners.- Specified by:
clearAssetEventListeners
in interfaceAssetManager
- See Also:
-
setShaderGenerator
Description copied from interface:AssetManager
Sets the shaderGenerator to generate shaders based on shaderNodes.- Specified by:
setShaderGenerator
in interfaceAssetManager
- Parameters:
generator
- the shaderGenerator
-
getShaderGenerator
Description copied from interface:AssetManager
Returns the shaderGenerator responsible for generating the shaders- Specified by:
getShaderGenerator
in interfaceAssetManager
- Parameters:
caps
- a set of required capabilities- Returns:
- the shaderGenerator
-
getFromCache
Description copied from interface:AssetManager
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.- Specified by:
getFromCache
in interfaceAssetManager
- 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
.
-
addToCache
Description copied from interface:AssetManager
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.- Specified by:
addToCache
in interfaceAssetManager
- 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.
-
deleteFromCache
Description copied from interface:AssetManager
Delete an asset from the asset cache.- Specified by:
deleteFromCache
in interfaceAssetManager
- 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.
-
clearCache
public void clearCache()Description copied from interface:AssetManager
Clears the asset cache.- Specified by:
clearCache
in interfaceAssetManager
-