Bugfix: fix to a bug that caused texture to be loaded more than once

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10202 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Kae..pl 12 years ago
parent fea4c37e06
commit d44727cfdd
  1. 39
      engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java

@ -141,8 +141,9 @@ public class TextureHelper extends AbstractBlenderHelper {
Pointer pImage = (Pointer) tex.getFieldValue("ima"); Pointer pImage = (Pointer) tex.getFieldValue("ima");
if (pImage.isNotNull()) { if (pImage.isNotNull()) {
Structure image = pImage.fetchData(blenderContext.getInputStream()).get(0); Structure image = pImage.fetchData(blenderContext.getInputStream()).get(0);
result = this.getTextureFromImage(image, blenderContext); Image loadedImage = this.loadImage(image, blenderContext);
if(result != null) { if(loadedImage != null) {
result = new Texture2D(loadedImage);
this.applyColorbandAndColorFactors(tex, result.getImage(), blenderContext); this.applyColorbandAndColorFactors(tex, result.getImage(), blenderContext);
} }
} }
@ -195,6 +196,8 @@ public class TextureHelper extends AbstractBlenderHelper {
if (type != TEX_IMAGE) {// only generated textures should have this key if (type != TEX_IMAGE) {// only generated textures should have this key
result.setKey(new GeneratedTextureKey(tex.getName())); result.setKey(new GeneratedTextureKey(tex.getName()));
} }
blenderContext.addLoadedFeatures(tex.getOldMemoryAddress(), tex.getName(), tex, result);
} }
return result; return result;
} }
@ -487,15 +490,15 @@ public class TextureHelper extends AbstractBlenderHelper {
* this exception is thrown when the blend file structure is * this exception is thrown when the blend file structure is
* somehow invalid or corrupted * somehow invalid or corrupted
*/ */
public Texture getTextureFromImage(Structure image, BlenderContext blenderContext) throws BlenderFileException { protected Image loadImage(Structure image, BlenderContext blenderContext) throws BlenderFileException {
LOGGER.log(Level.FINE, "Fetching texture with OMA = {0}", image.getOldMemoryAddress()); LOGGER.log(Level.FINE, "Fetching texture with OMA = {0}", image.getOldMemoryAddress());
Texture result = (Texture) blenderContext.getLoadedFeature(image.getOldMemoryAddress(), LoadedFeatureDataType.LOADED_FEATURE); Image result = (Image) blenderContext.getLoadedFeature(image.getOldMemoryAddress(), LoadedFeatureDataType.LOADED_FEATURE);
if (result == null) { if (result == null) {
String texturePath = image.getFieldValue("name").toString(); String texturePath = image.getFieldValue("name").toString();
Pointer pPackedFile = (Pointer) image.getFieldValue("packedfile"); Pointer pPackedFile = (Pointer) image.getFieldValue("packedfile");
if (pPackedFile.isNull()) { if (pPackedFile.isNull()) {
LOGGER.log(Level.INFO, "Reading texture from file: {0}", texturePath); LOGGER.log(Level.INFO, "Reading texture from file: {0}", texturePath);
result = this.loadTextureFromFile(texturePath, blenderContext); result = this.loadImageFromFile(texturePath, blenderContext);
} else { } else {
LOGGER.info("Packed texture. Reading directly from the blend file!"); LOGGER.info("Packed texture. Reading directly from the blend file!");
Structure packedFile = pPackedFile.fetchData(blenderContext.getInputStream()).get(0); Structure packedFile = pPackedFile.fetchData(blenderContext.getInputStream()).get(0);
@ -505,16 +508,11 @@ public class TextureHelper extends AbstractBlenderHelper {
ImageLoader imageLoader = new ImageLoader(); ImageLoader imageLoader = new ImageLoader();
// Should the texture be flipped? It works for sinbad .. // Should the texture be flipped? It works for sinbad ..
Image im = imageLoader.loadImage(blenderContext.getInputStream(), dataFileBlock.getBlockPosition(), true); result = imageLoader.loadImage(blenderContext.getInputStream(), dataFileBlock.getBlockPosition(), true);
if (im != null) {
result = new Texture2D(im);
}
} }
if (result != null) { if (result != null) {
result.setName(texturePath);
result.setWrap(Texture.WrapMode.Repeat);
if (LOGGER.isLoggable(Level.FINE)) { if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Adding texture {0} to the loaded features with OMA = {1}", new Object[] { texturePath, image.getOldMemoryAddress() }); LOGGER.log(Level.FINE, "Adding image {0} to the loaded features with OMA = {1}", new Object[] { texturePath, image.getOldMemoryAddress() });
} }
blenderContext.addLoadedFeatures(image.getOldMemoryAddress(), image.getName(), image, result); blenderContext.addLoadedFeatures(image.getOldMemoryAddress(), image.getName(), image, result);
} }
@ -693,14 +691,14 @@ public class TextureHelper extends AbstractBlenderHelper {
* the blender context * 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 loadTextureFromFile(String name, BlenderContext blenderContext) { protected Image loadImageFromFile(String name, BlenderContext blenderContext) {
if (!name.contains(".")) { if (!name.contains(".")) {
return null; // no extension means not a valid image return null; // no extension means not a valid image
} }
AssetManager assetManager = blenderContext.getAssetManager(); AssetManager assetManager = blenderContext.getAssetManager();
name = name.replaceAll("\\\\", "\\/"); name = name.replaceAll("\\\\", "\\/");
Texture result = null; Image result = null;
List<String> assetNames = new ArrayList<String>(); List<String> assetNames = new ArrayList<String>();
if (name.startsWith("//")) { if (name.startsWith("//")) {
@ -729,12 +727,13 @@ public class TextureHelper extends AbstractBlenderHelper {
TextureKey key = new TextureKey(assetName); TextureKey key = new TextureKey(assetName);
key.setGenerateMips(true); key.setGenerateMips(true);
key.setAsCube(false); key.setAsCube(false);
AssetInfo info = assetManager.locateAsset(key); AssetInfo info = assetManager.locateAsset(key);
if(info != null){ if(info != null){
result = assetManager.loadTexture(key); Texture texture = assetManager.loadTexture(key);
break;// if no exception is thrown then accept the located asset result = texture.getImage();//get only the image
// and break the loop break;// if no exception is thrown then accept the located asset
} // and break the loop
}
} catch (AssetNotFoundException e) { } catch (AssetNotFoundException e) {
LOGGER.fine(e.getLocalizedMessage()); LOGGER.fine(e.getLocalizedMessage());
} }

Loading…
Cancel
Save