Changing material breaks animations

I’m not sure why materials and animations are related but the thing is that changing the material of an animated model makes it animations to stop working. If the material is changed in the same frame it is added it works perfectly but the problem comes if it’s changed on another frame.

An example code:

public class Main extends SimpleApplication {

    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setWidth(640);
        settings.setHeight(480);
        settings.setFrameRate(60);

        new Main().start();
    }


    Spatial sinbad;
    Material genericMat;

    public void simpleInitApp() {
        // Lighting
        DirectionalLight dl = new DirectionalLight();
        dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
        rootNode.addLight(dl);

        // Lemur Gui setup
        GuiGlobals.initialize(stateManager.getApplication());
        BaseStyles.loadGlassStyle();
        GuiGlobals.getInstance().getStyles().setDefaultStyle("glass");

        Container mainWindow = new Container();
        mainWindow.addChild(new Label("--- Buttons Window ---"));

        Button button = mainWindow.addChild(new Button("Change Material"));
        button.addClickCommands(new Command<Button>() {
            @Override
            public void execute(Button source) {
                // Doesn't work - it changes the material but stops the animations forever :S
                changeMaterial();
            }
        });

        mainWindow.setLocalTranslation(300, 300, 0);
        guiNode.attachChild(mainWindow);



        // Model import
        sinbad = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");
        AnimControl anim = sinbad.getControl(AnimControl.class);
        anim.createChannel().setAnim(anim.getAnimationNames().iterator().next());

        rootNode.attachChild(sinbad);


        // WORKS
//        changeMaterial();
    }

    int i = 0;
    @Override
    public void simpleUpdate(float tpf) {
        super.simpleUpdate(tpf);

        // Works
//        if(i++ == 0) {
//            changeMaterial();
//        }

        // Doesn't work - it changes the material but stops the animations forever :S
//        if(i++ == 1) {
//            changeMaterial();
//        }
    }

    public void changeMaterial() {
        genericMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        genericMat.setColor("Color", ColorRGBA.Red);

        sinbad.setMaterial(genericMat);
    }
}
1 Like

It’s related only if you use hardware skinning.
Do you?

1 Like

Yep (it is using it by default), I am and if I disable it on the SkeletonControl it works fine. Is there a way to make it work with hardware skinning too?.

1 Like

What is it? Is it handling skinning with GPU instead of CPU ?
Will turning it on increase performance ?

yes and yes

1 Like

If I’m not mistaken, hardware skinning is like you said, using the GPU to perform the skinning, while without it is the CPU.

It hasn’t to be by force. If you have your CPU more stressed it would give you a performance boost, if your case is just the opposite, it would give you a performance penalty. Well, I suppose the best is to give the user an option to enable/disable and this is why I want it to be working…

Always?? O.o

1 Like

Almost, even with one model on android.

The main question is: Why do you need to change material?

Unshaded supports hardware skinning so maybe that’s a bug. One more thing to look into in the stack :wink:

I don’t know just now xD. The main problem I had was trying to attach clothes to the already animated model. On attaching, the model is still animated but the cloth isn’t. So I tried to export the model with the cloth and import it with it already worn and it just worked, so the problem wasn’t the way I added it (removing the skeleton from the cloth and adding the spatial to a subnode of the model where the skeletoncontrol could find it). It leaded me to the material thing xD.

This issue will be fixed one MPOs get ported from the experimental branch to master. Also this would allow models that use hardware skinning to share the same material.

3 Likes

Was this finally fixed?, I’m currently using 3.1 and I still have the problem.

It probably should be fixed now on master due to this PR:

2 Likes

I suppose this fixes aren’t being pushed into the 3.1, I’ll have to give a try to 3.2 really soon.