sharedCopy not cloning Apperance?

Hey guys. I may have found a bug with the Apperance section of the sharedCopy method, but I’m not positive. This may be a known issue, but I havent seen anyone talk about it directly on the forums here…

Im loading my models using Kevs awesome 3DS loader.

Then Im making a clone of it, so that I can have multiple objects only having to load the model one time. Makes sense right.

The problem I’m having is that when I go to change the Appearance of one of the cloned nodes, it changes the appearance for all nodes. It seems like the Appearance is not getting cloned at all, and that they are using a shared Appearance.

This is my code:

This sets the material.


public void setMaterial(Group group, Material mat) {
      
      java.util.List nodes = group.getChildren();
      
      for (int i = 0; i < nodes.size(); i++) {
            SceneGraphObject obj = (SceneGraphObject) nodes.get(i);
            
            if (obj instanceof Shape3D) {
                  Shape3D sh = (Shape3D) obj;
                  sh.getAppearance().setMaterial(mat);
                  sh.getAppearance().setChanged(true);  // for around for bug
                  
            } else if (obj instanceof Group) {
                  setMaterial( (Group) obj, mat);
            }
      }
}

Load the model from the model store and clone it into the characters branchgroup


TDSModel model = ModelStore.getModel(modelId);

// character is a branchgroup
character = (BranchGroup) model.sharedCopy(model);

Changing the appearance based on who “owns” the Character.


if (player == 1) {
      setMaterial(TextureStore.redMat);
      System.out.println("Color for: "+theName+" changed to red!");
} else
if (player == 2) {
      setMaterial(TextureStore.blueMat);
      System.out.println("Color for: "+theName+" changed to blue!");
                 } 

A possible workaround for this is to create a new Appearance in my setMaterial method. I will test this out and see if it works. However, if its just something I’m doing wrong, or a bug, I’d like to know!

Thanks

Adding the following code for a new appearance and then cloning the old one manually does seem to work. I will use this for now, but hopefully somone can answer the above q’s.

Thanks!


Appearance na = new Appearance();

na = (Appearance) sh.getAppearance().cloneNodeComponent(true);

na.setMaterial(mat);
na.setChanged(true);  // for around for bug

sh.setAppearance(na);

Here is a quote from JavaDoc:

[quote]A shared copy is one where the geometry and appearance is shared, but all other nodes are copied. This is a replacement for shared groups because of performance considerations. If you are loading the same model many times then this can save on memory and load times. The only allowable within the subtree are groups and shapes. This also copies a shapes bounds and turns autocomute off so that it is fast to insert the model into the scene.
[/quote]
This means that sharedCopy() should not copy the appearance.

I think you should use duplicateNode() instead, but you again should check if that part works as expected because of forceDuplicate support is still not implemented for some nodes.

Yuri