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
ConstructorsModifierConstructorDescriptionprotectedAsyncAssetManager(AssetManager assetManager) protectedAsyncAssetManager(AssetManager assetManager, Runner callbackRunner) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAssetEventListener(AssetEventListener listener) Add anAssetEventListenerto receive events from thisAssetManager.<T> voidaddToCache(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) voidRemoves all asset event listeners.voidClears the asset cache.voidclose()<T> booleandeleteFromCache(AssetKey<T> key) Delete an asset from the asset cache.<T> TgetFromCache(AssetKey<T> key) Retrieve an asset from the asset cache.getShaderGenerator(EnumSet<Caps> caps) Returns the shaderGenerator responsible for generating the shaders<T> TLoad an asset from a key, the asset will be located by one of theAssetLocatorimplementations 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> voidloadAssetAsync(AssetKey<T> key, BiConsumer<T, Throwable> callback) voidloadAssetAsync(String name, BiConsumer<Object, Throwable> callback) <T> TloadAssetFromStream(AssetKey<T> key, InputStream inputStream) Load an asset from anInputStream.<T> voidloadAssetFromStreamAsync(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.voidloadAudioAsync(AudioKey key, BiConsumer<AudioData, Throwable> callback) voidloadAudioAsync(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.voidloadFilterAsync(FilterKey key, BiConsumer<FilterPostProcessor, Throwable> callback) voidloadFilterAsync(String name, BiConsumer<FilterPostProcessor, Throwable> callback) Load a font file.voidloadFontAsync(String name, BiConsumer<BitmapFont, Throwable> callback) loadMaterial(String name) Load a material instance (J3M) file.voidloadMaterialAsync(String name, BiConsumer<Material, Throwable> callback) Loads a 3D model with a ModelKey.Loads a 3D model.voidloadModelAsync(ModelKey key, BiConsumer<Spatial, Throwable> callback) voidloadModelAsync(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.voidloadTextureAsync(TextureKey key, BiConsumer<Texture, Throwable> callback) voidloadTextureAsync(String name, BiConsumer<Texture, Throwable> callback) locateAsset(AssetKey<?> key) Manually locates an asset with the givenAssetKey.voidlocateAssetAsync(AssetKey<?> key, BiConsumer<AssetInfo, Throwable> callback) static AsyncAssetManagerof(AssetManager assetManager, Application app) voidregisterLoader(Class<? extends AssetLoader> loaderClass, String... extensions) Register anAssetLoaderby using a class object.voidregisterLocator(String rootPath, Class<? extends AssetLocator> locatorClass) Registers the given locator class for locating assets with thisAssetManager.voidremoveAssetEventListener(AssetEventListener listener) Remove anAssetEventListenerfrom receiving events from thisAssetManager<T> voidrunInLoaderThread(Function<AsyncAssetManager, T> function, BiConsumer<T, Throwable> callback) voidsetShaderGenerator(ShaderGenerator generator) Sets the shaderGenerator to generate shaders based on shaderNodes.voidunregisterLoader(Class<? extends AssetLoader> loaderClass) Unregister aAssetLoaderfrom loading its assigned extensions.voidunregisterLocator(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, waitMethods 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
runInLoaderThread
public <T> void runInLoaderThread(Function<AsyncAssetManager, T> function, BiConsumer<T, Throwable> callback) -
async
-
loadAssetAsync
-
loadAsset
Description copied from interface:AssetManagerLoad an asset from a key, the asset will be located by one of theAssetLocatorimplementations provided in theAssetManager.registerLocator(java.lang.String, java.lang.Class)call. If located successfully, it will be loaded via the appropriateAssetLoaderimplementation based on the file's extension, as specified in the callAssetManager.registerLoader(java.lang.Class, java.lang.String[]).- Specified by:
loadAssetin 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:AssetManagerLoad an asset by name, calling this method is the same as callingloadAsset(new AssetKey(name)).- Specified by:
loadAssetin 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:AssetManagerLoads texture file, supported types are BMP, JPG, PNG, GIF, TGA, DDS, PFM, and HDR.- Specified by:
loadTexturein interfaceAssetManager- Parameters:
key- TheTextureKeyto use for loading.- Returns:
- The loaded texture, or null if failed to be loaded.
- See Also:
-
loadTextureAsync
-
loadTexture
Description copied from interface:AssetManagerLoads 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:
loadTexturein interfaceAssetManager- Parameters:
name- The name of the texture to load.- Returns:
- The texture that was loaded
- See Also:
-
loadAudioAsync
-
loadAudio
Description copied from interface:AssetManagerLoad audio file, supported types are WAV or OGG.- Specified by:
loadAudioin interfaceAssetManager- Parameters:
key- Asset key of the audio file to load- Returns:
- The audio data loaded
- See Also:
-
loadAudioAsync
-
loadAudio
Description copied from interface:AssetManagerLoad audio file, supported types are WAV or OGG. The file is loaded without stream-mode.- Specified by:
loadAudioin interfaceAssetManager- Parameters:
name- Asset name of the audio file to load- Returns:
- The audio data loaded
- See Also:
-
loadModelAsync
-
loadModel
Description copied from interface:AssetManagerLoads a 3D model with a ModelKey. Models can be jME3 object files (J3O), OgreXML (mesh.xml), BLEND, FBX and OBJ files.- Specified by:
loadModelin interfaceAssetManager- Parameters:
key- Asset key of the model to load- Returns:
- The model that was loaded
- See Also:
-
loadModelAsync
-
loadModel
Description copied from interface:AssetManagerLoads a 3D model. Models can be jME3 object files (J3O), OgreXML (mesh.xml), BLEND, FBX and OBJ files.- Specified by:
loadModelin interfaceAssetManager- Parameters:
name- Asset name of the model to load- Returns:
- The model that was loaded
- See Also:
-
loadMaterialAsync
-
loadMaterial
Description copied from interface:AssetManagerLoad a material instance (J3M) file.- Specified by:
loadMaterialin interfaceAssetManager- Parameters:
name- Asset name of the material to load- Returns:
- The material that was loaded
- See Also:
-
loadFontAsync
-
loadFont
Description copied from interface:AssetManagerLoad a font file. Font files are in AngelCode text format, and are with the extension "fnt".- Specified by:
loadFontin interfaceAssetManager- Parameters:
name- Asset name of the font to load- Returns:
- The font loaded
- See Also:
-
loadFilterAsync
-
loadFilter
Description copied from interface:AssetManagerLoads a filter *.j3f file with a FilterKey.- Specified by:
loadFilterin 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:AssetManagerLoads a filter *.j3f file with a FilterKey.- Specified by:
loadFilterin 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:AssetManagerManually 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 theAssetLocatorthat are registered with thisAssetManager, in the same way that theAssetManager.loadAsset(com.jme3.asset.AssetKey)method locates assets.- Specified by:
locateAssetin interfaceAssetManager- Parameters:
key- TheAssetKeyto locate.- Returns:
- The
AssetInfoobject returned from theAssetLocatorthat 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:AssetManagerLoad 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:
loadAssetFromStreamin 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:AssetManagerRegister anAssetLoaderby using a class object.- Specified by:
registerLoaderin 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:AssetManagerUnregister aAssetLoaderfrom loading its assigned extensions. This undoes the effect of callingAssetManager.registerLoader(java.lang.Class, java.lang.String[]).- Specified by:
unregisterLoaderin interfaceAssetManager- Parameters:
loaderClass- The loader class to unregister.- See Also:
-
registerLocator
Description copied from interface:AssetManagerRegisters the given locator class for locating assets with thisAssetManager.AssetLocators are invoked in the order they were registered, to locate the asset by theAssetKey. Once anAssetLocatorreturns a non-null AssetInfo, it is sent to theAssetLoaderto load the asset. Once a locator is registered, it can be removed viaAssetManager.unregisterLocator(java.lang.String, java.lang.Class).- Specified by:
registerLocatorin 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 theAssetLocatorto register.- See Also:
-
unregisterLocator
Description copied from interface:AssetManagerUnregisters the given locator class. This essentially undoes the operation done byAssetManager.registerLocator(java.lang.String, java.lang.Class).- Specified by:
unregisterLocatorin 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:AssetManagerAdd anAssetEventListenerto receive events from thisAssetManager.- Specified by:
addAssetEventListenerin interfaceAssetManager- Parameters:
listener- The asset event listener to add
-
removeAssetEventListener
Description copied from interface:AssetManagerRemove anAssetEventListenerfrom receiving events from thisAssetManager- Specified by:
removeAssetEventListenerin interfaceAssetManager- Parameters:
listener- The asset event listener to remove
-
clearAssetEventListeners
public void clearAssetEventListeners()Description copied from interface:AssetManagerRemoves all asset event listeners.- Specified by:
clearAssetEventListenersin interfaceAssetManager- See Also:
-
setShaderGenerator
Description copied from interface:AssetManagerSets the shaderGenerator to generate shaders based on shaderNodes.- Specified by:
setShaderGeneratorin interfaceAssetManager- Parameters:
generator- the shaderGenerator
-
getShaderGenerator
Description copied from interface:AssetManagerReturns the shaderGenerator responsible for generating the shaders- Specified by:
getShaderGeneratorin interfaceAssetManager- Parameters:
caps- a set of required capabilities- Returns:
- the shaderGenerator
-
getFromCache
Description copied from interface:AssetManagerRetrieve 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:
getFromCachein 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:AssetManagerInject 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:
addToCachein 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:AssetManagerDelete an asset from the asset cache.- Specified by:
deleteFromCachein 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:AssetManagerClears the asset cache.- Specified by:
clearCachein interfaceAssetManager
-