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);
}
}