- Improve Blender TextureHelper external texture loading
git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10316 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
8b9e7a3877
commit
836bc37f5f
@ -683,15 +683,26 @@ 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
|
||||||
|
* 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
|
* @param name the path to the image
|
||||||
* the path to the image
|
* @param blenderContext the blender context
|
||||||
* @param blenderContext
|
|
||||||
* the blender context
|
|
||||||
* @return the loaded image or null if the image cannot be found
|
* @return the loaded image or null if the image cannot be found
|
||||||
*/
|
*/
|
||||||
protected Texture loadImageFromFile(String name, BlenderContext blenderContext) {
|
protected Texture loadImageFromFile(String name, BlenderContext blenderContext) {
|
||||||
|
// @Marcin: please, please disable the use of "TAB"
|
||||||
|
// in your IDE in favor of four spaces.
|
||||||
|
// All your code looks like this for us: http://i.imgur.com/sGcBv6Q.png
|
||||||
|
// spaces always work ;)
|
||||||
if (!name.contains(".")) {
|
if (!name.contains(".")) {
|
||||||
return null; // no extension means not a valid image
|
return null; // no extension means not a valid image
|
||||||
}
|
}
|
||||||
@ -700,16 +711,30 @@ public class TextureHelper extends AbstractBlenderHelper {
|
|||||||
name = name.replaceAll("\\\\", "\\/");
|
name = name.replaceAll("\\\\", "\\/");
|
||||||
Texture result = null;
|
Texture result = null;
|
||||||
|
|
||||||
List<String> assetNames = new ArrayList<String>();
|
|
||||||
if (name.startsWith("//")) {
|
if (name.startsWith("//")) {
|
||||||
|
// This is a relative path, so try to find it relative to the .blend file
|
||||||
String relativePath = name.substring(2);
|
String relativePath = name.substring(2);
|
||||||
// augument the path with blender key path
|
// Augument the path with blender key path
|
||||||
BlenderKey blenderKey = blenderContext.getBlenderKey();
|
BlenderKey blenderKey = blenderContext.getBlenderKey();
|
||||||
int idx = blenderKey.getName().lastIndexOf('/');
|
int idx = blenderKey.getName().lastIndexOf('/');
|
||||||
String blenderAssetFolder = blenderKey.getName().substring(0, idx != -1 ? idx : 0);
|
String blenderAssetFolder = blenderKey.getName().substring(0, idx != -1 ? idx : 0);
|
||||||
assetNames.add(blenderAssetFolder + '/' + relativePath);
|
String absoluteName = blenderAssetFolder + '/' + relativePath;
|
||||||
} else {// use every path from the asset name to the root (absolute
|
// Directly try to load texture so AssetManager can report missing textures
|
||||||
// path)
|
try {
|
||||||
|
TextureKey key = new TextureKey(absoluteName);
|
||||||
|
assetManager.loadTexture(key);
|
||||||
|
} catch (AssetNotFoundException 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("\\/");
|
String[] paths = name.split("\\/");
|
||||||
StringBuilder sb = new StringBuilder(paths[paths.length - 1]);// the asset name
|
StringBuilder sb = new StringBuilder(paths[paths.length - 1]);// the asset name
|
||||||
assetNames.add(paths[paths.length - 1]);
|
assetNames.add(paths[paths.length - 1]);
|
||||||
@ -719,9 +744,7 @@ public class TextureHelper extends AbstractBlenderHelper {
|
|||||||
sb.insert(0, paths[i]);
|
sb.insert(0, paths[i]);
|
||||||
assetNames.add(0, sb.toString());
|
assetNames.add(0, sb.toString());
|
||||||
}
|
}
|
||||||
}
|
// Now try to locate the asset
|
||||||
|
|
||||||
// now try to locate the asset
|
|
||||||
for (String assetName : assetNames) {
|
for (String assetName : assetNames) {
|
||||||
try {
|
try {
|
||||||
TextureKey key = new TextureKey(assetName);
|
TextureKey key = new TextureKey(assetName);
|
||||||
@ -729,14 +752,25 @@ public class TextureHelper extends AbstractBlenderHelper {
|
|||||||
AssetInfo info = assetManager.locateAsset(key);
|
AssetInfo info = assetManager.locateAsset(key);
|
||||||
if (info != null) {
|
if (info != null) {
|
||||||
Texture texture = assetManager.loadTexture(key);
|
Texture texture = assetManager.loadTexture(key);
|
||||||
result = texture;//get only the image
|
result = texture;
|
||||||
break;// if no exception is thrown then accept the located asset
|
//if texture is found return it;
|
||||||
// and break the loop
|
return result;
|
||||||
}
|
}
|
||||||
} catch (AssetNotFoundException e) {
|
} catch (AssetNotFoundException e) {
|
||||||
LOGGER.fine(e.getLocalizedMessage());
|
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.
|
||||||
|
try {
|
||||||
|
TextureKey key = new TextureKey(name);
|
||||||
|
assetManager.loadTexture(key);
|
||||||
|
} catch (AssetNotFoundException e) {
|
||||||
|
LOGGER.fine(e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user