use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class DesktopAssetManager method addToCache.
@Override
public <T> void addToCache(AssetKey<T> key, T asset) {
AssetCache cache = handler.getCache(key.getCacheType());
if (cache != null) {
cache.addToCache(key, asset);
cache.notifyNoAssetClone();
} else {
throw new IllegalArgumentException("Key " + key + " specifies no cache.");
}
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class WeakRefCloneAssetCache method getFromCache.
public <T> T getFromCache(AssetKey<T> key) {
AssetRef smartInfo = smartCache.get(key);
if (smartInfo == null) {
return null;
} else {
// NOTE: Optimization so that registerAssetClone()
// can check this and determine that the asset clone
// belongs to the asset retrieved here.
AssetKey keyForTheClone = smartInfo.get();
if (keyForTheClone == null) {
// (between here and smartCache.get)
return null;
}
// Prevent original key from getting collected
// while an asset is loaded for it.
ArrayList<AssetKey> loadStack = assetLoadStack.get();
loadStack.add(keyForTheClone);
return (T) smartInfo.asset;
}
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class SceneWithAnimationLoader method load.
@Override
public Object load(AssetInfo assetInfo) throws IOException {
AssetKey<?> key = assetInfo.getKey();
if (!(key instanceof ModelKey))
throw new AssetLoadException("Invalid asset key");
InputStream stream = assetInfo.openStream();
Scanner scanner = new Scanner(stream);
AnimationList animList = new AnimationList();
String modelName = null;
try {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#"))
continue;
if (modelName == null) {
modelName = line;
continue;
}
String[] split = split(line);
if (split.length < 3)
throw new IOException("Unparseable string \"" + line + "\"");
int start;
int end;
try {
start = Integer.parseInt(split[0]);
end = Integer.parseInt(split[1]);
} catch (NumberFormatException e) {
throw new IOException("Unparseable string \"" + line + "\"", e);
}
animList.add(split[2], split.length > 3 ? split[3] : null, start, end);
}
} finally {
scanner.close();
stream.close();
}
return assetInfo.getManager().loadAsset(new SceneKey(key.getFolder() + modelName, animList));
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class TextureHelper method loadImageFromFile.
/**
* This method loads the textre from outside the blend file using the
* AssetManager that the blend file was loaded with. It returns a texture
* with a full assetKey that references the original texture so it later
* doesn't need to ba packed when the model data is serialized. It searches
* the AssetManager for the full path if the model file is a relative path
* and will attempt to truncate the path if it is an absolute file path
* until the path can be found in the AssetManager. If the texture can not
* be found, it will issue a load attempt for the initial path anyway so the
* failed load can be reported by the AssetManagers callback methods for
* failed assets.
*
* @param name
* the path to the image
* @param imaflag
* the image flag
* @param blenderContext
* the blender context
* @return the loaded image or null if the image cannot be found
*/
protected Texture loadImageFromFile(String name, int imaflag, BlenderContext blenderContext) {
if (!name.contains(".")) {
// no extension means not a valid image
return null;
}
// decide if the mipmaps will be generated
boolean generateMipmaps = false;
switch(blenderContext.getBlenderKey().getMipmapGenerationMethod()) {
case ALWAYS_GENERATE:
generateMipmaps = true;
break;
case NEVER_GENERATE:
break;
case GENERATE_WHEN_NEEDED:
generateMipmaps = (imaflag & 0x04) != 0;
break;
default:
throw new IllegalStateException("Unknown mipmap generation method: " + blenderContext.getBlenderKey().getMipmapGenerationMethod());
}
AssetManager assetManager = blenderContext.getAssetManager();
name = name.replace('\\', '/');
Texture result = null;
if (name.startsWith("//")) {
// This is a relative path, so try to find it relative to the .blend file
String relativePath = name.substring(2);
// Augument the path with blender key path
BlenderKey blenderKey = blenderContext.getBlenderKey();
int idx = blenderKey.getName().lastIndexOf('/');
String blenderAssetFolder = blenderKey.getName().substring(0, idx != -1 ? idx : 0);
String absoluteName = blenderAssetFolder + '/' + relativePath;
// Directly try to load texture so AssetManager can report missing textures
try {
TextureKey key = new TextureKey(absoluteName);
key.setFlipY(true);
key.setGenerateMips(generateMipmaps);
result = assetManager.loadTexture(key);
result.setKey(key);
} catch (AssetNotFoundException | AssetLoadException e) {
LOGGER.fine(e.getLocalizedMessage());
}
} else {
// This is a full path, try to truncate it until the file can be found
// this works as the assetManager root is most probably a part of the
// image path. E.g. AssetManager has a locator at c:/Files/ and the
// texture path is c:/Files/Textures/Models/Image.jpg.
// For this we create a list with every possible full path name from
// the asset name to the root. Image.jpg, Models/Image.jpg,
// Textures/Models/Image.jpg (bingo) etc.
List<String> assetNames = new ArrayList<String>();
String[] paths = name.split("\\/");
// the asset name
StringBuilder sb = new StringBuilder(paths[paths.length - 1]);
assetNames.add(paths[paths.length - 1]);
for (int i = paths.length - 2; i >= 0; --i) {
sb.insert(0, '/');
sb.insert(0, paths[i]);
assetNames.add(0, sb.toString());
}
// Now try to locate the asset
for (String assetName : assetNames) {
try {
TextureKey key = new TextureKey(assetName);
key.setFlipY(true);
key.setGenerateMips(generateMipmaps);
AssetInfo info = assetManager.locateAsset(key);
if (info != null) {
Texture texture = assetManager.loadTexture(key);
result = texture;
// Set key explicitly here if other ways fail
texture.setKey(key);
// If texture is found return it;
return result;
}
} catch (AssetNotFoundException | AssetLoadException e) {
LOGGER.fine(e.getLocalizedMessage());
}
}
// the missing asset to subsystems.
try {
TextureKey key = new TextureKey(name);
assetManager.loadTexture(key);
} catch (AssetNotFoundException | AssetLoadException e) {
LOGGER.fine(e.getLocalizedMessage());
}
}
return result;
}
use of com.jme3.asset.AssetKey in project jmonkeyengine by jMonkeyEngine.
the class AssetLinkNode method attachLinkedChildren.
/**
* Loads the linked children AssetKeys from the AssetManager and attaches them to the Node<br>
* If they are already attached, they will be reloaded.
* @param manager
*/
public void attachLinkedChildren(AssetManager manager) {
detachLinkedChildren();
for (Iterator<ModelKey> it = assetLoaderKeys.iterator(); it.hasNext(); ) {
ModelKey assetKey = it.next();
Spatial curChild = assetChildren.get(assetKey);
if (curChild != null) {
curChild.removeFromParent();
}
Spatial child = manager.loadAsset(assetKey);
attachChild(child);
assetChildren.put(assetKey, child);
}
}
Aggregations