- Improve Blender TextureHelper external texture loading

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10316 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
nor..67 12 years ago
parent 8b9e7a3877
commit 836bc37f5f
  1. 146
      engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java

@ -682,63 +682,97 @@ public class TextureHelper extends AbstractBlenderHelper {
} }
} }
/** /**
* This method loads the textre from outside the blend file. * This method loads the textre from outside the blend file using the
* * AssetManager that the blend file was loaded with. It returns a texture
* @param name * with a full assetKey that references the original texture so it later
* the path to the image * doesn't need to ba packed when the model data is serialized. It searches
* @param blenderContext * the AssetManager for the full path if the model file is a relative path
* the blender context * and will attempt to truncate the path if it is an absolute file path
* @return the loaded image or null if the image cannot be found * 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
protected Texture loadImageFromFile(String name, BlenderContext blenderContext) { * failed load can be reported by the AssetManagers callback methods for
if (!name.contains(".")) { * failed assets.
return null; // no extension means not a valid image *
} * @param name the path to the image
* @param blenderContext the blender context
AssetManager assetManager = blenderContext.getAssetManager(); * @return the loaded image or null if the image cannot be found
name = name.replaceAll("\\\\", "\\/"); */
Texture result = null; protected Texture loadImageFromFile(String name, BlenderContext blenderContext) {
// @Marcin: please, please disable the use of "TAB"
List<String> assetNames = new ArrayList<String>(); // in your IDE in favor of four spaces.
if (name.startsWith("//")) { // All your code looks like this for us: http://i.imgur.com/sGcBv6Q.png
String relativePath = name.substring(2); // spaces always work ;)
// augument the path with blender key path if (!name.contains(".")) {
BlenderKey blenderKey = blenderContext.getBlenderKey(); return null; // no extension means not a valid image
int idx = blenderKey.getName().lastIndexOf('/'); }
String blenderAssetFolder = blenderKey.getName().substring(0, idx != -1 ? idx : 0);
assetNames.add(blenderAssetFolder + '/' + relativePath); AssetManager assetManager = blenderContext.getAssetManager();
} else {// use every path from the asset name to the root (absolute name = name.replaceAll("\\\\", "\\/");
// path) Texture result = null;
String[] paths = name.split("\\/");
StringBuilder sb = new StringBuilder(paths[paths.length - 1]);// the asset name if (name.startsWith("//")) {
assetNames.add(paths[paths.length - 1]); // This is a relative path, so try to find it relative to the .blend file
String relativePath = name.substring(2);
for (int i = paths.length - 2; i >= 0; --i) { // Augument the path with blender key path
sb.insert(0, '/'); BlenderKey blenderKey = blenderContext.getBlenderKey();
sb.insert(0, paths[i]); int idx = blenderKey.getName().lastIndexOf('/');
assetNames.add(0, sb.toString()); 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 {
// now try to locate the asset TextureKey key = new TextureKey(absoluteName);
for (String assetName : assetNames) { assetManager.loadTexture(key);
try { } catch (AssetNotFoundException e) {
TextureKey key = new TextureKey(assetName); LOGGER.fine(e.getLocalizedMessage());
key.setAsCube(false); }
AssetInfo info = assetManager.locateAsset(key); } else {
if(info != null){ // This is a full path, try to truncate it until the file can be found
Texture texture = assetManager.loadTexture(key); // this works as the assetManager root is most probably a part of the
result = texture;//get only the image // image path. E.g. AssetManager has a locator at c:/Files/ and the
break;// if no exception is thrown then accept the located asset // texture path is c:/Files/Textures/Models/Image.jpg.
// and break the loop // 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("\\/");
StringBuilder sb = new StringBuilder(paths[paths.length - 1]);// the asset name
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.setAsCube(false);
AssetInfo info = assetManager.locateAsset(key);
if (info != null) {
Texture texture = assetManager.loadTexture(key);
result = texture;
//if texture is found return it;
return result;
}
} catch (AssetNotFoundException e) {
LOGGER.fine(e.getLocalizedMessage());
} }
} catch (AssetNotFoundException e) { }
LOGGER.fine(e.getLocalizedMessage()); // The asset was not found in the loop above, call loadTexture with
} // the original path once anyway so that the AssetManager can report
} // the missing asset to subsystems.
return result; try {
} TextureKey key = new TextureKey(name);
assetManager.loadTexture(key);
} catch (AssetNotFoundException e) {
LOGGER.fine(e.getLocalizedMessage());
}
}
return result;
}
@Override @Override
public boolean shouldBeLoaded(Structure structure, BlenderContext blenderContext) { public boolean shouldBeLoaded(Structure structure, BlenderContext blenderContext) {

Loading…
Cancel
Save