CloneImportExport still buggy? (2 posts)

Topic tags: bug, CloneImportExport, cloning, jme2, performance
  • Profile picture of Wasserleiche Wasserleiche said 11 months, 1 week ago:

    Hi

    I want to use CloneImportExport to increase the performance when loading a model. It kinda worked in the past, but since months it seems to be buggy now. Since it is still today, I finally decided to post it here:
    1) When loading saved models that have textures the textures are not loaded (there exists a TextureState, but they don’t have any Texture object).
    2) I use a lot of the same models simultaneously (not the same java-objects, but the equal models of course). When I load saved models many of them just won’t appear and only one of them is actually rendererd, as if they share the same java-object (I guess it’s the problem like here: http://jmonkeyengine.org/groups/general-2/forum/topic/bug-in-cloneimportexport-identical-objects-in-hierarchy/ but it doesn’t seem to be fixed to me…).

    The first problem is minor since I can cirumvent it by manually saving addional information about the textures, which has worked so far.
    But the second problem prevents me from using CloneImportExport.

    Here is how I use it:
    Storing a model:

    CloneImportExport clone = new CloneImportExport();
    clone.saveClone(model);
    

    Loading a saved model:

    clone = new CloneImportExport();
    clone.saveClone(model);
    

    Am I using it the wrong way? Is there a possibility that it will be fixed in the near future (if it is an actually bug and not my fault)?
    Is there maybe another way of cloning/caching loaded objects to reuse them?

  • Profile picture of Wasserleiche Wasserleiche said 11 months, 1 week ago:

    I just replaced every HashMap with an IdentityHashMap (Search & Replace ftw :D ) and it works fine now. The loading time is much shorter now and loading stuff ingame is now negligable thanks to this. Maybe this helps someone with similiar problems :)
    My class which also handles the Texture issue looks like this:

    public class ClonedModel {
    	private CloneImportExport clone;
    	private HashMap<String, Texture> textures;
    	public ClonedModel(Node model) {
    		clone = new CloneImportExport();
    		clone.saveClone(model);
    		textures = new HashMap<String, Texture>();
    		saveTextures(model);
    	}
    	private void saveTextures(Node parent) {
    		for(Spatial child : parent.getChildren()) {
    			if(child instanceof Node) saveTextures((Node)child);
    			else {
    				assert(child instanceof TriMesh);
    				TriMesh mesh = (TriMesh)child;
    				TextureState ts = (TextureState)mesh.getRenderState(StateType.Texture);
    				if(ts != null) textures.put(mesh.getName(), ts.getTexture());
    			}
    		}
    	}
    	public Node loadClone() {
    		Node newNode = (Node)clone.loadClone();
    		putTextures(newNode);
    		return newNode;
    	}
    	private void putTextures(Node parent) {
    		for(Spatial child : parent.getChildren()) {
    			if(child instanceof Node) putTextures((Node)child);
    			else {
    				assert(child instanceof TriMesh);
    				TriMesh mesh = (TriMesh)child;
    				TextureState ts = (TextureState)mesh.getRenderState(StateType.Texture);
    				if(ts != null) ts.setTexture(textures.get(mesh.getName()), 0);
    			}
    		}
    	}
    }
    

    Note that this is only possible because I made sure that all Trimeshs of my models have unique names. I guess it could be modified to support models with non-unique names, but I need this property anyway :)
    It’s maybe not the best solution, but it works and is reasonably fast I guess. Hope this helps :)