Yes, I’m keeping a reference to the texture in a class TerrainTextureInfo. The main purpose of the class below is to provide the terrain system (different from jME) with the appropriate information of the texture. Then I encountered the described problem with duplicate textures, so I added support for texture management in that class too. The class now keeps track of the number of terrain blocks using a specific texture, as well as the total number of terrain textures that should be in memory, and the Texture object itself. The TerrainTextureInfo object will be shared among TerrainBlocks using the same texture file.
The thing is, I can also load the texture when the material is created (the material isn’t stored anywhere else in a TerrainBlock class, it is only created in a method) but the problem stays. Also, removing the three lines above, for unloading the texture, brings the increasing “Texture (M)”-number problem back.
I’ve tried each and every combination I can think of but the method described is the only way for me to get reasonable numbers in “Textures (M)”.
Does it help?
PS: I know: “ArrayList is the near Vector”
[java]
// *****************************************************************************
public class TerrainTextureInfo
// *****************************************************************************
{
private int face;
private double left;
private double upper;
private double dimension;
private Texture texture;
private Vector myBlocks=new Vector();
private String textureFileName;
public static int numTextures;
// =============================================================================
public TerrainTextureInfo(String textureFileName, int face, double left,
double upper, double dimension)
// =============================================================================
{ this.face=face;
this.left=left;
this.upper=upper;
this.dimension=dimension;
this.textureFileName=textureFileName;
} // ===========================================================================
// =============================================================================
public void addBlock(TerrainBlock addMe)
// =============================================================================
{ if (!myBlocks.contains(addMe))
{ myBlocks.add(addMe);
}
} // ===========================================================================
// =============================================================================
public int getFace() {return face;}
// =============================================================================
// =============================================================================
public String getTextureFileName() {return textureFileName;}
// =============================================================================
// =============================================================================
public double getLeft() {return left;}
// =============================================================================
// =============================================================================
public double getUpper() {return upper;}
// =============================================================================
// =============================================================================
public double getDimension() {return dimension;}
// =============================================================================
// =============================================================================
public int getNumBlocks() {return myBlocks.size();}
// =============================================================================
// =============================================================================
public Texture getTexture()
// =============================================================================
{
if (texture==null)
{
texture=JmeUtility.assetManager().loadTexture(textureFileName);
numTextures++;
}
return texture;
} // ===========================================================================
// =============================================================================
public void removeBlock(TerrainBlock removeMe)
// =============================================================================
{ myBlocks.remove(removeMe);
if (myBlocks.size()==0)
{
numTextures–;
((DesktopAssetManager)JmeUtility.assetManager()).deleteFromCache(
texture.getKey());
texture=null;
System.gc();
}
} // ===========================================================================
// =============================================================================
@Override
public String toString()
// =============================================================================
{ return “face=”+face+", left="+left+", upper="+upper+" dimension="+dimension;
} // ===========================================================================
}
[/java]